CppCommon  1.0.4.1
C++ Common Library
stack.inl
Go to the documentation of this file.
1 
9 namespace CppCommon {
10 
11 template <typename T>
12 template <class InputIterator>
13 inline Stack<T>::Stack(InputIterator first, InputIterator last) noexcept
14 {
15  for (auto it = first; it != last; ++it)
16  push(*it);
17 }
18 
19 template <typename T>
20 inline typename Stack<T>::iterator Stack<T>::begin() noexcept
21 {
22  return iterator(top());
23 }
24 
25 template <typename T>
26 inline typename Stack<T>::const_iterator Stack<T>::begin() const noexcept
27 {
28  return const_iterator(top());
29 }
30 
31 template <typename T>
32 inline typename Stack<T>::const_iterator Stack<T>::cbegin() const noexcept
33 {
34  return const_iterator(top());
35 }
36 
37 template <typename T>
38 inline typename Stack<T>::iterator Stack<T>::end() noexcept
39 {
40  return iterator(nullptr);
41 }
42 
43 template <typename T>
44 inline typename Stack<T>::const_iterator Stack<T>::end() const noexcept
45 {
46  return const_iterator(nullptr);
47 }
48 
49 template <typename T>
50 inline typename Stack<T>::const_iterator Stack<T>::cend() const noexcept
51 {
52  return const_iterator(nullptr);
53 }
54 
55 template <typename T>
56 inline void Stack<T>::push(T& item) noexcept
57 {
58  item.next = _top;
59  _top = &item;
60  ++_size;
61 }
62 
63 template <typename T>
64 inline T* Stack<T>::pop() noexcept
65 {
66  if (_top == nullptr)
67  return nullptr;
68 
69  T* result = _top;
70  _top = result->next;
71  result->next = nullptr;
72  --_size;
73  return result;
74 }
75 
76 template <typename T>
77 inline void Stack<T>::reverse() noexcept
78 {
79  T* current = _top;
80  T* prev = nullptr;
81  T* next = nullptr;
82 
83  while (current != nullptr)
84  {
85  next = current->next;
86  current->next = prev;
87  prev = current;
88  current = next;
89  }
90  _top = prev;
91 }
92 
93 template <typename T>
94 inline void Stack<T>::clear() noexcept
95 {
96  _size = 0;
97  _top = nullptr;
98 }
99 
100 template <typename T>
101 inline void Stack<T>::swap(Stack& stack) noexcept
102 {
103  using std::swap;
104  swap(_size, stack._size);
105  swap(_top, stack._top);
106 }
107 
108 template <typename T>
109 inline void swap(Stack<T>& stack1, Stack<T>& stack2) noexcept
110 {
111  stack1.swap(stack2);
112 }
113 
114 template <typename T>
116 {
117  if (_node != nullptr)
118  _node = _node->next;
119  return *this;
120 }
121 
122 template <typename T>
124 {
125  StackIterator<T> result(*this);
126  operator++();
127  return result;
128 }
129 
130 template <typename T>
132 {
133  assert((_node != nullptr) && "Iterator must be valid!");
134 
135  return *_node;
136 }
137 
138 template <typename T>
140 {
141  return _node;
142 }
143 
144 template <typename T>
146 {
147  using std::swap;
148  swap(_node, it._node);
149 }
150 
151 template <typename T>
152 void swap(StackIterator<T>& it1, StackIterator<T>& it2) noexcept
153 {
154  it1.swap(it2);
155 }
156 
157 template <typename T>
159 {
160  if (_node != nullptr)
161  _node = _node->next;
162  return *this;
163 }
164 
165 template <typename T>
167 {
168  StackConstIterator<T> result(*this);
169  operator++();
170  return result;
171 }
172 
173 template <typename T>
175 {
176  assert((_node != nullptr) && "Iterator must be valid!");
177 
178  return *_node;
179 }
180 
181 template <typename T>
183 {
184  return _node;
185 }
186 
187 template <typename T>
189 {
190  using std::swap;
191  swap(_node, it._node);
192 }
193 
194 template <typename T>
196 {
197  it1.swap(it2);
198 }
199 
200 } // namespace CppCommon
Intrusive stack constant iterator.
Definition: stack.h:245
const value_type & const_reference
Definition: stack.h:250
void swap(StackConstIterator &it) noexcept
Swap two instances.
Definition: stack.inl:188
const_reference operator*() const noexcept
Definition: stack.inl:174
const value_type * const_pointer
Definition: stack.h:252
StackConstIterator & operator++() noexcept
Definition: stack.inl:158
const_pointer operator->() const noexcept
Definition: stack.inl:182
Intrusive stack container.
Definition: stack.h:106
void push(T &item) noexcept
Push a new item into the top of the stack.
Definition: stack.inl:56
void reverse() noexcept
Reverse the stack.
Definition: stack.inl:77
const_iterator cend() const noexcept
Definition: stack.inl:50
T * pop() noexcept
Pop the item from the top of the stack.
Definition: stack.inl:64
iterator end() noexcept
Get the end stack iterator.
Definition: stack.inl:38
void swap(Stack &stack) noexcept
Swap two instances.
Definition: stack.inl:101
const_iterator cbegin() const noexcept
Definition: stack.inl:32
iterator begin() noexcept
Get the begin stack iterator.
Definition: stack.inl:20
Stack() noexcept
Definition: stack.h:127
void clear() noexcept
Clear the stack.
Definition: stack.inl:94
Intrusive stack iterator.
Definition: stack.h:193
pointer operator->() noexcept
Definition: stack.inl:139
value_type & reference
Definition: stack.h:199
StackIterator & operator++() noexcept
Definition: stack.inl:115
value_type * pointer
Definition: stack.h:201
reference operator*() noexcept
Definition: stack.inl:131
void swap(StackIterator &it) noexcept
Swap two instances.
Definition: stack.inl:145
C++ Common project definitions.
Definition: token_bucket.h:15
void swap(FileCache &cache1, FileCache &cache2) noexcept
Definition: filecache.inl:23
void swap(StackConstIterator< T > &it1, StackConstIterator< T > &it2) noexcept
Definition: stack.inl:195