mirror of
https://github.com/microsoft/GSL.git
synced 2024-11-03 17:56:43 -05:00
Implemented https://github.com/Microsoft/GSL/issues/260
This commit is contained in:
parent
4235b060fd
commit
c4817358aa
@ -1053,18 +1053,28 @@ template <typename Span>
|
||||
class general_span_iterator;
|
||||
|
||||
template <std::ptrdiff_t DimSize = dynamic_range>
|
||||
struct dim
|
||||
struct dim_t
|
||||
{
|
||||
static const std::ptrdiff_t value = DimSize;
|
||||
};
|
||||
template <>
|
||||
struct dim<dynamic_range>
|
||||
struct dim_t<dynamic_range>
|
||||
{
|
||||
static const std::ptrdiff_t value = dynamic_range;
|
||||
const std::ptrdiff_t dvalue;
|
||||
dim(std::ptrdiff_t size) : dvalue(size) {}
|
||||
dim_t(std::ptrdiff_t size) : dvalue(size) {}
|
||||
};
|
||||
|
||||
template <std::ptrdiff_t N>
|
||||
constexpr std::enable_if_t<(N >= 0),dim_t<N>> dim() noexcept {
|
||||
return dim_t<N>();
|
||||
}
|
||||
|
||||
template <std::ptrdiff_t N = dynamic_range>
|
||||
constexpr std::enable_if_t<N == dynamic_range,dim_t<N>> dim(std::ptrdiff_t n) noexcept {
|
||||
return dim_t<>(n);
|
||||
}
|
||||
|
||||
template <typename ValueType, std::ptrdiff_t FirstDimension = dynamic_range,
|
||||
std::ptrdiff_t... RestDimensions>
|
||||
class multi_span;
|
||||
@ -1133,13 +1143,13 @@ namespace details
|
||||
}
|
||||
template <typename T, typename Arg, typename... Args>
|
||||
std::enable_if_t<
|
||||
!std::is_same<Arg, dim<dynamic_range>>::value && !std::is_same<Arg, Sep>::value, T>
|
||||
!std::is_same<Arg, dim_t<dynamic_range>>::value && !std::is_same<Arg, Sep>::value, T>
|
||||
static_as_multi_span_helper(Arg, Args... args)
|
||||
{
|
||||
return static_as_multi_span_helper<T>(args...);
|
||||
}
|
||||
template <typename T, typename... Args>
|
||||
T static_as_multi_span_helper(dim<dynamic_range> val, Args... args)
|
||||
T static_as_multi_span_helper(dim_t<dynamic_range> val, Args... args)
|
||||
{
|
||||
return static_as_multi_span_helper<T>(args..., val.dvalue);
|
||||
}
|
||||
@ -1682,7 +1692,7 @@ constexpr auto as_multi_span(multi_span<byte, Dimensions...> s) noexcept
|
||||
}
|
||||
|
||||
template <typename T, std::ptrdiff_t... Dimensions>
|
||||
constexpr auto as_multi_span(T* const& ptr, dim<Dimensions>... args)
|
||||
constexpr auto as_multi_span(T* const& ptr, dim_t<Dimensions>... args)
|
||||
-> multi_span<std::remove_all_extents_t<T>, Dimensions...>
|
||||
{
|
||||
return {reinterpret_cast<std::remove_all_extents_t<T>*>(ptr),
|
||||
|
@ -687,9 +687,9 @@ SUITE(multi_span_tests)
|
||||
fn(av.bounds());
|
||||
auto av2 = as_multi_span(av, dim<60>());
|
||||
auto av3 = as_multi_span(av2, dim<3>(), dim<4>(), dim<5>());
|
||||
auto av4 = as_multi_span(av3, dim<4>(), dim<>(3), dim<5>());
|
||||
auto av4 = as_multi_span(av3, dim<4>(), dim(3), dim<5>());
|
||||
auto av5 = as_multi_span(av4, dim<3>(), dim<4>(), dim<5>());
|
||||
auto av6 = as_multi_span(av5, dim<12>(), dim<>(5));
|
||||
auto av6 = as_multi_span(av5, dim<12>(), dim(5));
|
||||
|
||||
fill(av6.begin(), av6.end(), 1);
|
||||
|
||||
@ -954,7 +954,7 @@ SUITE(multi_span_tests)
|
||||
|
||||
CHECK(s1 == s2);
|
||||
|
||||
multi_span<int, 20> s3 = as_multi_span(s1, dim<>(20));
|
||||
multi_span<int, 20> s3 = as_multi_span(s1, dim(20));
|
||||
CHECK(s3 == s2 && s3 == s1);
|
||||
}
|
||||
|
||||
@ -1122,7 +1122,7 @@ SUITE(multi_span_tests)
|
||||
CHECK(count == 34 * 60);
|
||||
overloaded_func(av, 34);
|
||||
|
||||
overloaded_func(as_multi_span(av, dim<>(4), dim<>(3), dim<>(5)), 34);
|
||||
overloaded_func(as_multi_span(av, dim(4), dim(3), dim(5)), 34);
|
||||
|
||||
// fixed_func(av, 34);
|
||||
delete[] data;
|
||||
@ -1137,7 +1137,7 @@ SUITE(multi_span_tests)
|
||||
|
||||
// size check will be done
|
||||
auto image_view =
|
||||
as_multi_span(as_multi_span(image_ptr, imgSize), dim<>(height), dim<>(width), dim<3>());
|
||||
as_multi_span(as_multi_span(image_ptr, imgSize), dim(height), dim(width), dim<3>());
|
||||
|
||||
iota(image_view.begin(), image_view.end(), 1);
|
||||
|
||||
@ -1165,7 +1165,7 @@ SUITE(multi_span_tests)
|
||||
{
|
||||
int* arr = new int[150];
|
||||
|
||||
auto av = as_multi_span(arr, dim<10>(), dim<>(3), dim<5>());
|
||||
auto av = as_multi_span(arr, dim<10>(), dim(3), dim<5>());
|
||||
|
||||
fill(av.begin(), av.end(), 24);
|
||||
overloaded_func(av, 24);
|
||||
@ -1174,13 +1174,13 @@ SUITE(multi_span_tests)
|
||||
|
||||
array<int, 15> stdarr{0};
|
||||
auto av2 = as_multi_span(stdarr);
|
||||
overloaded_func(as_multi_span(av2, dim<>(1), dim<3>(), dim<5>()), 0);
|
||||
overloaded_func(as_multi_span(av2, dim(1), dim<3>(), dim<5>()), 0);
|
||||
|
||||
string str = "ttttttttttttttt"; // size = 15
|
||||
auto t = str.data();
|
||||
(void) t;
|
||||
auto av3 = as_multi_span(str);
|
||||
overloaded_func(as_multi_span(av3, dim<>(1), dim<3>(), dim<5>()), 't');
|
||||
overloaded_func(as_multi_span(av3, dim(1), dim<3>(), dim<5>()), 't');
|
||||
}
|
||||
|
||||
{
|
||||
@ -1258,7 +1258,7 @@ SUITE(multi_span_tests)
|
||||
|
||||
CHECK(av[i] == 4);
|
||||
|
||||
auto av2 = as_multi_span(av, dim<4>(), dim<>(2));
|
||||
auto av2 = as_multi_span(av, dim<4>(), dim(2));
|
||||
ptrdiff_t a2[2] = {0, 1};
|
||||
index<2> i2 = a2;
|
||||
|
||||
@ -1490,17 +1490,17 @@ SUITE(multi_span_tests)
|
||||
|
||||
// first bound is dynamic
|
||||
{
|
||||
multi_span<int, dynamic_range, 2> av2 = as_multi_span(av, dim<>(height), dim<>(width));
|
||||
multi_span<int, dynamic_range, 2> av2 = as_multi_span(av, dim(height), dim(width));
|
||||
iterate_second_column(av2);
|
||||
}
|
||||
// second bound is dynamic
|
||||
{
|
||||
multi_span<int, 4, dynamic_range> av2 = as_multi_span(av, dim<>(height), dim<>(width));
|
||||
multi_span<int, 4, dynamic_range> av2 = as_multi_span(av, dim(height), dim(width));
|
||||
iterate_second_column(av2);
|
||||
}
|
||||
// both bounds are dynamic
|
||||
{
|
||||
multi_span<int, dynamic_range, dynamic_range> av2 = as_multi_span(av, dim<>(height), dim<>(width));
|
||||
multi_span<int, dynamic_range, dynamic_range> av2 = as_multi_span(av, dim(height), dim(width));
|
||||
iterate_second_column(av2);
|
||||
}
|
||||
|
||||
@ -1521,7 +1521,7 @@ SUITE(multi_span_tests)
|
||||
|
||||
CHECK_THROW(av1[10][3][4], fail_fast);
|
||||
|
||||
multi_span<const double, dynamic_range, 6, 4> av2 = as_multi_span(av1, dim<>(5), dim<6>(), dim<4>());
|
||||
multi_span<const double, dynamic_range, 6, 4> av2 = as_multi_span(av1, dim(5), dim<6>(), dim<4>());
|
||||
(void) av2;
|
||||
}
|
||||
|
||||
@ -1568,7 +1568,7 @@ SUITE(multi_span_tests)
|
||||
|
||||
{
|
||||
multi_span<int, dynamic_range> av = arr;
|
||||
multi_span<int, 2, 1> av2 = as_multi_span(av, dim<>(2), dim<>(2));
|
||||
multi_span<int, 2, 1> av2 = as_multi_span(av, dim(2), dim(2));
|
||||
auto workaround_macro = [&]() { return av2[{1, 0}] == 2; };
|
||||
CHECK(workaround_macro());
|
||||
}
|
||||
|
@ -463,7 +463,7 @@ SUITE(strided_span_tests)
|
||||
// retype strided array with regular strides - from multi_span
|
||||
{
|
||||
strided_bounds<2> bounds{ { 2, bytes.size() / 4 }, { bytes.size() / 2, 1 } };
|
||||
multi_span<const byte, 2, dynamic_range> bytes2 = as_multi_span(bytes, dim<2>(), dim<>(bytes.size() / 2));
|
||||
multi_span<const byte, 2, dynamic_range> bytes2 = as_multi_span(bytes, dim<2>(), dim(bytes.size() / 2));
|
||||
strided_span<const byte, 2> sav2{ bytes2, bounds };
|
||||
strided_span<int, 2> sav3 = sav2.as_strided_span<int>();
|
||||
CHECK(sav3[0][0] == 0);
|
||||
@ -475,7 +475,7 @@ SUITE(strided_span_tests)
|
||||
// retype strided array with not enough elements - last dimension of the array is too small
|
||||
{
|
||||
strided_bounds<2> bounds{ { 4,2 },{ 4, 1 } };
|
||||
multi_span<const byte, 2, dynamic_range> bytes2 = as_multi_span(bytes, dim<2>(), dim<>(bytes.size() / 2));
|
||||
multi_span<const byte, 2, dynamic_range> bytes2 = as_multi_span(bytes, dim<2>(), dim(bytes.size() / 2));
|
||||
strided_span<const byte, 2> sav2{ bytes2, bounds };
|
||||
CHECK_THROW(sav2.as_strided_span<int>(), fail_fast);
|
||||
}
|
||||
@ -483,7 +483,7 @@ SUITE(strided_span_tests)
|
||||
// retype strided array with not enough elements - strides are too small
|
||||
{
|
||||
strided_bounds<2> bounds{ { 4,2 },{ 2, 1 } };
|
||||
multi_span<const byte, 2, dynamic_range> bytes2 = as_multi_span(bytes, dim<2>(), dim<>(bytes.size() / 2));
|
||||
multi_span<const byte, 2, dynamic_range> bytes2 = as_multi_span(bytes, dim<2>(), dim(bytes.size() / 2));
|
||||
strided_span<const byte, 2> sav2{ bytes2, bounds };
|
||||
CHECK_THROW(sav2.as_strided_span<int>(), fail_fast);
|
||||
}
|
||||
@ -491,7 +491,7 @@ SUITE(strided_span_tests)
|
||||
// retype strided array with not enough elements - last dimension does not divide by the new typesize
|
||||
{
|
||||
strided_bounds<2> bounds{ { 2,6 },{ 4, 1 } };
|
||||
multi_span<const byte, 2, dynamic_range> bytes2 = as_multi_span(bytes, dim<2>(), dim<>(bytes.size() / 2));
|
||||
multi_span<const byte, 2, dynamic_range> bytes2 = as_multi_span(bytes, dim<2>(), dim(bytes.size() / 2));
|
||||
strided_span<const byte, 2> sav2{ bytes2, bounds };
|
||||
CHECK_THROW(sav2.as_strided_span<int>(), fail_fast);
|
||||
}
|
||||
@ -499,7 +499,7 @@ SUITE(strided_span_tests)
|
||||
// retype strided array with not enough elements - strides does not divide by the new typesize
|
||||
{
|
||||
strided_bounds<2> bounds{ { 2, 1 },{ 6, 1 } };
|
||||
multi_span<const byte, 2, dynamic_range> bytes2 = as_multi_span(bytes, dim<2>(), dim<>(bytes.size() / 2));
|
||||
multi_span<const byte, 2, dynamic_range> bytes2 = as_multi_span(bytes, dim<2>(), dim(bytes.size() / 2));
|
||||
strided_span<const byte, 2> sav2{ bytes2, bounds };
|
||||
CHECK_THROW(sav2.as_strided_span<int>(), fail_fast);
|
||||
}
|
||||
@ -675,17 +675,17 @@ SUITE(strided_span_tests)
|
||||
}
|
||||
|
||||
{
|
||||
auto av = as_multi_span(as_multi_span(arr, 24), dim<>(3), dim<4>(), dim<2>());
|
||||
auto av = as_multi_span(as_multi_span(arr, 24), dim(3), dim<4>(), dim<2>());
|
||||
iterate_second_slice(av);
|
||||
}
|
||||
|
||||
{
|
||||
auto av = as_multi_span(as_multi_span(arr, 24), dim<3>(), dim<>(4), dim<2>());
|
||||
auto av = as_multi_span(as_multi_span(arr, 24), dim<3>(), dim(4), dim<2>());
|
||||
iterate_second_slice(av);
|
||||
}
|
||||
|
||||
{
|
||||
auto av = as_multi_span(as_multi_span(arr, 24), dim<3>(), dim<4>(), dim<>(2));
|
||||
auto av = as_multi_span(as_multi_span(arr, 24), dim<3>(), dim<4>(), dim(2));
|
||||
iterate_second_slice(av);
|
||||
}
|
||||
delete[] arr;
|
||||
@ -704,7 +704,7 @@ SUITE(strided_span_tests)
|
||||
auto d1 = sizeof(int) * 12 / d2;
|
||||
|
||||
// convert to 4x12 array of bytes
|
||||
auto av = as_multi_span(as_bytes(as_multi_span(arr, 4)), dim<>(d1), dim<>(d2));
|
||||
auto av = as_multi_span(as_bytes(as_multi_span(arr, 4)), dim(d1), dim(d2));
|
||||
|
||||
CHECK(av.bounds().index_bounds()[0] == 4);
|
||||
CHECK(av.bounds().index_bounds()[1] == 12);
|
||||
|
Loading…
Reference in New Issue
Block a user