Aleph-w 7.10.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 <type_traits>
48# include <ahDry.H>
49# include <ahIterator.H>
50# include <ah-args-ctor.H>
51# include <htlist.H>
52#include <ah-iterator.H>
53
54namespace Aleph {
55
104template <typename T>
106 : public LocateFunctions<DynListStack<T>, T>,
107 public FunctionalMethods<DynListStack<T>, T>,
108 public GenericKeys<DynListStack<T>, T>,
109 public EqualSequenceMethod<DynListStack<T>>,
110 public StlAlephIterator<DynListStack<T>>
111{
113
114public:
115
118
120 using Item_Type = T;
121
123 using Key_Type = T;
124
135 void swap(DynListStack & other) noexcept
136 {
137 s.swap(other.s);
138 }
139
147 DynListStack() noexcept { /* empty */ }
148
161 : s(other.s)
162 {
163 // empty
164 }
165
176 DynListStack(DynListStack && other) noexcept
177 {
178 swap(other);
179 }
180
183
197 {
198 if (this == &rhs)
199 return *this;
200
201 s = rhs.s;
202
203 return *this;
204 }
205
218 {
219 s.swap(rhs.s);
220 return *this;
221 }
222
229 [[nodiscard]] size_t size() const noexcept { return s.size(); }
230
237 [[nodiscard]] bool is_empty() const noexcept { return s.is_empty(); }
238
250 T & push(const T & data)
251 {
252 return s.insert(data);
253 }
254
265 T & push(T && data)
266 {
267 return s.insert(std::forward<T>(data));
268 }
269
288 template <typename... Args>
289 T & emplace(Args&&... args)
290 {
291 return push(T(std::forward<Args>(args)...));
292 }
293
298 T & append(const T & data) { return push(data); }
299
301 T & append(T && data) { return push(std::forward<T>(data)); }
302
307 T & put(const T & data) { return push(data); }
308
310 T & put(T && data) { return push(std::forward<T>(data)); }
311
316 T & insert(const T & data) { return push(data); }
317
319 T & insert(T && data) { return push(std::forward<T>(data)); }
320
336 [[nodiscard]] T pop()
337 {
338 return s.remove_first();
339 }
340
345 [[nodiscard]] T get() { return pop(); }
346
358 [[nodiscard]] T & top()
359 {
360 return s.get_first();
361 }
362
372 [[nodiscard]] const T & top() const
373 {
374 return s.get_first();
375 }
376
381 [[nodiscard]] T & peek() { return top(); }
382
387 [[nodiscard]] const T & peek() const { return top(); }
388
395 void empty() noexcept
396 {
397 s.empty();
398 }
399
405 void clear() noexcept { empty(); }
406
420 template <class Operation>
422 bool traverse(Operation & operation)
423 noexcept(noexcept(s.traverse(operation)))
424 {
425 return s.traverse(operation);
426 }
427
429 template <class Operation>
431 bool traverse(Operation & operation) const
432 noexcept(noexcept(s.traverse(operation)))
433 {
434 return s.traverse(operation);
435 }
436
438 template <class Operation>
440 bool traverse(Operation && operation = Operation()) const
441 noexcept(noexcept(s.traverse(operation)))
442 {
443 return s.traverse(std::forward<Operation>(operation));
444 }
445
447 template <class Operation>
449 bool traverse(Operation && operation = Operation())
450 noexcept(noexcept(s.traverse(operation)))
451 {
452 return s.traverse(std::forward<Operation>(operation));
453 }
454
467 T * search(const T & key)
468 noexcept(noexcept(static_cast<bool>(std::declval<const T &>() == std::declval<const T &>())))
469 {
470 return this->find_ptr([&key](const T & item) { return static_cast<bool>(item == key); });
471 }
472
474 const T * search(const T & key) const
475 noexcept(noexcept(static_cast<bool>(std::declval<const T &>() == std::declval<const T &>())))
476 {
477 return this->find_ptr([&key](const T & item) { return static_cast<bool>(item == key); });
478 }
479
487 [[nodiscard]] bool contains(const T & key) const
488 noexcept(noexcept(static_cast<bool>(std::declval<const T &>() == std::declval<const T &>())))
489 {
490 return search(key) != nullptr;
491 }
492
494 [[nodiscard]] bool has(const T & key) const
495 noexcept(noexcept(static_cast<bool>(std::declval<const T &>() == std::declval<const T &>())))
496 {
497 return contains(key);
498 }
499
525 struct Iterator : public DynList<T>::Iterator
526 {
529
531 using Base = typename DynList<T>::Iterator;
532
534 using Base::Base;
535
543 Iterator(DynListStack<T> & stack) noexcept : Base(stack.s) {}
544
546 Iterator(const DynListStack<T> &) = delete;
547 };
548
551};
552
553} // end namespace Aleph
554# endif /* TPL_DYNLISTSTACK_H */
Variadic constructor macros for containers.
STL-compatible iterator adapters for Aleph containers.
DRY (Don't Repeat Yourself) utilities and macros.
#define Special_Ctors(Set_Type, Type)
Generates special constructors for containers.
Definition ahDry.H:123
Iterator traits and STL-compatible iterator wrappers.
Read-only view of an Aleph iterator: the iterator of a constant container.
Dynamic stack of elements of generic type T based on a singly linked list.
T * search(const T &key) noexcept(noexcept(static_cast< bool >(std::declval< const T & >()==std::declval< const T & >())))
Search for an item in the stack using equality comparison.
T & put(const T &data)
Alias for push() - for compatibility with queue-like interfaces.
DynListStack(const DynListStack &other)
Copy constructor.
void clear() noexcept
Empties the container.
T Key_Type
Alias for Item_Type.
T & top()
Return a modifiable reference to the top item of the stack.
bool is_empty() const noexcept
Check if the stack is empty.
DynListStack() noexcept
Construct an empty stack.
bool has(const T &key) const noexcept(noexcept(static_cast< bool >(std::declval< const T & >()==std::declval< const T & >())))
Alias for contains().
T & insert(const T &data)
Alias for push() - for STL-like insert semantics.
bool contains(const T &key) const noexcept(noexcept(static_cast< bool >(std::declval< const T & >()==std::declval< const T & >())))
Check if the stack contains a specific value.
T & emplace(Args &&... args)
Construct an item in place at the top of the stack.
bool traverse(Operation &&operation=Operation()) const noexcept(noexcept(s.traverse(operation)))
See traverse(Operation & operation).
size_t size() const noexcept
Return the number of elements in 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 & append(T &&data)
See append(const T & data).
T get()
Alias for pop() - removes and returns the top item.
DynListStack & operator=(const DynListStack &rhs)
Copy assignment operator.
T & append(const T &data)
Alias for push() - required by Special_Ctors macro.
const T & peek() const
Alias for top() const - returns const reference to top item.
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.
bool traverse(Operation &operation) const noexcept(noexcept(s.traverse(operation)))
See traverse(Operation & operation).
DynListStack(DynListStack &&other) noexcept
Move constructor.
bool traverse(Operation &operation) noexcept(noexcept(s.traverse(operation)))
Traverse all elements from top to bottom.
T & insert(T &&data)
See insert(const T & data).
T pop()
Remove and return the top item of the stack.
const T * search(const T &key) const noexcept(noexcept(static_cast< bool >(std::declval< const T & >()==std::declval< const T & >())))
See search(const T & key).
T & put(T &&data)
See put(const T & data).
T & push(const T &data)
Push an item by copy onto the top of the stack.
bool traverse(Operation &&operation=Operation()) noexcept(noexcept(s.traverse(operation)))
See traverse(Operation & operation).
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:1614
Doubly-linked list (defined in tpl_dynList.H).
Definition htlist.H:1231
T & get_first()
Return the first item of the list.
Definition htlist.H:1548
T & insert(const T &item)
Definition htlist.H:1308
T remove_first()
See remove().
Definition htlist.H:1473
size_t size() const noexcept
Return the number of items of the list, in constant time.
Definition htlist.H:1264
void empty() noexcept
empty the list
Definition htlist.H:1576
DynList & swap(DynList &l) noexcept
Definition htlist.H:1253
constexpr bool is_empty() const noexcept
Definition htlist.H:434
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
and Aleph::dry_detail::MutablyTraversable< typename Aleph::concepts_detail::defer< DynListStack< T >, Operation >::type, T > T * find_ptr(Operation &operation) noexcept(mutable_search_is_noexcept< Operation >())
Find a pointer to an item in the container according to a searching criterion.
Definition ah-dry.H:825
Mixin that adds STL begin()/end() and cbegin()/cend() to Aleph containers.
f(args...) is a valid call whose result is usable as a condition.
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
Iterator for traversing elements of the stack.
Iterator(const DynListStack< T > &)=delete
A constant stack is iterated through Const_Iterator.
Iterator(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:2896
bool traverse(Operation &operation) noexcept(traverse_is_noexcept< Operation, typename Aleph::concepts_detail::traverse_arg< Container, Operation >::type, Container & >())
Traverse the container via its iterator and performs a conditioned operation on each item.
Definition ah-dry.H:411