[ox/std] Add spancpy

This commit is contained in:
Gary Talent 2025-01-14 21:10:18 -06:00
parent 861d177a27
commit f7a468ea1e
2 changed files with 24 additions and 6 deletions

View File

@ -14,7 +14,7 @@
#include "iterator.hpp" #include "iterator.hpp"
#include "vector.hpp" #include "vector.hpp"
OX_CLANG_NOWARN_BEGIN(-Wunsafe-buffer-usage) OX_ALLOW_UNSAFE_BUFFERS_BEGIN
namespace ox { namespace ox {
@ -133,7 +133,7 @@ class Span {
return m_items[i]; return m_items[i];
} }
constexpr const T &operator[](std::size_t i) const noexcept { constexpr T const&operator[](std::size_t i) const noexcept {
ox::primitiveAssert(__FILE__, __LINE__, i < size(), "Span access overflow"); ox::primitiveAssert(__FILE__, __LINE__, i < size(), "Span access overflow");
return m_items[i]; return m_items[i];
} }
@ -168,8 +168,20 @@ class Span {
}; };
template<typename T> template<typename T>
using SpanView = Span<const T>; using SpanView = Span<T const>;
template<typename T>
constexpr void spancpy(ox::Span<T> const dst, ox::SpanView<T> const src) noexcept {
auto const sz = ox::min(dst.size(), src.size());
if (std::is_constant_evaluated() || std::is_trivially_copyable_v<T>) {
for (size_t i{}; i < sz; ++i) {
dst.data()[i] = src.data()[i];
}
} else {
memcpy(dst.data(), src.data(), sz * sizeof(T));
}
}
} }
OX_CLANG_NOWARN_END OX_ALLOW_UNSAFE_BUFFERS_END

View File

@ -19,12 +19,15 @@
namespace std { namespace std {
template<typename T> template<typename T>
constexpr bool is_union_v = __is_union(T); inline constexpr bool is_union_v = __is_union(T);
constexpr bool is_constant_evaluated() noexcept { inline constexpr bool is_constant_evaluated() noexcept {
return __builtin_is_constant_evaluated(); return __builtin_is_constant_evaluated();
} }
template<typename T>
inline constexpr bool is_trivially_copyable_v = __is_trivially_copyable(T);
} }
#endif #endif
@ -156,6 +159,9 @@ static_assert(is_class<int>::value == false);
template<typename T> template<typename T>
constexpr bool is_class_v = is_class<T>(); constexpr bool is_class_v = is_class<T>();
template<typename T>
inline constexpr bool is_trivially_copyable_v = std::is_trivially_copyable_v<T>;
template<typename T> template<typename T>
constexpr bool is_signed_v = integral_constant<bool, T(-1) < T(0)>::value; constexpr bool is_signed_v = integral_constant<bool, T(-1) < T(0)>::value;