-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharray_wrapper.hpp
More file actions
215 lines (180 loc) · 8.95 KB
/
array_wrapper.hpp
File metadata and controls
215 lines (180 loc) · 8.95 KB
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
#ifndef FIBB_ARRAY_WRAPPER
#define FIBB_ARRAY_WRAPPER
#include <iterator>
#include <type_traits>
#include <algorithm>
#include <stdexcept>
#include <array>
namespace fibb
{
/* The Array_Wrapper is intended to wrap a raw C array so that it can be passed to a template
expecting a std::array. All methods are designed to give the same functionality as those
of std::array.
The Array_Wrapper does not own the underlying array and as such the underlying array must
exist for the lifetime of the Array_Wrapper.
Although std::array supports a size of zero as a special case, zero sized raw arrays are illegal
in C++, therefore passing a zero sized raw array to Array_Wrapper is undefined behaviour. */
template <typename T, size_t N>
class Array_Wrapper
{
public:
/* TYPES */
using value_type = T;
using pointer = T*;
using const_pointer = const T*;
using reference = T&;
using const_reference = const T&;
using size_type = size_t;
using difference_type = ptrdiff_t;
using iterator = T*;
using const_iterator = const T*;
using reverse_iterator = std::reverse_iterator<iterator>;
using const_reverse_iterator = std::reverse_iterator<const_iterator>;
/* CONSTRUCTORS */
Array_Wrapper(T (&array_)[N]) // sized array
: m_array(array_)
{
static_assert(N > 0);
}
Array_Wrapper(T*& array_) // decayed array pointer, size must be known at compile time
: m_array(array_)
{
static_assert(N > 0);
}
/* COMPARISON */
constexpr bool operator==(const Array_Wrapper& other) const noexcept
{
return std::equal(begin(), end(), other.begin());
}
constexpr bool operator!=(const Array_Wrapper& other) const noexcept { return !(*this == other); }
constexpr bool operator<(const Array_Wrapper& other) const noexcept
{
return std::lexicographical_compare(begin(), end(), other.begin(), other.end());
}
constexpr bool operator>(const Array_Wrapper& other) const noexcept
{
auto comp = [] (const_reference a, const_reference b) -> bool { return a > b; };
return std::lexicographical_compare(begin(), end(), other.begin(), other.end(), comp);
}
constexpr bool operator<=(const Array_Wrapper& other) const noexcept
{
auto comp = [] (const_reference a, const_reference b) -> bool { return a <= b; };
return std::lexicographical_compare(begin(), end(), other.begin(), other.end(), comp);
}
constexpr bool operator>=(const Array_Wrapper& other) const noexcept
{
auto comp = [] (const_reference a, const_reference b) -> bool { return a >= b; };
return std::lexicographical_compare(begin(), end(), other.begin(), other.end(), comp);
}
/* ASSIGNMENT */
// copy assignment will copy elements into the underlying array
constexpr Array_Wrapper& operator=(const Array_Wrapper& other)
noexcept(std::is_nothrow_copy_assignable_v<value_type>)
{
if (*this != other)
{
std::copy(other.begin(), other.end(), begin());
}
return *this;
}
// copy assignment will copy elements into the underlying array
constexpr Array_Wrapper& operator=(const std::array<value_type, N>& other)
noexcept(std::is_nothrow_copy_assignable_v<value_type>)
{
if (m_array != other.data())
{
// Iterators for std::array are implementation defined
// The data() method always returns a raw ptr
std::copy(other.data(), other.data() + other.size(), begin());
}
return *this;
}
// move assignment will move elements into the underlying array
constexpr Array_Wrapper& operator=(Array_Wrapper&& other)
noexcept(std::is_nothrow_move_assignable_v<value_type>)
{
if (*this != other)
{
std::move(other.begin(), other.end(), begin());
}
return *this;
}
// move assignment will move elements into the underlying array
constexpr Array_Wrapper& operator=(std::array<value_type, N>&& other)
noexcept(std::is_nothrow_move_assignable_v<value_type>)
{
if (m_array != other.data())
{
// Iterators for std::array are implementation defined
// The data() method always returns a raw ptr
std::move(other.data(), other.data() + other.size(), begin());
}
return *this;
}
void fill(const_reference val) { std::fill_n(begin(), N, val); }
// Note that swap will not switch the internal pointers
// Array_Wrapper will behave as if it were std::array so swap actually swaps elements of the internal array
void swap(Array_Wrapper& other) noexcept(std::is_nothrow_swappable_v<value_type>)
{
if (*this != other)
{
std::swap_ranges(begin(), end(), other.begin());
}
}
void swap(std::array<value_type, N>& other) noexcept(std::is_nothrow_swappable_v<value_type>)
{
if (m_array != other.data())
{
// Swap ranges expects 3 iterators of the same type
// Iterators for std::array are implementation defined
// The data() method always returns a raw ptr
std::swap_ranges(begin(), end(), other.data());
}
}
/* ITERATORS */
constexpr iterator begin() noexcept { return m_array; }
constexpr const_iterator begin() const noexcept { return m_array; }
constexpr const_iterator cbegin() const noexcept { return m_array; }
constexpr iterator end() noexcept { return m_array + N; }
constexpr const_iterator end() const noexcept { return m_array + N; }
constexpr const_iterator cend() const noexcept { return m_array + N; }
constexpr reverse_iterator rbegin() noexcept { return reverse_iterator(end()); }
constexpr const_reverse_iterator rbegin() const noexcept { return const_reverse_iterator(cend()); }
constexpr const_reverse_iterator crbegin() const noexcept { return const_reverse_iterator(cend()); }
constexpr reverse_iterator rend() noexcept { return reverse_iterator(begin()); }
constexpr const_reverse_iterator rend() const noexcept { return const_reverse_iterator(cbegin()); }
constexpr const_reverse_iterator crend() const noexcept { return const_reverse_iterator(cbegin()); }
/* CAPACITY */
constexpr size_type size() const noexcept { return N; }
constexpr size_type max_size() const noexcept { return N; }
constexpr bool empty() const noexcept { return N == 0; }
/* ELEMENT ACCESS */
constexpr reference operator[](size_type pos) noexcept { return m_array[pos]; }
constexpr const_reference operator[](size_type pos) const noexcept { return m_array[pos]; }
constexpr reference at(size_type pos)
{
const Array_Wrapper& const_this = *this;
return const_cast<reference>(const_this.at(pos));
}
constexpr const_reference at(size_type pos) const
{
static_assert(std::is_unsigned_v<size_type>);
if (pos >= N) { raise_range_error(pos); }
return m_array[pos];
}
constexpr reference front() noexcept { return m_array[0]; }
constexpr const_reference front() const noexcept { return m_array[0]; }
constexpr reference back() noexcept { return m_array[N - 1]; }
constexpr const_reference back() const noexcept { return m_array[N - 1]; }
constexpr pointer data() noexcept { return m_array; }
constexpr const_pointer data() const noexcept { return m_array; }
private:
pointer m_array;
// ideally this would go into a separate cpp file to prevent inlining
constexpr void raise_range_error(size_type pos) const
{
throw std::out_of_range(std::string("Out of range: ") + std::to_string(pos));
}
};
}
#endif // FIBB_ARRAY_WRAPPER