Stroika Library 3.0d16
 
Loading...
Searching...
No Matches
Deque_DoublyLinkedList.inl
1/*
2 * Copyright(c) Sophist Solutions, Inc. 1990-2025. All rights reserved
3 */
4#include "Stroika/Foundation/Containers/DataStructures/DoublyLinkedList.h"
5#include "Stroika/Foundation/Containers/Private/IteratorImplHelper.h"
9
11
12 /*
13 ********************************************************************************
14 *********************** Deque_DoublyLinkedList<T>::Rep_ ************************
15 ********************************************************************************
16 */
17 template <typename T>
18 class Deque_DoublyLinkedList<T>::Rep_ : public Deque<T>::_IRep, public Memory::UseBlockAllocationIfAppropriate<Rep_> {
19 private:
20 using inherited = typename Deque<T>::_IRep;
21
22 public:
23 Rep_ () = default;
24 Rep_ (const Rep_& from) = default;
25
26 public:
27 nonvirtual Rep_& operator= (const Rep_&) = delete;
28
29 // Iterable<T>::_IRep overrides
30 public:
31 virtual shared_ptr<typename Iterable<T>::_IRep> Clone () const override
32 {
34 return Memory::MakeSharedPtr<Rep_> (*this);
35 }
36 virtual Iterator<value_type> MakeIterator () const override
37 {
39 return Iterator<value_type>{make_unique<IteratorRep_> (&fData_, &fChangeCounts_)};
40 }
41 virtual size_t size () const override
42 {
44 return fData_.size ();
45 }
46 virtual bool empty () const override
47 {
49 return fData_.empty ();
50 }
51 virtual void Apply (const function<void (ArgByValueType<value_type> item)>& doToElement, [[maybe_unused]] Execution::SequencePolicy seq) const override
52 {
54 fData_.Apply (doToElement);
55 }
56 virtual Iterator<value_type> Find (const function<bool (ArgByValueType<value_type> item)>& that,
57 [[maybe_unused]] Execution::SequencePolicy seq) const override
58 {
60 if (auto iLink = fData_.Find (that)) {
61 return Iterator<value_type>{make_unique<IteratorRep_> (&fData_, &fChangeCounts_, iLink)};
62 }
63 return nullptr;
64 }
65
66 // Queue<T>::_IRep overrides
67 public:
68 virtual shared_ptr<typename Queue<T>::_IRep> CloneEmpty () const override
69 {
70 return Memory::MakeSharedPtr<Rep_> ();
71 }
72 virtual void AddTail (ArgByValueType<value_type> item) override
73 {
75 fData_.push_back (item);
76 fChangeCounts_.PerformedChange ();
77 }
78 virtual value_type RemoveHead () override
79 {
81 T item = Memory::ValueOf (fData_.GetFirst ());
82 fData_.RemoveFirst ();
83 fChangeCounts_.PerformedChange ();
84 return item;
85 }
86 virtual optional<value_type> RemoveHeadIf () override
87 {
89 if (fData_.empty ()) {
90 return optional<T>{};
91 }
92 T item = Memory::ValueOf (fData_.GetFirst ());
93 fData_.RemoveFirst ();
94 fChangeCounts_.PerformedChange ();
95 return item;
96 }
97 virtual value_type Head () const override
98 {
100 return Memory::ValueOf (fData_.GetFirst ());
101 }
102 virtual optional<value_type> HeadIf () const override
103 {
105 return fData_.GetFirst ();
106 }
107
108 // Deque<T>::_IRep overrides
109 public:
110 virtual void AddHead (ArgByValueType<value_type> item) override
111 {
113 fData_.push_back (item);
114 fChangeCounts_.PerformedChange ();
115 }
116 virtual value_type RemoveTail () override
117 {
119 value_type item = Memory::ValueOf (fData_.GetFirst ());
120 fData_.RemoveLast ();
121 fChangeCounts_.PerformedChange ();
122 return item;
123 }
124 virtual value_type Tail () const override
125 {
127 return Memory::ValueOf (fData_.GetLast ());
128 }
129
130 private:
131 using DataStructureImplType_ = DataStructures::DoublyLinkedList<value_type>;
132 using IteratorRep_ = Private::IteratorImplHelper_<value_type, DataStructureImplType_>;
133
134 private:
135 DataStructureImplType_ fData_;
136 [[no_unique_address]] Private::ContainerDebugChangeCounts_ fChangeCounts_;
137 };
138
139 /*
140 ********************************************************************************
141 *************************** Deque_DoublyLinkedList<T> **************************
142 ********************************************************************************
143 */
144 template <typename T>
146 : inherited{Memory::MakeSharedPtr<Rep_> ()}
147 {
148 AssertRepValidType_ ();
149 }
150 template <typename T>
151 inline Deque_DoublyLinkedList<T>::Deque_DoublyLinkedList (const initializer_list<value_type>& src)
153 {
154 this->AddAllToTail (src);
155 AssertRepValidType_ ();
156 }
157#if !qCompilerAndStdLib_RequiresNotMatchInlineOutOfLineForTemplateClassBeingDefined_Buggy
158 template <typename T>
159 template <IIterableOfTo<T> ITERABLE_OF_ADDABLE>
160 requires (not derived_from<remove_cvref_t<ITERABLE_OF_ADDABLE>, Deque_DoublyLinkedList<T>>)
161 inline Deque_DoublyLinkedList<T>::Deque_DoublyLinkedList (ITERABLE_OF_ADDABLE&& src)
162 : Deque_DoublyLinkedList{}
163 {
164 this->AddAllToTail (forward<ITERABLE_OF_ADDABLE> (src));
165 AssertRepValidType_ ();
166 }
167#endif
168 template <typename T>
169 template <IInputIterator<T> ITERATOR_OF_ADDABLE>
170 inline Deque_DoublyLinkedList<T>::Deque_DoublyLinkedList (ITERATOR_OF_ADDABLE&& start, ITERATOR_OF_ADDABLE&& end)
171 : Deque_DoublyLinkedList{}
172 {
173 this->AddAllToTail (forward<ITERATOR_OF_ADDABLE> (start), forward<ITERATOR_OF_ADDABLE> (end));
174 AssertRepValidType_ ();
175 }
176 template <typename T>
177 inline void Deque_DoublyLinkedList<T>::AssertRepValidType_ () const
178 {
180 typename inherited::template _SafeReadRepAccessor<Rep_> tmp{this}; // for side-effect of AssertMemeber
181 }
182 }
183
184}
#define qStroika_Foundation_Debug_AssertionsChecked
The qStroika_Foundation_Debug_AssertionsChecked flag determines if assertions are checked and validat...
Definition Assertions.h:48
Deque_DoublyLinkedList<T> is an Array-based concrete implementation of the Deque<T> container pattern...
nonvirtual value_type RemoveTail()
Definition Deque.inl:71
nonvirtual void AddHead(ArgByValueType< value_type > item)
Definition Deque.inl:61
nonvirtual value_type Head() const
Definition Queue.inl:73
nonvirtual void AddAllToTail(ITERABLE_OF_ADDABLE &&s)
nonvirtual optional< value_type > RemoveHeadIf()
Definition Queue.inl:99
nonvirtual value_type RemoveHead()
Definition Queue.inl:88
nonvirtual void AddTail(ArgByValueType< value_type > item)
Definition Queue.inl:63
shared_lock< const AssertExternallySynchronizedMutex > ReadContext
Instantiate AssertExternallySynchronizedMutex::ReadContext to designate an area of code where protect...
unique_lock< AssertExternallySynchronizedMutex > WriteContext
Instantiate AssertExternallySynchronizedMutex::WriteContext to designate an area of code where protec...
nonvirtual void Apply(const function< void(ArgByValueType< T > item)> &doToElement, Execution::SequencePolicy seq=Execution::SequencePolicy::eDEFAULT) const
Run the argument function (or lambda) on each element of the container.
nonvirtual Iterator< T > Find(THAT_FUNCTION &&that, Execution::SequencePolicy seq=Execution::SequencePolicy::eDEFAULT) const
Run the argument bool-returning function (or lambda) on each element of the container,...
nonvirtual size_t size() const
Returns the number of items contained.
Definition Iterable.inl:300
nonvirtual bool empty() const
Returns true iff size() == 0.
Definition Iterable.inl:306
static constexpr default_sentinel_t end() noexcept
Support for ranged for, and STL syntax in general.
nonvirtual Iterator< T > MakeIterator() const
Create an iterator object which can be used to traverse the 'Iterable'.
Definition Iterable.inl:294
SequencePolicy
equivalent which of 4 types being used std::execution::sequenced_policy, parallel_policy,...