DeSiGNAR  0.5a
Data Structures General Library
stack.H
Go to the documentation of this file.
1 /*
2  This file is part of Designar.
3  Copyright (C) 2017 by Alejandro J. Mujica
4 
5  This program is free software: you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation, either version 3 of the License, or
8  (at your option) any later version.
9 
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with this program. If not, see <http://www.gnu.org/licenses/>.
17 
18  Any user request of this software, write to
19 
20  Alejandro Mujica
21 
22  aledrums@gmail.com
23 */
24 
25 # ifndef DSGSTACK_H
26 # define DSGSTACK_H
27 
28 # include <array.H>
29 # include <list.H>
30 
31 namespace Designar
32 {
33 
34  template <typename T, nat_t CAP = 100>
35  class FixedStack
36  {
37  T array[CAP];
38  nat_t num_items;
39 
40  void copy_stack(const FixedStack &);
41 
42  void swap(FixedStack & s)
43  {
44  std::swap(array, s.array);
45  std::swap(num_items, s.num_items);
46  }
47 
48  public:
49  using ItemType = T;
50  using KeyType = T;
51  using DataType = T;
52  using ValueType = T;
53  using SizeType = nat_t;
54 
56  : num_items(0)
57  {
58  // empty
59  }
60 
62  : num_items(s.num_items)
63  {
64  copy_stack(s);
65  }
66 
68  : FixedStack()
69  {
70  swap(s);
71  }
72 
74  {
75  if (this == &s)
76  return *this;
77 
78  num_items = s.num_items;
79  copy_stack(s);
80 
81  return *this;
82  }
83 
85  {
86  swap(s);
87  return *this;
88  }
89 
90  bool is_empty() const
91  {
92  return num_items == 0;
93  }
94 
95  bool is_full() const
96  {
97  return num_items == CAP;
98  }
99 
100  void clear()
101  {
102  num_items = 0;
103  }
104 
105  nat_t size() const
106  {
107  return num_items;
108  }
109 
111  {
112  return CAP;
113  }
114 
115  T & push(const T & item)
116  {
117  if (is_full())
118  throw std::overflow_error("Stack is full");
119 
120  array[num_items++] = item;
121  return array[num_items - 1];
122  }
123 
124  T & push(T && item)
125  {
126  if (is_full())
127  throw std::overflow_error("Stack is full");
128 
129  array[num_items++] = std::move(item);
130  return array[num_items - 1];
131  }
132 
133  T & top()
134  {
135  if (is_empty())
136  throw std::underflow_error("Stack is empty");
137 
138  return array[num_items - 1];
139  }
140 
141  const T & top() const
142  {
143  if (is_empty())
144  throw std::underflow_error("Stack is empty");
145 
146  return array[num_items - 1];
147  }
148 
149  T & base()
150  {
151  if (is_empty())
152  throw std::underflow_error("Stack is empty");
153 
154  return array[0];
155  }
156 
157  const T & base() const
158  {
159  if (is_empty())
160  throw std::underflow_error("Stack is empty");
161 
162  return array[0];
163  }
164 
165  T pop()
166  {
167  if (is_empty())
168  throw std::underflow_error("Stack is empty");
169 
170  return std::move(array[--num_items]);
171  }
172 
173  void popn(nat_t n)
174  {
175  if (n > num_items)
176  throw std::underflow_error("n is to large");
177 
178  num_items -= n;
179  }
180  };
181 
182  template <typename T, nat_t CAP>
184  {
185  for (nat_t i = 0; i < num_items; ++i)
186  array[i] = s.array[i];
187  }
188 
189  template <typename T>
190  class DynStack : private DynArray<T>
191  {
192  using BaseArray = DynArray<T>;
193 
194  public:
195  using ItemType = T;
196  using KeyType = T;
197  using DataType = T;
198  using ValueType = T;
199  using SizeType = nat_t;
200 
202  : BaseArray()
203  {
204  // empty
205  }
206 
208  : BaseArray(cap)
209  {
210  // empty
211  }
212 
213  DynStack(const DynStack & s)
214  : BaseArray(s)
215  {
216  // empty
217  }
218 
220  : BaseArray(std::forward<DynStack<T>>(s))
221  {
222  // empty
223  }
224 
226  {
227  if (this == &s)
228  return *this;
229 
230  (BaseArray &) *this = s;
231  return *this;
232  }
233 
235  {
236  BaseArray::swap(s);
237  return *this;
238  }
239 
240  bool is_empty() const
241  {
242  return BaseArray::is_empty();
243  }
244 
245  void clear()
246  {
247  BaseArray::clear();
248  }
249 
250  nat_t size() const
251  {
252  return BaseArray::size();
253  }
254 
255  T & push(const T & item)
256  {
257  BaseArray::append(item);
258  return BaseArray::get_last();
259  }
260 
261  T & push(T && item)
262  {
263  BaseArray::append(std::forward<T>(item));
264  return BaseArray::get_last();
265  }
266 
267  T & top()
268  {
269  if (is_empty())
270  throw std::underflow_error("Stack is empty");
271 
272  return BaseArray::get_last();
273  }
274 
275  const T & top() const
276  {
277  if (is_empty())
278  throw std::underflow_error("Stack is empty");
279 
280  return BaseArray::get_last();
281  }
282 
283  T & base()
284  {
285  if (is_empty())
286  throw std::underflow_error("Stack is empty");
287 
288  return BaseArray::get_first();
289  }
290 
291  const T & base() const
292  {
293  if (is_empty())
294  throw std::underflow_error("Stack is empty");
295 
296  return BaseArray::get_first();
297  }
298 
299  T pop()
300  {
301  if (is_empty())
302  throw std::underflow_error("Stack is empty");
303 
304  return BaseArray::remove_last();
305  }
306 
307  void popn(nat_t);
308  };
309 
310  template <typename T>
312  {
313  if (n > size())
314  throw std::underflow_error("n is to large");
315 
316  while (n-- > 0)
317  pop();
318  }
319 
320  template <typename T>
321  class ListStack : private SLList<T>
322  {
323  using BaseList = SLList<T>;
324 
325  public:
326  using ItemType = T;
327  using KeyType = T;
328  using DataType = T;
329  using ValueType = T;
330  using SizeType = nat_t;
331 
333  : BaseList()
334  {
335  // empty
336  }
337 
338  ListStack(const ListStack & s)
339  : BaseList(s)
340  {
341  // empty
342  }
343 
345  : BaseList(std::forward<ListStack<T>>(s))
346  {
347  // empty
348  }
349 
351  {
352  if (this == &s)
353  return *this;
354 
355  (BaseList &) *this = s;
356  return *this;
357  }
358 
360  {
361  BaseList::swap(s);
362  return *this;
363  }
364 
365  bool is_empty() const
366  {
367  return BaseList::is_empty();
368  }
369 
370  void clear()
371  {
372  BaseList::clear();
373  }
374 
375  nat_t size() const
376  {
377  return BaseList::size();
378  }
379 
380  T & push(const T & item)
381  {
382  return BaseList::insert(item);
383  }
384 
385  T & push(T && item)
386  {
387  return BaseList::insert(std::forward<T>(item));
388  }
389 
390  T & top()
391  {
392  if (is_empty())
393  throw std::underflow_error("Stack is empty");
394 
395  return BaseList::get_first();
396  }
397 
398  const T & top() const
399  {
400  if (is_empty())
401  throw std::underflow_error("Stack is empty");
402 
403  return BaseList::get_first();
404  }
405 
406  T & base()
407  {
408  if (is_empty())
409  throw std::underflow_error("Stack is empty");
410 
411  return BaseList::get_last();
412  }
413 
414  const T & base() const
415  {
416  if (is_empty())
417  throw std::underflow_error("Stack is empty");
418 
419  return BaseList::get_last();
420  }
421 
422  T pop()
423  {
424  if (is_empty())
425  throw std::underflow_error("Stack is empty");
426 
427  return BaseList::remove_first();
428  }
429 
430  void popn(nat_t);
431  };
432 
433  template <typename T>
435  {
436  if (n > size())
437  throw std::underflow_error("n is to large");
438 
439  while (n-- > 0)
440  pop();
441  }
442 
443 } // end namespace Designar
444 
445 # endif // DSGSTACK_H
T & base()
Definition: stack.H:283
const T & base() const
Definition: stack.H:414
Definition: list.H:35
T & top()
Definition: stack.H:390
T DataType
Definition: array.H:194
T DataType
Definition: stack.H:51
T & push(const T &item)
Definition: stack.H:380
Definition: stack.H:35
ListStack(ListStack &&s)
Definition: stack.H:344
T ValueType
Definition: stack.H:52
nat_t SizeType
Definition: stack.H:53
DynStack(DynStack &&s)
Definition: stack.H:219
nat_t size() const
Definition: stack.H:375
Definition: stack.H:190
T pop()
Definition: stack.H:165
T ValueType
Definition: list.H:211
Definition: stack.H:321
const T & top() const
Definition: stack.H:398
T KeyType
Definition: stack.H:50
void clear()
Definition: stack.H:370
const T & base() const
Definition: stack.H:157
const T & top() const
Definition: stack.H:141
DynStack()
Definition: stack.H:201
void popn(nat_t)
Definition: stack.H:311
DynStack(nat_t cap)
Definition: stack.H:207
FixedStack(const FixedStack &s)
Definition: stack.H:61
nat_t size() const
Definition: stack.H:105
T KeyType
Definition: list.H:209
T & push(T &&item)
Definition: stack.H:124
T & base()
Definition: stack.H:406
T pop()
Definition: stack.H:299
Definition: array.H:375
T ItemType
Definition: array.H:192
T & top()
Definition: stack.H:133
FixedStack & operator=(const FixedStack &s)
Definition: stack.H:73
const T & top() const
Definition: stack.H:275
DynStack(const DynStack &s)
Definition: stack.H:213
void clear()
Definition: stack.H:100
Definition: italgorithms.H:33
bool is_empty() const
Definition: stack.H:240
T ItemType
Definition: stack.H:49
ListStack(const ListStack &s)
Definition: stack.H:338
bool is_empty() const
Definition: stack.H:365
void popn(nat_t n)
Definition: stack.H:173
T DataType
Definition: list.H:210
T & top()
Definition: stack.H:267
nat_t SizeType
Definition: array.H:196
const T & base() const
Definition: stack.H:291
nat_t SizeType
Definition: list.H:212
T pop()
Definition: stack.H:422
Definition: array.H:182
nat_t size() const
Definition: stack.H:250
T & push(const T &item)
Definition: stack.H:115
Definition: array.H:32
T ValueType
Definition: array.H:195
unsigned long int nat_t
Definition: types.H:50
nat_t get_capacity() const
Definition: stack.H:110
bool is_full() const
Definition: stack.H:95
FixedStack()
Definition: stack.H:55
ListStack()
Definition: stack.H:332
bool is_empty() const
Definition: stack.H:90
void clear()
Definition: stack.H:245
T & base()
Definition: stack.H:149
void popn(nat_t)
Definition: stack.H:434
T & push(T &&item)
Definition: stack.H:385
T ItemType
Definition: list.H:208
T KeyType
Definition: array.H:193
FixedStack(FixedStack &&s)
Definition: stack.H:67
T & push(T &&item)
Definition: stack.H:261
T & push(const T &item)
Definition: stack.H:255