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