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 * * * * * * * * * * * * * //
101 
104 {
105  if (!size_)
106  {
108  << "list is empty"
110  }
111  return first_;
112 }
113 
114 
115 inline 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 
141 inline const Foam::DLListBase::link*
143 {
144  if (!size_)
145  {
147  << "list is empty"
148  << abort(FatalError);
149  }
150  return last_;
151 }
152 
153 
154 inline void Foam::DLListBase::clear()
155 {
156  first_ = nullptr;
157  last_ = nullptr;
158  size_ = 0;
159 }
160 
161 
162 inline 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 
175 inline void Foam::DLListBase::transfer(DLListBase& lst)
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_;
186  lst.clear();
187 }
188 
189 
192 (
194 )
195 {
196  return remove(iter.node_);
197 }
198 
199 
202 (
203  DLListBase::iterator& oldIter,
204  DLListBase::link* newItem
205 )
206 {
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)
224  {
225  copy_ = *node_;
226  }
227 }
228 
229 
232 {
233  return node_;
234 }
235 
237 inline bool Foam::DLListBase::iterator::good() const noexcept
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 
281 inline void Foam::DLListBase::iterator::operator=(const iterator& iter)
282 {
283  node_ = iter.node_;
284  list_ = iter.list_;
285  copy_ = iter.copy_;
286 }
287 
289 inline bool Foam::DLListBase::iterator::operator==(const iterator& iter) const
290 {
291  return node_ == iter.node_;
292 }
293 
294 
295 inline bool Foam::DLListBase::iterator::operator!=(const iterator& iter) const
296 {
297  return node_ != iter.node_;
298 }
299 
300 
303 {
304  if (size())
305  {
306  return iterator_first<iterator>();
307  }
308 
309  return end();
310 }
311 
312 
313 // * * * * * * * * * * * * * * STL const_iterator * * * * * * * * * * * * * //
314 
316 (
317  const DLListBase* list,
318  const DLListBase::link* item
319 )
320 :
321  node_(item),
322  list_(list)
323 {}
324 
325 
327 (
328  const DLListBase::iterator& iter
329 )
330 :
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
358  {
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
374  {
375  node_ = node_->next_;
376  }
377  }
378 }
379 
380 
381 inline bool Foam::DLListBase::const_iterator::operator==
382 (
383  const const_iterator& iter
384 ) const
385 {
386  return node_ == iter.node_;
387 }
388 
389 
390 inline 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  {
404  return iterator_first<const_iterator>();
405  }
406 
407  return cend();
408 }
409 
410 
413 {
414  if (size())
415  {
416  return iterator_last<const_iterator>();
417  }
418 
419  return crend();
420 }
421 
422 
423 // ************************************************************************* //
iterator(const iterator &)=default
Copy construct.
void prev()
Move backward through list.
Definition: DLListBaseI.H:236
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:598
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:168
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:288
iterator begin()
Iterator to first item in list with non-const access.
Definition: DLListBaseI.H:295
link * back()
Return last entry.
Definition: DLListBaseI.H:122
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:336
const const_iterator & cend() const
End of list for iterators.
Definition: DLListBaseI.H:80
void next()
Move forward through list.
Definition: DLListBaseI.H:255
bool operator==(const iterator &) const
Definition: DLListBaseI.H:282
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:224
label size() const noexcept
The number of elements in list.
Definition: DLListBase.H:194
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:96
void operator=(const iterator &iter)
Definition: DLListBaseI.H:274
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:393
const_iterator crbegin() const
Iterator to last item in list with const access.
Definition: DLListBaseI.H:405
void prev()
Move backward through list.
Definition: DLListBaseI.H:342
bool good() const noexcept
Pointing at a valid storage node.
Definition: DLListBaseI.H:230
void clear()
Clear the list.
Definition: DLListBaseI.H:147
void next()
Move forward through list.
Definition: DLListBaseI.H:358
friend class iterator
Definition: DLListBase.H:154
const link * get_node() const noexcept
The storage node.
Definition: DLListBaseI.H:330
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:155