diff --git a/README.md b/README.md index 161a82e..c814c80 100644 --- a/README.md +++ b/README.md @@ -134,6 +134,9 @@ public: void swap(vec& other) noexcept(is_nothrow_swappable_v); + constexpr pointer data() noexcept; + constexpr const_pointer data() const noexcept; + constexpr reference at(size_t index); constexpr const_reference at(size_t index) const; @@ -275,6 +278,9 @@ public: void swap(mat& other) noexcept(is_nothrow_swappable_v); + constexpr pointer data() noexcept; + constexpr const_pointer data() const noexcept; + constexpr reference at(size_t index); constexpr const_reference at(size_t index) const; diff --git a/headers/vmath.hpp/vmath_mat.hpp b/headers/vmath.hpp/vmath_mat.hpp index 9fa1acb..11471ae 100644 --- a/headers/vmath.hpp/vmath_mat.hpp +++ b/headers/vmath.hpp/vmath_mat.hpp @@ -204,10 +204,18 @@ namespace vmath_hpp void swap(mat& other) noexcept(std::is_nothrow_swappable_v) { for ( std::size_t i = 0; i < Size; ++i ) { using std::swap; - swap((*this)[i], other[i]); + swap(rows[i], other.rows[i]); } } + [[nodiscard]] constexpr pointer data() noexcept { + return &rows[0]; + } + + [[nodiscard]] constexpr const_pointer data() const noexcept { + return &rows[0]; + } + [[nodiscard]] constexpr reference operator[](std::size_t index) noexcept { return rows[index]; } diff --git a/headers/vmath.hpp/vmath_vec.hpp b/headers/vmath.hpp/vmath_vec.hpp index f15d325..dcb2874 100644 --- a/headers/vmath.hpp/vmath_vec.hpp +++ b/headers/vmath.hpp/vmath_vec.hpp @@ -175,6 +175,14 @@ namespace vmath_hpp } } + [[nodiscard]] constexpr pointer data() noexcept { + return &(*this)[0]; + } + + [[nodiscard]] constexpr const_pointer data() const noexcept { + return &(*this)[0]; + } + [[nodiscard]] constexpr reference at(std::size_t index) { if ( index >= Size ) { throw std::out_of_range("vec::at"); diff --git a/untests/vmath_mat_tests.cpp b/untests/vmath_mat_tests.cpp index 0f2abf9..7f7c3bb 100644 --- a/untests/vmath_mat_tests.cpp +++ b/untests/vmath_mat_tests.cpp @@ -121,6 +121,29 @@ TEST_CASE("vmath/mat") { } } + SUBCASE("data") { + { + int2x2 m2; + STATIC_REQUIRE(m2.data() == &m2[0]); + + int3x3 m3; + STATIC_REQUIRE(m3.data() == &m3[0]); + + int4x4 m4; + STATIC_REQUIRE(m4.data() == &m4[0]); + } + { + constexpr int2x2 m2; + STATIC_REQUIRE(m2.data() == &m2[0]); + + constexpr int3x3 m3; + STATIC_REQUIRE(m3.data() == &m3[0]); + + constexpr int4x4 m4; + STATIC_REQUIRE(m4.data() == &m4[0]); + } + } + SUBCASE("operator[]") { { STATIC_REQUIRE(int2x2()[0] == int2(1,0)); diff --git a/untests/vmath_vec_tests.cpp b/untests/vmath_vec_tests.cpp index 7c3d2da..71c41ae 100644 --- a/untests/vmath_vec_tests.cpp +++ b/untests/vmath_vec_tests.cpp @@ -118,6 +118,29 @@ TEST_CASE("vmath/vec") { } } + SUBCASE("data") { + { + int2 i2; + REQUIRE(i2.data() == &i2[0]); + + int3 i3; + REQUIRE(i3.data() == &i3[0]); + + int4 i4; + REQUIRE(i4.data() == &i4[0]); + } + { + constexpr int2 i2; + STATIC_REQUIRE(i2.data() == &i2[0]); + + constexpr int3 i3; + STATIC_REQUIRE(i3.data() == &i3[0]); + + constexpr int4 i4; + STATIC_REQUIRE(i4.data() == &i4[0]); + } + } + SUBCASE("operator[]") { { STATIC_REQUIRE(int2(1,2).x == 1);