Loading...
Searching...
No Matches
DLListBaseI.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
27\*---------------------------------------------------------------------------*/
29#include "error.H"
30#include "nullObject.H"
31
32// * * * * * * * * * * * * Protected Member Functions * * * * * * * * * * * //
33
34template<class IteratorType>
35inline const IteratorType& Foam::DLListBase::iterator_end()
36{
37 return *reinterpret_cast<const IteratorType*>(nullObjectPtr);
38}
39
40
41template<class IteratorType>
42inline const IteratorType& Foam::DLListBase::iterator_rend()
43{
44 return *reinterpret_cast<const IteratorType*>(nullObjectPtr);
45}
46
47
48template<class IteratorType>
49inline IteratorType Foam::DLListBase::iterator_first() const
50{
51 DLListBase* list = const_cast<DLListBase*>(this);
52
53 if (size())
54 {
55 return IteratorType(list, const_cast<DLListBase::link*>(first_));
56 }
58 // Return an end iterator
59 return IteratorType(list, nullptr);
60}
61
62
63template<class IteratorType>
64inline IteratorType Foam::DLListBase::iterator_last() const
65{
66 DLListBase* list = const_cast<DLListBase*>(this);
67
68 if (size())
69 {
70 return IteratorType(list, const_cast<DLListBase::link*>(last_));
71 }
72
73 // Return an end iterator
74 return IteratorType(list, nullptr);
75}
76
77
78// * * * * * * * * * * * * * * * Iterator ends * * * * * * * * * * * * * * * //
79
84
85
91
92
95{
97}
98
99
100// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
101
104{
105 if (!size_)
106 {
108 << "list is empty"
110 }
111 return first_;
112}
113
114
115inline const Foam::DLListBase::link*
117{
118 if (!size_)
119 {
121 << "list is empty"
123 }
124 return first_;
125}
126
127
130{
131 if (!size_)
132 {
134 << "list is empty"
136 }
137 return last_;
138}
139
140
141inline const Foam::DLListBase::link*
143{
144 if (!size_)
145 {
147 << "list is empty"
148 << abort(FatalError);
149 }
150 return last_;
151}
152
153
154inline void Foam::DLListBase::clear()
156 first_ = nullptr;
157 last_ = nullptr;
158 size_ = 0;
159}
160
161
162inline void Foam::DLListBase::swap(DLListBase& lst)
163{
164 if (this == &lst)
165 {
166 return; // Self-swap is a no-op
167 }
169 std::swap(first_, lst.first_);
170 std::swap(last_, lst.last_);
171 std::swap(size_, lst.size_);
172}
173
174
176{
177 if (this == &lst)
178 {
179 return; // Self-assignment is a no-op
180 }
181
182 first_ = lst.first_;
183 last_ = lst.last_;
184 size_ = lst.size_;
185
186 lst.clear();
187}
188
189
192(
195{
196 return remove(iter.node_);
197}
198
199
202(
203 DLListBase::iterator& oldIter,
204 DLListBase::link* newItem
205)
207 return replace(oldIter.node_, newItem);
208}
209
210
211// * * * * * * * * * * * * * * * STL iterator * * * * * * * * * * * * * * * //
212
214(
215 DLListBase* list,
216 DLListBase::link* item
217)
218:
219 node_(item),
220 list_(list),
221 copy_()
222{
223 if (node_ != nullptr)
225 copy_ = *node_;
226 }
227}
228
229
234}
235
238{
239 return (node_ != nullptr);
240}
241
242
244{
245 if (list_)
246 {
247 // Check if the node_ is the first element (points to itself)
248 // or if the list is empty because last element was removed
249 if (node_ == copy_.prev_ || list_->first_ == nullptr)
250 {
251 node_ = nullptr;
252 }
253 else
254 {
255 node_ = copy_.prev_;
256 copy_ = *node_;
257 }
258 }
259}
260
261
263{
264 if (list_)
265 {
266 // Check if the node_ is the last element (points to itself)
267 // or if the list is empty because last element was removed
268 if (node_ == copy_.next_ || list_->last_ == nullptr)
269 {
270 node_ = nullptr;
271 }
272 else
273 {
274 node_ = copy_.next_;
275 copy_ = *node_;
276 }
277 }
278}
279
280
281inline void Foam::DLListBase::iterator::operator=(const iterator& iter)
283 node_ = iter.node_;
284 list_ = iter.list_;
285 copy_ = iter.copy_;
286}
287
289inline bool Foam::DLListBase::iterator::operator==(const iterator& iter) const
290{
291 return node_ == iter.node_;
292}
293
294
296{
297 return node_ != iter.node_;
298}
299
300
303{
304 if (size())
305 {
307 }
309 return end();
310}
311
312
313// * * * * * * * * * * * * * * STL const_iterator * * * * * * * * * * * * * //
314
316(
317 const DLListBase* list,
318 const DLListBase::link* item
320:
321 node_(item),
322 list_(list)
323{}
324
325
327(
328 const DLListBase::iterator& iter
329)
331 node_(iter.node_),
332 list_(iter.list_)
333{}
334
335
338{
339 return node_;
340}
341
344{
345 return (node_ != nullptr);
346}
347
348
350{
351 if (list_ && node_)
352 {
353 if (node_ == list_->first_)
354 {
355 node_ = nullptr;
356 }
357 else
359 node_ = node_->prev_;
360 }
361 }
362}
363
364
366{
367 if (list_ && node_)
368 {
369 if (node_ == list_->last_)
370 {
371 node_ = nullptr;
372 }
373 else
375 node_ = node_->next_;
376 }
377 }
378}
379
380
381inline bool Foam::DLListBase::const_iterator::operator==
382(
383 const const_iterator& iter
384) const
385{
386 return node_ == iter.node_;
387}
388
389
390inline bool Foam::DLListBase::const_iterator::operator!=
391(
392 const const_iterator& iter
393) const
394{
395 return node_ != iter.node_;
396}
397
398
401{
402 if (size())
403 {
406
407 return cend();
408}
409
410
413{
414 if (size())
415 {
417 }
418
419 return crend();
420}
421
422
423// ************************************************************************* //
A primitive const node iterator (bidirectional).
Definition DLListBase.H:358
void next()
Move forward through list.
bool good() const noexcept
Pointing at a valid storage node.
const_iterator(const const_iterator &)=default
Copy construct.
const link * get_node() const noexcept
The storage node.
void prev()
Move backward through list.
A primitive non-const node iterator.
Definition DLListBase.H:287
void next()
Move forward through list.
bool operator!=(const iterator &) const
void operator=(const iterator &iter)
bool good() const noexcept
Pointing at a valid storage node.
bool operator==(const iterator &) const
iterator(const iterator &)=default
Copy construct.
void prev()
Move backward through list.
link * get_node() const noexcept
The storage node.
Base for doubly-linked lists.
Definition DLListBase.H:58
const const_iterator & cend() const
End of list for iterators.
Definition DLListBaseI.H:80
link * front()
Return first entry.
Definition DLListBaseI.H:96
const const_iterator & crend() const
End of list for reverse iterators.
Definition DLListBaseI.H:87
void transfer(DLListBase &lst)
Transfer the contents of the argument into this list and annul the argument list.
link * back()
Return last entry.
link * replace(link *oldLink, link *newLink)
Replace oldLink with newLink and return element.
Definition DLListBase.C:214
IteratorType iterator_first() const
Return iterator to first item or end-iterator if list is empty.
Definition DLListBaseI.H:42
void swap(DLListBase &lst)
Swap the contents of the list.
friend class iterator
Definition DLListBase.H:155
const iterator & end()
End of list for iterators.
Definition DLListBaseI.H:73
static const IteratorType & iterator_end()
Factory method to return an iterator end.
Definition DLListBaseI.H:28
iterator begin()
Iterator to first item in list with non-const access.
IteratorType iterator_last() const
Return iterator to last item or end-iterator if list is empty.
Definition DLListBaseI.H:57
static const IteratorType & iterator_rend()
Factory method to return an iterator reverse end.
Definition DLListBaseI.H:35
const_iterator cbegin() const
Iterator to first item in list with const access.
DLListBase()=default
Default construct.
label size() const noexcept
The number of elements in list.
Definition DLListBase.H:194
void clear()
Clear the list.
link * remove(link *item)
Remove and return element.
Definition DLListBase.C:181
const_iterator crbegin() const
Iterator to last item in list with const access.
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition error.H:600
const NullObject * nullObjectPtr
Pointer to the unique nullObject.
Definition nullObject.C:29
errorManip< error > abort(error &err)
Definition errorManip.H:139
const direction noexcept
Definition scalarImpl.H:265
error FatalError
Error stream (stdout output on all processes), with additional 'FOAM FATAL ERROR' header text and sta...