Aleph-w 7.10.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 Aleph_w
3
4 Data structures & Algorithms
5 version 2.0.0b
6 https://github.com/lrleon/Aleph-w
7
8 This file is part of Aleph-w library
9
10 Copyright (c) 2002-2026 Leandro Rabindranath Leon
11
12 Permission is hereby granted, free of charge, to any person obtaining a copy
13 of this software and associated documentation files (the "Software"), to deal
14 in the Software without restriction, including without limitation the rights
15 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
16 copies of the Software, and to permit persons to whom the Software is
17 furnished to do so, subject to the following conditions:
18
19 The above copyright notice and this permission notice shall be included in all
20 copies or substantial portions of the Software.
21
22 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28 SOFTWARE.
29*/
30
44#ifndef TPL_ARRAYSTACK_H
45#define TPL_ARRAYSTACK_H
46
47#include <iostream>
48#include <limits>
49#include <memory>
50#include <type_traits>
51#include <aleph.H>
52#include <htlist.H>
53#include <tpl_dynDlist.H>
54#include <ah-args-ctor.H>
55#include <ah-dry.H>
56#include <tpl_memArray.H>
57#include <ah-errors.H>
58#include <ah-iterator.H>
59
60namespace Aleph {
79template <typename T>
80class ArrayStack : public LocateFunctions<ArrayStack<T>, T>,
81 public FunctionalMethods<ArrayStack<T>, T>,
82 public GenericKeys<ArrayStack<T>, T>,
83 public EqualSequenceMethod<ArrayStack<T>>,
84 public StlAlephIterator<ArrayStack<T>>
85{
87
88public:
89 using Item_Type = T;
90
92 ArrayStack(size_t dim = 4) : array(dim)
93 { /* empty */
94 }
95
98 { /* empty */
99 }
100
102 ArrayStack(ArrayStack &&s) noexcept : array(std::move(s.array))
103 { /* empty */
104 }
105
107
110 {
111 if (this == &s)
112 return *this;
113
114 array = s.array;
115
116 return *this;
117 }
118
120 void swap(ArrayStack &s) noexcept
121 {
122 array.swap(s.array);
123 }
124
127 {
128 array.swap(s.array);
129 return *this;
130 }
131
138 T &push(const T &data)
139 {
140 return array.put(data);
141 }
142
149 T &push(T &&data)
150 {
151 return array.put(std::forward<T>(data));
152 }
153
155 T &append(const T &data)
156 {
157 return push(data);
158 }
159
161 T &append(T &&data)
162 {
163 return push(std::forward<T>(data));
164 }
165
167 T &insert(const T &data)
168 {
169 return push(data);
170 }
171
173 T &insert(T &&data)
174 {
175 return push(std::forward<T>(data));
176 }
177
188 T &pushn(const size_t &n = 1)
189 {
190 ah_invalid_argument_if(n == 0) << "ArrayStack::pushn: n must be positive";
191 array.putn(n);
192 return array.last();
193 }
194
201 {
202 return array.get(1);
203 }
204
205 T pop_ne() noexcept
206 {
207 return array.get_ne(1);
208 }
209
217 T popn(size_t n)
218 {
219 ah_invalid_argument_if(n == 0) << "ArrayStack::popn: n must be positive";
220 return array.get(n);
221 }
222
226 {
227 return array.last();
228 }
229
230 const T &top() const
231 {
232 return array.last();
233 }
234
240 {
241 return array.first();
242 }
243
248 const T &base() const
249 {
250 return array.first();
251 }
252
262 T &top(const size_t i)
263 {
264 const size_t sz = array.size();
265 ah_out_of_range_error_if(i >= sz) << "top index out of range";
266 return array.access(sz - i - 1);
267 }
268
269 const T &top(const size_t i) const
270 {
271 const size_t sz = array.size();
272 ah_out_of_range_error_if(i >= sz) << "top index out of range";
273 return array(sz - i - 1);
274 }
275
277 {
278 return top();
279 }
280
281 const T &get_last() const
282 {
283 return top();
284 }
285
287 void empty() noexcept
288 {
289 array.empty();
290 }
291
297 void clear() noexcept
298 {
299 empty();
300 }
301
303 bool is_empty() const noexcept
304 {
305 return array.size() == 0;
306 }
307
309 size_t size() const noexcept
310 {
311 return array.size();
312 }
313
315 size_t capacity() const noexcept
316 {
317 return array.capacity();
318 }
319
327 template <class Operation>
329 bool traverse(Operation &operation) noexcept(noexcept(array.traverse(operation)))
330 {
331 return array.traverse(operation);
332 }
333
335 template <class Operation>
337 bool traverse(Operation &operation) const noexcept(noexcept(array.traverse(operation)))
338 {
339 return array.traverse(operation);
340 }
341
343 template <class Operation>
345 bool traverse(Operation &&operation = Operation()) const
346 noexcept(noexcept(array.traverse(operation)))
347 {
348 return array.traverse(operation);
349 }
350
352 template <class Operation>
354 bool traverse(Operation &&operation = Operation()) noexcept(noexcept(array.traverse(operation)))
355 {
356 return array.traverse(operation);
357 }
358
365 class Iterator : public MemArray<T>::Iterator
366 {
367 public:
368 using Base = typename MemArray<T>::Iterator;
370
371 using Base::Base;
372
375
377 Iterator(const ArrayStack<T> &) = delete;
378 };
379
382};
383
396template <typename T>
397class FixedStack : public LocateFunctions<FixedStack<T>, T>,
398 public FunctionalMethods<FixedStack<T>, T>,
399 public GenericKeys<FixedStack<T>, T>,
400 public EqualSequenceMethod<FixedStack<T>>,
401 public StlAlephIterator<FixedStack<T>>
402{
403 T *array = nullptr;
404 size_t head = 0;
405 size_t dim = 0;
406
407public:
408 using Item_Type = T;
409
417 FixedStack(const size_t d = 1024)
418 // Don't change the default value because you would risk of
419 // breaking the tests
420 : array(nullptr), head(0), dim(d)
421 {
422 // Before new[]: for d items whose bytes do not fit in a size_t, what
423 // new[] asks of the allocator depends on the compiler
424 ah_overflow_error_if(d > std::numeric_limits<size_t>::max() / sizeof(T))
425 << "FixedStack capacity overflow";
426 array = new T[d];
427 }
428
430 {
431 if (array)
432 delete[] array;
433 }
434
436 FixedStack(const FixedStack &s) : array(nullptr), head(s.head), dim(s.dim)
437 {
438 std::unique_ptr<T[]> tmp(new T[s.dim]);
439 for (size_t i = 0; i < head; ++i)
440 tmp[i] = s.array[i];
441 array = tmp.release();
442 }
443
445
447 void swap(FixedStack &s) noexcept
448 {
449 std::swap(array, s.array);
450 std::swap(head, s.head);
451 std::swap(dim, s.dim);
452 }
453
455 FixedStack(FixedStack &&s) noexcept : array(nullptr), head(0), dim(0)
456 {
457 swap(s);
458 }
459
462 {
463 if (this == &s)
464 return *this;
465
466 FixedStack tmp(s);
467 swap(tmp);
468 return *this;
469 }
470
472 {
473 swap(s);
474 return *this;
475 }
476
482 T &push(const T &data) noexcept(std::is_nothrow_copy_assignable_v<T>)
483 {
484 assert(head < dim);
485 array[head++] = data;
486 return array[head - 1];
487 }
488
494 T &push(T &&data) noexcept(std::is_nothrow_move_assignable_v<T>)
495 {
496 assert(head < dim);
497 array[head++] = std::move(data);
498 return array[head - 1];
499 }
500
502 T &append(const T &data) noexcept(std::is_nothrow_copy_assignable_v<T>)
503 {
504 return push(data);
505 }
506
508 T &append(T &&data) noexcept(std::is_nothrow_move_assignable_v<T>)
509 {
510 return push(std::forward<T>(data));
511 }
512
514 T &insert(const T &data) noexcept(std::is_nothrow_copy_assignable_v<T>)
515 {
516 return push(data);
517 }
518
520 T &insert(T &&data) noexcept(std::is_nothrow_move_assignable_v<T>)
521 {
522 return push(std::forward<T>(data));
523 }
524
530 T &pushn(const size_t &n = 1) noexcept
531 {
532 assert(n > 0);
533 assert(n <= dim - head);
534
535 head += n;
536 return array[head - 1];
537 }
538
540 T pop() noexcept
541 {
542 assert(head > 0);
543
544 return std::move(array[--head]);
545 }
546
552 T popn(const int n) noexcept
553 {
554 assert(n >= 0);
555 const auto count = static_cast<size_t>(n);
556 assert(head >= count);
557
558 head -= count;
559 return std::move(array[head]);
560 }
561
563 T &top() noexcept
564 {
565 assert(head > 0);
566
567 return array[head - 1];
568 }
569
570 const T &top() const noexcept
571 {
572 assert(head > 0);
573
574 return array[head - 1];
575 }
576
577 T &get_last() noexcept
578 {
579 return top();
580 }
581
582 const T &get_last() const noexcept
583 {
584 return top();
585 }
586
588 T &base() noexcept
589 {
590 return array[0];
591 }
592
593 const T &base() const noexcept
594 {
595 return array[0];
596 }
597
605 T &top(const size_t i) noexcept
606 {
607 assert(i < head);
608 return array[head - i - 1];
609 }
610
611 const T &top(const size_t i) const noexcept
612 {
613 assert(i < head);
614 return array[head - i - 1];
615 }
616
618 [[nodiscard]] bool is_empty() const noexcept
619 {
620 return head == 0;
621 }
622
624 void empty() noexcept
625 {
626 head = 0;
627 }
628
634 void clear() noexcept
635 {
636 empty();
637 }
638
640 [[nodiscard]] size_t size() const noexcept
641 {
642 return head;
643 }
644
652 template <class Operation>
654 bool traverse(Operation &operation) noexcept(Aleph::NothrowPredicateWith<Operation &, T &>)
655 {
656 for (size_t i = 0; i < head; i++)
657 if (not static_cast<bool>(operation(array[i])))
658 return false;
659
660 return true;
661 }
662
664 template <class Operation>
666 bool traverse(Operation &operation) const
668 {
669 for (size_t i = 0; i < head; i++)
670 if (not static_cast<bool>(operation(static_cast<const T &>(array[i]))))
671 return false;
672 return true;
673 }
674
676 template <class Operation>
678 bool traverse(Operation &&operation = Operation()) const
679 noexcept(Aleph::NothrowPredicateWith<Operation &, const T &>)
680 {
681 return traverse<Operation>(operation);
682 }
683
685 template <class Operation>
687 bool traverse(Operation &&operation = Operation()) noexcept(
688 Aleph::NothrowPredicateWith<Operation &, T &>)
689 {
690 return traverse<Operation>(operation);
691 }
692
699 class Iterator : public Array_Iterator<T>
700 {
701 public:
703 using Base::Base;
705
708
710 Iterator(const FixedStack<T> &) = delete;
711 };
712
715};
716
739template <typename T, size_t N>
741{
742 static_assert(N > 0, "InlineStack needs a positive capacity");
743 static_assert(std::is_trivially_copyable_v<T>, "InlineStack holds trivially copyable items only");
744
745 T array[N]; // only [0, head) holds items; the rest is never read
746 size_t head = 0;
747
748public:
749 using Item_Type = T;
750
752 InlineStack() noexcept = default;
753
755 InlineStack(const InlineStack &s) noexcept : head(s.head)
756 {
757 for (size_t i = 0; i < head; ++i)
758 array[i] = s.array[i];
759 }
760
763 {
764 if (this == &s)
765 return *this;
766 head = s.head;
767 for (size_t i = 0; i < head; ++i)
768 array[i] = s.array[i];
769 return *this;
770 }
771
773 void swap(InlineStack &s) noexcept
774 {
775 InlineStack tmp(s);
776 s = *this;
777 *this = tmp;
778 }
779
781 [[nodiscard]] static constexpr size_t capacity() noexcept
782 {
783 return N;
784 }
785
790 T &push(const T &data) noexcept
791 {
792 assert(head < N);
793 array[head++] = data;
794 return array[head - 1];
795 }
796
798 T pop() noexcept
799 {
800 assert(head > 0);
801 return array[--head];
802 }
803
808 T popn(const size_t n) noexcept
809 {
810 assert(n <= head);
811 head -= n;
812 return array[head];
813 }
814
816 [[nodiscard]] T &top() noexcept
817 {
818 assert(head > 0);
819 return array[head - 1];
820 }
821
823 [[nodiscard]] const T &top() const noexcept
824 {
825 assert(head > 0);
826 return array[head - 1];
827 }
828
833 [[nodiscard]] T &top(const size_t i) noexcept
834 {
835 assert(i < head);
836 return array[head - i - 1];
837 }
838
840 [[nodiscard]] const T &top(const size_t i) const noexcept
841 {
842 assert(i < head);
843 return array[head - i - 1];
844 }
845
847 [[nodiscard]] T &base() noexcept
848 {
849 assert(head > 0);
850 return array[0];
851 }
852
854 [[nodiscard]] const T &base() const noexcept
855 {
856 assert(head > 0);
857 return array[0];
858 }
859
861 [[nodiscard]] bool is_empty() const noexcept
862 {
863 return head == 0;
864 }
865
867 [[nodiscard]] size_t size() const noexcept
868 {
869 return head;
870 }
871
873 void empty() noexcept
874 {
875 head = 0;
876 }
877
879 void clear() noexcept
880 {
881 head = 0;
882 }
883};
884} // end namespace Aleph
885
886#endif /* TPL_ARRAYSTACK_H */
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:727
#define ah_overflow_error_if(C)
Throws std::overflow_error if condition holds.
Definition ah-errors.H:611
#define ah_invalid_argument_if(C)
Throws std::invalid_argument if condition holds.
Definition ah-errors.H:787
STL-compatible iterator adapters for Aleph containers.
@ no_exception_ctor
Definition ahDefs.H:74
#define Special_Ctors(Set_Type, Type)
Generates special constructors for containers.
Definition ahDry.H:123
Core header for the Aleph-w library.
Iterator on the items of a stack.
typename MemArray< T >::Iterator Base
Iterator(ArrayStack< T > &s)
Iterator on the modifiable stack s.
Iterator(const ArrayStack< T > &)=delete
A constant stack is iterated through Const_Iterator.
Stack implemented with simple dynamic array and with bounds verification.
const T & get_last() const
void swap(ArrayStack &s) noexcept
Swap this with s
const T & base() const
Return a constant reference to the oldest element (the base).
T & insert(const T &data)
See push(const T & data).
T & push(const T &data)
Push into stack a copy of data
void empty() noexcept
Empty the stack.
ArrayStack(const ArrayStack &s)
Copy constructor.
ArrayStack(size_t dim=4)
The type of element.
T & top()
Return a modifiable reference to youngest element of stack (called the top)
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=Operation()) const noexcept(noexcept(array.traverse(operation)))
See traverse(Operation & operation).
size_t size() const noexcept
Return the number of elements stored in the stack.
bool traverse(Operation &operation) const noexcept(noexcept(array.traverse(operation)))
See traverse(Operation & operation).
bool traverse(Operation &operation) noexcept(noexcept(array.traverse(operation)))
Traverse all the items of stack from the youngest to oldest and conditionally performs an operation.
T & pushn(const size_t &n=1)
Push n cells into the stack.
T & append(T &&data)
See push(T && data).
T & append(const T &data)
See push(const T & data).
const T & top(const size_t i) const
T & insert(T &&data)
See push(T && data).
T & top(const size_t i)
Return a modifiable reference to the element located at i positions from top.
T popn(size_t n)
Extract in constant time the n more recently inserted elements of stack.
void clear() noexcept
Empties the container.
T & push(T &&data)
Push into stack data
T pop_ne() noexcept
T pop()
Extract the last more recently inserted element.
bool traverse(Operation &&operation=Operation()) noexcept(noexcept(array.traverse(operation)))
See traverse(Operation & operation).
MemArray< T > array
const T & top() const
ArrayStack(ArrayStack &&s) noexcept
Move constructor.
size_t capacity() const noexcept
Return the internal capacity.
T & base()
Return a modifiable reference to the oldest element (the base).
Iterator wrapper for C++ raw arrays.
Definition array_it.H:85
Read-only view of an Aleph iterator: the iterator of a constant container.
Iterator on the items of a stack.
Iterator(FixedStack< T > &s)
Iterator on the modifiable stack s.
Iterator(const FixedStack< T > &)=delete
A constant stack is iterated through Const_Iterator.
Fixed length stack.
T & base() noexcept
Return the internal array base.
bool traverse(Operation &&operation=Operation()) const noexcept(Aleph::NothrowPredicateWith< Operation &, const T & >)
See traverse(Operation & operation).
bool traverse(Operation &&operation=Operation()) noexcept(Aleph::NothrowPredicateWith< Operation &, T & >)
See traverse(Operation & operation).
FixedStack & operator=(const FixedStack &s)
Assign by copy s to this
T & push(T &&data) noexcept(std::is_nothrow_move_assignable_v< T >)
Push and move data
size_t size() const noexcept
Return the number of elements stored in the stack.
const T & top() const noexcept
T & pushn(const size_t &n=1) noexcept
Push in constant time nspaces into stack.
const T & get_last() const noexcept
T & insert(T &&data) noexcept(std::is_nothrow_move_assignable_v< T >)
See push(T && data).
T & append(T &&data) noexcept(std::is_nothrow_move_assignable_v< T >)
See push(T && data).
FixedStack(const size_t d=1024)
The type of element.
bool is_empty() const noexcept
Return true if stack is empty.
T popn(const int n) noexcept
Perform in constant time n pops.
FixedStack(const FixedStack &s)
Copy constructor.
T pop() noexcept
Pop by moving the top of stack.
T & top() noexcept
Return a modifiable reference to stack's top.
FixedStack(FixedStack &&s) noexcept
Move constructor.
T & push(const T &data) noexcept(std::is_nothrow_copy_assignable_v< T >)
Push a copy of data
void empty() noexcept
Empty the stack.
T & get_last() noexcept
T & insert(const T &data) noexcept(std::is_nothrow_copy_assignable_v< T >)
See push(const T & data).
const T & base() const noexcept
T & top(const size_t i) noexcept
Return a modifiable reference to item located i positions after top.
void clear() noexcept
Empties the container.
T & append(const T &data) noexcept(std::is_nothrow_copy_assignable_v< T >)
See push(const T & data).
const T & top(const size_t i) const noexcept
bool traverse(Operation &operation) noexcept(Aleph::NothrowPredicateWith< Operation &, T & >)
Traverse all the items of stack from the youngest to oldest and conditionally performs an operation.
void swap(FixedStack &s) noexcept
Swap in constant time s with this
bool traverse(Operation &operation) const noexcept(Aleph::NothrowPredicateWith< Operation &, const T & >)
See traverse(Operation & operation).
Stack of fixed capacity whose storage is inside the object.
T Item_Type
The type of element.
const T & base() const noexcept
See base().
size_t size() const noexcept
Return the number of items.
const T & top(const size_t i) const noexcept
See top(const size_t i).
static constexpr size_t capacity() noexcept
Maximum number of items.
T pop() noexcept
Pop the top; the stack must not be empty.
T & push(const T &data) noexcept
Push a copy of data.
T & top() noexcept
Return a modifiable reference to the top; the stack must not be empty.
void clear() noexcept
See empty().
T popn(const size_t n) noexcept
Perform n pops in constant time.
InlineStack() noexcept=default
Construct an empty stack. The storage is not initialized.
bool is_empty() const noexcept
Return true if the stack is empty.
T & base() noexcept
Return a modifiable reference to the bottom; the stack must not be empty.
const T & top() const noexcept
See top().
void empty() noexcept
Empty the stack.
InlineStack & operator=(const InlineStack &s) noexcept
Assign the items in use of s.
void swap(InlineStack &s) noexcept
Exchange the items in use of s and this, in time O(size()).
T & top(const size_t i) noexcept
Return a modifiable reference to the item i positions below the top.
Simple, scalable and fast dynamic array.
Number Theoretic Transform over Z / MOD Z.
Definition ntt.H:115
Mixin providing equality comparison for sequence containers.
Definition ah-dry.H:2945
Common methods to the Aleph-w ( ) containers.
Definition ah-dry.H:1235
Common sequential searching methods on containers.
Definition ah-dry.H:555
Mixin that adds STL begin()/end() and cbegin()/cend() to Aleph containers.
PredicateWith<F, Args...>, and neither the call nor the conversion of its result to bool can throw.
f(args...) is a valid call whose result is usable as a condition.
#define N
Definition fib.C:294
__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:4130
Singly linked list implementations with head-tail access.
Main namespace for Aleph-w library functions.
std::decay_t< typename HeadC::Item_Type > T
Definition ah-zip.H:152
Itor::difference_type count(const Itor &beg, const Itor &end, const T &value)
Count elements equal to a value.
Definition ahAlgo.H:127
Simple iterator on elements of array.
Generic list of items stored in a container.
Definition ah-dry.H:2896
Dynamic doubly linked list implementation.
Simple, scalable, contiguous dynamic array.