Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
tpl_arrayStack.H
Go to the documentation of this file.
1
2/*
3 Aleph_w
4
5 Data structures & Algorithms
6 version 2.0.0b
7 https://github.com/lrleon/Aleph-w
8
9 This file is part of Aleph-w library
10
11 Copyright (c) 2002-2026 Leandro Rabindranath Leon
12
13 Permission is hereby granted, free of charge, to any person obtaining a copy
14 of this software and associated documentation files (the "Software"), to deal
15 in the Software without restriction, including without limitation the rights
16 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
17 copies of the Software, and to permit persons to whom the Software is
18 furnished to do so, subject to the following conditions:
19
20 The above copyright notice and this permission notice shall be included in all
21 copies or substantial portions of the Software.
22
23 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
26 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
28 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29 SOFTWARE.
30*/
31
32
46#ifndef TPL_ARRAYSTACK_H
47#define TPL_ARRAYSTACK_H
48
49#include <iostream>
50#include <aleph.H>
51#include <htlist.H>
52#include <tpl_dynDlist.H>
53#include <ah-args-ctor.H>
54#include <ah-dry.H>
55#include <tpl_memArray.H>
56#include <ah-errors.H>
57
58namespace Aleph {
59
78 template <typename T>
79class ArrayStack : public LocateFunctions<ArrayStack<T>, T>,
80 public FunctionalMethods<ArrayStack<T>, T>,
81 public GenericKeys<ArrayStack<T>, T>,
82 public EqualToMethod<ArrayStack<T>>,
83 public StlAlephIterator<ArrayStack<T>>
84{
86
87public:
88
89 using Item_Type = T;
90
92 ArrayStack(size_t dim = 4) : array(dim) { /* empty */ }
93
95 ArrayStack(const ArrayStack & s) : array(s.array) { /* empty */ }
96
99 : array(std::move(s.array)) { /* empty */ }
100
102
105 {
106 if (this == &s)
107 return *this;
108
109 array = s.array;
110
111 return *this;
112 }
113
115 void swap(ArrayStack & s) noexcept
116 {
117 array.swap(s.array);
118 }
119
122 {
123 array.swap(s.array);
124 return *this;
125 }
126
133 T & push(const T & data)
134 {
135 return array.put(data);
136 }
137
144 T & push(T && data)
145 {
146 return array.put(std::forward<T>(data));
147 }
148
150 T & append(const T & data) { return push(data); }
151
153 T & append(T && data) { return push(std::forward<T>(data)); }
154
156 T & insert(const T & data) { return push(data); }
157
159 T & insert(T && data) { return push(std::forward<T>(data)); }
160
171 T & pushn(const size_t & n = 1)
172 {
173 array.putn(n);
174 return array.last();
175 }
176
183 {
184 return array.get(1);
185 }
186
188 {
189 return array.get_ne(1);
190 }
191
199 T popn(size_t n)
200 {
201 return array.get(n);
202 }
203
206 T & top() const
207 {
208 return array.last();
209 }
210
213 {
214 return array.first();
215 }
216
226 T & top(size_t i) const
227 {
228 const size_t sz = array.size();
229 ah_out_of_range_error_if(i >= sz) << "top index out of range";
230 return array.access(sz - i - 1);
231 }
232
233 T & get_last() const { return top(); }
234
236 void empty() noexcept { array.empty(); }
237
239 bool is_empty() const noexcept { return array.size() == 0; }
240
242 size_t size() const noexcept { return array.size(); }
243
245 size_t capacity() const noexcept { return array.capacity(); }
246
254 template <class Operation>
256 {
257 return array.traverse(operation);
258 }
259
261 template <class Operation>
263 {
264 return array.traverse(operation);
265 }
266
268 template <class Operation>
270 {
271 return array.traverse(operation);
272 }
273
275 template <class Operation>
277 {
278 return array.traverse(operation);
279 }
280
287 class Iterator : public MemArray<T>::Iterator
288 {
289 public:
290
291 using Base = typename MemArray<T>::Iterator;
293
294 using Base::Base;
295
297 Iterator(const ArrayStack<T> & s) : Base(s.array) {}
298 };
299};
300
313 template <typename T>
314class FixedStack : public LocateFunctions<FixedStack<T>, T>,
315 public FunctionalMethods<FixedStack<T>, T>,
316 public GenericKeys<FixedStack<T>, T>,
317 public EqualToMethod<FixedStack<T>>,
318 public StlAlephIterator<FixedStack<T>>
319{
320 T * array = nullptr;
321 size_t head = 0;
322 size_t dim = 0;
323
324public:
325
326 using Item_Type = T;
327
329 FixedStack(size_t d = 1024)
330 // Don't change the default value because you would risk of
331 // breaking the tests
332 : array(new T[d]), head(0), dim(d)
333 {
334 /* empty */
335 }
336
338 {
339 if (array)
340 delete [] array;
341 }
342
345 : array(new T [s.dim]), head(s.head), dim(s.dim)
346 {
347 for (int i = 0; i < head; ++i)
348 array[i] = s.array[i];
349 }
350
352
354 void swap(FixedStack & s) noexcept
355 {
356 std::swap(array, s.array);
357 std::swap(head, s.head);
358 std::swap(dim, s.dim);
359 }
360
363 : array(nullptr), head(0), dim(0)
364 {
365 swap(s);
366 }
367
370 {
371 if (this == &s)
372 return *this;
373
374 T * ptr = new T [s.dim];
375 if (array)
376 delete [] array;
377 array = ptr;
378 for (size_t i = 0; i < s.head; ++i)
379 array[i] = s.array[i];
380 head = s.head;
381 dim = s.dim;
382
383 return *this;
384 }
385
387 {
388 swap(s);
389 return *this;
390 }
391
397 T & push(const T & data) noexcept
398 {
399 assert(head < dim);
400 array[head++] = data;
401 return array[head - 1];
402 }
403
409 T & push(T && data) noexcept
410 {
411 assert(head < dim );
412 std::swap(array[head++], data);
413 return array[head - 1];
414 }
415
417 T & append(const T & data) noexcept { return push(data); }
418
420 T & append(T && data) noexcept { return push(std::forward<T>(data)); }
421
423 T & insert(const T & data) noexcept { return push(data); }
424
426 T & insert(T && data) noexcept { return push(std::forward<T>(data)); }
427
433 T & pushn(const size_t & n = 1) noexcept
434 {
435 assert(head + n <= dim);
436
437 head += n;
438 return array[head - 1];
439 }
440
443 {
444 assert(head > 0);
445
446 return std::move(array[--head]);
447 }
448
454 T popn(const int & n) noexcept
455 {
456 assert((int (head - n)) >= 0);
457
458 head -= n;
459 return std::move(array[head]);
460 }
461
464 {
465 assert(head > 0);
466
467 return array[head - 1];
468 }
469
470 T & get_last() const noexcept { return top(); }
471
473 T & base() const noexcept { return array[0]; }
474
482 T & top(size_t i) const noexcept
483 {
484 assert(i < head);
485 return array[head - i - 1];
486 }
487
489 bool is_empty() const noexcept { return head == 0; }
490
492 void empty() noexcept { head = 0; }
493
495 size_t size() const noexcept { return head; }
496
504 template <class Operation>
506 {
507 for (int i = 0; i < head; i++)
508 if (not operation(array[i]))
509 return false;
510
511 return true;
512 }
513
515 template <class Operation>
517 {
518 return const_cast<FixedStack*>(this)->traverse(operation);
519 }
520
522 template <class Operation>
524 {
526 }
527
529 template <class Operation>
531 {
533 }
534
541 class Iterator : public Array_Iterator<T>
542 {
543 public:
544
546 using Base::Base;
548
551 };
552};
553
554} // end namespace Aleph
555
556# endif /* TPL_ARRAYSTACK_H */
557
Variadic constructor macros for containers.
Container traversal and functional operation mixins.
Exception handling system with formatted messages for Aleph-w.
#define ah_out_of_range_error_if(C)
Throws std::out_of_range if condition holds.
Definition ah-errors.H:579
@ no_exception_ctor
Definition ahDefs.H:72
#define Special_Ctors(Set_Type, Type)
Generates special constructors for containers.
Definition ahDry.H:113
Core header for the Aleph-w library.
Iterator on the items of a stack.
typename MemArray< T >::Iterator Base
Iterator(const ArrayStack< T > &s)
Initialize an iterator on stack s
Stack implemented with simple dynamic array and with bounds verification.
void swap(ArrayStack &s) noexcept
Swap this with s
bool traverse(Operation &&operation=Operation())
T & insert(const T &data)
T & top() const
Return a modifiable reference to youngest element of stack (called the top)
void empty() noexcept
Empty the stack.
ArrayStack(const ArrayStack &s)
Copy constructor.
ArrayStack(size_t dim=4)
The type of element.
bool is_empty() const noexcept
Return true if stack is empty.
ArrayStack & operator=(const ArrayStack &s)
Assign by copy s to this
bool traverse(Operation &operation)
Traverse all the items of stack from the youngest to oldest and conditionally performs an operation.
size_t size() const noexcept
Return the number of elements stored in the stack.
T & pushn(const size_t &n=1)
Push n cells into the stack.
T & append(T &&data)
T & append(const T &data)
T & top(size_t i) const
Return a modifiable reference to the element located at i positions from top.
T & insert(T &&data)
bool traverse(Operation &operation) const
T & base() noexcept
Return a modifiable reference to first element of array.
T popn(size_t n)
Extract in constant time the n more recently inserted elements of stack.
T pop_ne() noexcept
T pop()
Extract the last more recently inserted element.
MemArray< T > array
bool traverse(Operation &&operation=Operation()) const
T & push(const T &data)
Push into stack a copy of data
ArrayStack(ArrayStack &&s) noexcept
Move constructor.
size_t capacity() const noexcept
Return the internal capacity.
T & push(T &&data)
Push into stack data
Iterator wrapper for C++ raw arrays.
Definition array_it.H:77
Iterator on the items of a stack.
Iterator(const FixedStack< T > &s)
Fixed length stack.
T & append(const T &data) noexcept
T & push(const T &data) noexcept
Push a copy of data
bool traverse(Operation &&operation=Operation()) const
FixedStack & operator=(const FixedStack &s)
Assign by copy s to this
size_t size() const noexcept
Return the number of elements stored in the stack.
T & pushn(const size_t &n=1) noexcept
Push in contant time nspaces into stack.
T & top(size_t i) const noexcept
Return a modifiable referecce to item located i positions after top.
T & insert(const T &data) noexcept
T popn(const int &n) noexcept
Perform in constant time n pops.
bool traverse(Operation &&operation=Operation())
T & base() const noexcept
Return the internal array base.
T & get_last() const noexcept
T & insert(T &&data) noexcept
bool is_empty() const noexcept
Return true if stack is empty.
bool traverse(Operation &operation)
Traverse all the items of stack from the youngest to oldest and conditionally performs an operation.
T & append(T &&data) noexcept
FixedStack(const FixedStack &s)
Copy constructor.
T & push(T &&data) noexcept
Push and move data
T & top() const noexcept
Return a modifiable referemce to stack's top.
T pop() noexcept
Pop by moving the top of stack.
FixedStack(FixedStack &&s) noexcept
Move constructor.
void empty() noexcept
Empty the stack.
bool traverse(Operation &operation) const
FixedStack(size_t d=1024)
The type of element.
void swap(FixedStack &s) noexcept
Swap in constant time s with this
Simple, scalable and fast dynamic array.
Equality test for containers.
Definition ah-dry.H:1534
Common methods to the Aleph-w ( ) containers.
Definition ah-dry.H:548
Aleph::DynList< __T > maps(Operation &op) const
Map the elements of the container.
Definition ah-dry.H:904
Common sequential searching methods on containers.
Definition ah-dry.H:164
Mixin that adds STL begin()/end() and cbegin()/cend() to Aleph containers.
__gmp_expr< typename __gmp_resolve_expr< T, V >::value_type, __gmp_binary_expr< __gmp_expr< T, U >, __gmp_expr< V, W >, __gmp_dim_function > > dim(const __gmp_expr< T, U > &expr1, const __gmp_expr< V, W > &expr2)
Definition gmpfrxx.h:4052
Singly linked list implementations with head-tail access.
Main namespace for Aleph-w library functions.
Definition ah-arena.H:89
std::decay_t< typename HeadC::Item_Type > T
Definition ah-zip.H:107
Simple iterator on elements of array.
Generic list of items stored in a container.
Definition ah-dry.H:1501
Dynamic doubly linked list implementation.
Simple, scalable, contiguous dynamic array.