LList.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-2021 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::LList
29 
30 Description
31  Template class for non-intrusive linked lists.
32 
33 SourceFiles
34  LList.C
35  LListIO.C
36 
37 \*---------------------------------------------------------------------------*/
38 
39 #ifndef LList_H
40 #define LList_H
41 
42 #include "label.H"
43 #include <initializer_list>
44 
45 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
46 
47 namespace Foam
48 {
49 
50 // Forward Declarations
51 
52 class Istream;
53 class Ostream;
54 
55 template<class LListBase, class T> class LList;
56 
57 template<class LListBase, class T>
58 Istream& operator>>
59 (
60  Istream& is,
62 );
63 
64 template<class LListBase, class T>
65 Ostream& operator<<
66 (
67  Ostream& os,
68  const LList<LListBase, T>& lst
69 );
70 
71 
72 /*---------------------------------------------------------------------------*\
73  Class LList Declaration
74 \*---------------------------------------------------------------------------*/
75 
76 template<class LListBase, class T>
77 class LList
78 :
79  public LListBase
80 {
81 public:
82 
83  // STL type definitions
84 
85  //- Type of values stored.
86  typedef T value_type;
87 
88  //- Pointer for value_type
89  typedef T* pointer;
90 
91  //- Const pointer for value_type
92  typedef const T* const_pointer;
93 
94  //- Reference for value_type
95  typedef T& reference;
96 
97  //- Const reference for value_type
98  typedef const T& const_reference;
99 
100  //- The type that can represent the container size
101  typedef label size_type;
102 
103  //- The difference between iterator objects
104  typedef label difference_type;
105 
106 
107  // Forward declaration of STL iterators
108 
109  class iterator;
110  class const_iterator;
111 
112  using base_iterator = typename LListBase::iterator;
113  using const_base_iterator = typename LListBase::const_iterator;
114 
115 
116  //- The storage of T with linked nodes
117  struct link
118  :
119  public LListBase::link
120  {
121  //- Stored object
122  T obj_;
123 
124  //- Copy construct from given object
125  link(const T& obj)
126  :
127  obj_(obj)
128  {}
129 
130  //- Move construct from given object
131  link(T&& obj)
132  :
133  obj_(std::move(obj))
134  {}
135 
136 
137  //- Dereference LListBase::link to obtain address of stored object
138  static constexpr T* ptr(typename LListBase::link* node)
139  {
140  return &(static_cast<link*>(node)->obj_);
141  }
142 
143  //- Dereference LListBase::link to obtain address of stored object
144  static constexpr const T* ptr(const typename LListBase::link* node)
145  {
146  return &(static_cast<const link*>(node)->obj_);
147  }
148 
149  //- Dereference LListBase::link to obtain the stored object
150  static constexpr T& ref(typename LListBase::link* node)
151  {
152  return static_cast<link*>(node)->obj_;
153  }
154 
155  //- Dereference LListBase::link to obtain the stored object
156  static constexpr const T& ref(const typename LListBase::link* node)
157  {
158  return static_cast<const link*>(node)->obj_;
159  }
160  };
161 
162 
163  // Constructors
164 
165  //- Default construct
166  LList() = default;
167 
168  //- Construct and copy insert the initial T item
169  explicit LList(const T& item)
170  {
171  this->insert(item);
172  }
173 
174  //- Construct and move insert the initial T item
175  explicit LList(T&& item)
176  {
177  this->insert(std::move(item));
178  }
179 
180  //- Construct from Istream
181  explicit LList(Istream& is);
182 
183  //- Copy construct
184  LList(const LList<LListBase, T>& lst);
185 
186  //- Move construct
187  LList(LList<LListBase, T>&& lst);
188 
189  //- Copy construct from an initializer list
190  LList(std::initializer_list<T> lst);
191 
192 
193  //- Destructor
194  ~LList();
195 
196 
197  // Member Functions
198 
199  //- The first entry in the list
200  reference first()
201  {
202  return link::ref(LListBase::first());
203  }
204 
205  //- The first entry in the list (const access)
206  const_reference first() const
207  {
208  return link::ref(LListBase::first());
209  }
210 
211  //- The last entry in the list
212  reference last()
213  {
214  return link::ref(LListBase::last());
215  }
216 
217  //- The last entry in the list (const access)
218  const_reference last() const
219  {
220  return link::ref(LListBase::last());
221  }
222 
223 
224  //- Add copy at head of list
225  void insert(const T& item)
226  {
227  LListBase::insert(new link(item));
228  }
229 
230  //- Move construct at head of list
231  void insert(T&& item)
232  {
233  LListBase::insert(new link(std::move(item)));
234  }
235 
236 
237  //- Add copy at tail of list
238  void append(const T& item)
239  {
240  LListBase::append(new link(item));
241  }
242 
243  //- Move construct at tail of list
244  void append(T&& item)
245  {
246  LListBase::append(new link(std::move(item)));
247  }
248 
249 
250  //- Remove and return head
251  T removeHead()
252  {
253  auto p = LListBase::removeHead();
254  T obj(std::move(link::ref(p)));
255  delete p;
256  return obj;
257  }
258 
259  //- Remove and return element
260  T remove(link* item)
261  {
262  auto p = LListBase::remove(item);
263  T obj(std::move(link::ref(p)));
264  delete p;
265  return obj;
266  }
267 
268  //- Remove and return element specified by iterator
269  T remove(iterator& iter)
270  {
271  auto p = LListBase::remove(iter);
272  T obj(std::move(link::ref(p)));
273  delete p;
274  return obj;
275  }
276 
277 
278  //- Delete contents of list
279  void clear();
280 
281  //- Transfer the contents of the argument into this List
282  // and annul the argument list.
283  void transfer(LList<LListBase, T>& lst);
284 
285 
286  // Member Operators
287 
288  //- Copy assignment
289  void operator=(const LList<LListBase, T>& lst);
290 
291  //- Move assignment
292  void operator=(LList<LListBase, T>&& lst);
293 
294  //- Copy assignment from an initializer list
295  void operator=(std::initializer_list<T> lst);
296 
297 
298  // IOstream Operators
299 
300  //- Read list from Istream
301  Istream& readList(Istream& is);
302 
303  //- Write LList with line-breaks when length exceeds shortLen.
304  // Using '0' suppresses line-breaks entirely.
305  Ostream& writeList(Ostream& os, const label shortLen=0) const;
306 
307  //- Read list from Istream
308  friend Istream& operator>> <LListBase, T>
309  (
310  Istream&,
312  );
313 
314  //- Write LList to Ostream with line breaks,
315  //- as per writeList with shortLen=-1
316  friend Ostream& operator<< <LListBase, T>
317  (
318  Ostream& os,
319  const LList<LListBase, T>& lst
320  );
321 
322 
323  // STL iterator
324 
325  //- An STL-conforming iterator
326  class iterator
327  :
328  public base_iterator
329  {
330  public:
331 
332  //- Construct from base iterator
333  iterator(base_iterator iter)
334  :
335  base_iterator(iter)
336  {}
337 
338  reference operator*() const
339  {
340  return link::ref(this->get_node());
341  }
342 
343  pointer operator->() const
344  {
345  return link::ptr(this->get_node());
346  }
347 
348  reference operator()() const
349  {
350  return operator*();
351  }
352 
354  {
355  this->next();
356  return *this;
357  }
358 
360  {
361  this->prev(); // May not be implemented
362  return *this;
363  }
364  };
365 
366 
367  // STL const_iterator
368 
369  //- An STL-conforming const_iterator
370  class const_iterator
371  :
372  public const_base_iterator
373  {
374  public:
375 
376  //- Construct from base iterator
378  :
379  const_base_iterator(iter)
380  {}
381 
382  //- Construct from base iterator
384  :
385  const_base_iterator(iter)
386  {}
387 
388  const_reference operator*() const
389  {
390  return link::ref(this->get_node());
391  }
392 
393  const_pointer operator->() const
394  {
395  return link::ptr(this->get_node());
396  }
397 
399  {
400  return operator*();
401  }
402 
404  {
405  this->next();
406  return *this;
407  }
408 
410  {
411  this->prev(); // May not be implemented
412  return *this;
413  }
414  };
415 
416 
417  // STL reverse_iterator
418 
419  //- A reverse_iterator, for LListBase classes that support
420  //- reverse iteration
421  class reverse_iterator
422  :
423  public base_iterator
424  {
425  public:
426 
427  //- Construct from base iterator
429  :
430  base_iterator(iter)
431  {}
432 
433  reference operator*() const
434  {
435  return link::ref(this->get_node());
436  }
437 
438  pointer operator->() const
439  {
440  return link::ptr(this->get_node());
441  }
442 
444  {
445  this->prev(); // Only if base iterator is bidirectional
446  return *this;
447  }
448 
450  {
451  this->next();
452  return *this;
453  }
454  };
455 
456 
457  // STL const_reverse_iterator
458 
459  //- A const_reverse_iterator, for LListBase classes that support
460  //- reverse iteration
462  :
463  public const_base_iterator
464  {
465  public:
466 
467  //- Construct from base iterator
469  :
470  const_base_iterator(iter)
471  {}
472 
473  const_reference operator*() const
474  {
475  return link::ref(this->get_node());
476  }
477 
478  const_pointer operator->() const
479  {
480  return link::ptr(this->get_node());
481  }
482 
484  {
485  this->prev(); // Only if base iterator is bidirectional
486  return *this;
487  }
488 
490  {
491  this->next();
492  return *this;
493  }
494  };
495 
496 
497  //- Iterator to first item in list with non-const access
498  inline iterator begin()
499  {
500  return LListBase::template iterator_first<base_iterator>();
501  }
502 
503  //- Iterator to first item in list with const access
504  inline const_iterator cbegin() const
505  {
506  return LListBase::template iterator_first<const_base_iterator>();
507  }
508 
509  //- Iterator to last item in list with non-const access
510  inline reverse_iterator rbegin()
511  {
512  return LListBase::template iterator_last<base_iterator>();
513  }
514 
515  //- Iterator to last item in list with const access
516  inline const_reverse_iterator crbegin() const
517  {
518  return LListBase::template iterator_last<const_base_iterator>();
519  }
520 
521  //- Iterator to first item in list with const access
522  inline const_iterator begin() const
523  {
524  return LListBase::cbegin();
525  }
526 
527  //- Iterator to last item in list with const access
528  inline const_reverse_iterator rbegin() const
529  {
530  return crbegin();
531  }
532 
533 
534  //- End of list for forward iterators
535  inline const iterator& end()
536  {
537  return LListBase::template iterator_end<iterator>();
538  }
539 
540  //- End of list for forward iterators
541  inline const const_iterator& cend() const
542  {
543  return LListBase::template iterator_end<const_iterator>();
544  }
545 
546  //- End of list for reverse iterators
547  inline const reverse_iterator& rend()
548  {
549  return LListBase::template iterator_rend<reverse_iterator>();
550  }
551 
552  //- End of list for reverse iterators
553  inline const const_reverse_iterator& crend() const
554  {
555  return LListBase::template iterator_rend<const_reverse_iterator>();
556  }
557 
558  //- End of list for forward iterators
559  inline const const_iterator& end() const
560  {
561  return cend();
562  }
563 
564  //- End of list for reverse iterators
565  inline const const_reverse_iterator& rend() const
566  {
567  return crend();
568  }
569 
570 };
571 
572 
573 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
574 
575 } // End namespace Foam
576 
577 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
578 
579 #ifdef NoRepository
580  #include "LList.C"
581  #include "LListIO.C"
582 #endif
583 
584 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
585 
586 #endif
587 
588 // ************************************************************************* //
Foam::LList::append
void append(const T &item)
Add copy at tail of list.
Definition: LList.H:237
insert
srcOptions insert("case", fileName(rootDirSource/caseDirSource))
Foam::LList::remove
T remove(iterator &iter)
Remove and return element specified by iterator.
Definition: LList.H:268
p
volScalarField & p
Definition: createFieldRefs.H:8
Foam::LList::first
reference first()
The first entry in the list.
Definition: LList.H:199
Foam::LList::const_reference
const typedef T & const_reference
Const reference for value_type.
Definition: LList.H:97
Foam::LList::const_iterator::operator*
const_reference operator*() const
Definition: LList.H:387
Foam::LList::reverse_iterator
Definition: LList.H:420
LList.C
Foam::LList::rbegin
reverse_iterator rbegin()
Iterator to last item in list with non-const access.
Definition: LList.H:509
Foam::LList::reverse_iterator::operator*
reference operator*() const
Definition: LList.H:432
Foam::LList::reverse_iterator::operator->
pointer operator->() const
Definition: LList.H:437
Foam::LList::iterator::operator--
iterator & operator--()
Definition: LList.H:358
Foam::LList::const_reverse_iterator
Definition: LList.H:460
Foam::LList::iterator
An STL-conforming iterator.
Definition: LList.H:325
append
rAUs append(new volScalarField(IOobject::groupName("rAU", phase1.name()), 1.0/(U1Eqn.A()+byDt(max(phase1.residualAlpha() - alpha1, scalar(0)) *rho1))))
Foam::LList::end
const const_iterator & end() const
End of list for forward iterators.
Definition: LList.H:558
Foam::LList::const_reverse_iterator::operator++
const_reverse_iterator & operator++()
Definition: LList.H:482
Foam::LList::size_type
label size_type
The type that can represent the container size.
Definition: LList.H:100
Foam::LList::begin
const_iterator begin() const
Iterator to first item in list with const access.
Definition: LList.H:521
Foam::LList::clear
void clear()
Delete contents of list.
Definition: LList.C:77
Foam::LList::LList
LList(const T &item)
Construct and copy insert the initial T item.
Definition: LList.H:168
Foam::LList< Foam::chemPointISAT< CompType, ThermoType > * >::base_iterator
typename Foam::chemPointISAT< CompType, ThermoType > *::iterator base_iterator
Definition: LList.H:111
Foam::LList::rend
const const_reverse_iterator & rend() const
End of list for reverse iterators.
Definition: LList.H:564
Foam::LList::end
const iterator & end()
End of list for forward iterators.
Definition: LList.H:534
Foam::LList
Template class for non-intrusive linked lists.
Definition: LList.H:54
Foam::LList::const_iterator::operator--
const_iterator & operator--()
Definition: LList.H:408
Foam::LList< Foam::chemPointISAT< CompType, ThermoType > * >::const_base_iterator
typename Foam::chemPointISAT< CompType, ThermoType > *::const_iterator const_base_iterator
Definition: LList.H:112
Foam::LList::insert
void insert(T &&item)
Move construct at head of list.
Definition: LList.H:230
Foam::LList::iterator::operator*
reference operator*() const
Definition: LList.H:337
Foam::LList::~LList
~LList()
Destructor.
Definition: LList.C:68
Foam::Istream
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition: Istream.H:61
Foam::LList::operator=
void operator=(const LList< LListBase, T > &lst)
Copy assignment.
Definition: LList.C:100
Foam::LList::const_reverse_iterator::const_reverse_iterator
const_reverse_iterator(const_base_iterator iter)
Construct from base iterator.
Definition: LList.H:467
Foam::LList::begin
iterator begin()
Iterator to first item in list with non-const access.
Definition: LList.H:497
Foam::LList::iterator::operator->
pointer operator->() const
Definition: LList.H:342
Foam::T
void T(FieldField< Field, Type > &f1, const FieldField< Field, Type > &f2)
Definition: FieldFieldFunctions.C:58
Foam::LList::iterator::operator()
reference operator()() const
Definition: LList.H:347
Foam::LList::readList
Istream & readList(Istream &is)
Read list from Istream.
Definition: LListIO.C:45
Foam::LList::reference
T & reference
Reference for value_type.
Definition: LList.H:94
LListIO.C
Foam::LList::const_iterator::operator->
const_pointer operator->() const
Definition: LList.H:392
Foam::LList::const_reverse_iterator::operator--
const_reverse_iterator & operator--()
Definition: LList.H:488
os
OBJstream os(runTime.globalPath()/outputName)
Foam::LList::last
const_reference last() const
The last entry in the list (const access)
Definition: LList.H:217
Foam
Namespace for OpenFOAM.
Definition: atmBoundaryLayer.C:33
Foam::LList::LList
LList(T &&item)
Construct and move insert the initial T item.
Definition: LList.H:174
Foam::LList::reverse_iterator::operator--
reverse_iterator & operator--()
Definition: LList.H:448
Foam::LList::append
void append(T &&item)
Move construct at tail of list.
Definition: LList.H:243
Foam::LList::difference_type
label difference_type
The difference between iterator objects.
Definition: LList.H:103
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
Foam::LList::const_iterator::const_iterator
const_iterator(base_iterator iter)
Construct from base iterator.
Definition: LList.H:382
Foam::LList::cend
const const_iterator & cend() const
End of list for forward iterators.
Definition: LList.H:540
label.H
Foam::LList::crend
const const_reverse_iterator & crend() const
End of list for reverse iterators.
Definition: LList.H:552
Foam::LList::iterator::iterator
iterator(base_iterator iter)
Construct from base iterator.
Definition: LList.H:332
Foam::LList::rbegin
const_reverse_iterator rbegin() const
Iterator to last item in list with const access.
Definition: LList.H:527
Foam::LList::reverse_iterator::reverse_iterator
reverse_iterator(base_iterator iter)
Construct from base iterator.
Definition: LList.H:427
Foam::LList::crbegin
const_reverse_iterator crbegin() const
Iterator to last item in list with const access.
Definition: LList.H:515
Foam::LList::reverse_iterator::operator++
reverse_iterator & operator++()
Definition: LList.H:442
Foam::LList::const_reverse_iterator::operator->
const_pointer operator->() const
Definition: LList.H:477
Foam::LList::const_pointer
const typedef T * const_pointer
Const pointer for value_type.
Definition: LList.H:91
Foam::LList::last
reference last()
The last entry in the list.
Definition: LList.H:211
Foam::LList::pointer
T * pointer
Pointer for value_type.
Definition: LList.H:88
Foam::LList::cbegin
const_iterator cbegin() const
Iterator to first item in list with const access.
Definition: LList.H:503
Foam::LList::rend
const reverse_iterator & rend()
End of list for reverse iterators.
Definition: LList.H:546
Foam::LList::LList
LList()=default
Default construct.
Foam::LList::transfer
void transfer(LList< LListBase, T > &lst)
Transfer the contents of the argument into this List.
Definition: LList.C:90
Foam::Ostream
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:56
Foam::LList::iterator::operator++
iterator & operator++()
Definition: LList.H:352
Foam::LList::first
const_reference first() const
The first entry in the list (const access)
Definition: LList.H:205
Foam::LList::const_iterator::const_iterator
const_iterator(const_base_iterator iter)
Construct from base iterator.
Definition: LList.H:376
Foam::LList::const_reverse_iterator::operator*
const_reference operator*() const
Definition: LList.H:472
Foam::LList::insert
void insert(const T &item)
Add copy at head of list.
Definition: LList.H:224
Foam::LList::const_iterator
An STL-conforming const_iterator.
Definition: LList.H:369
Foam::LList::remove
T remove(link *item)
Remove and return element.
Definition: LList.H:259
Foam::LList::value_type
T value_type
Type of values stored.
Definition: LList.H:85
Foam::LList::removeHead
T removeHead()
Remove and return head.
Definition: LList.H:250
Foam::LList::const_iterator::operator()
const_reference operator()() const
Definition: LList.H:397
Foam::LList::const_iterator::operator++
const_iterator & operator++()
Definition: LList.H:402
Foam::LList::writeList
Ostream & writeList(Ostream &os, const label shortLen=0) const
Write LList with line-breaks when length exceeds shortLen.
Definition: LListIO.C:123