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