DLListBase.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::DLListBase
29
30Description
31 Base for doubly-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 DLListBaseI.H
42 DLListBase.C
43
44\*---------------------------------------------------------------------------*/
45
46#ifndef Foam_DLListBase_H
47#define Foam_DLListBase_H
48
49#include "label.H"
50#include "uLabel.H"
51#include "stdFoam.H"
52
53// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
54
55namespace Foam
56{
57
58/*---------------------------------------------------------------------------*\
59 Class DLListBase Declaration
60\*---------------------------------------------------------------------------*/
62class DLListBase
63{
64public:
65
66 //- The structure for a doubly-linked storage node
67 struct link
68 {
69 //- Pointer to prev entry in list
70 link* prev_ = nullptr;
71
72 //- Pointer to next entry in list
73 link* next_ = nullptr;
74
75 //- Default construct
76 link() = default;
77
78 //- Check if the node is registered with the list
79 inline bool registered() const noexcept;
80
81 //- Deregister the node after removal
82 inline void deregister() noexcept;
83 };
84
85
86private:
87
88 // Private Data
89
90 //- Pointer to first element
91 link* first_ = nullptr;
92
93 //- Pointer to last element
94 link* last_ = nullptr;
95
96 //- Number of elements in the list
97 label size_ = 0;
98
99
100protected:
101
102 // Protected Member Functions
103
104 //- Factory method to return an iterator end
105 // Simply reinterprets a NullObject as a DLListBase iterator.
106 template<class IteratorType>
107 inline static const IteratorType& iterator_end();
108
109 //- Factory method to return an iterator reverse end
110 // Simply reinterprets a NullObject as a DLListBase iterator.
111 template<class IteratorType>
112 inline static const IteratorType& iterator_rend();
113
114 //- Return iterator to first item or end-iterator if list is empty
115 // Removes constness which the caller promises to manage.
116 template<class IteratorType>
117 inline IteratorType iterator_first() const;
118
119 //- Return iterator to last item or end-iterator if list is empty
120 // Removes constness which the caller promises to manage.
121 template<class IteratorType>
122 inline IteratorType iterator_last() const;
123
124
125public:
126
127 // Forward declaration of iterators
128
129 class iterator;
130 friend class iterator;
131
133 friend class const_iterator;
134
135
136 // Generated Methods
137
138 //- Default construct
139 DLListBase() = default;
140
141 //- No copy construct
142 DLListBase(const DLListBase&) = delete;
143
144 //- No copy assignment
145 void operator=(const DLListBase&) = delete;
146
147 //- Destructor
148 ~DLListBase() = default;
149
150
151 // Member Functions
152
153 //- The number of elements in list
154 inline label size() const noexcept;
155
156 //- True if the list is empty
157 inline bool empty() const noexcept;
158
159 //- Return first entry
160 inline link* first();
161
162 //- Return const access to first entry
163 inline const link* first() const;
164
165 //- Return last entry
166 inline link* last();
167
168 //- Return const access to last entry
169 inline const link* last() const;
170
171
172 //- Add at front of list
173 void prepend(link* item);
174
175 //- Add at back of list
176 void append(link* item);
177
178 //- Swap this element with the one above unless it is at the top
179 bool swapUp(link* item);
180
181 //- Swap this element with the one below unless it is at the bottom
182 bool swapDown(link* item);
183
184 //- Remove and return first entry
185 link* removeHead();
186
187 //- Remove and return element
188 link* remove(link* item);
189
190 //- Remove and return element specified by iterator
191 inline link* remove(iterator& iter);
192
193 //- Replace oldLink with newLink and return element
194 link* replace(link* oldLink, link* newLink);
195
196 //- Replace oldIter with newItem and return element
197 inline link* replace(iterator& oldIter, link* newitem);
198
199 //- Clear the list
200 inline void clear();
201
202 //- Swap the contents of the list
203 inline void swap(DLListBase& lst);
204
205 //- Transfer the contents of the argument into this list
206 //- and annul the argument list.
207 inline void transfer(DLListBase& lst);
208
209
210 // iterator
211
212 //- A primitive non-const node iterator.
213 // Needs to be extended by inheriting classes.
214 class iterator
216 friend class DLListBase;
217 friend class const_iterator;
218
219 //- The selected node.
220 // MUST be the first member for easy comparison between iterators
221 // and for reinterpret_cast from nullObject
222 link* node_;
223
224 //- The list being iterated on
225 DLListBase* list_;
226
227 //- Copy of the node prev/next pointers (to use after removal)
228 link copy_;
229
230 public:
231
232 //- Copy construct
233 iterator(const iterator&) = default;
234
235 //- Construct for a node on a list
236 inline iterator(DLListBase* list, link* item);
237
238 //- The storage node
239 inline link* get_node() const noexcept;
240
241 //- Pointing at a valid storage node
242 inline bool good() const noexcept;
243
244 //- Move backward through list
245 inline void prev();
246
247 //- Move forward through list
248 inline void next();
249
250 inline void operator=(const iterator& iter);
251
252 inline bool operator==(const iterator&) const;
253 inline bool operator!=(const iterator&) const;
254 };
255
256
257 // const_iterator
258
259 //- A primitive const node iterator (bidirectional).
260 // Must normally be extended by inheriting classes.
261 // Since this iterator works bidirectionally, it can be used as the
262 // basis for a derived const_reverse_iterator
263 class const_iterator
264 {
265 //- The selected node.
266 // MUST be the first member for easy comparison between iterators
267 // and for reinterpret_cast from nullObject
268 const link* node_;
269
270 //- The list being iterated on (as pointer for bitwise copy)
271 const DLListBase* list_;
272
273 public:
274
275 //- Copy construct
276 const_iterator(const const_iterator&) = default;
277
278 //- Construct for a node on a list
279 inline const_iterator(const DLListBase* list, const link* item);
280
281 //- Copy construct from a non-const iterator
282 inline const_iterator(const DLListBase::iterator& iter);
283
284 //- The storage node
285 inline const link* get_node() const noexcept;
286
287 //- Pointing at a valid storage node
288 inline bool good() const noexcept;
289
290 //- Move backward through list
291 inline void prev();
292
293 //- Move forward through list
294 inline void next();
295
296 //- Copy assignment
297 const_iterator& operator=(const const_iterator&) = default;
298
299 inline bool operator==(const const_iterator&) const;
300 inline bool operator!=(const const_iterator&) const;
301 };
302
303
304 //- Iterator to first item in list with non-const access
305 inline iterator begin();
306
307 //- Iterator to first item in list with const access
308 inline const_iterator cbegin() const;
309
310 //- Iterator to last item in list with const access
311 // Note that this is not a const_reverse_iterator, this is the
312 // responsibility of any derived classes.
313 inline const_iterator crbegin() const;
314
315 //- End of list for iterators
316 inline const iterator& end();
317
318 //- End of list for iterators
319 inline const const_iterator& cend() const;
320
321 //- End of list for reverse iterators
322 inline const const_iterator& crend() const;
323};
324
325
326// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
327
328} // End namespace Foam
329
330// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
331
332#include "DLListBaseI.H"
333
334// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
335
336#endif
337
338// ************************************************************************* //
A primitive const node iterator (bidirectional).
Definition: DLListBase.H:263
const_iterator(const const_iterator &)=default
Copy construct.
A primitive non-const node iterator.
Definition: DLListBase.H:214
iterator(const iterator &)=default
Copy construct.
Base for doubly-linked lists.
Definition: DLListBase.H:62
const const_iterator & cend() const
End of list for iterators.
Definition: DLListBaseI.H:87
link * first()
Return first entry.
Definition: DLListBaseI.H:127
const const_iterator & crend() const
End of list for reverse iterators.
Definition: DLListBaseI.H:94
void transfer(DLListBase &lst)
Definition: DLListBaseI.H:199
bool empty() const noexcept
True if the list is empty.
Definition: DLListBaseI.H:120
link * last()
Return last entry.
Definition: DLListBaseI.H:153
link * replace(link *oldLink, link *newLink)
Replace oldLink with newLink and return element.
Definition: DLListBase.C:219
IteratorType iterator_first() const
Return iterator to first item or end-iterator if list is empty.
Definition: DLListBaseI.H:49
void swap(DLListBase &lst)
Swap the contents of the list.
Definition: DLListBaseI.H:186
void prepend(link *item)
Add at front of list.
Definition: DLListBase.C:34
const iterator & end()
End of list for iterators.
Definition: DLListBaseI.H:80
static const IteratorType & iterator_end()
Factory method to return an iterator end.
Definition: DLListBaseI.H:35
bool swapDown(link *item)
Swap this element with the one below unless it is at the bottom.
Definition: DLListBase.C:123
label size() const noexcept
The number of elements in list.
Definition: DLListBaseI.H:114
iterator begin()
Iterator to first item in list with non-const access.
Definition: DLListBaseI.H:326
IteratorType iterator_last() const
Return iterator to last item or end-iterator if list is empty.
Definition: DLListBaseI.H:64
static const IteratorType & iterator_rend()
Factory method to return an iterator reverse end.
Definition: DLListBaseI.H:42
link * removeHead()
Remove and return first entry.
Definition: DLListBase.C:162
const_iterator cbegin() const
Iterator to first item in list with const access.
Definition: DLListBaseI.H:424
void append(link *item)
Add at back of list.
Definition: DLListBase.C:59
void clear()
Clear the list.
Definition: DLListBaseI.H:178
link * remove(link *item)
Remove and return element.
Definition: DLListBase.C:186
const_iterator crbegin() const
Iterator to last item in list with const access.
Definition: DLListBaseI.H:436
bool swapUp(link *item)
Swap this element with the one above unless it is at the top.
Definition: DLListBase.C:84
Namespace for OpenFOAM.
const direction noexcept
Definition: Scalar.H:223
Includes some standard C++ headers, defines global macros and templates used in multiple places by Op...