Aleph-w  1.9
General library for algorithms and data structures
tpl_arrayStack.H
1 
2 /* Aleph-w
3 
4  / \ | | ___ _ __ | |__ __ __
5  / _ \ | |/ _ \ '_ \| '_ \ ____\ \ /\ / / Data structures & Algorithms
6  / ___ \| | __/ |_) | | | |_____\ V V / version 1.9b
7  /_/ \_\_|\___| .__/|_| |_| \_/\_/ https://github.com/lrleon/Aleph-w
8  |_|
9 
10  This file is part of Aleph-w library
11 
12  Copyright (c) 2002-2018 Leandro Rabindranath Leon & Alejandro Mujica
13 
14  This program is free software: you can redistribute it and/or modify
15  it under the terms of the GNU General Public License as published by
16  the Free Software Foundation, either version 3 of the License, or
17  (at your option) any later version.
18 
19  This program is distributed in the hope that it will be useful, but
20  WITHOUT ANY WARRANTY; without even the implied warranty of
21  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22  General Public License for more details.
23 
24  You should have received a copy of the GNU General Public License
25  along with this program. If not, see <https://www.gnu.org/licenses/>.
26 */
27 
28 # ifndef TPL_ARRAYSTACK_H
29 # define TPL_ARRAYSTACK_H
30 
31 # include <iostream>
32 # include <aleph.H>
33 # include <htlist.H>
34 # include <tpl_dynDlist.H>
35 # include <ah-args-ctor.H>
36 # include <ah-dry.H>
37 # include <tpl_memArray.H>
38 
39 using namespace std;
40 
41 using namespace Aleph;
42 
43 namespace Aleph {
44 
63  template <typename T>
64 class ArrayStack : public LocateFunctions<ArrayStack<T>, T>,
65  public FunctionalMethods<ArrayStack<T>, T>,
66  public GenericKeys<ArrayStack<T>, T>,
67  public EqualToMethod<ArrayStack<T>>,
68  public StlAlephIterator<ArrayStack<T>>
69 {
70  MemArray<T> array;
71 
72 public:
73 
74  using Item_Type = T;
75 
77  ArrayStack(size_t dim = 4) : array(dim) { /* empty */ }
78 
80  ArrayStack(const ArrayStack & s) : array(s.array) { /* empty */ }
81 
84  : array(std::forward<MemArray<T>>(s.array)) { /* empty */ }
85 
86  Special_Ctors(ArrayStack, T);
87 
89  ArrayStack & operator = (const ArrayStack & s)
90  {
91  if (this == &s)
92  return *this;
93 
94  array = s.array;
95 
96  return *this;
97  }
98 
100  void swap(ArrayStack & s) noexcept
101  {
102  std::swap(*this, s);
103  }
104 
106  ArrayStack & operator = (ArrayStack && s) noexcept
107  {
108  array.swap(s.array);
109  return *this;
110  }
111 
118  T & push(const T & data)
119  {
120  return array.put(data);
121  }
122 
129  T & push(T && data)
130  {
131  return array.put(std::forward<T>(data));
132  }
133 
135  T & append(const T & data) { return push(data); }
136 
138  T & append(T && data) { return push(std::forward<T>(data)); }
139 
141  T & insert(const T & data) { return push(data); }
142 
144  T & insert(T && data) { return push(std::forward<T>(data)); }
145 
156  T & pushn(const size_t & n = 1)
157  {
158  array.putn(n);
159  return array.last();
160  }
161 
167  T pop()
168  {
169  return array.get(1);
170  }
171 
172  T pop_ne() noexcept
173  {
174  return array.get_ne(1);
175  }
176 
184  T popn(size_t n)
185  {
186  return array.get(n);
187  }
188 
191  T & top() const
192  {
193  return array.last();
194  }
195 
197  T & base() noexcept
198  {
199  return array.first();
200  }
201 
211  T & top(size_t i) const
212  {
213  const size_t sz = array.size();
214  if (i >= sz)
215  throw std::out_of_range("top index out of range");
216  return array.access(sz - i - 1);
217  }
218 
219  T & get_last() const { return top(); }
220 
222  void empty() noexcept { array.empty(); }
223 
225  bool is_empty() const noexcept { return array.size() == 0; }
226 
228  size_t size() const noexcept { return array.size(); }
229 
231  size_t capacity() const noexcept { return array.capacity(); }
232 
240  template <class Operation>
241  bool traverse(Operation & operation) noexcept(noexcept(operation))
242  {
243  return array.traverse(operation);
244  }
245 
247  template <class Operation>
248  bool traverse(Operation & operation) const noexcept(noexcept(operation))
249  {
250  return array.traverse(operation);
251  }
252 
254  template <class Operation>
255  bool traverse(Operation && operation = Operation()) const
256  noexcept(noexcept(operation))
257  {
258  return array.traverse(operation);
259  }
260 
262  template <class Operation>
263  bool traverse(Operation && operation = Operation())
264  noexcept(noexcept(operation))
265  {
266  return array.traverse(operation);
267  }
268 
275  class Iterator : public MemArray<T>::Iterator
276  {
277  public:
278 
279  using Base = typename MemArray<T>::Iterator;
280  using Set_Type = ArrayStack;
281 
282  using Base::Base;
283 
285  Iterator(const ArrayStack<T> & s) : Base(s.array) {}
286  };
287 };
288 
301  template <typename T>
302 class FixedStack : public LocateFunctions<FixedStack<T>, T>,
303  public FunctionalMethods<FixedStack<T>, T>,
304  public GenericKeys<FixedStack<T>, T>,
305  public EqualToMethod<FixedStack<T>>,
306  public StlAlephIterator<FixedStack<T>>
307 {
308  T * array = nullptr;
309  size_t head = 0;
310  size_t dim = 0;
311 
312 public:
313 
314  using Item_Type = T;
315 
317  FixedStack(size_t d = 1024)
318  // Don't change the default value because you would risk of
319  // breaking the tests
320  : array(new T[d]), head(0), dim(d)
321  {
322  /* empty */
323  }
324 
325  ~FixedStack()
326  {
327  if (array)
328  delete [] array;
329  }
330 
333  : array(new T [s.dim]), head(s.head), dim(s.dim)
334  {
335  for (int i = 0; i < head; ++i)
336  array[i] = s.array[i];
337  }
338 
339  Special_Ctors(FixedStack, T);
340 
342  void swap(FixedStack & s) noexcept
343  {
344  std::swap(array, s.array);
345  std::swap(head, s.head);
346  std::swap(dim, s.dim);
347  }
348 
350  FixedStack(FixedStack && s) noexcept
351  : array(nullptr), head(0), dim(0)
352  {
353  swap(s);
354  }
355 
357  FixedStack & operator = (const FixedStack & s)
358  {
359  if (this == &s)
360  return *this;
361 
362  T * ptr = new T [s.dim];
363  if (array)
364  delete [] array;
365  array = ptr;
366  for (size_t i = 0; i < s.head; ++i)
367  array[i] = s.array[i];
368  head = s.head;
369  dim = s.dim;
370 
371  return *this;
372  }
373 
374  FixedStack & operator = (FixedStack && s) noexcept
375  {
376  swap(s);
377  return *this;
378  }
379 
385  T & push(const T & data) noexcept
386  {
387  assert(head < dim);
388  array[head++] = data;
389  return array[head - 1];
390  }
391 
397  T & push(T && data) noexcept
398  {
399  assert(head < dim );
400  std::swap(array[head++], data);
401  return array[head - 1];
402  }
403 
405  T & append(const T & data) noexcept { return push(data); }
406 
408  T & append(T && data) noexcept { return push(std::forward<T>(data)); }
409 
411  T & insert(const T & data) noexcept { return push(data); }
412 
414  T & insert(T && data) noexcept { return push(std::forward<T>(data)); }
415 
421  T & pushn(const size_t & n = 1) noexcept
422  {
423  assert(head + n <= dim);
424 
425  head += n;
426  return array[head - 1];
427  }
428 
430  T pop() noexcept
431  {
432  assert(head > 0);
433 
434  return std::move(array[--head]);
435  }
436 
442  T popn(const int & n) noexcept
443  {
444  assert((int (head - n)) >= 0);
445 
446  head -= n;
447  return std::move(array[head]);
448  }
449 
451  T & top() const noexcept
452  {
453  assert(head > 0);
454 
455  return array[head - 1];
456  }
457 
458  T & get_last() const noexcept { return top(); }
459 
461  T & base() const noexcept { return array[0]; }
462 
470  T & top(size_t i) const noexcept
471  {
472  assert(i < head);
473  return array[head - i - 1];
474  }
475 
477  bool is_empty() const noexcept { return head == 0; }
478 
480  void empty() noexcept { head = 0; }
481 
483  size_t size() const noexcept { return head; }
484 
492  template <class Operation>
493  bool traverse(Operation & operation) noexcept(noexcept(operation))
494  {
495  for (int i = 0; i < head; i++)
496  if (not operation(array[i]))
497  return false;
498 
499  return true;
500  }
501 
503  template <class Operation>
504  bool traverse(Operation & operation) const noexcept(noexcept(operation))
505  {
506  return const_cast<FixedStack*>(this)->traverse(operation);
507  }
508 
510  template <class Operation>
511  bool traverse(Operation && operation = Operation())
512  const noexcept(noexcept(operation))
513  {
514  return traverse<Operation>(operation);
515  }
516 
518  template <class Operation>
519  bool traverse(Operation && operation = Operation())
520  noexcept(noexcept(operation))
521  {
522  return traverse<Operation>(operation);
523  }
524 
531  class Iterator : public Array_Iterator<T>
532  {
533  public:
534 
535  using Base = Array_Iterator<T>;
536  using Base::Base;
537  using Set_Type = FixedStack;
538 
539  Iterator(const FixedStack<T> & s)
540  : Base(no_exception_ctor, s.array, s.dim, s.head) {}
541  };
542 };
543 
544 } // end namespace Aleph
545 
546 # endif /* TPL_ARRAYSTACK_H */
547 
void swap(FixedStack &s) noexcept
Swap in constant time s with this
Definition: tpl_arrayStack.H:342
Definition: tpl_memArray.H:73
bool is_empty() const noexcept
Retrun true if stack is empty.
Definition: tpl_arrayStack.H:225
T & append(T &&data) noexcept
Definition: tpl_arrayStack.H:408
T & access(const size_t i) const noexcept
Definition: tpl_memArray.H:565
Definition: htlist.H:450
bool traverse(Operation &&operation=Operation()) const noexcept(noexcept(operation))
Definition: tpl_arrayStack.H:255
Definition: htlist.H:1290
T & pushn(const size_t &n=1) noexcept
Definition: tpl_arrayStack.H:421
Definition: htlist.H:133
ArrayStack(ArrayStack &&s)
Move constructor.
Definition: tpl_arrayStack.H:83
size_t capacity() const noexcept
The type of element of array.
Definition: tpl_memArray.H:200
T pop() noexcept
Pop by moving the top of stack.
Definition: tpl_arrayStack.H:430
T & first() const
Return a modifiable reference to the first element.
Definition: tpl_memArray.H:542
Definition: tpl_arrayStack.H:64
T & insert(T &&data)
Definition: tpl_arrayStack.H:144
T & top(size_t i) const noexcept
Definition: tpl_arrayStack.H:470
void swap(MemArray &a) noexcept
Swap in constant time this with a
Definition: tpl_memArray.H:238
size_t size() const noexcept
Return the number of elements stored in the stack.
Definition: tpl_arrayStack.H:228
T & insert(const T &data)
Definition: tpl_arrayStack.H:141
T & last() const
Return a modifiable reference to the last element.
Definition: tpl_memArray.H:534
Definition: array_it.H:45
bool traverse(Operation &operation)
Definition: tpl_memArray.H:598
T & insert(T &&data) noexcept
Definition: tpl_arrayStack.H:414
size_t size() const noexcept
Return the number of elements stored in the stack.
Definition: tpl_arrayStack.H:483
size_t size() const noexcept
Return the number of elements.
Definition: tpl_memArray.H:203
T & push(const T &data)
Definition: tpl_arrayStack.H:118
T popn(const int &n) noexcept
Definition: tpl_arrayStack.H:442
ArrayStack(const ArrayStack &s)
Copy constructor.
Definition: tpl_arrayStack.H:80
T & push(T &&data)
Definition: tpl_arrayStack.H:129
T & top() const
Definition: tpl_arrayStack.H:191
T & top() const noexcept
Return a modifiable referemce to stack&#39;s top.
Definition: tpl_arrayStack.H:451
T & insert(const T &data) noexcept
Definition: tpl_arrayStack.H:411
Definition: ah-comb.H:35
Definition: tpl_arrayStack.H:275
FixedStack(size_t d=1024)
The type of element.
Definition: tpl_arrayStack.H:317
bool traverse(Operation &operation) noexcept(noexcept(operation))
Definition: tpl_arrayStack.H:493
T & base() const noexcept
Return the internal array base.
Definition: tpl_arrayStack.H:461
Definition: tpl_arrayStack.H:302
Definition: tpl_memArray.H:635
void putn(const size_t more)
Definition: tpl_memArray.H:431
T & append(const T &data)
Definition: tpl_arrayStack.H:135
bool is_empty() const noexcept
Return true if stack is empty.
Definition: tpl_arrayStack.H:477
void empty() noexcept
Empty the stack.
Definition: tpl_arrayStack.H:222
FixedStack(FixedStack &&s) noexcept
Move constructor.
Definition: tpl_arrayStack.H:350
T & base() noexcept
Return a modifiable reference to first element of array.
Definition: tpl_arrayStack.H:197
bool traverse(Operation &&operation=Operation()) const noexcept(noexcept(operation))
Definition: tpl_arrayStack.H:511
T popn(size_t n)
Definition: tpl_arrayStack.H:184
size_t capacity() const noexcept
Return the internal capacity.
Definition: tpl_arrayStack.H:231
bool traverse(Operation &operation) const noexcept(noexcept(operation))
Definition: tpl_arrayStack.H:504
T & top(size_t i) const
Definition: tpl_arrayStack.H:211
T & append(T &&data)
Definition: tpl_arrayStack.H:138
void empty() noexcept
Empty the stack.
Definition: tpl_arrayStack.H:480
bool traverse(Operation &&operation=Operation()) noexcept(noexcept(operation))
Definition: tpl_arrayStack.H:263
T pop()
Definition: tpl_arrayStack.H:167
void empty()
Empty the container. The array is not contracted.
Definition: tpl_memArray.H:292
T & push(const T &data) noexcept
Definition: tpl_arrayStack.H:385
bool traverse(Operation &&operation=Operation()) noexcept(noexcept(operation))
Definition: tpl_arrayStack.H:519
T & put(const T &item)
Definition: tpl_memArray.H:310
FixedStack(const FixedStack &s)
Copy constructor.
Definition: tpl_arrayStack.H:332
T & pushn(const size_t &n=1)
Definition: tpl_arrayStack.H:156
T get(size_t i=1)
Definition: tpl_memArray.H:502
bool traverse(Operation &operation) noexcept(noexcept(operation))
Definition: tpl_arrayStack.H:241
Definition: tpl_arrayStack.H:531
Iterator(const ArrayStack< T > &s)
Initialize an iterator on stack s
Definition: tpl_arrayStack.H:285
bool traverse(Operation &operation) const noexcept(noexcept(operation))
Definition: tpl_arrayStack.H:248
T & push(T &&data) noexcept
Definition: tpl_arrayStack.H:397
T & append(const T &data) noexcept
Definition: tpl_arrayStack.H:405
Definition: htlist.H:1323
ArrayStack(size_t dim=4)
The type of element.
Definition: tpl_arrayStack.H:77
void swap(ArrayStack &s) noexcept
Swap this with s
Definition: tpl_arrayStack.H:100

Leandro Rabindranath León