-
Notifications
You must be signed in to change notification settings - Fork 1
/
span.cc
95 lines (80 loc) · 2.04 KB
/
span.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
// Span is a (pointer,count) pair with support for range-based for loop.
#include <cstddef>
#include <memory>
#include <numeric>
#include <functional>
#define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN
#include "doctest/doctest.h"
// Span captures pointer-and-count for range-based for loop.
template <typename T>
struct Span
{
Span() = default;
Span(T* p, std::size_t count)
: p(p)
, count(count)
{ }
~Span() = default; // Span doesn't own any resource.
T* p = nullptr;
std::size_t count = 0;
};
// begin satisfies requirements for range-based for loop.
template <typename T>
T* begin(Span<T>& s)
{
return s.p;
}
// end satisfies requirements for range-based for loop.
template <typename T>
T* end(Span<T>& s)
{
return s.count > 0 ? s.p+s.count : nullptr;
}
// cbegin satisfies requirements for range-based for loop.
template <typename T>
const T* cbegin(const Span<T>& s)
{
return s.p;
}
// cend satisfies requirements for range-based for loop.
template <typename T>
const T* cend(const Span<T>& s)
{
return s.count > 0 ? s.p+s.count : nullptr;
}
TEST_CASE("[Span]")
{
// One use-case for Span is to wrap a C-style array.
constexpr std::size_t N = 100;
std::unique_ptr<int, std::function<void(int*)>> a1(
static_cast<int*>(std::malloc(sizeof(int)*N)),
[](int* pa) { std::free(pa); } // Invoke when exiting scope.
);
int *pa1 = a1.get();
for (std::size_t i = 0; i < N; ++i) {
pa1[i] = int(i)+1;
}
SUBCASE("Range-based for loop, non-const reference")
{
Span s{pa1, N};
int i = 1;
for (auto& v : s) {
REQUIRE(v == i++);
}
}
SUBCASE("Range-based for loop, const reference")
{
Span s{pa1, N};
int i = 1;
for (const auto& v : s) {
REQUIRE(v == i++);
}
}
SUBCASE("Use begin/end with algorithm")
{
Span s{pa1, N};
auto sum = std::accumulate(begin(s), end(s), 0);
auto expected = N*(N+1)/2;
REQUIRE(sum == expected);
}
}