Skip to content

Commit f5a2c52

Browse files
Update SpanUtils and tests
1 parent 29cee0f commit f5a2c52

File tree

2 files changed

+79
-95
lines changed

2 files changed

+79
-95
lines changed

include/kf/SpanUtils.h

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@
44
namespace kf
55
{
66
template<class T, class U>
7-
inline constexpr std::span<T> span_cast(std::span<U> input) noexcept
7+
constexpr std::span<T> span_cast(std::span<U> input) noexcept
88
{
99
return { reinterpret_cast<T*>(input.data()), input.size_bytes() / sizeof(T) };
1010
}
1111

1212
template<class T, class U>
13-
inline constexpr std::span<T> span_cast(U* data, size_t size) noexcept
13+
constexpr std::span<T> span_cast(U* data, size_t size) noexcept
1414
{
1515
return { reinterpret_cast<T*>(data), size * sizeof(U) / sizeof(T) };
1616
}
@@ -22,7 +22,7 @@ namespace kf
2222

2323
// TODO: rename to asBytes
2424
template<class T, size_t N>
25-
inline constexpr auto as_bytes(const T(&p)[N]) noexcept
25+
constexpr auto as_bytes(const T(&p)[N]) noexcept
2626
{
2727
return std::span<const std::byte, sizeof(T)* N>{ reinterpret_cast<const std::byte*>(p), sizeof(p) };
2828
}
@@ -34,25 +34,25 @@ namespace kf
3434

3535
// TODO: rename to asWritableBytes
3636
template<class T, size_t N>
37-
inline constexpr auto as_writable_bytes(T(&p)[N]) noexcept
37+
constexpr auto as_writable_bytes(T(&p)[N]) noexcept
3838
{
3939
return std::span<std::byte, sizeof(T) * N>{ reinterpret_cast<std::byte*>(p), sizeof(p) };
4040
}
4141

42-
template<class T, size_t dstExtent, size_t srcExtent>
43-
inline constexpr std::span<T> copyTruncate(std::span<T, dstExtent> dst, std::span<const T, srcExtent> src) noexcept
42+
template<class T, class Y, size_t dstExtent, size_t srcExtent> requires std::is_same_v<T, std::remove_const_t<Y>>
43+
constexpr std::span<T> copyTruncate(std::span<T, dstExtent> dst, std::span<Y, srcExtent> src) noexcept
4444
{
4545
//
4646
// Source can be larger than destination, truncate in such case
4747
//
4848

49-
src = src.first(min(src.size(), dst.size()));
49+
auto truncatedSrc = src.first(min(src.size(), dst.size()));
5050

51-
return { dst.begin(), copy(src.begin(), src.end(), dst.begin()) };
51+
return { dst.begin(), copy(truncatedSrc.begin(), truncatedSrc.end(), dst.begin()) };
5252
}
5353

54-
template<class T, size_t dstExtent, size_t srcExtent>
55-
inline constexpr std::span<T> copyExact(std::span<T, dstExtent> dst, std::span<const T, srcExtent> src) noexcept
54+
template<class T, class Y, size_t dstExtent, size_t srcExtent> requires std::is_same_v<T, std::remove_const_t<Y>>
55+
constexpr std::span<T> copyExact(std::span<T, dstExtent> dst, std::span<Y, srcExtent> src) noexcept
5656
{
5757
//
5858
// Source MUST be equal to destination
@@ -73,8 +73,8 @@ namespace kf
7373
return { dst.begin(), copy(src.begin(), src.end(), dst.begin()) };
7474
}
7575

76-
template<class T, size_t dstExtent, size_t srcExtent>
77-
inline constexpr std::span<T> copy(std::span<T, dstExtent> dst, std::span<const T, srcExtent> src) noexcept
76+
template<class T, class Y, size_t dstExtent, size_t srcExtent> requires std::is_same_v<T, std::remove_const_t<Y>>
77+
constexpr std::span<T> copy(std::span<T, dstExtent> dst, std::span<Y, srcExtent> src) noexcept
7878
{
7979
//
8080
// Source MUST be smaller or equal to destination
@@ -96,13 +96,13 @@ namespace kf
9696
}
9797

9898
template<class T, size_t LeftExtent, size_t RightExtent>
99-
inline constexpr bool equals(std::span<T, LeftExtent> left, std::span<T, RightExtent> right) noexcept
99+
constexpr bool equals(std::span<T, LeftExtent> left, std::span<T, RightExtent> right) noexcept
100100
{
101101
return std::equal(left.begin(), left.end(), right.begin(), right.end());
102102
}
103103

104-
template<class T>
105-
inline constexpr ptrdiff_t indexOf(std::span<T> input, typename std::span<T>::const_reference elem, ptrdiff_t fromIndex = 0) noexcept
104+
template<class T, size_t extent>
105+
constexpr ptrdiff_t indexOf(std::span<T, extent> input, typename std::span<T, extent>::const_reference elem, ptrdiff_t fromIndex = 0) noexcept
106106
{
107107
for (auto i = fromIndex; i < ssize(input); ++i)
108108
{
@@ -115,8 +115,8 @@ namespace kf
115115
return -1;
116116
}
117117

118-
template<class T>
119-
inline constexpr std::span<T> split(std::span<T> input, typename std::span<T>::const_reference separator, _Inout_ ptrdiff_t& fromIndex) noexcept
118+
template<class T, size_t extent>
119+
constexpr std::span<T> split(std::span<T, extent> input, typename std::span<T, extent>::const_reference separator, _Inout_ ptrdiff_t& fromIndex) noexcept
120120
{
121121
auto originalFromIndex = fromIndex;
122122

@@ -132,8 +132,8 @@ namespace kf
132132
return input.subspan(originalFromIndex, count);
133133
}
134134

135-
template<class T>
136-
inline constexpr T atOrDefault(std::span<T> input, size_t index, std::convertible_to<T> auto defaultValue) noexcept
135+
template<class T, size_t extent>
136+
constexpr T atOrDefault(std::span<T, extent> input, size_t index, std::convertible_to<T> auto defaultValue) noexcept
137137
{
138138
return input.size() > index ? input[index] : defaultValue;
139139
}

0 commit comments

Comments
 (0)