siplasplas
arrayview.hpp
1 #ifndef SIPLASPLAS_CONSTEXPR_ARRAYVIEW_HPP
2 #define SIPLASPLAS_CONSTEXPR_ARRAYVIEW_HPP
3 
4 #include "algorithm.hpp"
5 #include <array>
6 #include <ostream>
7 
8 namespace cpp
9 {
10 
11 namespace constexp
12 {
13 
14 template<typename T>
15 class ArrayView
16 {
17 public:
18  constexpr ArrayView(T* begin, std::size_t size) :
19  _begin{begin},
20  _size{size}
21  {}
22 
23  constexpr ArrayView(T* begin, T* end) :
24  ArrayView{begin, static_cast<std::size_t>(end - begin)}
25  {}
26 
27  constexpr ArrayView(std::initializer_list<T> initList) :
28  ArrayView{const_cast<T*>(initList.begin()), const_cast<T*>(initList.end())}
29  {}
30 
31  template<std::size_t N>
32  constexpr ArrayView(const T (&array)[N]) :
33  ArrayView{array, N}
34  {}
35 
36  template<std::size_t N>
37  constexpr ArrayView(const std::array<T, N>& array) :
38  ArrayView{array.begin(), N}
39  {}
40 
41  constexpr std::size_t size() const
42  {
43  return _size;
44  }
45 
46  constexpr T* begin() const
47  {
48  return _begin;
49  }
50 
51  constexpr T* end() const
52  {
53  return _begin + _size;
54  }
55 
56  constexpr T* const cbegin() const
57  {
58  return begin();
59  }
60 
61  constexpr T* const cend() const
62  {
63  return end();
64  }
65 
66  constexpr ArrayView operator()(std::size_t begin, std::size_t end) const
67  {
68  return { _begin + begin, _begin + end };
69  }
70 
71  constexpr T* operator()(std::size_t i) const
72  {
73  return _begin + i;
74  }
75 
76  constexpr T operator[](std::size_t i) const
77  {
78  return _begin[i];
79  }
80 
81 private:
82  T* _begin;
83  std::size_t _size;
84 };
85 
86 template<typename T>
88 {
89 public:
90  constexpr ConstArrayView(const T* begin, std::size_t size) :
91  _begin{begin},
92  _size{size}
93  {}
94 
95  constexpr ConstArrayView(const T* begin, const T* end) :
96  ConstArrayView{begin, static_cast<std::size_t>(end - begin)}
97  {}
98 
99  template<std::size_t N>
100  constexpr ConstArrayView(const T (&array)[N]) :
101  ConstArrayView{array, N}
102  {}
103 
104  template<std::size_t N>
105  constexpr ConstArrayView(const std::array<T, N>& array) :
106  ConstArrayView{array.begin(), N}
107  {}
108 
109  constexpr ConstArrayView(std::initializer_list<T> initList) :
110  ConstArrayView{initList.begin(), initList.end()}
111  {}
112 
113  constexpr std::size_t size() const
114  {
115  return _size;
116  }
117 
118  constexpr const T* begin() const
119  {
120  return _begin;
121  }
122 
123  constexpr const T* end() const
124  {
125  return _begin + _size;
126  }
127 
128  constexpr const T* const cbegin() const
129  {
130  return begin();
131  }
132 
133  constexpr const T* const cend() const
134  {
135  return end();
136  }
137 
138  constexpr ConstArrayView operator()(std::size_t begin, std::size_t end) const
139  {
140  return { _begin + begin, _begin + end };
141  }
142 
143  constexpr const T* operator()(std::size_t i) const
144  {
145  return _begin + i;
146  }
147 
148  constexpr T operator[](std::size_t i) const
149  {
150  return _begin[i];
151  }
152 
153 private:
154  const T* _begin;
155  std::size_t _size;
156 };
157 
158 template<typename T>
159 std::ostream& operator<<(std::ostream& os, const ArrayView<T>& arrayView)
160 {
161  os << "{";
162 
163  for(std::size_t i = 0; i < arrayView.size(); ++i)
164  {
165  if(i < arrayView.size() - 1)
166  {
167  os << arrayView[i] << ", ";
168  }
169  else
170  {
171  os << arrayView[i];
172  }
173  }
174 
175  return os << "}";
176 }
177 
178 template<typename T>
179 std::ostream& operator<<(std::ostream& os, const ConstArrayView<T>& arrayView)
180 {
181  os << "{";
182 
183  for(std::size_t i = 0; i < arrayView.size(); ++i)
184  {
185  if(i < arrayView.size() - 1)
186  {
187  os << arrayView[i] << ", ";
188  }
189  else
190  {
191  os << arrayView[i];
192  }
193  }
194 
195  return os << "}";
196 }
197 
198 inline std::ostream& operator<<(std::ostream& os, const ArrayView<char>& arrayView)
199 {
200  for(const char c : arrayView)
201  {
202  os << c;
203  }
204 
205  return os;
206 }
207 
208 inline std::ostream& operator<<(std::ostream& os, const ConstArrayView<char>& arrayView)
209 {
210  for(const char c : arrayView)
211  {
212  os << c;
213  }
214 
215  return os;
216 }
217 
218 template<typename T, typename U>
219 constexpr bool operator==(const ArrayView<T>& lhs, const ArrayView<U>& rhs)
220 {
221  return cpp::constexp::equal(
222  lhs.begin(), lhs.end(),
223  rhs.begin(), rhs.end()
224  );
225 }
226 
227 template<typename T, typename U>
228 constexpr bool operator==(const ConstArrayView<T>& lhs, const ArrayView<U>& rhs)
229 {
230  return cpp::constexp::equal(
231  lhs.begin(), lhs.end(),
232  rhs.begin(), rhs.end()
233  );
234 }
235 
236 template<typename T, typename U>
237 constexpr bool operator==(const ConstArrayView<T>& lhs, const ConstArrayView<U>& rhs)
238 {
239  return cpp::constexp::equal(
240  lhs.begin(), lhs.end(),
241  rhs.begin(), rhs.end()
242  );
243 }
244 
245 template<typename T, typename U>
246 constexpr bool operator==(const ArrayView<T>& lhs, const ConstArrayView<U>& rhs)
247 {
248  return cpp::constexp::equal(
249  lhs.begin(), lhs.end(),
250  rhs.begin(), rhs.end()
251  );
252 }
253 
254 template<typename T, typename U>
255 constexpr bool operator!=(const ArrayView<T>& lhs, const ArrayView<U>& rhs)
256 {
257  return !(lhs == rhs);
258 }
259 
260 template<typename T, typename U>
261 constexpr bool operator!=(const ConstArrayView<T>& lhs, const ArrayView<U>& rhs)
262 {
263  return !(lhs == rhs);
264 }
265 
266 template<typename T, typename U>
267 constexpr bool operator!=(const ConstArrayView<T>& lhs, const ConstArrayView<U>& rhs)
268 {
269  return !(lhs == rhs);
270 }
271 
272 template<typename T, typename U>
273 constexpr bool operator!=(const ArrayView<T>& lhs, const ConstArrayView<U>& rhs)
274 {
275  return !(lhs == rhs);
276 }
277 
278 template<typename T, std::size_t N>
279 constexpr ArrayView<T> arrayView(const T (&array)[N])
280 {
281  return { array };
282 }
283 
284 template<typename T, std::size_t N>
285 constexpr ConstArrayView<T> constArrayView(const T (&array)[N])
286 {
287  return { array };
288 }
289 
290 }
291 
292 }
293 
294 #endif // SIPLASPLAS_CONSTEXPR_ARRAYVIEW_HPP
Definition: arrayview.hpp:87
Definition: messaging.hpp:8
constexpr bool equal(Begin1 begin1, End1 end1, Begin2 begin2, End2 end2, Compare compare)
Compares if two sequences are equal.
Definition: algorithm.hpp:137
constexpr auto end(const Sequence &sequence)
Returns an iterator pointing to the end of a sequence.
Definition: algorithm.hpp:86
constexpr auto begin(const Sequence &sequence)
Returns an iterator pointing to the beginning of a sequence.
Definition: algorithm.hpp:62
Definition: arrayview.hpp:15