UILList.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::UILList
29 
30 Description
31  Template class for intrusive linked lists.
32 
33 SourceFiles
34  UILList.C
35  UILListIO.C
36 
37 \*---------------------------------------------------------------------------*/
38 
39 #ifndef Foam_UILList_H
40 #define Foam_UILList_H
41 
42 #include "label.H"
43 #include "uLabel.H"
44 #include "stdFoam.H"
45 
46 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
47 
48 namespace Foam
49 {
50 
51 // Forward Declarations
52 template<class LListBase, class T> class UILList;
53 
54 template<class LListBase, class T>
55 Ostream& operator<<
56 (
57  Ostream& os,
58  const UILList<LListBase, T>& lst
59 );
60 
61 
62 /*---------------------------------------------------------------------------*\
63  Class UILList Declaration
64 \*---------------------------------------------------------------------------*/
65 
66 template<class LListBase, class T>
67 class UILList
68 :
69  public LListBase
70 {
71 public:
72 
73  // STL type definitions
74 
75  //- Type of values stored
76  typedef T value_type;
77 
78  //- Pointer for value_type
79  typedef T* pointer;
80 
81  //- Const pointer for value_type
82  typedef const T* const_pointer;
83 
84  //- Reference for value_type
85  typedef T& reference;
86 
87  //- Const reference for value_type
88  typedef const T& const_reference;
89 
90  //- The type that can represent the container size
91  typedef label size_type;
92 
93  //- The difference between iterator objects
94  typedef label difference_type;
95 
96 
97  // Forward Declarations (iterators)
98 
99  class iterator;
100  class const_iterator;
101 
102  using base_iterator = typename LListBase::iterator;
103  using const_base_iterator = typename LListBase::const_iterator;
104 
105 
106  // Constructors
107 
108  //- Default construct
109  UILList() = default;
110 
111  //- Construct and add initial item pointer
112  explicit UILList(T* item)
113  {
114  this->push_front(item);
115  }
116 
117  //- Construct as copy
118  UILList(const UILList<LListBase, T>& list);
119 
120 
121  // Member Functions
122 
123  //- The first entry in the list
124  T* front()
125  {
126  return static_cast<T*>(LListBase::front());
127  }
128 
129  //- The first entry in the list (const access)
130  const T* front() const
131  {
132  return static_cast<const T*>(LListBase::front());
133  }
134 
135  //- The last entry in the list
136  T* back()
137  {
138  return static_cast<T*>(LListBase::back());
139  }
140 
141  //- The last entry in the list (const access)
142  const T* back() const
143  {
144  return static_cast<const T*>(LListBase::back());
145  }
146 
147 
148  //- Remove and return head
150  {
151  return static_cast<T*>(LListBase::removeHead());
152  }
153 
154  //- Remove and return element
155  T* remove(T* item)
156  {
157  return static_cast<T*>(LListBase::remove(item));
158  }
159 
160  //- Remove and return item specified by iterator
161  T* remove(iterator& iter)
162  {
163  return static_cast<T*>(LListBase::remove(iter));
164  }
166 
167  // Member Operators
168 
169  //- Copy assignment
170  void operator=(const UILList<LListBase, T>& lst);
171 
172  //- Equality. True both lists are element-wise equal
173  // (using value_type::operator==). Takes linear time.
174  bool operator==(const UILList<LListBase, T>& lst) const;
175 
176  //- The opposite of the equality operation. Takes linear time.
177  bool operator!=(const UILList<LListBase, T>& lst) const;
178 
179 
180  // IOstream Operators
181 
182  //- Write UILList with line-breaks when length exceeds shortLen.
183  // Using '0' suppresses line-breaks entirely.
184  Ostream& writeList(Ostream& os, const label shortLen=0) const;
185 
186  //- Write UILList to Ostream with line breaks,
187  //- as per writeList() with shortLen=-1
188  friend Ostream& operator<< <LListBase, T>
189  (
191  const UILList<LListBase, T>& lst
192  );
193 
194 
195  // STL iterator
196 
197  //- A non-const iterator
198  class iterator
199  :
200  public base_iterator
201  {
202  public:
203 
204  iterator(base_iterator iter)
205  :
206  base_iterator(iter)
207  {}
208 
209  //- Return the address of the object being referenced
210  pointer get() const
211  {
212  return static_cast<T*>(base_iterator::get_node());
213  }
214 
215  reference operator*() const
216  {
217  return *(this->get());
218  }
219 
220  pointer operator->() const
221  {
222  return this->get();
223  }
224 
225  reference operator()() const
226  {
227  return operator*();
228  }
229 
231  {
232  this->next();
233  return *this;
234  }
235  };
236 
237 
238  // STL const_iterator
239 
240  //- A const_iterator
242  :
243  public const_base_iterator
244  {
245  public:
246 
247  //- Construct from base const_iterator
249  :
250  const_base_iterator(iter)
251  {}
252 
253  //- Construct from base iterator
255  :
256  const_base_iterator(iter)
257  {}
258 
259  //- Return the address of the object being referenced
260  const_pointer get() const
261  {
262  return static_cast<const T*>(const_base_iterator::get_node());
263  }
264 
266  {
267  return *(this->get());
268  }
269 
270  const_pointer operator->() const
271  {
272  return this->get();
273  }
274 
275  const_reference operator()() const
276  {
277  return operator*();
278  }
279 
281  {
282  this->next();
283  return *this;
284  }
285  };
286 
287 
288  // STL reverse_iterator
289 
290  //- A reverse_iterator, for LListBase classes that support
291  //- reverse iteration
292  class reverse_iterator
293  :
294  public base_iterator
295  {
296  public:
299  :
300  base_iterator(iter)
301  {}
302 
303  //- Return the address of the object being referenced
304  pointer get() const
305  {
306  return static_cast<T*>(base_iterator::get_node());
307  }
308 
309  reference operator*() const
310  {
311  return *(this->get());
312  }
314  pointer operator->() const
315  {
316  return this->get();
317  }
319  reference operator()() const
320  {
321  return operator*();
322  }
325  {
326  this->prev(); // Only if base iterator is bidirectional
327  return *this;
328  }
329  };
330 
331 
332  // STL const_reverse_iterator
334  //- A const_reverse_iterator, for LListBase classes that support
335  //- reverse iteration
337  :
338  public const_base_iterator
339  {
340  public:
341 
343  :
344  const_base_iterator(iter)
345  {}
346 
347  //- Return the address of the object being referenced
348  const_pointer get() const
349  {
350  return static_cast<const T*>(const_base_iterator::get_node());
351  }
352 
354  {
355  return *(this->get());
356  }
357 
358  const_pointer operator->() const
359  {
360  return this->get();
361  }
362 
363  const_reference operator()() const
364  {
365  return operator*();
366  }
367 
369  {
370  this->prev(); // Only if base iterator is bidirectional
371  return *this;
372  }
373  };
374 
375 
376  //- Iterator to first item in list with non-const access
377  inline iterator begin()
378  {
379  return LListBase::template iterator_first<base_iterator>();
380  }
382  //- Iterator to first item in list with const access
383  inline const_iterator cbegin() const
384  {
385  return LListBase::template iterator_first<const_base_iterator>();
386  }
387 
388  //- Iterator to last item in list with non-const access
389  inline reverse_iterator rbegin()
390  {
391  return LListBase::template iterator_last<base_iterator>();
392  }
393 
394  //- Iterator to last item in list with const access
396  {
397  return LListBase::template iterator_last<const_base_iterator>();
398  }
399 
400  //- Iterator to first item in list with const access
401  inline const_iterator begin() const
402  {
403  return LListBase::cbegin();
404  }
405 
406  //- Iterator to last item in list with const access
407  inline const_reverse_iterator rbegin() const
408  {
409  return crbegin();
410  }
411 
412 
413  //- End of list for forward iterators
414  inline const iterator& end()
415  {
416  return LListBase::template iterator_end<iterator>();
417  }
418 
419  //- End of list for forward iterators
420  inline const const_iterator& cend() const
421  {
422  return LListBase::template iterator_end<const_iterator>();
423  }
425  //- End of list for reverse iterators
426  inline const reverse_iterator& rend()
427  {
428  return LListBase::template iterator_rend<reverse_iterator>();
429  }
430 
431  //- End of list for reverse iterators
432  inline const const_reverse_iterator& crend() const
433  {
434  return LListBase::template iterator_rend<const_reverse_iterator>();
435  }
436 
437  //- End of list for forward iterators
438  inline const const_iterator& end() const
439  {
440  return cend();
441  }
442 
443  //- End of list for reverse iterators
444  inline const const_reverse_iterator& rend() const
445  {
446  return crend();
447  }
449 
450  // Housekeeping
451 
452  //- The first entry in the list
453  //FOAM_DEPRECATED_FOR(2022-10, "front()")
454  T* first() { return front(); }
455 
456  //- The first entry in the list (const access)
457  //FOAM_DEPRECATED_FOR(2022-10, "front()")
458  const T* first() const { return front(); }
459 
460  //- The last entry in the list
461  //FOAM_DEPRECATED_FOR(2022-10, "back()")
462  T* last() { return back(); }
463 
464  //- The last entry in the list (const access)
465  //FOAM_DEPRECATED_FOR(2022-10, "back()")
466  const T* last() const { return back(); }
467 };
468 
469 
470 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
471 
472 } // End namespace Foam
473 
474 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
475 
476 #ifdef NoRepository
477  #include "UILList.C"
478  #include "UILListIO.C"
479 #endif
481 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
482 
483 #endif
484 
485 // ************************************************************************* //
void operator=(const UILList< LListBase, T > &lst)
Copy assignment.
Definition: UILList.C:39
const const_iterator & cend() const
End of list for forward iterators.
Definition: UILList.H:497
Template class for intrusive linked lists.
Definition: UILList.H:47
bool operator==(const UILList< LListBase, T > &lst) const
Equality. True both lists are element-wise equal.
Definition: UILList.C:52
const_iterator(const_base_iterator iter)
Construct from base const_iterator.
Definition: UILList.H:297
const T & const_reference
Const reference for value_type.
Definition: UILList.H:93
A reverse_iterator, for LListBase classes that support reverse iteration.
Definition: UILList.H:347
T * front()
The first entry in the list.
Definition: UILList.H:141
const T * const_pointer
Const pointer for value_type.
Definition: UILList.H:83
iterator & operator++()
Definition: UILList.H:275
pointer operator->() const
Definition: UILList.H:371
iterator(base_iterator iter)
Definition: UILList.H:247
UILList()=default
Default construct.
T * last()
The last entry in the list.
Definition: UILList.H:556
reverse_iterator & operator++()
Definition: UILList.H:381
reference operator()() const
Definition: UILList.H:270
A const_iterator.
Definition: UILList.H:288
bool operator!=(const UILList< LListBase, T > &lst) const
The opposite of the equality operation. Takes linear time.
Definition: UILList.C:77
const_reference operator*() const
Definition: UILList.H:318
reference operator()() const
Definition: UILList.H:376
const_iterator cbegin() const
Iterator to first item in list with const access.
Definition: UILList.H:448
const_reference operator()() const
Definition: UILList.H:424
const_iterator & operator++()
Definition: UILList.H:333
reference operator*() const
Definition: UILList.H:366
const_reference operator*() const
Definition: UILList.H:414
reverse_iterator rbegin()
Iterator to last item in list with non-const access.
Definition: UILList.H:456
A non-const iterator.
Definition: UILList.H:241
const iterator & end()
End of list for forward iterators.
Definition: UILList.H:489
reference operator*() const
Definition: UILList.H:260
const_pointer operator->() const
Definition: UILList.H:419
const reverse_iterator & rend()
End of list for reverse iterators.
Definition: UILList.H:505
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:56
T * pointer
Pointer for value_type.
Definition: UILList.H:78
const_reverse_iterator(const_base_iterator iter)
Definition: UILList.H:401
label difference_type
The difference between iterator objects.
Definition: UILList.H:103
typename LListBase::const_iterator const_base_iterator
Definition: UILList.H:112
const_reverse_iterator crbegin() const
Iterator to last item in list with const access.
Definition: UILList.H:464
OBJstream os(runTime.globalPath()/outputName)
T value_type
Type of values stored.
Definition: UILList.H:73
T & reference
Reference for value_type.
Definition: UILList.H:88
constexpr auto cbegin(const C &c) -> decltype(c.begin())
Return const_iterator to the beginning of the container c.
Definition: stdFoam.H:190
void T(FieldField< Field, Type > &f1, const FieldField< Field, Type > &f2)
A const_reverse_iterator, for LListBase classes that support reverse iteration.
Definition: UILList.H:395
const_pointer operator->() const
Definition: UILList.H:323
pointer operator->() const
Definition: UILList.H:265
typename LListBase::iterator base_iterator
Definition: UILList.H:111
label size_type
The type that can represent the container size.
Definition: UILList.H:98
const_reverse_iterator & operator++()
Definition: UILList.H:429
Ostream & writeList(Ostream &os, const label shortLen=0) const
Write UILList with line-breaks when length exceeds shortLen.
Definition: UILListIO.C:30
Includes some standard C++ headers, defines global macros and templates used in multiple places by Op...
reverse_iterator(base_iterator iter)
Definition: UILList.H:353
const_reference operator()() const
Definition: UILList.H:328
T * first()
The first entry in the list.
Definition: UILList.H:542
T * removeHead()
Remove and return head.
Definition: UILList.H:174
iterator begin()
Iterator to first item in list with non-const access.
Definition: UILList.H:440
const const_reverse_iterator & crend() const
End of list for reverse iterators.
Definition: UILList.H:513
Namespace for OpenFOAM.
T * back()
The last entry in the list.
Definition: UILList.H:157