Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
tpl_dynListStack.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
44# ifndef TPL_DYNLISTSTACK_H
45# define TPL_DYNLISTSTACK_H
46
47# include <ahDry.H>
48# include <ahIterator.H>
49# include <ah-args-ctor.H>
50# include <htlist.H>
51
52namespace Aleph {
53
102template <typename T>
104 : public LocateFunctions<DynListStack<T>, T>,
105 public FunctionalMethods<DynListStack<T>, T>,
106 public GenericKeys<DynListStack<T>, T>,
107 public EqualToMethod<DynListStack<T>>,
108 public StlAlephIterator<DynListStack<T>>
109{
111 size_t num_items = 0;
112
113public:
114
117
119 using Item_Type = T;
120
122 using Key_Type = T;
123
134 void swap(DynListStack & other) noexcept
135 {
136 std::swap(num_items, other.num_items);
137 s.swap(other.s);
138 }
139
147 DynListStack() noexcept : num_items(0) { /* empty */ }
148
162 {
163 // empty
164 }
165
177 {
178 swap(other);
179 }
180
183
197 {
198 if (this == &rhs)
199 return *this;
200
201 s = rhs.s;
202 num_items = rhs.num_items;
203
204 return *this;
205 }
206
219 {
220 std::swap(num_items, rhs.num_items);
221 s.swap(rhs.s);
222 return *this;
223 }
224
231 [[nodiscard]] constexpr size_t size() const noexcept { return num_items; }
232
239 [[nodiscard]] bool is_empty() const noexcept { return num_items == 0; }
240
252 T & push(const T & data)
253 {
254 T & ret_val = s.insert(data);
255 ++num_items;
256 return ret_val;
257 }
258
269 T & push(T && data)
270 {
271 T & ret_val = s.insert(std::forward<T>(data));
272 ++num_items;
273 return ret_val;
274 }
275
294 template <typename... Args>
296 {
297 return push(T(std::forward<Args>(args)...));
298 }
299
304 T & append(const T & data) { return push(data); }
305
307 T & append(T && data) { return push(std::forward<T>(data)); }
308
313 T & put(const T & data) { return push(data); }
314
316 T & put(T && data) { return push(std::forward<T>(data)); }
317
322 T & insert(const T & data) { return push(data); }
323
325 T & insert(T && data) { return push(std::forward<T>(data)); }
326
340 {
342 --num_items;
343 return ret_val;
344 }
345
350 [[nodiscard]] T get() { return pop(); }
351
364 {
365 return s.get_first();
366 }
367
377 [[nodiscard]] const T & top() const
378 {
379 return s.get_first();
380 }
381
386 [[nodiscard]] T & peek() { return top(); }
387
392 [[nodiscard]] const T & peek() const { return top(); }
393
401 {
402 s.empty();
403 num_items = 0;
404 }
405
410 void clear() noexcept { empty(); }
411
425 template <class Operation>
427 {
428 return s.traverse(operation);
429 }
430
432 template <class Operation>
434 {
435 return s.traverse(operation);
436 }
437
439 template <class Operation>
441 {
442 return s.traverse(std::forward<Operation>(operation));
443 }
444
446 template <class Operation>
448 {
449 return s.traverse(std::forward<Operation>(operation));
450 }
451
465 T * search(const T & key) noexcept
466 {
467 return this->find_ptr([&key](const T & item) { return item == key; });
468 }
469
471 const T * search(const T & key) const noexcept
472 {
473 return const_cast<DynListStack*>(this)->search(key);
474 }
475
483 [[nodiscard]] bool contains(const T & key) const noexcept
484 {
485 return search(key) != nullptr;
486 }
487
489 [[nodiscard]] bool has(const T & key) const noexcept
490 {
491 return contains(key);
492 }
493
519 struct Iterator : public DynList<T>::Iterator
520 {
523
525 using Base = typename DynList<T>::Iterator;
526
528 using Base::Base;
529
536 Iterator(const DynListStack<T> & stack) noexcept : Base(stack.s) {}
537 };
538};
539
540} // end namespace Aleph
541# endif /* TPL_DYNLISTSTACK_H */
Variadic constructor macros for containers.
DRY (Don't Repeat Yourself) utilities and macros.
#define Special_Ctors(Set_Type, Type)
Generates special constructors for containers.
Definition ahDry.H:113
Iterator traits and STL-compatible iterator wrappers.
Dynamic stack of elements of generic type T based on a singly linked list.
T & append(const T &data)
Alias for push() - required by Special_Ctors macro.
DynListStack(const DynListStack &other)
Copy constructor.
void clear() noexcept
Alias for empty() - removes all elements.
T Key_Type
Alias for Item_Type, required by EqualToMethod mixin.
T & top()
Return a modifiable reference to the top item of the stack.
bool traverse(Operation &&operation=Operation())
bool is_empty() const noexcept
Check if the stack is empty.
bool traverse(Operation &operation) const
DynListStack() noexcept
Construct an empty stack.
constexpr size_t size() const noexcept
Return the number of elements in the stack.
T & emplace(Args &&... args)
Construct an item in place at the top of the stack.
T & peek()
Alias for top() - returns reference to top item.
void swap(DynListStack &other) noexcept
Swap the contents of this stack with another.
T * search(const T &key) noexcept
Search for an item in the stack using equality comparison.
T get()
Alias for pop() - removes and returns the top item.
const T * search(const T &key) const noexcept
DynListStack & operator=(const DynListStack &rhs)
Copy assignment operator.
bool traverse(Operation &operation)
Traverse all elements from top to bottom.
const T & peek() const
Alias for top() const - returns const reference to top item.
bool contains(const T &key) const noexcept
Check if the stack contains a specific value.
T & push(T &&data)
Push an item by move onto the top of the stack.
T Item_Type
The type of elements stored in the stack.
DynListStack(DynListStack &&other) noexcept
Move constructor.
bool has(const T &key) const noexcept
Alias for contains().
T & put(const T &data)
Alias for push() - for compatibility with queue-like interfaces.
bool traverse(Operation &&operation=Operation()) const
T & insert(const T &data)
Alias for push() - for STL-like insert semantics.
T pop()
Remove and return the top item of the stack.
T & push(const T &data)
Push an item by copy onto the top of the stack.
const T & top() const
Return a const reference to the top item of the stack.
void empty() noexcept
Remove all elements from the stack.
Iterator on the items of list.
Definition htlist.H:1714
Dynamic singly linked list with functional programming support.
Definition htlist.H:1423
T & insert(const T &item)
Insert a new item by copy.
Definition htlist.H:1502
T & get_first() const
Return the first item of the list.
Definition htlist.H:1675
void empty() noexcept
empty the list
Definition htlist.H:1689
DynList & swap(DynList &l) noexcept
Swap this with l.
Definition htlist.H:1448
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
T * find_ptr(Operation &operation) noexcept(operation_is_noexcept< Operation >())
Find a pointer to an item in the container according to a searching criteria.
Definition ah-dry.H:318
Mixin that adds STL begin()/end() and cbegin()/cend() to Aleph containers.
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
Iterator for traversing elements of the stack.
Iterator(const DynListStack< T > &stack) noexcept
Construct an iterator for a stack.
typename DynList< T >::Iterator Base
Base iterator type.
Generic list of items stored in a container.
Definition ah-dry.H:1501
bool traverse(Operation &operation) noexcept(traverse_is_noexcept< Operation >())
Traverse the container via its iterator and performs a conditioned operation on each item.
Definition ah-dry.H:95