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