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\*---------------------------------------------------------------------------*/
28
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 }
57
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
81{
82 return iterator_end<DLListBase::iterator>();
83}
84
85
88{
89 return iterator_end<DLListBase::const_iterator>();
90}
91
92
95{
96 return iterator_rend<DLListBase::const_iterator>();
97}
98
99
100// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
101
103{
104 return prev_ != nullptr && next_ != nullptr;
105}
106
107
109{
110 prev_ = next_ = nullptr;
111}
112
113
114inline Foam::label Foam::DLListBase::size() const noexcept
115{
116 return size_;
117}
118
119
121{
122 return !size_;
123}
124
125
128{
129 if (!size_)
130 {
132 << "list is empty"
133 << abort(FatalError);
134 }
135 return first_;
136}
137
138
139inline const Foam::DLListBase::link*
141{
142 if (!size_)
143 {
145 << "list is empty"
146 << abort(FatalError);
147 }
148 return first_;
149}
150
151
154{
155 if (!size_)
156 {
158 << "list is empty"
159 << abort(FatalError);
160 }
161 return last_;
162}
163
164
165inline const Foam::DLListBase::link*
167{
168 if (!size_)
169 {
171 << "list is empty"
172 << abort(FatalError);
173 }
174 return last_;
175}
176
177
179{
180 first_ = nullptr;
181 last_ = nullptr;
182 size_ = 0;
183}
184
185
187{
188 if (this == &lst)
189 {
190 return; // Self-swap is a no-op
191 }
192
193 std::swap(first_, lst.first_);
194 std::swap(last_, lst.last_);
195 std::swap(size_, lst.size_);
196}
197
198
200{
201 if (this == &lst)
202 {
203 return; // Self-assignment is a no-op
204 }
205
206 first_ = lst.first_;
207 last_ = lst.last_;
208 size_ = lst.size_;
209
210 lst.clear();
211}
212
213
216(
218)
219{
220 return remove(iter.node_);
221}
222
223
226(
227 DLListBase::iterator& oldIter,
228 DLListBase::link* newItem
229)
230{
231 return replace(oldIter.node_, newItem);
232}
233
234
235// * * * * * * * * * * * * * * * STL iterator * * * * * * * * * * * * * * * //
236
238(
239 DLListBase* list,
240 DLListBase::link* item
241)
242:
243 node_(item),
244 list_(list),
245 copy_()
246{
247 if (node_ != nullptr)
248 {
249 copy_ = *node_;
250 }
251}
252
253
256{
257 return node_;
258}
259
260
262{
263 return (node_ != nullptr);
264}
265
266
268{
269 if (list_)
270 {
271 // Check if the node_ is the first element (points to itself)
272 // or if the list is empty because last element was removed
273 if (node_ == copy_.prev_ || list_->first_ == nullptr)
274 {
275 node_ = nullptr;
276 }
277 else
278 {
279 node_ = copy_.prev_;
280 copy_ = *node_;
281 }
282 }
283}
284
285
287{
288 if (list_)
289 {
290 // Check if the node_ is the last element (points to itself)
291 // or if the list is empty because last element was removed
292 if (node_ == copy_.next_ || list_->last_ == nullptr)
293 {
294 node_ = nullptr;
295 }
296 else
297 {
298 node_ = copy_.next_;
299 copy_ = *node_;
300 }
301 }
302}
303
304
306{
307 node_ = iter.node_;
308 list_ = iter.list_;
309 copy_ = iter.copy_;
310}
311
312
314{
315 return node_ == iter.node_;
316}
317
318
320{
321 return node_ != iter.node_;
322}
323
324
327{
328 if (size())
329 {
330 return iterator_first<iterator>();
331 }
332
333 return end();
334}
335
336
337// * * * * * * * * * * * * * * STL const_iterator * * * * * * * * * * * * * //
338
340(
341 const DLListBase* list,
342 const DLListBase::link* item
343)
344:
345 node_(item),
346 list_(list)
347{}
348
349
351(
352 const DLListBase::iterator& iter
353)
354:
355 node_(iter.node_),
356 list_(iter.list_)
357{}
358
359
360inline const Foam::DLListBase::link*
362{
363 return node_;
364}
365
366
368{
369 return (node_ != nullptr);
370}
371
372
374{
375 if (list_ && node_)
376 {
377 if (node_ == list_->first_)
378 {
379 node_ = nullptr;
380 }
381 else
382 {
383 node_ = node_->prev_;
384 }
385 }
386}
387
388
390{
391 if (list_ && node_)
392 {
393 if (node_ == list_->last_)
394 {
395 node_ = nullptr;
396 }
397 else
398 {
399 node_ = node_->next_;
400 }
401 }
402}
403
404
406(
407 const const_iterator& iter
408) const
409{
410 return node_ == iter.node_;
411}
412
413
415(
416 const const_iterator& iter
417) const
418{
419 return node_ != iter.node_;
420}
421
422
425{
426 if (size())
427 {
428 return iterator_first<const_iterator>();
429 }
430
431 return cend();
432}
433
434
437{
438 if (size())
439 {
440 return iterator_last<const_iterator>();
441 }
442
443 return crend();
444}
445
446
447// ************************************************************************* //
A primitive const node iterator (bidirectional).
Definition: DLListBase.H:263
void next()
Move forward through list.
Definition: DLListBaseI.H:389
bool good() const noexcept
Pointing at a valid storage node.
Definition: DLListBaseI.H:367
const link * get_node() const noexcept
The storage node.
Definition: DLListBaseI.H:361
void prev()
Move backward through list.
Definition: DLListBaseI.H:373
A primitive non-const node iterator.
Definition: DLListBase.H:214
void next()
Move forward through list.
Definition: DLListBaseI.H:286
void operator=(const iterator &iter)
Definition: DLListBaseI.H:305
bool good() const noexcept
Pointing at a valid storage node.
Definition: DLListBaseI.H:261
void prev()
Move backward through list.
Definition: DLListBaseI.H:267
link * get_node() const noexcept
The storage node.
Definition: DLListBaseI.H:255
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
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
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
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
const_iterator cbegin() const
Iterator to first item in list with const access.
Definition: DLListBaseI.H:424
void clear()
Clear the list.
Definition: DLListBaseI.H:178
const_iterator crbegin() const
Iterator to last item in list with const access.
Definition: DLListBaseI.H:436
Forward iterator with non-const access.
Definition: HashTable.H:720
unsigned int remove()
Remove and return the last element.
Definition: PackedListI.H:709
A const_iterator for iterating across on values.
Definition: bitSet.H:505
friend Ostream & operator(Ostream &, const faMatrix< Type > &)
friend bool operator!=(const refineCell &rc1, const refineCell &rc2)
Definition: refineCell.H:106
friend bool operator==(const refineCell &rc1, const refineCell &rc2)
Definition: refineCell.H:97
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:453
const NullObject * nullObjectPtr
Pointer to the unique nullObject.
Definition: nullObject.C:33
errorManip< error > abort(error &err)
Definition: errorManip.H:144
const direction noexcept
Definition: Scalar.H:223
error FatalError