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-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 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 Foam_DLListBase_H
47 #define Foam_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() noexcept = default;
77 
78  //- Node registered (linked) in a list?
79  bool registered() const noexcept { return prev_ && next_; }
80 
81  //- Deregister the node (after removal)
82  void deregister() noexcept { prev_ = next_ = nullptr; }
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 Declarations (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  //- True if the list is empty
154  bool empty() const noexcept { return !size_; }
155 
156  //- The number of elements in list
157  label size() const noexcept { return size_; }
158 
159  //- Return first entry
160  inline link* front();
161 
162  //- Return const access to first entry
163  inline const link* front() const;
164 
165  //- Return last entry
166  inline link* back();
167 
168  //- Return const access to last entry
169  inline const link* back() const;
170 
171 
172  //- Add at front of list
173  void push_front(link* item);
174 
175  //- Add at back of list
176  void push_back(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 first entry
185  link* removeHead();
186 
187  //- Remove and return element
188  link* remove(link* item);
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 noexcept;
240 
241  //- Pointing at a valid storage node
242  inline bool good() const noexcept;
243 
244  //- Move backward through list
245  inline void prev();
246 
247  //- Move forward through list
248  inline void next();
249 
250  inline void operator=(const iterator& iter);
251 
252  inline bool operator==(const iterator&) const;
253  inline bool operator!=(const iterator&) const;
254  };
255 
256 
257  // const_iterator
258 
259  //- A primitive const node iterator (bidirectional).
260  // Must normally be extended by inheriting classes.
261  // Since this iterator works bidirectionally, it can be used as the
262  // basis for a derived const_reverse_iterator
263  class const_iterator
264  {
265  //- The selected node.
266  // MUST be the first member for easy comparison between iterators
267  // and for reinterpret_cast from nullObject
268  const link* node_;
269 
270  //- The list being iterated on (as pointer for bitwise copy)
271  const DLListBase* list_;
272 
273  public:
274 
275  //- Copy construct
276  const_iterator(const const_iterator&) = default;
277 
278  //- Construct for a node on a list
279  inline const_iterator(const DLListBase* list, const link* item);
280 
281  //- Copy construct from a non-const iterator
282  inline const_iterator(const DLListBase::iterator& iter);
283 
284  //- The storage node
285  inline const link* get_node() const noexcept;
287  //- Pointing at a valid storage node
288  inline bool good() const noexcept;
290  //- Move backward through list
291  inline void prev();
292 
293  //- Move forward through list
294  inline void next();
295 
296  //- Copy assignment
297  const_iterator& operator=(const const_iterator&) = default;
298 
299  inline bool operator==(const const_iterator&) const;
300  inline bool operator!=(const const_iterator&) const;
301  };
302 
303 
304  //- Iterator to first item in list with non-const access
305  inline iterator begin();
306 
307  //- Iterator to first item in list with const access
308  inline const_iterator cbegin() const;
309 
310  //- Iterator to last item in list with const access
311  // Note that this is not a const_reverse_iterator, this is the
312  // responsibility of any derived classes.
313  inline const_iterator crbegin() const;
314 
315  //- End of list for iterators
316  inline const iterator& end();
317 
318  //- End of list for iterators
319  inline const const_iterator& cend() const;
320 
321  //- End of list for reverse iterators
322  inline const const_iterator& crend() const;
323 
324 
325  // Housekeeping
326 
327  //- Return first entry
328  //FOAM_DEPRECATED_FOR(2022-10, "front()")
329  link* first() { return front(); }
330 
331  //- Return const access to first entry
332  //FOAM_DEPRECATED_FOR(2022-10, "front()")
333  const link* first() const { return front(); }
334 
335  //- Return last entry
336  //FOAM_DEPRECATED_FOR(2022-10, "back()")
337  link* last() { return back(); }
338 
339  //- Return const access to last entry
340  //FOAM_DEPRECATED_FOR(2022-10, "back()")
341  const link* last() const { return back(); }
342 
343  //- Add at front of list
344  //FOAM_DEPRECATED_FOR(2022-10, "push_front()")
345  void prepend(link* item) { push_front(item); }
346 
347  //- Add at back of list
348  //FOAM_DEPRECATED_FOR(2022-10, "push_back()")
349  void append(link* item) { push_back(item); }
350 };
351 
352 
353 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
354 
355 } // End namespace Foam
356 
357 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
358 
359 #include "DLListBaseI.H"
360 
361 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
362 
363 #endif
364 
365 // ************************************************************************* //
void operator=(const DLListBase &)=delete
No copy assignment.
iterator(const iterator &)=default
Copy construct.
link * first()
Return first entry.
Definition: DLListBase.H:460
void prev()
Move backward through list.
Definition: DLListBaseI.H:236
const const_iterator & crend() const
End of list for reverse iterators.
Definition: DLListBaseI.H:87
Base for doubly-linked lists.
Definition: DLListBase.H:57
friend class const_iterator
Definition: DLListBase.H:289
void transfer(DLListBase &lst)
Transfer the contents of the argument into this list and annul the argument list. ...
Definition: DLListBaseI.H:168
link * last()
Return last entry.
Definition: DLListBase.H:474
link * removeHead()
Remove and return first entry.
Definition: DLListBase.C:155
bool swapDown(link *item)
Swap this element with the one below unless it is at the bottom.
Definition: DLListBase.C:116
link * replace(link *oldLink, link *newLink)
Replace oldLink with newLink and return element.
Definition: DLListBase.C:214
~DLListBase()=default
Destructor.
void append(link *item)
Add at back of list.
Definition: DLListBase.H:495
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
const const_iterator & cend() const
End of list for iterators.
Definition: DLListBaseI.H:80
void prepend(link *item)
Add at front of list.
Definition: DLListBase.H:488
void next()
Move forward through list.
Definition: DLListBaseI.H:255
DLListBase()=default
Default construct.
A primitive const node iterator (bidirectional).
Definition: DLListBase.H:357
A primitive non-const node iterator.
Definition: DLListBase.H:286
void push_front(link *item)
Add at front of list.
Definition: DLListBase.C:27
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 iterator & end()
End of list for iterators.
Definition: DLListBaseI.H:73
link * front()
Return first entry.
Definition: DLListBaseI.H:96
void push_back(link *item)
Add at back of list.
Definition: DLListBase.C:52
bool empty() const noexcept
True if the list is empty.
Definition: DLListBase.H:189
bool swapUp(link *item)
Swap this element with the one above unless it is at the top.
Definition: DLListBase.C:77
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
bool good() const noexcept
Pointing at a valid storage node.
Definition: DLListBaseI.H:230
Includes some standard C++ headers, defines global macros and templates used in multiple places by Op...
void clear()
Clear the list.
Definition: DLListBaseI.H:147
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
Namespace for OpenFOAM.
void swap(DLListBase &lst)
Swap the contents of the list.
Definition: DLListBaseI.H:155