From bdbb254db5d6a3d58cb5d87bcd1fdc1495a54b26 Mon Sep 17 00:00:00 2001 From: BlackMATov Date: Fri, 20 Nov 2020 23:49:53 +0700 Subject: [PATCH] new headers structure --- headers/vmath.hpp/vmath.hpp | 254 +----------------- headers/vmath.hpp/vmath_fwd.hpp | 37 +++ headers/vmath.hpp/vmath_vec.hpp | 236 ++++++++++++++++ .../{vmath_tests.cpp => vmath_vec_tests.cpp} | 2 +- 4 files changed, 275 insertions(+), 254 deletions(-) create mode 100644 headers/vmath.hpp/vmath_fwd.hpp create mode 100644 headers/vmath.hpp/vmath_vec.hpp rename untests/{vmath_tests.cpp => vmath_vec_tests.cpp} (99%) diff --git a/headers/vmath.hpp/vmath.hpp b/headers/vmath.hpp/vmath.hpp index 89242df..88a871e 100644 --- a/headers/vmath.hpp/vmath.hpp +++ b/headers/vmath.hpp/vmath.hpp @@ -6,256 +6,4 @@ #pragma once -#include -#include -#include -#include -#include - -namespace vmath_hpp -{ - struct uninit_t {}; - inline constexpr uninit_t uninit; -} - -namespace vmath_hpp::detail -{ - template < typename T, std::size_t Size > - class vec_base; - - template < typename T > - class vec_base { - public: - T data[2]; - public: - constexpr vec_base() : data{} {} - constexpr explicit vec_base(uninit_t) {} - - constexpr explicit vec_base(const T& v) - : data{v, v} {} - - constexpr vec_base(T x, T y) - : data{std::move(x), std::move(y)} {} - - constexpr explicit vec_base(vec_base&& xy) - : data{std::move(xy.x()), std::move(xy.y())} {} - constexpr explicit vec_base(const vec_base& xy) - : data{xy.x(), xy.y()} {} - - constexpr explicit vec_base(vec_base&& xy) - : data{std::move(xy.x()), std::move(xy.y())} {} - constexpr explicit vec_base(const vec_base& xy) - : data{xy.x(), xy.y()} {} - - constexpr T& x() noexcept { return data[0]; } - constexpr const T& x() const noexcept { return data[0]; } - - constexpr T& y() noexcept { return data[1]; } - constexpr const T& y() const noexcept { return data[1]; } - }; - - template < typename T > - class vec_base { - public: - T data[3]; - public: - constexpr vec_base() : data{} {} - constexpr explicit vec_base(uninit_t) {} - - constexpr explicit vec_base(const T& v) - : data{v, v, v} {} - - constexpr vec_base(T x, T y, T z) - : data{std::move(x), std::move(y), std::move(z)} {} - - constexpr explicit vec_base(vec_base&& xy, T z) - : data{std::move(xy.x()), std::move(xy.y()), std::move(z)} {} - constexpr explicit vec_base(const vec_base& xy, T z) - : data{xy.x(), xy.y(), std::move(z)} {} - - constexpr explicit vec_base(vec_base&& xyz) - : data{std::move(xyz.x()), std::move(xyz.y()), std::move(xyz.z())} {} - constexpr explicit vec_base(const vec_base& xyz) - : data{xyz.x(), xyz.y(), xyz.z()} {} - - constexpr T& x() noexcept { return data[0]; } - constexpr const T& x() const noexcept { return data[0]; } - - constexpr T& y() noexcept { return data[1]; } - constexpr const T& y() const noexcept { return data[1]; } - - constexpr T& z() noexcept { return data[2]; } - constexpr const T& z() const noexcept { return data[2]; } - }; - - template < typename T > - class vec_base { - public: - T data[4]; - public: - constexpr vec_base() : data{} {} - constexpr explicit vec_base(uninit_t) {} - - constexpr explicit vec_base(const T& v) - : data{v, v, v, v} {} - - constexpr vec_base(T x, T y, T z, T w) - : data{std::move(x), std::move(y), std::move(z), std::move(w)} {} - - constexpr explicit vec_base(vec_base&& xy, T z, T w) - : data{std::move(xy.x()), std::move(xy.y()), std::move(z), std::move(w)} {} - constexpr explicit vec_base(const vec_base& xy, T z, T w) - : data{xy.x(), xy.y(), std::move(z), std::move(w)} {} - - constexpr explicit vec_base(vec_base&& xyz, T w) - : data{std::move(xyz.x()), std::move(xyz.y()), std::move(xyz.z()), std::move(w)} {} - constexpr explicit vec_base(const vec_base& xyz, T w) - : data{xyz.x(), xyz.y(), xyz.z(), std::move(w)} {} - - constexpr T& x() noexcept { return data[0]; } - constexpr const T& x() const noexcept { return data[0]; } - - constexpr T& y() noexcept { return data[1]; } - constexpr const T& y() const noexcept { return data[1]; } - - constexpr T& z() noexcept { return data[2]; } - constexpr const T& z() const noexcept { return data[2]; } - - constexpr T& w() noexcept { return data[3]; } - constexpr const T& w() const noexcept { return data[3]; } - }; -} - -namespace vmath_hpp -{ - template < typename T, std::size_t Size > - class vec final : public detail::vec_base { - public: - using self_type = vec; - using value_type = T; - - using pointer = value_type*; - using const_pointer = const value_type*; - - using reference = value_type&; - using const_reference = const value_type&; - - using iterator = value_type*; - using const_iterator = const value_type*; - - using reverse_iterator = std::reverse_iterator; - using const_reverse_iterator = std::reverse_iterator; - - using size_type = std::size_t; - using difference_type = std::ptrdiff_t; - public: - using detail::vec_base::data; - using detail::vec_base::vec_base; - - vec(vec&&) = default; - vec& operator=(vec&&) = default; - - vec(const vec&) = default; - vec& operator=(const vec&) = default; - - void swap(vec& other) noexcept(std::is_nothrow_swappable_v) { - for ( std::size_t i = 0; i < Size; ++i ) { - using std::swap; - swap(data[i], other.data[i]); - } - } - - constexpr iterator begin() noexcept { return iterator(data); } - constexpr const_iterator begin() const noexcept { return const_iterator(data); } - constexpr const_iterator cbegin() const noexcept { return const_iterator(data); } - - constexpr iterator end() noexcept { return iterator(data + Size); } - constexpr const_iterator end() const noexcept { return const_iterator(data + Size); } - constexpr const_iterator cend() const noexcept { return const_iterator(data + Size); } - - constexpr reverse_iterator rbegin() noexcept { return reverse_iterator(end()); } - constexpr const_reverse_iterator rbegin() const noexcept { return const_reverse_iterator(end()); } - constexpr const_reverse_iterator crbegin() const noexcept { return const_reverse_iterator(end()); } - - constexpr reverse_iterator rend() noexcept { return reverse_iterator(begin()); } - constexpr const_reverse_iterator rend() const noexcept { return const_reverse_iterator(begin()); } - constexpr const_reverse_iterator crend() const noexcept { return const_reverse_iterator(begin()); } - - constexpr size_type size() const noexcept { return Size; } - constexpr size_type max_size() const noexcept { return Size; } - constexpr bool empty() const noexcept { return !Size; } - - constexpr reference operator[](size_type index) noexcept { - return data[index]; - } - - constexpr const_reference operator[](size_type index) const noexcept { - return data[index]; - } - - constexpr reference at(size_type index) { - if ( index < Size ) { - return data[index]; - } - throw std::out_of_range("vec::at"); - } - - constexpr const_reference at(size_type index) const { - if ( index < Size ) { - return data[index]; - } - throw std::out_of_range("vec::at"); - } - }; - - template < typename T, std::size_t Size > - void swap(vec& l, vec& r) noexcept(noexcept(l.swap(r))) { - l.swap(r); - } -} - -namespace vmath_hpp -{ - template < typename T, std::size_t Size > - constexpr bool operator==(const vec& l, const vec& r) { - for ( std::size_t i = 0; i < Size; ++i ) { - if ( !(l[i] == r[i]) ) { - return false; - } - } - return true; - } - - template < typename T, std::size_t Size > - constexpr bool operator!=(const vec& l, const vec& r) { - return !(l == r); - } - - template < typename T, std::size_t Size > - constexpr bool operator<(const vec& l, const vec& r) { - for ( std::size_t i = 0; i < Size; ++i ) { - if ( l[i] < r[i] ) { - return true; - } - if ( r[i] < l[i] ) { - return false; - } - } - return false; - } -} - -namespace vmath_hpp -{ - using vec2i = vec; - using vec3i = vec; - using vec4i = vec; - - using vec2b = vec; - using vec3b = vec; - using vec4b = vec; - - using vec2f = vec; - using vec3f = vec; - using vec4f = vec; -} +#include "vmath_vec.hpp" diff --git a/headers/vmath.hpp/vmath_fwd.hpp b/headers/vmath.hpp/vmath_fwd.hpp new file mode 100644 index 0000000..31621fe --- /dev/null +++ b/headers/vmath.hpp/vmath_fwd.hpp @@ -0,0 +1,37 @@ +/******************************************************************************* + * This file is part of the "https://github.com/blackmatov/vmath.hpp" + * For conditions of distribution and use, see copyright notice in LICENSE.md + * Copyright (C) 2020, by Matvey Cherevko (blackmatov@gmail.com) + ******************************************************************************/ + +#pragma once + +#include +#include +#include +#include +#include + +namespace vmath_hpp +{ + struct uninit_t {}; + inline constexpr uninit_t uninit; +} + +namespace vmath_hpp +{ + template < typename T, std::size_t Size > + class vec; + + using vec2i = vec; + using vec3i = vec; + using vec4i = vec; + + using vec2b = vec; + using vec3b = vec; + using vec4b = vec; + + using vec2f = vec; + using vec3f = vec; + using vec4f = vec; +} diff --git a/headers/vmath.hpp/vmath_vec.hpp b/headers/vmath.hpp/vmath_vec.hpp new file mode 100644 index 0000000..22e8056 --- /dev/null +++ b/headers/vmath.hpp/vmath_vec.hpp @@ -0,0 +1,236 @@ +/******************************************************************************* + * This file is part of the "https://github.com/blackmatov/vmath.hpp" + * For conditions of distribution and use, see copyright notice in LICENSE.md + * Copyright (C) 2020, by Matvey Cherevko (blackmatov@gmail.com) + ******************************************************************************/ + +#pragma once + +#include "vmath_fwd.hpp" + +namespace vmath_hpp::detail +{ + template < typename T, std::size_t Size > + class vec_base; + + template < typename T > + class vec_base { + public: + T data[2]; + public: + constexpr vec_base() : data{} {} + constexpr explicit vec_base(uninit_t) {} + + constexpr explicit vec_base(const T& v) + : data{v, v} {} + + constexpr vec_base(T x, T y) + : data{std::move(x), std::move(y)} {} + + constexpr explicit vec_base(vec_base&& xy) + : data{std::move(xy.x()), std::move(xy.y())} {} + constexpr explicit vec_base(const vec_base& xy) + : data{xy.x(), xy.y()} {} + + constexpr explicit vec_base(vec_base&& xy) + : data{std::move(xy.x()), std::move(xy.y())} {} + constexpr explicit vec_base(const vec_base& xy) + : data{xy.x(), xy.y()} {} + + constexpr T& x() noexcept { return data[0]; } + constexpr const T& x() const noexcept { return data[0]; } + + constexpr T& y() noexcept { return data[1]; } + constexpr const T& y() const noexcept { return data[1]; } + }; + + template < typename T > + class vec_base { + public: + T data[3]; + public: + constexpr vec_base() : data{} {} + constexpr explicit vec_base(uninit_t) {} + + constexpr explicit vec_base(const T& v) + : data{v, v, v} {} + + constexpr vec_base(T x, T y, T z) + : data{std::move(x), std::move(y), std::move(z)} {} + + constexpr explicit vec_base(vec_base&& xy, T z) + : data{std::move(xy.x()), std::move(xy.y()), std::move(z)} {} + constexpr explicit vec_base(const vec_base& xy, T z) + : data{xy.x(), xy.y(), std::move(z)} {} + + constexpr explicit vec_base(vec_base&& xyz) + : data{std::move(xyz.x()), std::move(xyz.y()), std::move(xyz.z())} {} + constexpr explicit vec_base(const vec_base& xyz) + : data{xyz.x(), xyz.y(), xyz.z()} {} + + constexpr T& x() noexcept { return data[0]; } + constexpr const T& x() const noexcept { return data[0]; } + + constexpr T& y() noexcept { return data[1]; } + constexpr const T& y() const noexcept { return data[1]; } + + constexpr T& z() noexcept { return data[2]; } + constexpr const T& z() const noexcept { return data[2]; } + }; + + template < typename T > + class vec_base { + public: + T data[4]; + public: + constexpr vec_base() : data{} {} + constexpr explicit vec_base(uninit_t) {} + + constexpr explicit vec_base(const T& v) + : data{v, v, v, v} {} + + constexpr vec_base(T x, T y, T z, T w) + : data{std::move(x), std::move(y), std::move(z), std::move(w)} {} + + constexpr explicit vec_base(vec_base&& xy, T z, T w) + : data{std::move(xy.x()), std::move(xy.y()), std::move(z), std::move(w)} {} + constexpr explicit vec_base(const vec_base& xy, T z, T w) + : data{xy.x(), xy.y(), std::move(z), std::move(w)} {} + + constexpr explicit vec_base(vec_base&& xyz, T w) + : data{std::move(xyz.x()), std::move(xyz.y()), std::move(xyz.z()), std::move(w)} {} + constexpr explicit vec_base(const vec_base& xyz, T w) + : data{xyz.x(), xyz.y(), xyz.z(), std::move(w)} {} + + constexpr T& x() noexcept { return data[0]; } + constexpr const T& x() const noexcept { return data[0]; } + + constexpr T& y() noexcept { return data[1]; } + constexpr const T& y() const noexcept { return data[1]; } + + constexpr T& z() noexcept { return data[2]; } + constexpr const T& z() const noexcept { return data[2]; } + + constexpr T& w() noexcept { return data[3]; } + constexpr const T& w() const noexcept { return data[3]; } + }; +} + +namespace vmath_hpp +{ + template < typename T, std::size_t Size > + class vec final : public detail::vec_base { + public: + using self_type = vec; + using value_type = T; + + using pointer = value_type*; + using const_pointer = const value_type*; + + using reference = value_type&; + using const_reference = const value_type&; + + using iterator = value_type*; + using const_iterator = const value_type*; + + using reverse_iterator = std::reverse_iterator; + using const_reverse_iterator = std::reverse_iterator; + + using size_type = std::size_t; + using difference_type = std::ptrdiff_t; + public: + using detail::vec_base::data; + using detail::vec_base::vec_base; + + vec(vec&&) = default; + vec& operator=(vec&&) = default; + + vec(const vec&) = default; + vec& operator=(const vec&) = default; + + void swap(vec& other) noexcept(std::is_nothrow_swappable_v) { + for ( std::size_t i = 0; i < Size; ++i ) { + using std::swap; + swap(data[i], other.data[i]); + } + } + + constexpr iterator begin() noexcept { return iterator(data); } + constexpr const_iterator begin() const noexcept { return const_iterator(data); } + constexpr const_iterator cbegin() const noexcept { return const_iterator(data); } + + constexpr iterator end() noexcept { return iterator(data + Size); } + constexpr const_iterator end() const noexcept { return const_iterator(data + Size); } + constexpr const_iterator cend() const noexcept { return const_iterator(data + Size); } + + constexpr reverse_iterator rbegin() noexcept { return reverse_iterator(end()); } + constexpr const_reverse_iterator rbegin() const noexcept { return const_reverse_iterator(end()); } + constexpr const_reverse_iterator crbegin() const noexcept { return const_reverse_iterator(end()); } + + constexpr reverse_iterator rend() noexcept { return reverse_iterator(begin()); } + constexpr const_reverse_iterator rend() const noexcept { return const_reverse_iterator(begin()); } + constexpr const_reverse_iterator crend() const noexcept { return const_reverse_iterator(begin()); } + + constexpr size_type size() const noexcept { return Size; } + constexpr size_type max_size() const noexcept { return Size; } + constexpr bool empty() const noexcept { return !Size; } + + constexpr reference operator[](size_type index) noexcept { + return data[index]; + } + + constexpr const_reference operator[](size_type index) const noexcept { + return data[index]; + } + + constexpr reference at(size_type index) { + if ( index < Size ) { + return data[index]; + } + throw std::out_of_range("vec::at"); + } + + constexpr const_reference at(size_type index) const { + if ( index < Size ) { + return data[index]; + } + throw std::out_of_range("vec::at"); + } + }; + + template < typename T, std::size_t Size > + void swap(vec& l, vec& r) noexcept(noexcept(l.swap(r))) { + l.swap(r); + } +} + +namespace vmath_hpp +{ + template < typename T, std::size_t Size > + constexpr bool operator==(const vec& l, const vec& r) { + for ( std::size_t i = 0; i < Size; ++i ) { + if ( !(l[i] == r[i]) ) { + return false; + } + } + return true; + } + + template < typename T, std::size_t Size > + constexpr bool operator!=(const vec& l, const vec& r) { + return !(l == r); + } + + template < typename T, std::size_t Size > + constexpr bool operator<(const vec& l, const vec& r) { + for ( std::size_t i = 0; i < Size; ++i ) { + if ( l[i] < r[i] ) { + return true; + } + if ( r[i] < l[i] ) { + return false; + } + } + return false; + } +} diff --git a/untests/vmath_tests.cpp b/untests/vmath_vec_tests.cpp similarity index 99% rename from untests/vmath_tests.cpp rename to untests/vmath_vec_tests.cpp index 89e5322..6ba6642 100644 --- a/untests/vmath_tests.cpp +++ b/untests/vmath_vec_tests.cpp @@ -4,7 +4,7 @@ * Copyright (C) 2020, by Matvey Cherevko (blackmatov@gmail.com) ******************************************************************************/ -#include +#include #define CATCH_CONFIG_FAST_COMPILE #include