DLListBase.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 Class
28  Foam::DLListBase
29 
30 Description
31  Base for doubly-linked lists.
32 
33  The iterators associated with the list only have a core functionality
34  for navigation, with additional functionality to be added by inheriting
35  classes. The node iterators always have a node-pointer as the
36  first member data, which allows reinterpret_cast from anything else with
37  a nullptr as its first data member.
38  The nullObject is such an item (with a nullptr data member).
39 
40 SourceFiles
41  DLListBaseI.H
42  DLListBase.C
43 
44 \*---------------------------------------------------------------------------*/
45 
46 #ifndef DLListBase_H
47 #define DLListBase_H
48 
49 #include "label.H"
50 #include "uLabel.H"
51 #include "stdFoam.H"
52 
53 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
54 
55 namespace Foam
56 {
57 
58 /*---------------------------------------------------------------------------*\
59  Class DLListBase Declaration
60 \*---------------------------------------------------------------------------*/
61 
62 class DLListBase
63 {
64 public:
65 
66  //- The structure for a doubly-linked storage node
67  struct link
68  {
69  //- Pointer to prev entry in list
70  link* prev_ = nullptr;
71 
72  //- Pointer to next entry in list
73  link* next_ = nullptr;
74 
75  //- Null construct
76  link() = default;
77 
78  //- Check if the node is registered with the list
79  inline bool registered() const;
80 
81  //- Deregister the node after removal
82  inline void deregister();
83  };
84 
85 
86 private:
87 
88  // Private Member Data
89 
90  //- Pointer to first element
91  link *first_ = nullptr;
92 
93  //- Pointer to last element
94  link *last_ = nullptr;
95 
96  //- Number of elements in the list
97  label size_ = 0;
98 
99 
100  // Private Member Functions
101 
102  //- No copy construct
103  DLListBase(const DLListBase&) = delete;
104 
105  //- No copy assignment
106  void operator=(const DLListBase&) = delete;
107 
108 
109 protected:
110 
111  // Protected Member Functions
112 
113  //- Factory method to return an iterator end
114  // Simply reinterprets a NullObject as a DLListBase iterator.
115  template<class IteratorType>
116  inline static const IteratorType& iterator_end();
117 
118  //- Factory method to return an iterator reverse end
119  // Simply reinterprets a NullObject as a DLListBase iterator.
120  template<class IteratorType>
121  inline static const IteratorType& iterator_rend();
122 
123  //- Return iterator to first item or end-iterator if list is empty
124  // Removes constness which the caller promises to manage.
125  template<class IteratorType>
126  inline IteratorType iterator_first() const;
127 
128  //- Return iterator to last item or end-iterator if list is empty
129  // Removes constness which the caller promises to manage.
130  template<class IteratorType>
131  inline IteratorType iterator_last() const;
132 
133 
134 public:
135 
136  // Forward declaration of iterators
137 
138  class iterator;
139  friend class iterator;
140 
141  class const_iterator;
142  friend class const_iterator;
143 
144 
145  // Constructors
146 
147  //- Null construct
148  DLListBase() = default;
149 
150 
151  //- Destructor
152  ~DLListBase() = default;
153 
154 
155  // Member Functions
156 
157  //- The number of elements in list
158  inline label size() const noexcept;
159 
160  //- True if the list is empty
161  inline bool empty() const noexcept;
162 
163  //- Return first entry
164  inline link* first();
165 
166  //- Return const access to first entry
167  inline const link* first() const;
168 
169  //- Return last entry
170  inline link* last();
171 
172  //- Return const access to last entry
173  inline const link* last() const;
174 
175 
176  //- Add at head of list
177  void insert(link* item);
178 
179  //- Add at tail of list
180  void append(link* item);
181 
182  //- Swap this element with the one above unless it is at the top
183  bool swapUp(link* item);
184 
185  //- Swap this element with the one below unless it is at the bottom
186  bool swapDown(link* item);
187 
188  //- Remove and return head
189  link* removeHead();
190 
191  //- Remove and return element
192  link* remove(link* item);
193 
194  // Remove and return element specified by iterator
195  inline link* remove(iterator& iter);
196 
197  //- Replace oldLink with newLink and return element
198  link* replace(link* oldLink, link* newLink);
199 
200  //- Replace oldIter with newItem and return element
201  inline link* replace(iterator& oldIter, link* newitem);
202 
203  //- Clear the list
204  inline void clear();
205 
206  //- Swap the contents of the list
207  inline void swap(DLListBase& lst);
208 
209  //- Transfer the contents of the argument into this list
210  //- and annul the argument list.
211  inline void transfer(DLListBase& lst);
212 
213 
214  // iterator
215 
216  //- A primitive non-const node iterator.
217  // Needs to be extended by inheriting classes.
218  class iterator
219  {
220  friend class DLListBase;
221  friend class const_iterator;
222 
223  //- The selected node.
224  // MUST be the first member for easy comparison between iterators
225  // and for reinterpret_cast from nullObject
226  link* node_;
227 
228  //- The list being iterated on
229  DLListBase* list_;
230 
231  //- Copy of the node prev/next pointers (to use after removal)
232  link copy_;
233 
234  public:
235 
236  //- Construct for a node on a list
237  inline iterator(DLListBase* list, link* item);
238 
239  //- The storage node
240  inline link* get_node() const;
241 
242  //- Pointing at a valid storage node
243  inline bool good() const;
244 
245  //- Deprecated(2019-01) Pointing at a valid storage node
246  // \deprecated(2019-01) - use good() method
247  bool FOAM_DEPRECATED_FOR(2019-01, "good() method") found() const
248  {
249  return this->good();
250  }
251 
252  //- Move backward through list
253  inline void prev();
254 
255  //- Move forward through list
256  inline void next();
257 
258  inline void operator=(const iterator& iter);
259 
260  inline bool operator==(const iterator&) const;
261  inline bool operator!=(const iterator&) const;
262  };
263 
264 
265  // const_iterator
266 
267  //- A primitive const node iterator (bidirectional).
268  // Must normally be extended by inheriting classes.
269  // Since this iterator works bidirectionally, it can be used as the
270  // basis for a derived const_reverse_iterator
271  class const_iterator
272  {
273  //- The selected node.
274  // MUST be the first member for easy comparison between iterators
275  // and for reinterpret_cast from nullObject
276  const link* node_;
277 
278  //- The list being iterated on (as pointer for bitwise copy)
279  const DLListBase* list_;
280 
281  public:
282 
283  //- Construct for a node on a list
284  inline const_iterator(const DLListBase* list, const link* item);
285 
286  //- Copy construct from a non-const iterator
287  inline const_iterator(const DLListBase::iterator& iter);
288 
289  //- Copy construct
290  const_iterator(const const_iterator&) = default;
291 
292  //- The storage node
293  inline const link* get_node() const;
294 
295  //- Pointing at a valid storage node
296  inline bool good() const;
297 
298  //- Deprecated(2019-01) Pointing at a valid storage node
299  // \deprecated(2019-01) - use good() method
300  bool FOAM_DEPRECATED_FOR(2019-01, "good() method") found() const
301  {
302  return this->good();
303  }
304 
305  //- Move backward through list
306  inline void prev();
307 
308  //- Move forward through list
309  inline void next();
310 
311  const_iterator& operator=(const const_iterator&) = default;
312 
313  inline bool operator==(const const_iterator&) const;
314  inline bool operator!=(const const_iterator&) const;
315  };
316 
317 
318  //- Iterator to first item in list with non-const access
319  inline iterator begin();
320 
321  //- Iterator to first item in list with const access
322  inline const_iterator cbegin() const;
323 
324  //- Iterator to last item in list with const access
325  // Note that this is not a const_reverse_iterator, this is the
326  // responsibility of any derived classes.
327  inline const_iterator crbegin() const;
328 
329  //- End of list for iterators
330  inline const iterator& end();
331 
332  //- End of list for iterators
333  inline const const_iterator& cend() const;
334 
335  //- End of list for reverse iterators
336  inline const const_iterator& crend() const;
337 };
338 
339 
340 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
341 
342 } // End namespace Foam
343 
344 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
345 
346 #include "DLListBaseI.H"
347 
348 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
349 
350 #endif
351 
352 // ************************************************************************* //
DLListBaseI.H
Foam::DLListBase::swap
void swap(DLListBase &lst)
Swap the contents of the list.
Definition: DLListBaseI.H:186
Foam::DLListBase::swapUp
bool swapUp(link *item)
Swap this element with the one above unless it is at the top.
Definition: DLListBase.C:84
Foam::DLListBase::remove
link * remove(link *item)
Remove and return element.
Definition: DLListBase.C:186
Foam::DLListBase::insert
void insert(link *item)
Add at head of list.
Definition: DLListBase.C:34
Foam::DLListBase::crend
const const_iterator & crend() const
End of list for reverse iterators.
Definition: DLListBaseI.H:94
FOAM_DEPRECATED_FOR
#define FOAM_DEPRECATED_FOR(since, replacement)
Definition: stdFoam.H:65
Foam::DLListBase::swapDown
bool swapDown(link *item)
Swap this element with the one below unless it is at the bottom.
Definition: DLListBase.C:123
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:217
Foam::DLListBase::end
const iterator & end()
End of list for iterators.
Definition: DLListBaseI.H:80
Foam::DLListBase::iterator
friend class iterator
Definition: DLListBase.H:137
Foam::DLListBase::crbegin
const_iterator crbegin() const
Iterator to last item in list with const access.
Definition: DLListBaseI.H:436
Foam::DLListBase::first
link * first()
Return first entry.
Definition: DLListBaseI.H:127
Foam::DLListBase::const_iterator::FOAM_DEPRECATED_FOR
bool FOAM_DEPRECATED_FOR(2019-01, "good() method") found() const
Deprecated(2019-01) Pointing at a valid storage node.
Definition: DLListBase.H:299
Foam::operator!=
bool operator!=(const eddy &a, const eddy &b)
Definition: eddy.H:235
Foam::DLListBase::DLListBase
DLListBase()=default
Null construct.
Foam::label
intWM_LABEL_SIZE_t label
A label is an int32_t or int64_t as specified by the pre-processor macro WM_LABEL_SIZE.
Definition: label.H:62
Foam::operator==
tmp< faMatrix< Type > > operator==(const faMatrix< Type > &, const faMatrix< Type > &)
Foam::DLListBase::begin
iterator begin()
Iterator to first item in list with non-const access.
Definition: DLListBaseI.H:326
Foam::DLListBase::last
link * last()
Return last entry.
Definition: DLListBaseI.H:153
Foam::DLListBase::~DLListBase
~DLListBase()=default
Destructor.
Foam::DLListBase::const_iterator
friend class const_iterator
Definition: DLListBase.H:140
Foam::DLListBase::const_iterator::operator!=
bool operator!=(const const_iterator &) const
Definition: DLListBaseI.H:415
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::DLListBase::transfer
void transfer(DLListBase &lst)
Definition: DLListBaseI.H:199
Foam
Namespace for OpenFOAM.
Definition: atmBoundaryLayer.C:33
found
bool found
Definition: TABSMDCalcMethod2.H:32
Foam::DLListBase::removeHead
link * removeHead()
Remove and return head.
Definition: DLListBase.C:162
Foam::DLListBase
Base for doubly-linked lists.
Definition: DLListBase.H:61
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
label.H
stdFoam.H
Foam::DLListBase::const_iterator::const_iterator
const_iterator(const DLListBase *list, const link *item)
Construct for a node on a list.
Definition: DLListBaseI.H:340
Foam::DLListBase::const_iterator::operator=
const_iterator & operator=(const const_iterator &)=default
Foam::DLListBase::append
void append(link *item)
Add at tail of list.
Definition: DLListBase.C:59
Foam::DLListBase::size
label size() const noexcept
The number of elements in list.
Definition: DLListBaseI.H:114
Foam::DLListBase::const_iterator::operator==
bool operator==(const const_iterator &) const
Definition: DLListBaseI.H:406
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
uLabel.H
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