Lodestar
An integrated real-time control package in C++
ArrayStack.hpp
1 //
2 // Created by Hamza El-Kebir on 6/15/21.
3 //
4 
5 #ifndef LODESTAR_ARRAYSTACK_HPP
6 #define LODESTAR_ARRAYSTACK_HPP
7 
8 #include <array>
9 #include <cassert>
10 #include <stdexcept>
11 
12 namespace ls {
13  namespace aux {
20  template<typename TType, size_t TSize>
21  struct ArrayStack {
22  public:
23  ArrayStack() : size_(0)
24  {}
25 
27  using arrayType = std::array<TType, TSize>;
28 
34  static type zero()
35  {
36  type a;
37  a.fill(TType{});
38 
39  return a;
40  }
41 
47  inline size_t size() const noexcept
48  {
49  return size_;
50  }
51 
57  inline constexpr size_t max_size() const noexcept
58  {
59  return TSize;
60  }
61 
68  inline void clear() noexcept
69  {
70  size_ = 0;
71  }
72 
83  inline TType &operator[](size_t idx)
84  {
85  if (idx < 0)
86  throw std::out_of_range("ArrayStack index less than zero.");
87  if (idx >= size_)
88  throw std::out_of_range("ArrayStack index greater than size.");
89 
90  return array_[idx];
91  }
92 
103  inline const TType &operator[](size_t idx) const
104  {
105  if (idx < 0)
106  throw std::out_of_range("ArrayStack index less than zero.");
107  if (idx >= size_)
108  throw std::out_of_range("ArrayStack index greater than size.");
109 
110  return array_[idx];
111  }
112 
118  inline void fill(const TType &value)
119  {
120  std::fill_n(begin(), max_size(), value);
121  size_ = max_size();
122  }
123 
131  inline bool empty() const noexcept
132  {
133  return (size_ == 0);
134  }
135 
144  inline void push(const TType &value)
145  {
146  // Pop last element
147  if (size_ == TSize)
148  pop_back();
149 
150  // Shift right
151  for (int i = size_ - 1; i >= 0; i--)
152  array_[i + 1] = array_[i];
153 
154  array_[0] = value;
155  size_++;
156  }
157 
163  inline void pop_front()
164  {
165  // Shift left
166  for (int i = 0; i < size_ - 1; i++)
167  array_[i] = array_[i + 1];
168 
169  size_--;
170  }
171 
175  inline void pop_back()
176  {
177  if (size_ == 0)
178  return;
179 
180  size_--;
181  }
182 
190  inline TType &front()
191  {
192  // TODO: Replace exception by StatusOr<TType>.
193  if (size_ == 0)
194  throw std::out_of_range("ArrayStack has size 0.");
195 
196  return array_[0];
197  }
198 
206  inline const TType &front() const
207  {
208  // TODO: Replace exception by StatusOr<TType>.
209  if (size_ == 0)
210  throw std::out_of_range("ArrayStack has size 0.");
211 
212  return array_[0];
213  }
214 
222  inline TType &back()
223  {
224  // TODO: Replace exception by StatusOr<TType>.
225  if (size_ == 0)
226  throw std::out_of_range("ArrayStack has size 0.");
227 
228  return array_[size_ - 1];
229  }
230 
238  inline const TType &back() const
239  {
240  // TODO: Replace exception by StatusOr<TType>.
241  if (size_ == 0)
242  throw std::out_of_range("ArrayStack has size 0.");
243 
244  return array_[size_ - 1];
245  }
246 
252  inline typename arrayType::iterator begin() noexcept
253  {
254  return array_.begin();
255  }
256 
262  inline typename arrayType::const_iterator begin() const noexcept
263  {
264  return array_.begin();
265  }
266 
272  inline typename arrayType::iterator end() noexcept
273  {
274  return array_.begin() + size_;
275  }
276 
282  inline typename arrayType::const_iterator end() const noexcept
283  {
284  return array_.begin() + size_;
285  }
286 
287  const arrayType &array() const noexcept
288  {
289  return array_;
290  }
291 
292  protected:
293  arrayType array_;
294  size_t size_;
295  };
296  }
297 }
298 
299 #endif //LODESTAR_ARRAYSTACK_HPP
ls::aux::ArrayStack::operator[]
const TType & operator[](size_t idx) const
Returns a const reference to the element at idx.
Definition: ArrayStack.hpp:103
ls::aux::ArrayStack::pop_front
void pop_front()
Pops first element.
Definition: ArrayStack.hpp:163
ls::aux::ArrayStack::clear
void clear() noexcept
Clears the stack.
Definition: ArrayStack.hpp:68
ls::aux::ArrayStack::back
TType & back()
Returns a reference to the rear value in the stack.
Definition: ArrayStack.hpp:222
ls::aux::ArrayStack::end
arrayType::iterator end() noexcept
Returns an iterator the end of the stack.
Definition: ArrayStack.hpp:272
ls::aux::ArrayStack::begin
arrayType::iterator begin() noexcept
Returns an iterator the beginning of the stack.
Definition: ArrayStack.hpp:252
ls::aux::ArrayStack::zero
static type zero()
Construct a stack filled with the default values of TType.
Definition: ArrayStack.hpp:34
ls::aux::ArrayStack::operator[]
TType & operator[](size_t idx)
Returns a reference to the element at idx.
Definition: ArrayStack.hpp:83
ls::aux::ArrayStack::back
const TType & back() const
Returns a const reference to the rear value in the stack.
Definition: ArrayStack.hpp:238
ls::aux::ArrayStack::end
arrayType::const_iterator end() const noexcept
Returns a const iterator the end of the stack.
Definition: ArrayStack.hpp:282
ls::aux::ArrayStack::front
TType & front()
Returns a reference to the front value in the stack.
Definition: ArrayStack.hpp:190
ls::aux::ArrayStack::fill
void fill(const TType &value)
Fills the stack to full capacity with value.
Definition: ArrayStack.hpp:118
ls
Main Lodestar code.
Definition: BilinearTransformation.hpp:12
ls::aux::ArrayStack::empty
bool empty() const noexcept
Returns true if stack is empty.
Definition: ArrayStack.hpp:131
ls::aux::ArrayStack::front
const TType & front() const
Returns a const reference to the front value in the stack.
Definition: ArrayStack.hpp:206
ls::aux::ArrayStack::begin
arrayType::const_iterator begin() const noexcept
Returns a const iterator the beginning of the stack.
Definition: ArrayStack.hpp:262
ls::aux::ArrayStack
A statically sized stack with full member access.
Definition: ArrayStack.hpp:21
ls::aux::ArrayStack::size
size_t size() const noexcept
Gives current stack size.
Definition: ArrayStack.hpp:47
ls::aux::ArrayStack::pop_back
void pop_back()
Pops last element.
Definition: ArrayStack.hpp:175
ls::aux::ArrayStack::push
void push(const TType &value)
Pushes value to front of ArrayStack.
Definition: ArrayStack.hpp:144
ls::aux::ArrayStack::max_size
constexpr size_t max_size() const noexcept
Gives maximum stack size.
Definition: ArrayStack.hpp:57