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 -------------------------------------------------------------------------------
11 License
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 
34 template<class IteratorType>
35 inline const IteratorType& Foam::DLListBase::iterator_end()
36 {
37  return *reinterpret_cast<const IteratorType*>(nullObjectPtr);
38 }
39 
40 
41 template<class IteratorType>
42 inline const IteratorType& Foam::DLListBase::iterator_rend()
43 {
44  return *reinterpret_cast<const IteratorType*>(nullObjectPtr);
45 }
46 
47 
48 template<class IteratorType>
49 inline 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 
63 template<class IteratorType>
64 inline 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 * * * * * * * * * * * * * //
102 inline Foam::label Foam::DLListBase::size() const noexcept
103 {
104  return size_;
105 }
106 
107 
109 {
110  return !size_;
111 }
112 
113 
116 {
117  if (!size_)
118  {
120  << "list is empty"
122  }
123  return first_;
124 }
125 
126 
127 inline const Foam::DLListBase::link*
129 {
130  if (!size_)
131  {
133  << "list is empty"
135  }
136  return first_;
137 }
138 
139 
142 {
143  if (!size_)
144  {
146  << "list is empty"
148  }
149  return last_;
150 }
151 
152 
153 inline const Foam::DLListBase::link*
155 {
156  if (!size_)
157  {
159  << "list is empty"
160  << abort(FatalError);
161  }
162  return last_;
163 }
164 
165 
166 inline void Foam::DLListBase::clear()
167 {
168  first_ = nullptr;
169  last_ = nullptr;
170  size_ = 0;
171 }
172 
173 
174 inline void Foam::DLListBase::swap(DLListBase& lst)
175 {
176  if (this == &lst)
177  {
178  return; // Self-swap is a no-op
179  }
181  std::swap(first_, lst.first_);
182  std::swap(last_, lst.last_);
183  std::swap(size_, lst.size_);
184 }
185 
186 
187 inline void Foam::DLListBase::transfer(DLListBase& lst)
188 {
189  if (this == &lst)
190  {
191  return; // Self-assignment is a no-op
192  }
193 
194  first_ = lst.first_;
195  last_ = lst.last_;
196  size_ = lst.size_;
198  lst.clear();
199 }
200 
201 
204 (
206 )
207 {
208  return remove(iter.node_);
209 }
210 
211 
214 (
215  DLListBase::iterator& oldIter,
216  DLListBase::link* newItem
217 )
218 {
219  return replace(oldIter.node_, newItem);
220 }
221 
222 
223 // * * * * * * * * * * * * * * * STL iterator * * * * * * * * * * * * * * * //
224 
226 (
227  DLListBase* list,
228  DLListBase::link* item
229 )
230 :
231  node_(item),
232  list_(list),
233  copy_()
234 {
235  if (node_ != nullptr)
236  {
237  copy_ = *node_;
238  }
239 }
240 
241 
244 {
245  return node_;
246 }
247 
249 inline bool Foam::DLListBase::iterator::good() const noexcept
250 {
251  return (node_ != nullptr);
252 }
253 
254 
256 {
257  if (list_)
258  {
259  // Check if the node_ is the first element (points to itself)
260  // or if the list is empty because last element was removed
261  if (node_ == copy_.prev_ || list_->first_ == nullptr)
262  {
263  node_ = nullptr;
264  }
265  else
266  {
267  node_ = copy_.prev_;
268  copy_ = *node_;
269  }
270  }
271 }
272 
273 
275 {
276  if (list_)
277  {
278  // Check if the node_ is the last element (points to itself)
279  // or if the list is empty because last element was removed
280  if (node_ == copy_.next_ || list_->last_ == nullptr)
281  {
282  node_ = nullptr;
283  }
284  else
285  {
286  node_ = copy_.next_;
287  copy_ = *node_;
288  }
289  }
290 }
291 
292 
293 inline void Foam::DLListBase::iterator::operator=(const iterator& iter)
294 {
295  node_ = iter.node_;
296  list_ = iter.list_;
297  copy_ = iter.copy_;
298 }
299 
301 inline bool Foam::DLListBase::iterator::operator==(const iterator& iter) const
302 {
303  return node_ == iter.node_;
304 }
305 
306 
307 inline bool Foam::DLListBase::iterator::operator!=(const iterator& iter) const
308 {
309  return node_ != iter.node_;
310 }
311 
312 
315 {
316  if (size())
317  {
318  return iterator_first<iterator>();
319  }
320 
321  return end();
322 }
323 
324 
325 // * * * * * * * * * * * * * * STL const_iterator * * * * * * * * * * * * * //
326 
328 (
329  const DLListBase* list,
330  const DLListBase::link* item
331 )
332 :
333  node_(item),
334  list_(list)
335 {}
336 
337 
339 (
340  const DLListBase::iterator& iter
341 )
342 :
343  node_(iter.node_),
344  list_(iter.list_)
345 {}
346 
347 
350 {
351  return node_;
352 }
353 
356 {
357  return (node_ != nullptr);
358 }
359 
360 
362 {
363  if (list_ && node_)
364  {
365  if (node_ == list_->first_)
366  {
367  node_ = nullptr;
368  }
369  else
370  {
371  node_ = node_->prev_;
372  }
373  }
374 }
375 
376 
378 {
379  if (list_ && node_)
380  {
381  if (node_ == list_->last_)
382  {
383  node_ = nullptr;
384  }
385  else
386  {
387  node_ = node_->next_;
388  }
389  }
390 }
391 
392 
393 inline bool Foam::DLListBase::const_iterator::operator==
394 (
395  const const_iterator& iter
396 ) const
397 {
398  return node_ == iter.node_;
399 }
400 
401 
402 inline bool Foam::DLListBase::const_iterator::operator!=
403 (
404  const const_iterator& iter
405 ) const
406 {
407  return node_ != iter.node_;
408 }
409 
410 
413 {
414  if (size())
415  {
416  return iterator_first<const_iterator>();
417  }
418 
419  return cend();
420 }
421 
422 
425 {
426  if (size())
427  {
428  return iterator_last<const_iterator>();
429  }
430 
431  return crend();
432 }
433 
434 
435 // ************************************************************************* //
iterator(const iterator &)=default
Copy construct.
void prev()
Move backward through list.
Definition: DLListBaseI.H:248
link * remove(link *item)
Remove and return element.
Definition: DLListBase.C:181
const_iterator(const const_iterator &)=default
Copy construct.
const const_iterator & crend() const
End of list for reverse iterators.
Definition: DLListBaseI.H:87
error FatalError
Error stream (stdout output on all processes), with additional &#39;FOAM FATAL ERROR&#39; header text and sta...
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:578
Base for doubly-linked lists.
Definition: DLListBase.H:57
void transfer(DLListBase &lst)
Transfer the contents of the argument into this list and annul the argument list. ...
Definition: DLListBaseI.H:180
link * replace(link *oldLink, link *newLink)
Replace oldLink with newLink and return element.
Definition: DLListBase.C:214
bool operator!=(const iterator &) const
Definition: DLListBaseI.H:300
iterator begin()
Iterator to first item in list with non-const access.
Definition: DLListBaseI.H:307
link * back()
Return last entry.
Definition: DLListBaseI.H:134
label size() const noexcept
The number of elements in list.
Definition: DLListBaseI.H:95
static const IteratorType & iterator_end()
Factory method to return an iterator end.
Definition: DLListBaseI.H:28
bool good() const noexcept
Pointing at a valid storage node.
Definition: DLListBaseI.H:348
const const_iterator & cend() const
End of list for iterators.
Definition: DLListBaseI.H:80
void next()
Move forward through list.
Definition: DLListBaseI.H:267
bool operator==(const iterator &) const
Definition: DLListBaseI.H:294
A primitive const node iterator (bidirectional).
Definition: DLListBase.H:357
A primitive non-const node iterator.
Definition: DLListBase.H:286
errorManip< error > abort(error &err)
Definition: errorManip.H:139
friend class const_iterator
Definition: DLListBase.H:157
const direction noexcept
Definition: Scalar.H:258
link * get_node() const noexcept
The storage node.
Definition: DLListBaseI.H:236
const NullObject * nullObjectPtr
Pointer to the unique nullObject.
Definition: nullObject.C:29
const iterator & end()
End of list for iterators.
Definition: DLListBaseI.H:73
link * front()
Return first entry.
Definition: DLListBaseI.H:108
bool empty() const noexcept
True if the list is empty.
Definition: DLListBaseI.H:101
void operator=(const iterator &iter)
Definition: DLListBaseI.H:286
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.
Definition: DLListBaseI.H:405
const_iterator crbegin() const
Iterator to last item in list with const access.
Definition: DLListBaseI.H:417
void prev()
Move backward through list.
Definition: DLListBaseI.H:354
bool good() const noexcept
Pointing at a valid storage node.
Definition: DLListBaseI.H:242
void clear()
Clear the list.
Definition: DLListBaseI.H:159
void next()
Move forward through list.
Definition: DLListBaseI.H:370
friend class iterator
Definition: DLListBase.H:154
const link * get_node() const noexcept
The storage node.
Definition: DLListBaseI.H:342
IteratorType iterator_first() const
Return iterator to first item or end-iterator if list is empty.
Definition: DLListBaseI.H:42
IteratorType iterator_last() const
Return iterator to last item or end-iterator if list is empty.
Definition: DLListBaseI.H:57
void swap(DLListBase &lst)
Swap the contents of the list.
Definition: DLListBaseI.H:167