Stroika Library 3.0d16
 
Loading...
Searching...
No Matches
Stack.h
Go to the documentation of this file.
1/*
2 * Copyright(c) Sophist Solutions, Inc. 1990-2025. All rights reserved
3 */
4#ifndef _Stroika_Foundation_Containers_Stack_h_
5#define _Stroika_Foundation_Containers_Stack_h_ 1
6
7#include "Stroika/Foundation/StroikaPreComp.h"
8
9#include <ranges>
10
12#include "Stroika/Foundation/Common/Concepts.h"
13#include "Stroika/Foundation/Containers/Common.h"
15
16/**
17 * \file
18 *
19 * \note Code-Status: <a href="Code-Status.md#Beta">Beta</a>
20 *
21 * TODO:
22 * @todo Embellish test cases (regression tests), and fix/make sure copying works.
23 *
24 * @todo Review
25 * http://github.com/SophistSolutions/Stroika/blob/master/Archive/Stroika_FINAL_for_STERL_1992/Library/Foundation/Headers/Stack.hh
26 * for any important lackings
27 *
28 */
29
31
33 using Traversal::IInputIterator;
34 using Traversal::IIterableOfTo;
35 using Traversal::Iterable;
36 using Traversal::Iterator;
37
38 /**
39 * Standard LIFO (Last in first out) Stack. See Sedgewick, 30-31.
40 *
41 * \note Iteration proceeds from the top (last in & first out) to the bottom of the stack (first in & last out). Top
42 * is the LAST IN (also first out).
43 *
44 * *Design Note*:
45 * We considered NOT having Stack<T> inherit from Iterable<T>, but that made copying of
46 * a stack intrinsically more costly, as you had to copy, and then pop items to see them,
47 * and put them into a new stack. A special copy API (private to stack) would have limited
48 * the ease of interoperating the Stack<T> container with other sorts of containers.
49 *
50 * \em Concrete Implementations:
51 * o @see Concrete::Stack_LinkedList<>
52 *
53 * \em Factory:
54 * @see Stack_Factory<> to see default implementations.
55 *
56 * \note \em Thread-Safety <a href="Thread-Safety.md#C++-Standard-Thread-Safety">C++-Standard-Thread-Safety</a>
57 *
58 * \note See <a href="./ReadMe.md">ReadMe.md</a> for common features of all Stroika containers (especially
59 * constructors, iterators, etc)
60 *
61 * \note <a href="Design-Overview.md#Comparisons">Comparisons</a>:
62 * o static_assert (equality_comparable<T> ==> equality_comparable<Stack<T>>);
63 * o EqualsComparer provided as alias to SequentialEqualsComparer
64 * Two Queues are considered equal if they contain the same elements (by comparing them
65 * with EQUALS_COMPARER (which defaults to equal_to<T>)
66 * in exactly the same order (iteration).
67 * o Since ordering in a Queue is well defined, we can use this ordering between elements to define
68 * the obvious sequential ordering three way comparison on queues (Iterable::SequentialThreeWayComparer)
69 */
70 template <typename T>
71 class [[nodiscard]] Stack : public Iterable<T> {
72 private:
73 using inherited = Iterable<T>;
74
75 protected:
76 class _IRep;
77
78 public:
79 /**
80 * Use this typedef in templates to recover the basic functional container pattern of concrete types.
81 */
83
84 public:
85 /**
86 * @see inherited::value_type
87 */
89
90 public:
91 /**
92 * \note When copying an Iterable<> or range, the copy is done by repeatedly pushing
93 * the arguments in the reverse order they are encountered, thus preserving the 'iteration order'
94 * of argument and copied stack.
95 *
96 * \note <a href="ReadMe.md#Container Constructors">See general information about container constructors that applies here</a>
97 */
98 Stack ();
99 Stack (Stack&&) noexcept = default;
100 Stack (const Stack&) noexcept = default;
101#if qCompilerAndStdLib_RequiresNotMatchInlineOutOfLineForTemplateClassBeingDefined_Buggy
102 template <typename ITERABLE_OF_ADDABLE,
103 enable_if_t<IIterableOfTo<ITERABLE_OF_ADDABLE, T> and not derived_from<remove_cvref_t<ITERABLE_OF_ADDABLE>, Stack<T>>>* = nullptr>
104 explicit Stack (ITERABLE_OF_ADDABLE&& src);
105#else
106 template <IIterableOfTo<T> ITERABLE_OF_ADDABLE>
107 explicit Stack (ITERABLE_OF_ADDABLE&& src)
108 requires (not derived_from<remove_cvref_t<ITERABLE_OF_ADDABLE>, Stack<T>>);
109#endif
110 template <IInputIterator<T> ITERATOR_OF_ADDABLE>
111 Stack (ITERATOR_OF_ADDABLE&& start, ITERATOR_OF_ADDABLE&& end);
112
113 protected:
114 explicit Stack (shared_ptr<_IRep>&& src) noexcept;
115 explicit Stack (const shared_ptr<_IRep>& src) noexcept;
116
117 public:
118 /**
119 */
120 nonvirtual Stack& operator= (Stack&&) noexcept = default;
121 nonvirtual Stack& operator= (const Stack&) = default;
122
123 public:
124 /**
125 * \note mutates container
126 */
127 nonvirtual void Push (ArgByValueType<value_type> item);
128
129 public:
130 /**
131 * \note mutates container
132 */
133 nonvirtual value_type Pop ();
134
135 public:
136 /**
137 */
138 nonvirtual value_type Top () const;
139
140 public:
141 /**
142 * \note mutates container
143 */
144 nonvirtual void RemoveAll ();
145
146 public:
147 /**
148 * \brief simply indirect to @Iterable<T>::SequentialEqualsComparer
149 *
150 * Two Stack are considered equal if they contain the same elements in the same order.
151 * That is - @Iterable<T>::SequenceEquals.
152 */
153 template <typename T_EQUALS_COMPARER = equal_to<T>>
154 using EqualsComparer = typename Iterable<T>::template SequentialEqualsComparer<T_EQUALS_COMPARER>;
155
156 public:
157 /**
158 */
159 template <typename ELEMENT_COMPARER = compare_three_way>
160 using ThreeWayComparer = typename Iterable<T>::template SequentialThreeWayComparer<ELEMENT_COMPARER>;
161
162 public:
163 /**
164 * simply indirect to @Stack<>::EqualsComparer
165 */
166 nonvirtual bool operator== (const Stack& rhs) const
167 requires (equality_comparable<T>);
168
169 public:
170 /**
171 * simply indirect to @Stack<>::operator (only defined if ???comparethreeway?<T> is defined)
172 */
173 nonvirtual auto operator<=> (const Stack& rhs) const
174 requires (three_way_comparable<T>);
175
176 public:
177 /**
178 * \brief STL-ish alias for RemoveAll ().
179 *
180 * \note mutates container
181 */
182 nonvirtual void clear ();
183
184 protected:
185 /**
186 */
187 template <typename T2>
188 using _SafeReadRepAccessor = typename inherited::template _SafeReadRepAccessor<T2>;
189
190 protected:
191 /**
192 */
193 template <typename T2>
194 using _SafeReadWriteRepAccessor = typename inherited::template _SafeReadWriteRepAccessor<T2>;
195
196 protected:
197 nonvirtual void _AssertRepValidType () const;
198 };
199
200 /**
201 * \brief Implementation detail for Stack<T> implementors.
202 *
203 * Protected abstract interface to support concrete implementations of
204 * the Stack<T> container API.
205 */
206 template <typename T>
207 class Stack<T>::_IRep : public Iterable<T>::_IRep {
208 protected:
209 _IRep () = default;
210
211 public:
212 virtual ~_IRep () = default;
213
214 public:
215 virtual shared_ptr<_IRep> CloneEmpty () const = 0;
216 virtual void Push (ArgByValueType<value_type> item) = 0;
217 virtual value_type Pop () = 0;
218 virtual value_type Top () const = 0;
219 };
220
221}
222
223/*
224 ********************************************************************************
225 ******************************* Implementation Details *************************
226 ********************************************************************************
227 */
228
229#include "Stack.inl"
230
231#endif /*_Stroika_Foundation_Containers_Stack_h_ */
Implementation detail for Stack<T> implementors.
Definition Stack.h:207
typename Iterable< T >::template SequentialEqualsComparer< T_EQUALS_COMPARER > EqualsComparer
simply indirect to @Iterable<T>::SequentialEqualsComparer
Definition Stack.h:154
typename inherited::value_type value_type
Definition Stack.h:88
Iterable<T> is a base class for containers which easily produce an Iterator<T> to traverse them.
Definition Iterable.h:237
T value_type
value_type is an alias for the type iterated over - like vector<T>::value_type
Definition Iterable.h:248
conditional_t<(sizeof(CHECK_T)<=2 *sizeof(void *)) and is_trivially_copyable_v< CHECK_T >, CHECK_T, const CHECK_T & > ArgByValueType
This is an alias for 'T' - but how we want to pass it on stack as formal parameter.
Definition TypeHints.h:32