Embedded Template Library  1.0
 All Classes Files Functions Variables Typedefs Friends Modules Pages
array.h
Go to the documentation of this file.
1 
3 /******************************************************************************
4 The MIT License(MIT)
5 
6 Embedded Template Library.
7 
8 Copyright(c) 2014 jwellbelove
9 
10 Permission is hereby granted, free of charge, to any person obtaining a copy
11 of this software and associated documentation files(the "Software"), to deal
12 in the Software without restriction, including without limitation the rights
13 to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
14 copies of the Software, and to permit persons to whom the Software is
15 furnished to do so, subject to the following conditions :
16 
17 The above copyright notice and this permission notice shall be included in all
18 copies or substantial portions of the Software.
19 
20 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
23 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26 SOFTWARE.
27 ******************************************************************************/
28 
29 #ifndef __ETL_ARRAY__
30 #define __ETL_ARRAY__
31 
32 #include <iterator>
33 #include <functional>
34 #include <algorithm>
35 #include <stddef.h>
36 
37 #include "exception.h"
38 #include "type_traits.h"
39 #include "parameter_type.h"
40 #include "static_assert.h"
41 
42 #ifndef ETL_THROW_EXCEPTIONS
43 #include "error_handler.h"
44 #endif
45 
49 
50 namespace etl
51 {
52  //***************************************************************************
55  //***************************************************************************
56  class array_exception : public exception
57  {
58  public:
59 
60  array_exception(const char* what)
61  : exception(what)
62  {
63  }
64  };
65 
66  //***************************************************************************
69  //***************************************************************************
71  {
72  public:
73 
75  : array_exception("array: out of range")
76  {
77  }
78  };
79 
80  //***************************************************************************
83  //***************************************************************************
84  template <typename T, const size_t SIZE>
85  class array
86  {
87  private:
88 
89  typedef typename parameter_type<T>::type parameter_t;
90 
91  public:
92 
93  typedef T value_type;
94  typedef std::size_t size_type;
95  typedef std::ptrdiff_t difference_type;
96  typedef T& reference;
97  typedef const T& const_reference;
98  typedef T* pointer;
99  typedef const T* const_pointer;
100  typedef T* iterator;
101  typedef const T* const_iterator;
102  typedef std::reverse_iterator<iterator> reverse_iterator;
103  typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
104 
105  //*************************************************************************
106  // Element access
107  //*************************************************************************
108 
109  //*************************************************************************
114  //*************************************************************************
115  reference at(size_t i)
116  {
117 
118  if (i >= SIZE)
119  {
120 #ifdef ETL_THROW_EXCEPTIONS
121  throw array_out_of_range();
122 #else
124 #endif
125  }
126 
127  return _buffer[i];
128  }
129 
130  //*************************************************************************
135  //*************************************************************************
136  const_reference at(size_t i) const
137  {
138  if (i >= SIZE)
139  {
140 #ifdef ETL_THROW_EXCEPTIONS
141  throw array_out_of_range();
142 #else
144 #endif
145  }
146 
147  return _buffer[i];
148  }
149 
150  //*************************************************************************
154  //*************************************************************************
155  reference operator[](size_t i)
156  {
157  return _buffer[i];
158  }
159 
160  //*************************************************************************
164  //*************************************************************************
165  const_reference operator[](size_t i) const
166  {
167  return _buffer[i];
168  }
169 
170  //*************************************************************************
172  //*************************************************************************
173  reference front()
174  {
175  return _buffer[0];
176  }
177 
178  //*************************************************************************
180  //*************************************************************************
181  const_reference front() const
182  {
183  return _buffer[0];
184  }
185 
186  //*************************************************************************
188  //*************************************************************************
189  reference back()
190  {
191  return _buffer[SIZE - 1];
192  }
193 
194  //*************************************************************************
196  //*************************************************************************
197  const_reference back() const
198  {
199  return _buffer[SIZE - 1];
200  }
201 
202  //*************************************************************************
204  //*************************************************************************
205  pointer data()
206  {
207  return &_buffer[0];
208  }
209 
210  //*************************************************************************
212  //*************************************************************************
213  const_pointer data() const
214  {
215  return &_buffer[0];
216  }
217 
218  //*************************************************************************
219  // Iterators
220  //*************************************************************************
221 
222  //*************************************************************************
224  //*************************************************************************
225  iterator begin()
226  {
227  return &_buffer[0];
228  }
229 
230  //*************************************************************************
232  //*************************************************************************
233  const_iterator begin() const
234  {
235  return &_buffer[0];
236  }
237 
238  //*************************************************************************
240  //*************************************************************************
241  const_iterator cbegin() const
242  {
243  return begin();
244  }
245 
246  //*************************************************************************
248  //*************************************************************************
249  iterator end()
250  {
251  return &_buffer[SIZE];
252  }
253 
254  //*************************************************************************
256  //*************************************************************************
257  const_iterator end() const
258  {
259  return &_buffer[SIZE];
260  }
261 
262  //*************************************************************************
263  // Returns a const iterator to the end of the array.
264  //*************************************************************************
265  const_iterator cend() const
266  {
267  return &_buffer[SIZE];
268  }
269 
270  //*************************************************************************
271  // Returns an reverse iterator to the reverse beginning of the array.
272  //*************************************************************************
273  reverse_iterator rbegin()
274  {
275  return reverse_iterator(end());
276  }
277 
278  //*************************************************************************
280  //*************************************************************************
281  const_reverse_iterator rbegin() const
282  {
283  return const_reverse_iterator(end());
284  }
285 
286  //*************************************************************************
288  //*************************************************************************
289  const_reverse_iterator crbegin() const
290  {
291  return const_reverse_iterator(end());
292  }
293 
294  //*************************************************************************
296  //*************************************************************************
297  reverse_iterator rend()
298  {
299  return reverse_iterator(begin());
300  }
301 
302  //*************************************************************************
304  //*************************************************************************
305  const_reverse_iterator rend() const
306  {
307  return const_reverse_iterator(begin());
308  }
309 
310  //*************************************************************************
312  //*************************************************************************
313  const_reverse_iterator crend() const
314  {
315  return const_reverse_iterator(begin());
316  }
317 
318  //*************************************************************************
319  // Capacity
320  //*************************************************************************
321 
322  //*************************************************************************
324  //*************************************************************************
325  bool empty() const
326  {
327  return (SIZE == 0);
328  }
329 
330  //*************************************************************************
332  //*************************************************************************
333  size_t size() const
334  {
335  return SIZE;
336  }
337 
338  //*************************************************************************
340  //*************************************************************************
341  size_t max_size() const
342  {
343  return SIZE;
344  }
345 
346  //*************************************************************************
347  // Operations
348  //*************************************************************************
349 
350  //*************************************************************************
353  //*************************************************************************
354  void fill(parameter_t value)
355  {
356  std::fill(begin(), end(), value);
357  }
358 
359  //*************************************************************************
362  //*************************************************************************
363  void swap(array& other)
364  {
365  for (size_t i = 0; i < SIZE; ++i)
366  {
367  std::swap(_buffer[i], other._buffer[i]);
368  }
369  }
370 
372  T _buffer[SIZE];
373  };
374 
375  //*************************************************************************
379  //*************************************************************************
380  template <typename T, const size_t SIZE>
382  {
383  lhs.swap(rhs);
384  }
385 
386  //*************************************************************************
391  //*************************************************************************
392  template <typename T, std::size_t SIZE>
393  bool operator ==(const etl::array<T, SIZE>& lhs, const etl::array<T, SIZE>& rhs)
394  {
395  return std::equal(lhs.cbegin(), lhs.cend(), rhs.cbegin());
396  }
397 
398  //*************************************************************************
403  //*************************************************************************
404  template <typename T, std::size_t SIZE>
405  bool operator !=(const etl::array<T, SIZE>& lhs, const etl::array<T, SIZE>& rhs)
406  {
407  return !(lhs == rhs);
408  }
409 
410  //*************************************************************************
415  //*************************************************************************
416  template <typename T, std::size_t SIZE>
417  bool operator <(const etl::array<T, SIZE>& lhs, const etl::array<T, SIZE>& rhs)
418  {
419  return std::lexicographical_compare(lhs.cbegin(),
420  lhs.cend(),
421  rhs.cbegin(),
422  rhs.cend());
423  }
424 
425  //*************************************************************************
430  //*************************************************************************
431  template <typename T, std::size_t SIZE>
432  bool operator <=(const etl::array<T, SIZE>& lhs, const etl::array<T, SIZE>& rhs)
433  {
434  return !std::lexicographical_compare(lhs.cbegin(),
435  lhs.cend(),
436  rhs.cbegin(),
437  rhs.cend(),
438  std::greater<T>());
439  }
440 
441  //*************************************************************************
446  template <typename T, std::size_t SIZE>
447  //*************************************************************************
448  bool operator >(const etl::array<T, SIZE>& lhs, const etl::array<T, SIZE>& rhs)
449  {
450  return std::lexicographical_compare(lhs.cbegin(),
451  lhs.cend(),
452  rhs.cbegin(),
453  rhs.cend(),
454  std::greater<T>());
455  }
456 
457  //*************************************************************************
462  //*************************************************************************
463  template <typename T, std::size_t SIZE>
464  bool operator >=(const etl::array<T, SIZE>& lhs, const etl::array<T, SIZE>& rhs)
465  {
466  return !std::lexicographical_compare(lhs.cbegin(),
467  lhs.cend(),
468  rhs.cbegin(),
469  rhs.cend());
470  }
471 
472  //*************************************************************************
479  //*************************************************************************
480  template <std::size_t I, typename T, std::size_t N>
481  inline T& get(array<T, N>& a)
482  {
483  STATIC_ASSERT(I < N, "Index out of bounds");
484  return a[I];
485  }
486 
487  //*************************************************************************
494  //*************************************************************************
495  template <std::size_t I, typename T, std::size_t N>
496  inline const T& get(const array<T, N>& a)
497  {
498  STATIC_ASSERT(I < N, "Index out of bounds");
499  return a[I];
500  }
501 }
502 
503 #endif
504 
const_reference operator[](size_t i) const
Definition: array.h:165
pointer data()
Returns a pointer to the first element of the internal buffer.
Definition: array.h:205
reference operator[](size_t i)
Definition: array.h:155
bool empty() const
Returns true if the array size is zero.
Definition: array.h:325
void fill(parameter_t value)
Definition: array.h:354
Definition: array.h:70
const_reverse_iterator crbegin() const
Returns a const reverse iterator to the reverse beginning of the array.
Definition: array.h:289
void swap(etl::bitset< N > &lhs, etl::bitset< N > &rhs)
swap
Definition: bitset.h:1200
Determine how to pass parameters.
Definition: parameter_type.h:40
size_t max_size() const
Returns the maximum possible size of the array.
Definition: array.h:341
reference at(size_t i)
Definition: array.h:115
size_t size() const
Returns the size of the array.
Definition: array.h:333
const_iterator end() const
Returns a const iterator to the end of the array.
Definition: array.h:257
exception(value_type reason)
Constructor.
Definition: exception.h:51
Definition: array.h:56
Definition: algorithm.h:43
iterator begin()
Returns an iterator to the beginning of the array.
Definition: array.h:225
reference back()
Returns a reference to the last element.
Definition: array.h:189
Definition: array.h:85
const_pointer data() const
Returns a const pointer to the first element of the internal buffer.
Definition: array.h:213
const_iterator begin() const
Returns a const iterator to the beginning of the array.
Definition: array.h:233
const_reverse_iterator rend() const
Returns a const reverse iterator to the end of the array.
Definition: array.h:305
Definition: exception.h:42
const_iterator cbegin() const
Returns a const iterator to the beginning of the array.
Definition: array.h:241
iterator end()
Returns an iterator to the end of the array.
Definition: array.h:249
const_reverse_iterator crend() const
Returns a const reverse iterator to the end of the array.
Definition: array.h:313
value_type what() const
Definition: exception.h:60
static void error(const exception &e)
Definition: error_handler.cpp:50
const_reference back() const
Returns a const reference to the last element.
Definition: array.h:197
reverse_iterator rend()
Returns a reverse iterator to the end of the array.
Definition: array.h:297
T _buffer[SIZE]
The array data.
Definition: array.h:372
void swap(array &other)
Definition: array.h:363
const_reference front() const
Returns a const reference to the first element.
Definition: array.h:181
const_reverse_iterator rbegin() const
Returns a const reverse iterator to the reverse beginning of the array.
Definition: array.h:281
reference front()
Returns a reference to the first element.
Definition: array.h:173
const_reference at(size_t i) const
Definition: array.h:136