data function for vector and matrix

This commit is contained in:
BlackMATov
2020-12-03 06:34:28 +07:00
parent 6513122fc5
commit 76174171f3
5 changed files with 69 additions and 1 deletions

View File

@@ -134,6 +134,9 @@ public:
void swap(vec& other) noexcept(is_nothrow_swappable_v<T>);
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<T>);
constexpr pointer data() noexcept;
constexpr const_pointer data() const noexcept;
constexpr reference at(size_t index);
constexpr const_reference at(size_t index) const;

View File

@@ -204,10 +204,18 @@ namespace vmath_hpp
void swap(mat& other) noexcept(std::is_nothrow_swappable_v<T>) {
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];
}

View File

@@ -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");

View File

@@ -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));

View File

@@ -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);