Stroika Library 3.0d16
 
Loading...
Searching...
No Matches
Queue.inl
1/*
2 * Copyright(c) Sophist Solutions, Inc. 1990-2025. All rights reserved
3 */
4
5#include "Stroika/Foundation/Common/Concepts.h"
7#include "Stroika/Foundation/Containers/Private/IterableUtils.h"
9
11
12 /*
13 ********************************************************************************
14 ************************************ Queue<T> **********************************
15 ********************************************************************************
16 */
17 template <typename T>
19 : inherited{Factory::Queue_Factory<T>::Default () ()}
20 {
21 _AssertRepValidType ();
22 }
23 template <typename T>
24 inline Queue<T>::Queue (const initializer_list<value_type>& src)
25 : Queue{}
26 {
27 AddAllToTail (src);
28 _AssertRepValidType ();
29 }
30#if !qCompilerAndStdLib_RequiresNotMatchInlineOutOfLineForTemplateClassBeingDefined_Buggy
31 template <typename T>
32 template <IIterableOfTo<T> ITERABLE_OF_ADDABLE>
33 requires (not derived_from<remove_cvref_t<ITERABLE_OF_ADDABLE>, Queue<T>>)
34 inline Queue<T>::Queue (ITERABLE_OF_ADDABLE&& src)
35 : Queue{}
36 {
37 AddAllToTail (forward<ITERABLE_OF_ADDABLE> (src));
38 _AssertRepValidType ();
39 }
40#endif
41 template <typename T>
42 inline Queue<T>::Queue (const shared_ptr<_IRep>& rep) noexcept
43 : inherited{(RequireExpression (rep != nullptr), rep)}
44 {
45 _AssertRepValidType ();
46 RequireNotNull (rep);
47 }
48 template <typename T>
49 inline Queue<T>::Queue (shared_ptr<_IRep>&& rep) noexcept
50 : inherited{(RequireExpression (rep != nullptr), move (rep))}
51 {
52 _AssertRepValidType ();
53 }
54 template <typename T>
55 template <IInputIterator<T> ITERATOR_OF_ADDABLE>
56 inline Queue<T>::Queue (ITERATOR_OF_ADDABLE&& start, ITERATOR_OF_ADDABLE&& end)
57 : Queue{}
58 {
59 AddAllToTail (start, end);
60 _AssertRepValidType ();
61 }
62 template <typename T>
63 inline void Queue<T>::AddTail (ArgByValueType<value_type> item)
64 {
65 _SafeReadWriteRepAccessor<_IRep>{this}._GetWriteableRep ().AddTail (item);
66 }
67 template <typename T>
68 inline void Queue<T>::push_back (ArgByValueType<value_type> item)
69 {
70 AddTail (item);
71 }
72 template <typename T>
73 inline auto Queue<T>::Head () const -> value_type
74 {
75 return _SafeReadRepAccessor<_IRep>{this}._ConstGetRep ().Head ();
76 }
77 template <typename T>
78 nonvirtual T Queue<T>::front () const
79 {
80 return Head ();
81 }
82 template <typename T>
83 inline auto Queue<T>::HeadIf () const -> optional<value_type>
84 {
85 return _SafeReadRepAccessor<_IRep>{this}._ConstGetRep ().HeadIf ();
86 }
87 template <typename T>
89 {
90 Require (not this->empty ());
91 return _SafeReadWriteRepAccessor<_IRep>{this}._GetWriteableRep ().RemoveHead ();
92 }
93 template <typename T>
95 {
96 return RemoveHead ();
97 }
98 template <typename T>
99 inline auto Queue<T>::RemoveHeadIf () -> optional<value_type>
100 {
101 return _SafeReadWriteRepAccessor<_IRep>{this}._GetWriteableRep ().RemoveHeadIf ();
102 }
103 template <typename T>
104 inline void Queue<T>::Enqueue (ArgByValueType<value_type> item)
105 {
106 _SafeReadWriteRepAccessor<_IRep>{this}._GetWriteableRep ().AddTail (item);
107 }
108 template <typename T>
110 {
111 Require (not this->empty ());
112 return _SafeReadWriteRepAccessor<_IRep>{this}._GetWriteableRep ().RemoveHead ();
113 }
114 template <typename T>
115 template <IIterableOfTo<T> ITERABLE_OF_ADDABLE>
116 inline void Queue<T>::AddAllToTail (ITERABLE_OF_ADDABLE&& s)
117 {
118 _SafeReadWriteRepAccessor<_IRep> tmp{this};
119 for (const auto& i : s) {
120 tmp._GetWriteableRep ().AddTail (i);
121 }
123 template <typename T>
124 template <IInputIterator<T> ITERATOR_OF_ADDABLE>
125 inline void Queue<T>::AddAllToTail (ITERATOR_OF_ADDABLE&& start, ITERATOR_OF_ADDABLE&& end)
126 {
127 _SafeReadWriteRepAccessor<_IRep> tmp{this};
128 for (auto i = forward<ITERATOR_OF_ADDABLE> (start); i != forward<ITERATOR_OF_ADDABLE> (end); ++i) {
129 tmp._GetWriteableRep ().AddTail (*i);
130 }
131 }
132 template <typename T>
133 inline void Queue<T>::RemoveAll ()
134 {
135 _SafeReadRepAccessor<_IRep> tmp{this}; // important to use READ not WRITE accessor, because write accessor would have already cloned the data
136 if (not tmp._ConstGetRep ().empty ()) {
137 this->_fRep = tmp._ConstGetRep ().CloneEmpty ();
138 }
139 }
140 template <typename T>
141 inline void Queue<T>::clear ()
142 {
143 RemoveAll ();
144 }
145 template <typename T>
146 inline void Queue<T>::_AssertRepValidType () const noexcept
147 {
149 _SafeReadRepAccessor<_IRep>{this};
150 }
151 }
152 template <typename T>
153 inline bool Queue<T>::operator== (const Queue& rhs) const
154 requires (equality_comparable<T>)
155 {
156 return EqualsComparer<>{}(*this, rhs);
157 }
158 template <typename T>
159 inline auto Queue<T>::operator<=> (const Queue& rhs) const
160 requires (three_way_comparable<T>)
161 {
162 return ThreeWayComparer<>{}(*this, rhs);
163 }
165}
#define qStroika_Foundation_Debug_AssertionsChecked
The qStroika_Foundation_Debug_AssertionsChecked flag determines if assertions are checked and validat...
Definition Assertions.h:48
#define RequireNotNull(p)
Definition Assertions.h:347
#define RequireExpression(c)
Definition Assertions.h:267
A Queue is a first-in-first-out (FIFO) data structure, where elements are arranged in well-ordered fa...
Definition Queue.h:95
nonvirtual value_type Head() const
Definition Queue.inl:73
nonvirtual bool operator==(const Queue &rhs) const
Definition Queue.inl:153
nonvirtual void AddAllToTail(ITERABLE_OF_ADDABLE &&s)
nonvirtual void push_back(ArgByValueType< value_type > item)
Add the given item to the end of the Q, so it will be removed last of all the items currently in the ...
Definition Queue.inl:68
nonvirtual value_type Dequeue()
Alias for RemoveHead () - remove item from the head of the Q (line).
Definition Queue.inl:109
nonvirtual optional< value_type > RemoveHeadIf()
Definition Queue.inl:99
nonvirtual auto operator<=>(const Queue &rhs) const
Definition Queue.inl:159
typename inherited::value_type value_type
Definition Queue.h:109
typename Iterable< T >::template SequentialEqualsComparer< T_EQUALS_COMPARER > EqualsComparer
Simply indirect to @Iterable<T>::SequentialEqualsComparer.
Definition Queue.h:261
nonvirtual value_type RemoveHead()
Definition Queue.inl:88
nonvirtual void Enqueue(ArgByValueType< value_type > item)
add item to the end of the Q (line).
Definition Queue.inl:104
nonvirtual void AddTail(ArgByValueType< value_type > item)
Definition Queue.inl:63
nonvirtual void clear()
STL-ish alias for RemoveAll ().
Definition Queue.inl:141
nonvirtual value_type pop_back()
Definition Queue.inl:94
Iterable<T> is a base class for containers which easily produce an Iterator<T> to traverse them.
Definition Iterable.h:237
static constexpr default_sentinel_t end() noexcept
Support for ranged for, and STL syntax in general.