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 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
31
45#ifndef TPL_ARRAYSTACK_H
46#define TPL_ARRAYSTACK_H
47
48#include <iostream>
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
59namespace Aleph
60{
79 template <typename T>
80 class ArrayStack : public LocateFunctions<ArrayStack<T>, T>,
81 public FunctionalMethods<ArrayStack<T>, T>,
82 public GenericKeys<ArrayStack<T>, T>,
83 public EqualToMethod<ArrayStack<T>>,
84 public StlAlephIterator<ArrayStack<T>>
85 {
87
88 public:
89 using Item_Type = T;
90
92 ArrayStack(size_t dim = 4) : array(dim)
93 { /* empty */
94 }
95
98 { /* empty */
99 }
100
103 : array(std::move(s.array))
104 { /* empty */
105 }
106
108
111 {
112 if (this == &s)
113 return *this;
114
115 array = s.array;
116
117 return *this;
118 }
119
121 void swap(ArrayStack & s) noexcept
122 {
123 array.swap(s.array);
124 }
125
128 {
129 array.swap(s.array);
130 return *this;
131 }
132
139 T &push(const T & data)
140 {
141 return array.put(data);
142 }
143
150 T &push(T && data)
151 {
152 return array.put(std::forward<T>(data));
153 }
154
156 T &append(const T & data) { return push(data); }
157
159 T &append(T && data) { return push(std::forward<T>(data)); }
160
162 T &insert(const T & data) { return push(data); }
163
165 T &insert(T && data) { return push(std::forward<T>(data)); }
166
177 T &pushn(const size_t & n = 1)
178 {
179 ah_invalid_argument_if(n == 0) << "ArrayStack::pushn: n must be positive";
180 array.putn(n);
181 return array.last();
182 }
183
190 {
191 return array.get(1);
192 }
193
195 {
196 return array.get_ne(1);
197 }
198
206 T popn(size_t n)
207 {
208 ah_invalid_argument_if(n == 0) << "ArrayStack::popn: n must be positive";
209 return array.get(n);
210 }
211
215 {
216 return array.last();
217 }
218
219 const T &top() const
220 {
221 return array.last();
222 }
223
226 {
227 return array.first();
228 }
229
231 {
232 return array.first();
233 }
234
244 T &top(size_t i)
245 {
246 const size_t sz = array.size();
247 ah_out_of_range_error_if(i >= sz) << "top index out of range";
248 return array.access(sz - i - 1);
249 }
250
251 const T &top(size_t i) const
252 {
253 const size_t sz = array.size();
254 ah_out_of_range_error_if(i >= sz) << "top index out of range";
255 return array(sz - i - 1);
256 }
257
258 T &get_last() { return top(); }
259
260 const T &get_last() const { return top(); }
261
263 void empty() noexcept { array.empty(); }
264
270 void clear() noexcept { empty(); }
271
273 bool is_empty() const noexcept { return array.size() == 0; }
274
276 size_t size() const noexcept { return array.size(); }
277
279 size_t capacity() const noexcept { return array.capacity(); }
280
288 template <class Operation>
290 {
291 return array.traverse(operation);
292 }
293
295 template <class Operation>
297 {
298 return array.traverse(operation);
299 }
300
302 template <class Operation>
304 {
305 return array.traverse(operation);
306 }
307
309 template <class Operation>
311 {
312 return array.traverse(operation);
313 }
314
321 class Iterator : public MemArray<T>::Iterator
322 {
323 public:
324 using Base = typename MemArray<T>::Iterator;
326
327 using Base::Base;
328
330 Iterator(const ArrayStack<T> & s) : Base(s.array) {}
331 };
332 };
333
346 template <typename T>
347 class FixedStack : public LocateFunctions<FixedStack<T>, T>,
348 public FunctionalMethods<FixedStack<T>, T>,
349 public GenericKeys<FixedStack<T>, T>,
350 public EqualToMethod<FixedStack<T>>,
351 public StlAlephIterator<FixedStack<T>>
352 {
353 T *array = nullptr;
354 size_t head = 0;
355 size_t dim = 0;
356
357 public:
358 using Item_Type = T;
359
361 FixedStack(size_t d = 1024)
362 // Don't change the default value because you would risk of
363 // breaking the tests
364 : array(new T[d]), head(0), dim(d)
365 {
366 /* empty */
367 }
368
370 {
371 if (array)
372 delete [] array;
373 }
374
377 : array(nullptr), head(s.head), dim(s.dim)
378 {
379 std::unique_ptr<T[]> tmp(new T[s.dim]);
380 for (size_t i = 0; i < head; ++i)
381 tmp[i] = s.array[i];
382 array = tmp.release();
383 }
384
386
388 void swap(FixedStack & s) noexcept
389 {
390 std::swap(array, s.array);
391 std::swap(head, s.head);
392 std::swap(dim, s.dim);
393 }
394
397 : array(nullptr), head(0), dim(0)
398 {
399 swap(s);
400 }
401
404 {
405 if (this == &s)
406 return *this;
407
408 FixedStack tmp(s);
409 swap(tmp);
410 return *this;
411 }
412
414 {
415 swap(s);
416 return *this;
417 }
418
424 T &push(const T & data) noexcept(std::is_nothrow_copy_assignable_v<T>)
425 {
426 assert(head < dim);
427 array[head++] = data;
428 return array[head - 1];
429 }
430
436 T &push(T && data) noexcept(std::is_nothrow_move_assignable_v<T>)
437 {
438 assert(head < dim);
439 array[head++] = std::move(data);
440 return array[head - 1];
441 }
442
444 T &append(const T & data) noexcept(std::is_nothrow_copy_assignable_v<T>)
445 {
446 return push(data);
447 }
448
450 T &append(T && data) noexcept(std::is_nothrow_move_assignable_v<T>)
451 {
452 return push(std::forward<T>(data));
453 }
454
456 T &insert(const T & data) noexcept(std::is_nothrow_copy_assignable_v<T>)
457 {
458 return push(data);
459 }
460
462 T &insert(T && data) noexcept(std::is_nothrow_move_assignable_v<T>)
463 {
464 return push(std::forward<T>(data));
465 }
466
472 T &pushn(const size_t & n = 1) noexcept
473 {
474 assert(n > 0);
475 assert(n <= dim - head);
476
477 head += n;
478 return array[head - 1];
479 }
480
483 {
484 assert(head > 0);
485
486 return std::move(array[--head]);
487 }
488
494 T popn(const int & n) noexcept
495 {
496 assert(n >= 0);
497 const auto count = static_cast<size_t>(n);
498 assert(head >= count);
499
500 head -= count;
501 return std::move(array[head]);
502 }
503
506 {
507 assert(head > 0);
508
509 return array[head - 1];
510 }
511
513 {
514 assert(head > 0);
515
516 return array[head - 1];
517 }
518
519 T &get_last() noexcept { return top(); }
520
521 const T &get_last() const noexcept { return top(); }
522
524 T &base() noexcept { return array[0]; }
525
526 const T &base() const noexcept { return array[0]; }
527
535 T &top(const size_t i) noexcept
536 {
537 assert(i < head);
538 return array[head - i - 1];
539 }
540
541 const T &top(const size_t i) const noexcept
542 {
543 assert(i < head);
544 return array[head - i - 1];
545 }
546
548 [[nodiscard]] bool is_empty() const noexcept { return head == 0; }
549
551 void empty() noexcept { head = 0; }
552
558 void clear() noexcept { empty(); }
559
561 [[nodiscard]] size_t size() const noexcept { return head; }
562
570 template <class Operation>
572 {
573 for (size_t i = 0; i < head; i++)
574 if (not operation(array[i]))
575 return false;
576
577 return true;
578 }
579
581 template <class Operation>
583 {
584 return const_cast<FixedStack *>(this)->traverse(operation);
585 }
586
588 template <class Operation>
590 {
592 }
593
595 template <class Operation>
597 {
599 }
600
607 class Iterator : public Array_Iterator<T>
608 {
609 public:
611 using Base::Base;
613
616 };
617 };
618} // end namespace Aleph
619
620# 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:579
#define ah_invalid_argument_if(C)
Throws std::invalid_argument if condition holds.
Definition ah-errors.H:639
@ 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.
const T & get_last() const
void swap(ArrayStack &s) noexcept
Swap this with s
bool traverse(Operation &&operation=Operation())
T & insert(const T &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)
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.
const T & base() const noexcept
T & pushn(const size_t &n=1)
Push n cells into the stack.
T & append(T &&data)
T & append(const T &data)
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.
const T & top(size_t i) const
void clear() noexcept
Empties the container.
T & top(size_t i)
Return a modifiable reference to the element located at i positions from top.
T pop_ne() noexcept
T pop()
Extract the last more recently inserted element.
MemArray< T > array
bool traverse(Operation &&operation=Operation()) const
const T & top() 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 & base() noexcept
Return the internal array base.
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.
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 >)
T popn(const int &n) noexcept
Perform in constant time n pops.
bool traverse(Operation &&operation=Operation())
T & append(T &&data) noexcept(std::is_nothrow_move_assignable_v< T >)
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.
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.
void empty() noexcept
Empty the stack.
T & get_last() noexcept
T & insert(const T &data) noexcept(std::is_nothrow_copy_assignable_v< T >)
const T & base() const noexcept
T & top(const size_t i) noexcept
Return a modifiable reference to item located i positions after top.
T & push(const T &data) noexcept(std::is_nothrow_copy_assignable_v< T >)
Push a copy of data
void clear() noexcept
Empties the container.
T & append(const T &data) noexcept(std::is_nothrow_copy_assignable_v< T >)
bool traverse(Operation &operation) const
FixedStack(size_t d=1024)
The type of element.
const T & top(const size_t i) const noexcept
T & push(T &&data) noexcept(std::is_nothrow_move_assignable_v< T >)
Push and move data
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:1826
Common methods to the Aleph-w ( ) containers.
Definition ah-dry.H:642
Common sequential searching methods on containers.
Definition ah-dry.H:196
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
Divide_Conquer_DP_Result< Cost > divide_and_conquer_partition_dp(const size_t groups, const size_t n, Transition_Cost_Fn transition_cost, const Cost inf=dp_optimization_detail::default_inf< Cost >())
Optimize partition DP using divide-and-conquer optimization.
std::decay_t< typename HeadC::Item_Type > T
Definition ah-zip.H:105
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:1714
Dynamic doubly linked list implementation.
Simple, scalable, contiguous dynamic array.