CatapultServer  v0.5.0.1 (Elephant)
CompactArrayStack.h
Go to the documentation of this file.
1 
21 #pragma once
22 #include "catapult/constants.h"
23 #include "catapult/exceptions.h"
24 #include "catapult/types.h"
25 
26 namespace catapult { namespace state {
27 
29  template<typename T, size_t N>
31  public:
32  // region const_iterator
33 
38  public:
39  using difference_type = std::ptrdiff_t;
40  using value_type = const T;
41  using pointer = const T*;
42  using reference = const T&;
43  using iterator_category = std::forward_iterator_tag;
44 
45  public:
47  explicit const_iterator(const std::array<T, N>* pArray, size_t index)
48  : m_pArray(pArray)
49  , m_index(index)
50  {}
51 
52  public:
54  bool operator==(const const_iterator& rhs) const {
55  return m_pArray == rhs.m_pArray && m_index == rhs.m_index;
56  }
57 
59  bool operator!=(const const_iterator& rhs) const {
60  return !(*this == rhs);
61  }
62 
63  public:
66  if (isEnd())
67  CATAPULT_THROW_OUT_OF_RANGE("cannot advance iterator beyond end");
68 
69  ++m_index;
70  return *this;
71  }
72 
75  auto copy = *this;
76  ++*this;
77  return copy;
78  }
79 
80  public:
82  reference operator*() const {
83  return *(this->operator->());
84  }
85 
87  pointer operator->() const {
88  if (isEnd())
89  CATAPULT_THROW_OUT_OF_RANGE("cannot dereference at end");
90 
91  return m_pArray ? &(*m_pArray)[m_index] : &m_defaultValue;
92  }
93 
94  private:
95  bool isEnd() const {
96  return N == m_index;
97  }
98 
99  private:
100  const std::array<T, N>* m_pArray;
101  size_t m_index;
103  };
104 
105  // endregion
106 
107  public:
110  {}
111 
114  *this = stack;
115  }
116 
119  : m_pArray(std::move(stack.m_pArray))
120  , m_size(stack.m_size) {
121  stack.m_size = 0;
122  }
123 
124  public:
127  if (stack.m_pArray)
128  m_pArray = std::make_unique<std::array<T, N>>(*stack.m_pArray);
129 
130  m_size = stack.m_size;
131  return *this;
132  }
133 
136  m_pArray = std::move(stack.m_pArray);
137  m_size = stack.m_size;
138  stack.m_size = 0;
139  return *this;
140  }
141 
142  public:
144  size_t size() const {
145  return m_size;
146  }
147 
148  public:
150  const_iterator begin() const {
151  return const_iterator(m_pArray.get(), 0);
152  }
153 
155  const_iterator end() const {
156  return const_iterator(m_pArray.get(), N);
157  }
158 
159  public:
161  const T& peek() const {
162  if (0 == m_size)
163  CATAPULT_THROW_OUT_OF_RANGE("cannot peek when empty");
164 
165  return m_pArray->front();
166  }
167 
169  T& peek() {
170  if (0 == m_size)
171  CATAPULT_THROW_OUT_OF_RANGE("cannot peek when empty");
172 
173  return m_pArray->front();
174  }
175 
176  public:
178  void push(const T& value) {
179  shiftRight();
180  if (m_size < N)
181  ++m_size;
182 
183  if (!m_pArray)
184  m_pArray = std::make_unique<std::array<T, N>>();
185 
186  m_pArray->front() = value;
187  }
188 
190  void pop() {
191  shiftLeft();
192  m_pArray->back() = T();
193  --m_size;
194 
195  if (0 == m_size)
196  m_pArray.reset();
197  }
198 
199  private:
200  void shiftLeft() {
201  if (!m_pArray)
202  CATAPULT_THROW_OUT_OF_RANGE("cannot pop when empty");
203 
204  auto& array = *m_pArray;
205  for (auto i = 0u; i < array.size() - 1; ++i)
206  array[i] = array[i + 1];
207  }
208 
209  void shiftRight() {
210  if (!m_pArray)
211  return;
212 
213  auto& array = *m_pArray;
214  for (auto i = array.size() - 1; i > 0; --i)
215  array[i] = array[i - 1];
216  }
217 
218  private:
219  std::unique_ptr<std::array<T, N>> m_pArray;
220  size_t m_size;
221  };
222 }}
catapult::state::CompactArrayStack::const_iterator::reference
const T & reference
Definition: CompactArrayStack.h:42
catapult::state::CompactArrayStack::CompactArrayStack
CompactArrayStack(const CompactArrayStack &stack)
Copy constructor that makes a deep copy of stack.
Definition: CompactArrayStack.h:113
catapult::state::CompactArrayStack::const_iterator::m_pArray
const std::array< T, N > * m_pArray
Definition: CompactArrayStack.h:100
exceptions.h
catapult::state::CompactArrayStack::CompactArrayStack
CompactArrayStack()
Creates an empty stack.
Definition: CompactArrayStack.h:109
catapult::state::CompactArrayStack::const_iterator::operator++
const_iterator operator++(int)
Advances the iterator to the next position.
Definition: CompactArrayStack.h:74
catapult::state::CompactArrayStack::const_iterator::value_type
const T value_type
Definition: CompactArrayStack.h:40
catapult::state::CompactArrayStack::const_iterator::operator==
bool operator==(const const_iterator &rhs) const
Returns true if this iterator and rhs are equal.
Definition: CompactArrayStack.h:54
catapult::state::CompactArrayStack::const_iterator::pointer
const T * pointer
Definition: CompactArrayStack.h:41
catapult::state::CompactArrayStack::peek
const T & peek() const
Returns a const reference to the element on the top of the stack.
Definition: CompactArrayStack.h:161
catapult::state::CompactArrayStack::operator=
CompactArrayStack & operator=(CompactArrayStack &&stack)
Move assignment operator that assigns stack.
Definition: CompactArrayStack.h:135
catapult::state::CompactArrayStack::operator=
CompactArrayStack & operator=(const CompactArrayStack &stack)
Assignment operator that makes a deep copy of stack.
Definition: CompactArrayStack.h:126
catapult::state::CompactArrayStack::m_size
size_t m_size
Definition: CompactArrayStack.h:220
catapult::state::CompactArrayStack::begin
const_iterator begin() const
Returns a const iterator to the first element of the underlying container.
Definition: CompactArrayStack.h:150
catapult::state::CompactArrayStack::const_iterator::m_index
size_t m_index
Definition: CompactArrayStack.h:101
CATAPULT_THROW_OUT_OF_RANGE
#define CATAPULT_THROW_OUT_OF_RANGE(MESSAGE)
Macro used to throw a catapult out of range.
Definition: exceptions.h:191
catapult::state::CompactArrayStack::shiftLeft
void shiftLeft()
Definition: CompactArrayStack.h:200
catapult::state::CompactArrayStack::shiftRight
void shiftRight()
Definition: CompactArrayStack.h:209
constants.h
catapult::state::CompactArrayStack::const_iterator::iterator_category
std::forward_iterator_tag iterator_category
Definition: CompactArrayStack.h:43
catapult::state::CompactArrayStack
Compact array-based stack that allocates memory dynamically only when it is not empty.
Definition: CompactArrayStack.h:30
catapult::state::CompactArrayStack::const_iterator::operator++
const_iterator & operator++()
Advances the iterator to the next position.
Definition: CompactArrayStack.h:65
catapult::state::CompactArrayStack::end
const_iterator end() const
Returns a const iterator to the element following the last element of the underlying container.
Definition: CompactArrayStack.h:155
catapult::state::CompactArrayStack::push
void push(const T &value)
Pushes value onto the stack.
Definition: CompactArrayStack.h:178
catapult::state::CompactArrayStack::peek
T & peek()
Returns a reference to the element on the top of the stack.
Definition: CompactArrayStack.h:169
catapult::state::CompactArrayStack::const_iterator::operator->
pointer operator->() const
Returns a pointer to the current value.
Definition: CompactArrayStack.h:87
types.h
catapult::state::CompactArrayStack::const_iterator::m_defaultValue
T m_defaultValue
Definition: CompactArrayStack.h:102
catapult
Definition: AddressExtractionExtension.cpp:28
catapult::state::CompactArrayStack::const_iterator::operator*
reference operator*() const
Returns a reference to the current value.
Definition: CompactArrayStack.h:82
catapult::state::CompactArrayStack::pop
void pop()
Pops the top value from the stack.
Definition: CompactArrayStack.h:190
catapult::state::CompactArrayStack::CompactArrayStack
CompactArrayStack(CompactArrayStack &&stack)
Move constructor that move constructs a stack from stack.
Definition: CompactArrayStack.h:118
catapult::state::CompactArrayStack::const_iterator::isEnd
bool isEnd() const
Definition: CompactArrayStack.h:95
catapult::state::CompactArrayStack::const_iterator::difference_type
std::ptrdiff_t difference_type
Definition: CompactArrayStack.h:39
catapult::state::CompactArrayStack::const_iterator::const_iterator
const_iterator(const std::array< T, N > *pArray, size_t index)
Creates an iterator around pArray with index current position.
Definition: CompactArrayStack.h:47
catapult::state::CompactArrayStack::m_pArray
std::unique_ptr< std::array< T, N > > m_pArray
Definition: CompactArrayStack.h:219
catapult::state::CompactArrayStack::const_iterator::operator!=
bool operator!=(const const_iterator &rhs) const
Returns true if this iterator and rhs are not equal.
Definition: CompactArrayStack.h:59
catapult::state::CompactArrayStack::size
size_t size() const
Gets number of non-default values contained in the stack.
Definition: CompactArrayStack.h:144
catapult::state::CompactArrayStack::const_iterator
Definition: CompactArrayStack.h:37