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-2019 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 
103 {
104  return prev_ != nullptr && next_ != nullptr;
105 }
106 
107 
109 {
110  prev_ = next_ = nullptr;
111 }
112 
113 
114 inline Foam::label Foam::DLListBase::size() const noexcept
115 {
116  return size_;
117 }
118 
119 
120 inline bool Foam::DLListBase::empty() const noexcept
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 
139 inline 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 
165 inline 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 
313 inline bool Foam::DLListBase::iterator::operator==(const iterator& iter) const
314 {
315  return node_ == iter.node_;
316 }
317 
318 
319 inline bool Foam::DLListBase::iterator::operator!=(const iterator& iter) const
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 
360 inline 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 
405 inline bool Foam::DLListBase::const_iterator::operator==
406 (
407  const const_iterator& iter
408 ) const
409 {
410  return node_ == iter.node_;
411 }
412 
413 
414 inline bool Foam::DLListBase::const_iterator::operator!=
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 // ************************************************************************* //
Foam::DLListBase::swap
void swap(DLListBase &lst)
Swap the contents of the list.
Definition: DLListBaseI.H:186
Foam::nullObjectPtr
const NullObject * nullObjectPtr
Pointer to the unique nullObject.
Definition: nullObject.C:33
Foam::DLListBase::iterator::next
void next()
Move forward through list.
Definition: DLListBaseI.H:286
Foam::DLListBase::iterator::operator==
bool operator==(const iterator &) const
Definition: DLListBaseI.H:313
stdFoam::crend
constexpr auto crend(const C &c) -> decltype(c.rend())
Return const_reverse_iterator to reverse-end of container c.
Definition: stdFoam.H:188
Foam::DLListBase::iterator::prev
void prev()
Move backward through list.
Definition: DLListBaseI.H:267
Foam::DLListBase::iterator::good
bool good() const
Pointing at a valid storage node.
Definition: DLListBaseI.H:261
Foam::DLListBase::remove
link * remove(link *item)
Remove and return element.
Definition: DLListBase.C:186
Foam::DLListBase::crend
const const_iterator & crend() const
End of list for reverse iterators.
Definition: DLListBaseI.H:94
Foam::DLListBase::const_iterator::next
void next()
Move forward through list.
Definition: DLListBaseI.H:389
Foam::DLListBase::iterator
A primitive non-const node iterator.
Definition: DLListBase.H:213
Foam::DLListBase::iterator::operator!=
bool operator!=(const iterator &) const
Definition: DLListBaseI.H:319
Foam::DLListBase::end
const iterator & end()
End of list for iterators.
Definition: DLListBaseI.H:80
Foam::DLListBase::crbegin
const_iterator crbegin() const
Iterator to last item in list with const access.
Definition: DLListBaseI.H:436
Foam::DLListBase::iterator::operator=
void operator=(const iterator &iter)
Definition: DLListBaseI.H:305
Foam::DLListBase::iterator::get_node
link * get_node() const
The storage node.
Definition: DLListBaseI.H:255
Foam::DLListBase::iterator::iterator
iterator(const iterator &)=default
Copy construct.
Foam::DLListBase::first
link * first()
Return first entry.
Definition: DLListBaseI.H:127
Foam::DLListBase::begin
iterator begin()
Iterator to first item in list with non-const access.
Definition: DLListBaseI.H:326
error.H
Foam::DLListBase::const_iterator::const_iterator
const_iterator(const const_iterator &)=default
Copy construct.
Foam::DLListBase::last
link * last()
Return last entry.
Definition: DLListBaseI.H:153
Foam::DLListBase::const_iterator::good
bool good() const
Pointing at a valid storage node.
Definition: DLListBaseI.H:367
Foam::DLListBase::const_iterator::get_node
const link * get_node() const
The storage node.
Definition: DLListBaseI.H:361
Foam::DLListBase::empty
bool empty() const noexcept
True if the list is empty.
Definition: DLListBaseI.H:120
Foam::DLListBase::cend
const const_iterator & cend() const
End of list for iterators.
Definition: DLListBaseI.H:87
Foam::DLListBase::iterator_last
IteratorType iterator_last() const
Return iterator to last item or end-iterator if list is empty.
Definition: DLListBaseI.H:64
Foam::FatalError
error FatalError
Foam::DLListBase::transfer
void transfer(DLListBase &lst)
Definition: DLListBaseI.H:199
stdFoam::end
constexpr auto end(C &c) -> decltype(c.end())
Return iterator to the end of the container c.
Definition: stdFoam.H:121
Foam::abort
errorManip< error > abort(error &err)
Definition: errorManip.H:144
stdFoam::cend
constexpr auto cend(const C &c) -> decltype(c.end())
Return const_iterator to the end of the container c.
Definition: stdFoam.H:137
Foam::DLListBase
Base for doubly-linked lists.
Definition: DLListBase.H:61
FatalErrorInFunction
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:453
Foam::DLListBase::const_iterator::prev
void prev()
Move backward through list.
Definition: DLListBaseI.H:373
Foam::DLListBase::cbegin
const_iterator cbegin() const
Iterator to first item in list with const access.
Definition: DLListBaseI.H:424
Foam::DLListBase::size
label size() const noexcept
The number of elements in list.
Definition: DLListBaseI.H:114
nullObject.H
Foam::DLListBase::replace
link * replace(link *oldLink, link *newLink)
Replace oldLink with newLink and return element.
Definition: DLListBase.C:219
Foam::DLListBase::iterator_first
IteratorType iterator_first() const
Return iterator to first item or end-iterator if list is empty.
Definition: DLListBaseI.H:49
Foam::DLListBase::iterator_rend
static const IteratorType & iterator_rend()
Factory method to return an iterator reverse end.
Definition: DLListBaseI.H:42
Foam::DLListBase::clear
void clear()
Clear the list.
Definition: DLListBaseI.H:178
Foam::DLListBase::iterator_end
static const IteratorType & iterator_end()
Factory method to return an iterator end.
Definition: DLListBaseI.H:35
Foam::DLListBase::const_iterator
A primitive const node iterator (bidirectional).
Definition: DLListBase.H:270