Embedded Template Library 1.0
Loading...
Searching...
No Matches
intrusive_stack.h
Go to the documentation of this file.
1
2
3/******************************************************************************
4The MIT License(MIT)
5
6Embedded Template Library.
7https://github.com/ETLCPP/etl
8https://www.etlcpp.com
9
10Copyright(c) 2016 John Wellbelove
11
12Permission is hereby granted, free of charge, to any person obtaining a copy
13of this software and associated documentation files(the "Software"), to deal
14in the Software without restriction, including without limitation the rights
15to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
16copies of the Software, and to permit persons to whom the Software is
17furnished to do so, subject to the following conditions :
18
19The above copyright notice and this permission notice shall be included in all
20copies or substantial portions of the Software.
21
22THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
25AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28SOFTWARE.
29******************************************************************************/
30
31#ifndef ETL_INTRUSIVE_STACK_INCLUDED
32#define ETL_INTRUSIVE_STACK_INCLUDED
33
34#include "platform.h"
35#include "type_traits.h"
36#include "error_handler.h"
37#include "intrusive_links.h"
38
39#include <stddef.h>
40
41namespace etl
42{
43 //***************************************************************************
46 //***************************************************************************
47 class intrusive_stack_exception : public etl::exception
48 {
49 public:
50
51 intrusive_stack_exception(string_type reason_, string_type file_name_, numeric_type line_number_)
52 : exception(reason_, file_name_, line_number_)
53 {
54 }
55 };
56
57 //***************************************************************************
60 //***************************************************************************
61 class intrusive_stack_empty : public intrusive_stack_exception
62 {
63 public:
64
65 intrusive_stack_empty(string_type file_name_, numeric_type line_number_)
66 : intrusive_stack_exception(ETL_ERROR_TEXT("intrusive_stack:empty", ETL_INTRUSIVE_STACK_FILE_ID"A"), file_name_, line_number_)
67 {
68 }
69 };
70
71 //***************************************************************************
74 //***************************************************************************
75 class intrusive_stack_value_is_already_linked : public intrusive_stack_exception
76 {
77 public:
78
79 intrusive_stack_value_is_already_linked(string_type file_name_, numeric_type line_number_)
80 : intrusive_stack_exception(ETL_ERROR_TEXT("intrusive_stack:value is already linked", ETL_INTRUSIVE_STACK_FILE_ID"B"), file_name_, line_number_)
81 {
82 }
83 };
84
85 //***************************************************************************
89 //***************************************************************************
90 template <typename TLink>
92 {
93 public:
94
95 // Node typedef.
96 typedef TLink link_type;
97
98 //*************************************************************************
101 //*************************************************************************
102 void push(link_type& value)
103 {
104 ETL_ASSERT_OR_RETURN(!value.is_linked(), ETL_ERROR(intrusive_stack_value_is_already_linked));
105
106 value.etl_next = p_top;
107 p_top = &value;
108
109 ++current_size;
110 }
111
112 //*************************************************************************
115 //*************************************************************************
116 void pop()
117 {
118 ETL_ASSERT_CHECK_PUSH_POP_OR_RETURN(!empty(), ETL_ERROR(intrusive_stack_empty));
119
120 link_type* p_next = p_top->etl_next;
121 p_top->clear();
122 p_top = p_next;
123 --current_size;
124 }
125
126 //*************************************************************************
130 //*************************************************************************
131 template <typename TContainer>
132 void pop_into(TContainer& destination)
133 {
134 link_type* p_link = p_top;
135 pop();
136 destination.push(*p_link);
137 }
138
139 //*************************************************************************
141 //*************************************************************************
142 void reverse()
143 {
144 link_type* previous = &terminator;
145 link_type* current = p_top;
146 link_type* next;
147
148 while (current != &terminator)
149 {
150 next = current->etl_next;
151 current->etl_next = previous;
152 previous = current;
153 current = next;
154 }
155
156 p_top = previous;
157 }
158
159 //*************************************************************************
161 //*************************************************************************
162 void clear()
163 {
164 while (!empty())
165 {
166 pop();
167 }
168
169 current_size = 0;
170 }
171
172 //*************************************************************************
174 //*************************************************************************
175 bool empty() const
176 {
177 return current_size == 0;
178 }
179
180 //*************************************************************************
182 //*************************************************************************
183 size_t size() const
184 {
185 return current_size;
186 }
187
188 protected:
189
190 //*************************************************************************
192 //*************************************************************************
194 : p_top(&terminator)
195 , current_size(0)
196 {
197 }
198
199 //*************************************************************************
201 //*************************************************************************
205
206 link_type* p_top;
207 link_type terminator;
208
210 };
211
212 //***************************************************************************
218 //***************************************************************************
219 template <typename TValue, typename TLink>
221 {
222 public:
223
224 // Node typedef.
225 typedef typename etl::intrusive_stack_base<TLink>::link_type link_type;
226
227 // STL style typedefs.
228 typedef TValue value_type;
229 typedef value_type* pointer;
230 typedef const value_type* const_pointer;
231 typedef value_type& reference;
232 typedef const value_type& const_reference;
233 typedef size_t size_type;
234
235 //*************************************************************************
237 //*************************************************************************
239 : intrusive_stack_base<TLink>()
240 {
241 }
242
243 //*************************************************************************
247 //*************************************************************************
248 reference top()
249 {
250 return *static_cast<TValue*>(this->p_top);
251 }
252
253 //*************************************************************************
256 //*************************************************************************
257 const_reference top() const
258 {
259 return *static_cast<const TValue*>(this->p_top);
260 }
261
262 private:
263
264 // Disable copy construction and assignment.
266 intrusive_stack& operator = (const intrusive_stack& rhs);
267 };
268}
269
270#endif
Definition intrusive_stack.h:62
Definition intrusive_stack.h:76
ETL_CONSTEXPR exception(string_type reason_, string_type, numeric_type line_)
Constructor.
Definition exception.h:69
Definition exception.h:47
void pop()
Definition intrusive_stack.h:116
size_t size() const
Returns the number of elements.
Definition intrusive_stack.h:183
reference top()
Definition intrusive_stack.h:248
void reverse()
Reverses the stack order.
Definition intrusive_stack.h:142
void clear()
Clears the stack to the empty state.
Definition intrusive_stack.h:162
const_reference top() const
Definition intrusive_stack.h:257
~intrusive_stack_base()
Destructor.
Definition intrusive_stack.h:202
void pop_into(TContainer &destination)
Definition intrusive_stack.h:132
intrusive_stack()
Constructor.
Definition intrusive_stack.h:238
size_t current_size
Definition intrusive_stack.h:209
bool empty() const
Checks if the stack is in the empty state.
Definition intrusive_stack.h:175
link_type * p_top
Definition intrusive_stack.h:206
link_type terminator
Definition intrusive_stack.h:207
void push(link_type &value)
Definition intrusive_stack.h:102
intrusive_stack_base()
Constructor.
Definition intrusive_stack.h:193
Definition intrusive_stack.h:221
Definition intrusive_stack.h:92
bitset_ext
Definition absolute.h:39