Loading...
Searching...
No Matches
SLListBase.H
Go to the documentation of this file.
1/*---------------------------------------------------------------------------*\
2 ========= |
3 \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4 \\ / O peration |
5 \\ / A nd | www.openfoam.com
6 \\/ M anipulation |
7-------------------------------------------------------------------------------
8 Copyright (C) 2011-2016 OpenFOAM Foundation
9 Copyright (C) 2017-2022 OpenCFD Ltd.
10-------------------------------------------------------------------------------
11License
12 This file is part of OpenFOAM.
13
14 OpenFOAM is free software: you can redistribute it and/or modify it
15 under the terms of the GNU General Public License as published by
16 the Free Software Foundation, either version 3 of the License, or
17 (at your option) any later version.
18
19 OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
20 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
21 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22 for more details.
23
24 You should have received a copy of the GNU General Public License
25 along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
26
27Class
28 Foam::SLListBase
29
30Description
31 Base for singly-linked lists.
32
33 The iterators associated with the list only have a core functionality
34 for navigation, with additional functionality to be added by inheriting
35 classes. The node iterators always have a node-pointer as the
36 first member data, which allows reinterpret_cast from anything else with
37 a nullptr as its first data member.
38 The nullObject is such an item (with a nullptr data member).
39
40SourceFiles
41 SLListBaseI.H
42 SLListBase.C
43
44\*---------------------------------------------------------------------------*/
45
46#ifndef Foam_SLListBase_H
47#define Foam_SLListBase_H
48
49#include "label.H"
50#include "uLabel.H"
51#include "stdFoam.H"
52
53// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
54
55namespace Foam
56{
58/*---------------------------------------------------------------------------*\
59 Class SLListBase Declaration
60\*---------------------------------------------------------------------------*/
61
62class SLListBase
63{
64public:
65
66 //- The structure for a singly-linked storage node
67 struct link
68 {
69 //- Pointer to next entry in list
70 link* next_ = nullptr;
71
72 //- Default construct
73 link() noexcept = default;
75 //- Deregister the node (after removal)
76 void deregister() noexcept { next_ = nullptr; }
77 };
78
80private:
81
82 // Private Data
83
84 //- A pointer to the last element.
85 // last_->next_ points to first element, i.e. circular storage
86 link* last_ = nullptr;
87
88 //- Number of elements in the list
89 label size_ = 0;
90
91
92protected:
93
94 // Protected Member Functions
95
96 //- Factory method to return an iterator end
97 // Simply reinterprets a NullObject as a SLListBase iterator.
98 template<class IteratorType>
99 inline static const IteratorType& iterator_end();
100
101 //- Factory method to return an iterator rend
102 // Deleted for SLListBase
103 template<class IteratorType>
104 static const IteratorType& iterator_rend() = delete;
105
106 //- Return iterator to first item or end-iterator if list is empty
107 // Removes constness which the caller promises to manage.
108 template<class IteratorType>
109 inline IteratorType iterator_first() const;
110
111 //- Return iterator to last item or end-iterator if list is empty
112 // Removes constness which the caller promises to manage.
113 template<class IteratorType>
114 inline IteratorType iterator_last() const;
115
116public:
117
118 // Forward Declarations (iterators)
119
120 class iterator;
121 friend class iterator;
122
123 class const_iterator;
124 friend class const_iterator;
125
126
127 // Generated Methods
128
129 //- Default construct
130 SLListBase() = default;
131
132 //- No copy construct
133 SLListBase(const SLListBase&) = delete;
134
135 //- No copy assignment
136 void operator=(const SLListBase&) = delete;
137
138 //- Destructor
139 ~SLListBase() = default;
140
142 // Member Functions
143
144 //- True if the list is empty
145 bool empty() const noexcept { return !size_; }
146
147 //- The number of elements in list
148 label size() const noexcept { return size_; }
149
150 //- Return first entry
151 inline link* front();
153 //- Return const access to first entry
154 inline const link* front() const;
155
156 //- Return last entry
157 inline link* back();
158
159 //- Return const access to last entry
160 inline const link* back() const;
161
163 //- Add at front of list
164 void push_front(link* item);
165
166 //- Add at back of list
167 void push_back(link* item);
168
169 //- Remove and return first entry
170 link* removeHead();
171
172 // Remove and return element
173 link* remove(link* item);
174
175 // Remove and return element specified by iterator
176 inline link* remove(iterator& iter);
177
178 //- Clear the list
179 inline void clear();
181 //- Swap the contents of list
182 inline void swap(SLListBase& lst);
183
184 //- Transfer the contents of the argument into this list
185 //- and annul the argument list.
186 inline void transfer(SLListBase& lst);
187
188
189 // iterator
190
191 //- A primitive non-const node iterator.
192 // Must normally be extended by inheriting classes.
193 class iterator
194 {
195 friend class SLListBase;
196 friend class const_iterator;
197
198 //- The selected node.
199 // MUST be the first member for easy comparison between iterators
200 // and for reinterpret_cast from nullObject
201 link* node_;
202
203 //- The list being iterated on (as pointer for bitwise copy)
204 SLListBase* list_;
205
206 //- Copy of the node next pointer (to use after removal)
207 link copy_;
208
209 public:
210
211 //- Copy construct
212 iterator(const iterator&) = default;
213
214 //- Construct for a node on the list
215 inline iterator(SLListBase* list, link* item);
216
217 //- The storage node
218 inline link* get_node() const noexcept;
219
220 //- Pointing at a valid storage node
221 inline bool good() const noexcept;
222
223 //- Cannot move backward through list
224 inline void prev() = delete;
225
226 //- Move forward through list
227 inline void next();
228
229 //- Copy assignment
230 inline void operator=(const iterator& iter);
231
232 inline bool operator==(const iterator& iter) const;
233 inline bool operator!=(const iterator& iter) const;
234 };
235
236
237 // STL const_iterator
238
239 //- A primitive const node iterator.
240 // Must normally be extended by inheriting classes.
241 class const_iterator
242 {
243 //- The selected node.
244 // MUST be the first member for easy comparison between iterators
245 // and for reinterpret_cast from nullObject
246 const link* node_;
247
248 //- The list being iterated on (as pointer for bitwise copy)
249 const SLListBase* list_;
251 public:
252
253 //- Copy construct
254 const_iterator(const const_iterator&) = default;
255
256 //- Construct for a node on the list
257 inline const_iterator(const SLListBase* list, const link* item);
258
259 //- Construct from a non-const iterator
260 inline const_iterator(const SLListBase::iterator& iter);
261
262 //- The storage node
263 inline const link* get_node() const noexcept;
264
265 //- Pointing at a valid storage node
266 inline bool good() const noexcept;
267
268 //- Cannot move backward through list
269 inline void prev() = delete;
270
271 //- Move forward through list
272 inline void next();
273
274 //- Copy assignment
275 const_iterator& operator=(const const_iterator&) = default;
277 inline bool operator==(const const_iterator& iter) const;
278 inline bool operator!=(const const_iterator& iter) const;
279 };
280
281
282 //- Iterator to first item in list with non-const access
283 inline iterator begin();
284
285 //- Iterator to first item in list with const access
286 inline const_iterator cbegin() const;
287
288 //- No reverse iteration
289 const_iterator crbegin() const = delete;
290
291 //- End of list for iterators
292 inline const iterator& end();
293
294 //- End of list for iterators
295 inline const const_iterator& cend() const;
297 //- No reverse iteration
298 const const_iterator& crend() const = delete;
299
300
301 // Housekeeping
302
303 //- Return first entry
304 //FOAM_DEPRECATED_FOR(2022-10, "front()")
305 link* first() { return front(); }
306
307 //- Return const access to first entry
308 //FOAM_DEPRECATED_FOR(2022-10, "front()")
309 const link* first() const { return front(); }
310
311 //- Return last entry
312 //FOAM_DEPRECATED_FOR(2022-10, "back()")
313 link* last() { return back(); }
314
315 //- Return const access to last entry
316 //FOAM_DEPRECATED_FOR(2022-10, "back()")
317 const link* last() const { return back(); }
318
319 //- Add at front of list
320 //FOAM_DEPRECATED_FOR(2022-10, "push_front()")
321 void prepend(link* item) { push_front(item); }
322
323 //- Add at back of list
324 //FOAM_DEPRECATED_FOR(2022-10, "push_back()")
325 void append(link* item) { push_back(item); }
326};
327
328
329// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
330
331} // End namespace Foam
332
333// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
334
335#include "SLListBaseI.H"
336
337// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
338
339#endif
341// ************************************************************************* //
A primitive const node iterator.
Definition SLListBase.H:321
void prev()=delete
Cannot move backward through list.
void next()
Move forward through list.
bool good() const noexcept
Pointing at a valid storage node.
const_iterator(const const_iterator &)=default
Copy construct.
A primitive non-const node iterator.
Definition SLListBase.H:249
void prev()=delete
Cannot move backward through list.
void next()
Move forward through list.
bool good() const noexcept
Pointing at a valid storage node.
iterator(const iterator &)=default
Copy construct.
link * get_node() const noexcept
The storage node.
Base for singly-linked lists.
Definition SLListBase.H:58
link * remove(link *item)
Definition SLListBase.C:99
const_iterator cbegin() const
Iterator to first item in list with const access.
link * last()
Return last entry.
Definition SLListBase.H:434
const const_iterator & cend() const
End of list for iterators.
void transfer(SLListBase &lst)
Transfer the contents of the argument into this list and annul the argument list.
link * first()
Return first entry.
Definition SLListBase.H:420
link * front()
Return first entry.
Definition SLListBaseI.H:67
const iterator & end()
End of list for iterators.
bool empty() const noexcept
True if the list is empty.
Definition SLListBase.H:175
void push_back(link *item)
Add at back of list.
Definition SLListBase.C:49
const_iterator crbegin() const =delete
No reverse iteration.
const link * last() const
Return const access to last entry.
Definition SLListBase.H:441
link * removeHead()
Remove and return first entry.
Definition SLListBase.C:70
const link * first() const
Return const access to first entry.
Definition SLListBase.H:427
IteratorType iterator_first() const
Return iterator to first item or end-iterator if list is empty.
Definition SLListBaseI.H:35
void prepend(link *item)
Add at front of list.
Definition SLListBase.H:448
friend class iterator
Definition SLListBase.H:141
static const IteratorType & iterator_end()
Factory method to return an iterator end.
Definition SLListBaseI.H:28
IteratorType iterator_last() const
Return iterator to last item or end-iterator if list is empty.
Definition SLListBaseI.H:50
void swap(SLListBase &lst)
Swap the contents of list.
void append(link *item)
Add at back of list.
Definition SLListBase.H:455
~SLListBase()=default
Destructor.
const const_iterator & crend() const =delete
No reverse iteration.
iterator begin()
Iterator to first item in list with non-const access.
label size() const noexcept
The number of elements in list.
Definition SLListBase.H:180
void clear()
Clear the list.
SLListBase(const SLListBase &)=delete
No copy construct.
void push_front(link *item)
Add at front of list.
Definition SLListBase.C:27
static const IteratorType & iterator_rend()=delete
Factory method to return an iterator rend.
link * back()
Return last entry.
Definition SLListBaseI.H:93
SLListBase()=default
Default construct.
void operator=(const SLListBase &)=delete
No copy assignment.
Namespace for OpenFOAM.
const direction noexcept
Definition scalarImpl.H:265
Includes some common C++ headers, defines global macros and templates used in multiple places by Open...