Stroika Library 3.0d16
 
Loading...
Searching...
No Matches
Stack_LinkedList.inl
1/*
2 * Copyright(c) Sophist Solutions, Inc. 1990-2025. All rights reserved
3 */
4#include "Stroika/Foundation/Containers/DataStructures/LinkedList.h"
5#include "Stroika/Foundation/Containers/Private/IteratorImplHelper.h"
9
11
12 /*
13 ********************************************************************************
14 ************************* Stack_LinkedList<T>::Rep_ ****************************
15 ********************************************************************************
16 */
17 template <typename T>
18 class Stack_LinkedList<T>::Rep_ : public Stack<T>::_IRep, public Memory::UseBlockAllocationIfAppropriate<Rep_> {
19 private:
20 using inherited = typename Stack<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 // Stack<T>::_IRep overrides
67 public:
68 virtual shared_ptr<typename Stack<T>::_IRep> CloneEmpty () const override
69 {
70 return Memory::MakeSharedPtr<Rep_> ();
71 }
72 virtual void Push (ArgByValueType<value_type> item) override
73 {
75 fData_.push_front (item); // top of stack is first elt of linked list
76 fChangeCounts_.PerformedChange ();
77 }
78 virtual value_type Pop () override
79 {
81 value_type result = Memory::ValueOf (fData_.GetFirst ());
82 fData_.RemoveFirst ();
83 fChangeCounts_.PerformedChange ();
84 return result;
85 }
86 virtual value_type Top () const override
87 {
89 return Memory::ValueOf (fData_.GetFirst ());
90 }
91
92 private:
93 using DataStructureImplType_ = DataStructures::LinkedList<value_type>;
94 using IteratorRep_ = Private::IteratorImplHelper_<value_type, DataStructureImplType_>;
95
96 private:
97 DataStructureImplType_ fData_;
98 [[no_unique_address]] Private::ContainerDebugChangeCounts_ fChangeCounts_;
99 };
100
101 /*
102 ********************************************************************************
103 ******************************** Stack_LinkedList<T> ***************************
104 ********************************************************************************
105 */
106 template <typename T>
107 inline Stack_LinkedList<T>::Stack_LinkedList ()
108 : inherited{Memory::MakeSharedPtr<Rep_> ()}
109 {
110 AssertRepValidType_ ();
111 }
112#if !qCompilerAndStdLib_RequiresNotMatchInlineOutOfLineForTemplateClassBeingDefined_Buggy
113 template <typename T>
114 template <IIterableOfTo<T> ITERABLE_OF_ADDABLE>
115 requires (not derived_from<remove_cvref_t<ITERABLE_OF_ADDABLE>, Stack_LinkedList<T>>)
116 inline Stack_LinkedList<T>::Stack_LinkedList (ITERABLE_OF_ADDABLE&& src)
117 : Stack_LinkedList{src.begin (), src.end ()}
118 {
119 }
120#endif
121 template <typename T>
122 template <IInputIterator<T> ITERATOR_OF_ADDABLE>
123 Stack_LinkedList<T>::Stack_LinkedList (ITERATOR_OF_ADDABLE&& start, ITERATOR_OF_ADDABLE&& end)
124 : Stack_LinkedList{}
125 {
126 // sadly intrinsically expensive to copy an Iterable using the stack API
127 // @todo find a more efficient way - for example - if there is a way to get a reverse-iterator from 'src' this can be much cheaper! - or at least copy PTRS
128 // @todo use if constexpr here on types provided!!!
129 vector<T> tmp;
130 copy (forward<ITERATOR_OF_ADDABLE> (start), forward<ITERATOR_OF_ADDABLE> (end), back_inserter (tmp));
131 for (auto ri = tmp.rbegin (); ri != tmp.rend (); ++ri) {
132 this->Push (*ri);
133 }
134 }
135 template <typename T>
136 inline void Stack_LinkedList<T>::AssertRepValidType_ () const
137 {
139 typename inherited::template _SafeReadRepAccessor<Rep_> tmp{this}; // for side-effect of AssertMember
140 }
141 }
142
143}
#define qStroika_Foundation_Debug_AssertionsChecked
The qStroika_Foundation_Debug_AssertionsChecked flag determines if assertions are checked and validat...
Definition Assertions.h:48
auto MakeSharedPtr(ARGS_TYPE &&... args) -> shared_ptr< T >
same as make_shared, but if type T has block allocation, then use block allocation for the 'shared pa...
nonvirtual value_type Pop()
Definition Stack.inl:64
nonvirtual void Push(ArgByValueType< value_type > item)
Definition Stack.inl:59
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
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,...