SLListBase.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::SLListBase
29 
30 Description
31  Base for singly-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  SLListBaseI.H
42  SLListBase.C
43 
44 \*---------------------------------------------------------------------------*/
45 
46 #ifndef Foam_SLListBase_H
47 #define Foam_SLListBase_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 SLListBase Declaration
60 \*---------------------------------------------------------------------------*/
61 
62 class SLListBase
63 {
64 public:
65 
66  //- The structure for a singly-linked storage node
67  struct link
68  {
69  //- Pointer to next entry in list
70  link* next_ = nullptr;
71 
72  //- Default construct
73  link() noexcept = default;
74 
75  //- Deregister the node (after removal)
76  void deregister() noexcept { next_ = nullptr; }
77  };
78 
79 
80 private:
81 
82  // Private Data
83 
84  //- A pointer to the last element.
85  // last_->next_ points to first element, i.e. circular storage
86  link* last_ = nullptr;
87 
88  //- Number of elements in the list
89  label size_ = 0;
90 
91 
92 protected:
93 
94  // Protected Member Functions
95 
96  //- Factory method to return an iterator end
97  // Simply reinterprets a NullObject as a SLListBase iterator.
98  template<class IteratorType>
99  inline static const IteratorType& iterator_end();
100 
101  //- Factory method to return an iterator rend
102  // Deleted for SLListBase
103  template<class IteratorType>
104  static const IteratorType& iterator_rend() = delete;
105 
106  //- Return iterator to first item or end-iterator if list is empty
107  // Removes constness which the caller promises to manage.
108  template<class IteratorType>
109  inline IteratorType iterator_first() const;
110 
111  //- Return iterator to last item or end-iterator if list is empty
112  // Removes constness which the caller promises to manage.
113  template<class IteratorType>
114  inline IteratorType iterator_last() const;
115 
116 public:
117 
118  // Forward Declarations (iterators)
119 
120  class iterator;
121  friend class iterator;
122 
123  class const_iterator;
124  friend class const_iterator;
125 
126 
127  // Generated Methods
128 
129  //- Default construct
130  SLListBase() = default;
131 
132  //- No copy construct
133  SLListBase(const SLListBase&) = delete;
134 
135  //- No copy assignment
136  void operator=(const SLListBase&) = delete;
137 
138  //- Destructor
139  ~SLListBase() = default;
141 
142  // Member Functions
144  //- True if the list is empty
145  bool empty() const noexcept { return !size_; }
146 
147  //- The number of elements in list
148  label size() const noexcept { return size_; }
149 
150  //- Return first entry
151  inline link* front();
152 
153  //- Return const access to first entry
154  inline const link* front() const;
155 
156  //- Return last entry
157  inline link* back();
158 
159  //- Return const access to last entry
160  inline const link* back() const;
161 
162 
163  //- Add at front of list
164  void push_front(link* item);
165 
166  //- Add at back of list
167  void push_back(link* item);
168 
169  //- Remove and return first entry
170  link* removeHead();
171 
172  // Remove and return element
173  link* remove(link* item);
174 
175  // Remove and return element specified by iterator
176  inline link* remove(iterator& iter);
177 
178  //- Clear the list
179  inline void clear();
181  //- Swap the contents of list
182  inline void swap(SLListBase& lst);
183 
184  //- Transfer the contents of the argument into this list
185  //- and annul the argument list.
186  inline void transfer(SLListBase& lst);
187 
188 
189  // iterator
190 
191  //- A primitive non-const node iterator.
192  // Must normally be extended by inheriting classes.
193  class iterator
194  {
195  friend class SLListBase;
196  friend class const_iterator;
197 
198  //- The selected node.
199  // MUST be the first member for easy comparison between iterators
200  // and for reinterpret_cast from nullObject
201  link* node_;
202 
203  //- The list being iterated on (as pointer for bitwise copy)
204  SLListBase* list_;
205 
206  //- Copy of the node next pointer (to use after removal)
207  link copy_;
208 
209  public:
210 
211  //- Copy construct
212  iterator(const iterator&) = default;
213 
214  //- Construct for a node on the list
215  inline iterator(SLListBase* list, link* item);
216 
217  //- The storage node
218  inline link* get_node() const noexcept;
219 
220  //- Pointing at a valid storage node
221  inline bool good() const noexcept;
222 
223  //- Cannot move backward through list
224  inline void prev() = delete;
225 
226  //- Move forward through list
227  inline void next();
228 
229  //- Copy assignment
230  inline void operator=(const iterator& iter);
231 
232  inline bool operator==(const iterator& iter) const;
233  inline bool operator!=(const iterator& iter) const;
234  };
235 
236 
237  // STL const_iterator
238 
239  //- A primitive const node iterator.
240  // Must normally be extended by inheriting classes.
241  class const_iterator
242  {
243  //- The selected node.
244  // MUST be the first member for easy comparison between iterators
245  // and for reinterpret_cast from nullObject
246  const link* node_;
247 
248  //- The list being iterated on (as pointer for bitwise copy)
249  const SLListBase* list_;
251  public:
252 
253  //- Copy construct
254  const_iterator(const const_iterator&) = default;
255 
256  //- Construct for a node on the list
257  inline const_iterator(const SLListBase* list, const link* item);
258 
259  //- Construct from a non-const iterator
260  inline const_iterator(const SLListBase::iterator& iter);
261 
262  //- The storage node
263  inline const link* get_node() const noexcept;
264 
265  //- Pointing at a valid storage node
266  inline bool good() const noexcept;
267 
268  //- Cannot move backward through list
269  inline void prev() = delete;
270 
271  //- Move forward through list
272  inline void next();
273 
274  //- Copy assignment
275  const_iterator& operator=(const const_iterator&) = default;
276 
277  inline bool operator==(const const_iterator& iter) const;
278  inline bool operator!=(const const_iterator& iter) const;
279  };
280 
281 
282  //- Iterator to first item in list with non-const access
283  inline iterator begin();
284 
285  //- Iterator to first item in list with const access
286  inline const_iterator cbegin() const;
287 
288  //- No reverse iteration
289  const_iterator crbegin() const = delete;
290 
291  //- End of list for iterators
292  inline const iterator& end();
293 
294  //- End of list for iterators
295  inline const const_iterator& cend() const;
296 
297  //- No reverse iteration
298  const const_iterator& crend() const = delete;
299 
300 
301  // Housekeeping
302 
303  //- Return first entry
304  //FOAM_DEPRECATED_FOR(2022-10, "front()")
305  link* first() { return front(); }
306 
307  //- Return const access to first entry
308  //FOAM_DEPRECATED_FOR(2022-10, "front()")
309  const link* first() const { return front(); }
310 
311  //- Return last entry
312  //FOAM_DEPRECATED_FOR(2022-10, "back()")
313  link* last() { return back(); }
314 
315  //- Return const access to last entry
316  //FOAM_DEPRECATED_FOR(2022-10, "back()")
317  const link* last() const { return back(); }
318 
319  //- Add at front of list
320  //FOAM_DEPRECATED_FOR(2022-10, "push_front()")
321  void prepend(link* item) { push_front(item); }
322 
323  //- Add at back of list
324  //FOAM_DEPRECATED_FOR(2022-10, "push_back()")
325  void append(link* item) { push_back(item); }
326 };
327 
328 
329 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
330 
331 } // End namespace Foam
332 
333 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
334 
335 #include "SLListBaseI.H"
336 
337 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
338 
339 #endif
340 
341 // ************************************************************************* //
link * get_node() const noexcept
The storage node.
Definition: SLListBaseI.H:180
void swap(SLListBase &lst)
Swap the contents of list.
Definition: SLListBaseI.H:125
bool empty() const noexcept
True if the list is empty.
Definition: SLListBase.H:175
link * first()
Return first entry.
Definition: SLListBase.H:420
const const_iterator & crend() const =delete
No reverse iteration.
SLListBase()=default
Default construct.
Base for singly-linked lists.
Definition: SLListBase.H:57
void clear()
Clear the list.
Definition: SLListBaseI.H:118
A primitive const node iterator.
Definition: SLListBase.H:320
const_iterator cbegin() const
Iterator to first item in list with const access.
Definition: SLListBaseI.H:326
link * back()
Return last entry.
Definition: SLListBaseI.H:93
iterator(const iterator &)=default
Copy construct.
const const_iterator & cend() const
End of list for iterators.
Definition: SLListBaseI.H:249
bool good() const noexcept
Pointing at a valid storage node.
Definition: SLListBaseI.H:186
label size() const noexcept
The number of elements in list.
Definition: SLListBase.H:180
void prev()=delete
Cannot move backward through list.
friend class const_iterator
Definition: SLListBase.H:251
link * front()
Return first entry.
Definition: SLListBaseI.H:67
link * last()
Return last entry.
Definition: SLListBase.H:434
const direction noexcept
Definition: Scalar.H:258
const_iterator crbegin() const =delete
No reverse iteration.
const iterator & end()
End of list for iterators.
Definition: SLListBaseI.H:242
IteratorType iterator_last() const
Return iterator to last item or end-iterator if list is empty.
Definition: SLListBaseI.H:50
void push_front(link *item)
Add at front of list.
Definition: SLListBase.C:27
iterator begin()
Iterator to first item in list with non-const access.
Definition: SLListBaseI.H:230
void append(link *item)
Add at back of list.
Definition: SLListBase.H:455
void operator=(const SLListBase &)=delete
No copy assignment.
~SLListBase()=default
Destructor.
Includes some standard C++ headers, defines global macros and templates used in multiple places by Op...
void prepend(link *item)
Add at front of list.
Definition: SLListBase.H:448
link * removeHead()
Remove and return first entry.
Definition: SLListBase.C:70
static const IteratorType & iterator_rend()=delete
Factory method to return an iterator rend.
void transfer(SLListBase &lst)
Transfer the contents of the argument into this list and annul the argument list. ...
Definition: SLListBaseI.H:137
void push_back(link *item)
Add at back of list.
Definition: SLListBase.C:49
A primitive non-const node iterator.
Definition: SLListBase.H:248
IteratorType iterator_first() const
Return iterator to first item or end-iterator if list is empty.
Definition: SLListBaseI.H:35
Namespace for OpenFOAM.
static const IteratorType & iterator_end()
Factory method to return an iterator end.
Definition: SLListBaseI.H:28
void next()
Move forward through list.
Definition: SLListBaseI.H:192