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-2020 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  //- Default 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 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 protected:
101 
102  // Protected Member Functions
103 
104  //- Factory method to return an iterator end
105  // Simply reinterprets a NullObject as a DLListBase iterator.
106  template<class IteratorType>
107  inline static const IteratorType& iterator_end();
108 
109  //- Factory method to return an iterator reverse end
110  // Simply reinterprets a NullObject as a DLListBase iterator.
111  template<class IteratorType>
112  inline static const IteratorType& iterator_rend();
113 
114  //- Return iterator to first item or end-iterator if list is empty
115  // Removes constness which the caller promises to manage.
116  template<class IteratorType>
117  inline IteratorType iterator_first() const;
118 
119  //- Return iterator to last item or end-iterator if list is empty
120  // Removes constness which the caller promises to manage.
121  template<class IteratorType>
122  inline IteratorType iterator_last() const;
123 
124 
125 public:
126 
127  // Forward declaration of iterators
128 
129  class iterator;
130  friend class iterator;
131 
132  class const_iterator;
133  friend class const_iterator;
134 
135 
136  // Generated Methods
137 
138  //- Default construct
139  DLListBase() = default;
140 
141  //- No copy construct
142  DLListBase(const DLListBase&) = delete;
143 
144  //- No copy assignment
145  void operator=(const DLListBase&) = delete;
146 
147  //- Destructor
148  ~DLListBase() = default;
149 
150 
151  // Member Functions
152 
153  //- The number of elements in list
154  inline label size() const noexcept;
155 
156  //- True if the list is empty
157  inline bool empty() const noexcept;
158 
159  //- Return first entry
160  inline link* first();
161 
162  //- Return const access to first entry
163  inline const link* first() const;
164 
165  //- Return last entry
166  inline link* last();
167 
168  //- Return const access to last entry
169  inline const link* last() const;
170 
171 
172  //- Add at head of list
173  void insert(link* item);
174 
175  //- Add at tail of list
176  void append(link* item);
177 
178  //- Swap this element with the one above unless it is at the top
179  bool swapUp(link* item);
180 
181  //- Swap this element with the one below unless it is at the bottom
182  bool swapDown(link* item);
183 
184  //- Remove and return head
185  link* removeHead();
186 
187  //- Remove and return element
188  link* remove(link* item);
189 
190  // Remove and return element specified by iterator
191  inline link* remove(iterator& iter);
192 
193  //- Replace oldLink with newLink and return element
194  link* replace(link* oldLink, link* newLink);
195 
196  //- Replace oldIter with newItem and return element
197  inline link* replace(iterator& oldIter, link* newitem);
198 
199  //- Clear the list
200  inline void clear();
201 
202  //- Swap the contents of the list
203  inline void swap(DLListBase& lst);
204 
205  //- Transfer the contents of the argument into this list
206  //- and annul the argument list.
207  inline void transfer(DLListBase& lst);
208 
209 
210  // iterator
211 
212  //- A primitive non-const node iterator.
213  // Needs to be extended by inheriting classes.
214  class iterator
215  {
216  friend class DLListBase;
217  friend class const_iterator;
218 
219  //- The selected node.
220  // MUST be the first member for easy comparison between iterators
221  // and for reinterpret_cast from nullObject
222  link* node_;
223 
224  //- The list being iterated on
225  DLListBase* list_;
226 
227  //- Copy of the node prev/next pointers (to use after removal)
228  link copy_;
229 
230  public:
231 
232  //- Copy construct
233  iterator(const iterator&) = default;
234 
235  //- Construct for a node on a list
236  inline iterator(DLListBase* list, link* item);
237 
238  //- The storage node
239  inline link* get_node() const;
240 
241  //- Pointing at a valid storage node
242  inline bool good() const;
243 
244  //- Deprecated(2019-01) Pointing at a valid storage node
245  // \deprecated(2019-01) - use good() method
246  FOAM_DEPRECATED_FOR(2019-01, "good() method")
247  bool 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  //- Copy construct
284  const_iterator(const const_iterator&) = default;
285 
286  //- Construct for a node on a list
287  inline const_iterator(const DLListBase* list, const link* item);
288 
289  //- Copy construct from a non-const iterator
290  inline const_iterator(const DLListBase::iterator& iter);
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  FOAM_DEPRECATED_FOR(2019-01, "good() method")
301  bool found() const
302  {
303  return this->good();
304  }
305 
306  //- Move backward through list
307  inline void prev();
308 
309  //- Move forward through list
310  inline void next();
311 
312  //- Copy assignment
313  const_iterator& operator=(const const_iterator&) = default;
314 
315  inline bool operator==(const const_iterator&) const;
316  inline bool operator!=(const const_iterator&) const;
317  };
318 
319 
320  //- Iterator to first item in list with non-const access
321  inline iterator begin();
322 
323  //- Iterator to first item in list with const access
324  inline const_iterator cbegin() const;
325 
326  //- Iterator to last item in list with const access
327  // Note that this is not a const_reverse_iterator, this is the
328  // responsibility of any derived classes.
329  inline const_iterator crbegin() const;
330 
331  //- End of list for iterators
332  inline const iterator& end();
333 
334  //- End of list for iterators
335  inline const const_iterator& cend() const;
336 
337  //- End of list for reverse iterators
338  inline const const_iterator& crend() const;
339 };
340 
341 
342 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
343 
344 } // End namespace Foam
345 
346 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
347 
348 #include "DLListBaseI.H"
349 
350 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
351 
352 #endif
353 
354 // ************************************************************************* //
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::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:213
Foam::DLListBase::end
const iterator & end()
End of list for iterators.
Definition: DLListBaseI.H:80
Foam::DLListBase::iterator
friend class iterator
Definition: DLListBase.H:128
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::operator!=
bool operator!=(const eddy &a, const eddy &b)
Definition: eddy.H:235
Foam::DLListBase::operator=
void operator=(const DLListBase &)=delete
No copy assignment.
Foam::DLListBase::DLListBase
DLListBase()=default
Default construct.
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::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::~DLListBase
~DLListBase()=default
Destructor.
Foam::DLListBase::const_iterator
friend class const_iterator
Definition: DLListBase.H:131
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
Foam::DLListBase::const_iterator::found
bool found() const
Deprecated(2019-01) Pointing at a valid storage node.
Definition: DLListBase.H:300
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::operator=
const_iterator & operator=(const const_iterator &)=default
Copy assignment.
bool
bool
Definition: EEqn.H:20
Foam::FOAM_DEPRECATED_FOR
class FOAM_DEPRECATED_FOR(2017-05, "Foam::Enum") NamedEnum
Definition: NamedEnum.H:69
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