PX4 Firmware
PX4 Autopilot Software http://px4.io
Array.hpp
Go to the documentation of this file.
1 /****************************************************************************
2  *
3  * Copyright (c) 2016 PX4 Development Team. All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  * notice, this list of conditions and the following disclaimer in
13  * the documentation and/or other materials provided with the
14  * distribution.
15  * 3. Neither the name PX4 nor the names of its contributors may be
16  * used to endorse or promote products derived from this software
17  * without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
22  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
23  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
24  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
25  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
26  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
27  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
29  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  * POSSIBILITY OF SUCH DAMAGE.
31  *
32  ****************************************************************************/
33 
34 #pragma once
35 
36 #include <stdlib.h>
37 
38 namespace px4
39 {
40 
41 template <class T, size_t N>
42 class Array
43 {
44 
45 public:
46 
47  bool push_back(const T &x)
48  {
49  if (_size == N) {
50  _overflow = true;
51  return false;
52 
53  } else {
54  _items[_size] = x;
55  ++_size;
56  return true;
57  }
58  }
59 
60  void remove(unsigned idx)
61  {
62  if (idx < _size) {
63  --_size;
64 
65  for (unsigned i = idx; i < _size; ++i) {
66  _items[i] = _items[i + 1];
67  }
68  }
69  }
70 
71  void erase(T *item)
72  {
73  if (item - _items < static_cast<int>(_size)) {
74  --_size;
75 
76  for (T *it = item; it != &_items[_size]; ++it) {
77  *it = *(it + 1);
78  }
79  }
80  }
81 
82  T &operator[](size_t n) { return _items[n]; }
83  const T &operator[](size_t n) const { return _items[n]; }
84 
85  T &at(size_t n) { return _items[n]; }
86  const T &at(size_t n) const { return _items[n]; }
87 
88  size_t size() const { return _size; }
89  size_t max_size() const { return N; }
90  size_t capacity() const { return N; }
91 
92  bool empty() const { return _size == 0; }
93 
94  bool is_overflowed() { return _overflow; }
95 
96  T *begin() { return &_items[0]; }
97  T *end() { return &_items[_size]; }
98 
99  const T *begin() const { return &_items[0]; }
100  const T *end() const { return &_items[_size]; }
101 
102 private:
103  T _items[N];
104  size_t _size{0};
105  bool _overflow{false};
106 };
107 
108 }
T * begin()
Definition: Array.hpp:96
T & at(size_t n)
Definition: Array.hpp:85
size_t size() const
Definition: Array.hpp:88
bool push_back(const T &x)
Definition: Array.hpp:47
const T * begin() const
Definition: Array.hpp:99
bool empty() const
Definition: Array.hpp:92
const T & operator[](size_t n) const
Definition: Array.hpp:83
size_t capacity() const
Definition: Array.hpp:90
size_t max_size() const
Definition: Array.hpp:89
size_t _size
Definition: Array.hpp:104
bool _overflow
Definition: Array.hpp:105
Definition: bst.cpp:62
void erase(T *item)
Definition: Array.hpp:71
T _items[N]
Definition: Array.hpp:103
T & operator[](size_t n)
Definition: Array.hpp:82
const T * end() const
Definition: Array.hpp:100
T * end()
Definition: Array.hpp:97
const T & at(size_t n) const
Definition: Array.hpp:86
bool is_overflowed()
Definition: Array.hpp:94