iterators are confusing

This commit is contained in:
Andrew Cassidy 2022-05-29 16:51:35 -07:00
parent b9c7c7cf6e
commit 53a6427dcc
2 changed files with 9 additions and 16 deletions

View File

@ -19,6 +19,7 @@
#pragma once
#include <algorithm>
#include <cstdint>
#include <numeric>
#include <xsimd/xsimd.hpp>
@ -52,13 +53,11 @@ template <typename T, size_t N> class Vec {
}
/**
* Create a vector from a naked pointer
* @tparam S Source data type
* @param ptr Pointer to the start of the source data. N values will be read.
* Create a vector from an iterator
* @tparam II input iterator type
* @param input_iterator iterator to copy from
*/
template <typename S> Vec(const S *ptr) {
for (unsigned i = 0; i < N; i++) { at(i) = static_cast<T>(ptr[i]); }
}
template <typename II> Vec(const II input_iterator) { std::copy_n(input_iterator, N, begin()); }
/**
* Create a vector from a std::array
@ -157,13 +156,7 @@ template <typename T, size_t N> class Vec {
bool operator!=(const Vec &rhs) const { return _c != rhs._c; };
// endregion
template <typename U> void write(U *ptr) const {
if constexpr (std::is_same_v<T, U>) {
std::memcpy(ptr, _c.begin(), N * sizeof(T));
} else {
for (unsigned i = 0; i < N; i++) { ptr[i] = static_cast<U>(_c[i]); }
}
}
template <typename OI> void copy(OI output_iterator) const { std::copy(begin(), end(), output_iterator); }
template <typename P = T, typename W = size_t>
requires std::is_unsigned_v<P> && std::is_integral_v<T>
@ -196,7 +189,7 @@ template <typename T, size_t N> class Vec {
return map(*this, [](T val) { return quicktex::abs(val); });
}
Vec clamp(const float &low, const float &high) {
Vec clamp(float low, float high) {
return map(*this, [&low, &high](T val) { return quicktex::clamp(val, low, high); });
}

View File

@ -124,7 +124,7 @@ UTEST(Vec_int, pack) {
ASSERT_EQ(result, 0xFD41);
}
UTEST(Vec_int, write) {
UTEST(Vec_int, copy) {
std::array<int, 4> arr{1, 3, 1, 2};
Vec<int, 4> a(arr);
Vec<int, 4> expected{1, 3, 1, 2};
@ -132,7 +132,7 @@ UTEST(Vec_int, write) {
ASSERT_TRUE(a == expected);
std::array<int, 4> out{-1, -3, -1, -2};
a.write(out.begin());
a.copy(out.begin());
ASSERT_TRUE(out == arr);
}