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 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  //- Null 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  //- Write LList with line-breaks when length exceeds shortLen.
301  // Using '0' suppresses line-breaks entirely.
302  Ostream& writeList(Ostream& os, const label shortLen=0) const;
303 
304  //- Read list from Istream
305  friend Istream& operator>> <LListBase, T>
306  (
307  Istream&,
309  );
310 
311  //- Write LList to Ostream with line breaks,
312  //- as per writeList with shortLen=-1
313  friend Ostream& operator<< <LListBase, T>
314  (
315  Ostream& os,
316  const LList<LListBase, T>& lst
317  );
318 
319 
320  // STL iterator
321 
322  //- An STL-conforming iterator
323  class iterator
324  :
325  public base_iterator
326  {
327  public:
328 
329  //- Construct from base iterator
330  iterator(base_iterator iter)
331  :
332  base_iterator(iter)
333  {}
334 
335  reference operator*() const
336  {
337  return link::ref(this->get_node());
338  }
339 
340  pointer operator->() const
341  {
342  return link::ptr(this->get_node());
343  }
344 
345  reference operator()() const
346  {
347  return operator*();
348  }
349 
351  {
352  this->next();
353  return *this;
354  }
355 
357  {
358  this->prev(); // May not be implemented
359  return *this;
360  }
361  };
362 
363 
364  // STL const_iterator
365 
366  //- An STL-conforming const_iterator
367  class const_iterator
368  :
369  public const_base_iterator
370  {
371  public:
372 
373  //- Construct from base iterator
375  :
376  const_base_iterator(iter)
377  {}
378 
379  //- Construct from base iterator
381  :
382  const_base_iterator(iter)
383  {}
384 
385  const_reference operator*() const
386  {
387  return link::ref(this->get_node());
388  }
389 
390  const_pointer operator->() const
391  {
392  return link::ptr(this->get_node());
393  }
394 
396  {
397  return operator*();
398  }
399 
401  {
402  this->next();
403  return *this;
404  }
405 
407  {
408  this->prev(); // May not be implemented
409  return *this;
410  }
411  };
412 
413 
414  // STL reverse_iterator
415 
416  //- A reverse_iterator, for LListBase classes that support
417  //- reverse iteration
418  class reverse_iterator
419  :
420  public base_iterator
421  {
422  public:
423 
424  //- Construct from base iterator
426  :
427  base_iterator(iter)
428  {}
429 
430  reference operator*() const
431  {
432  return link::ref(this->get_node());
433  }
434 
435  pointer operator->() const
436  {
437  return link::ptr(this->get_node());
438  }
439 
441  {
442  this->prev(); // Only if base iterator is bidirectional
443  return *this;
444  }
445 
447  {
448  this->next();
449  return *this;
450  }
451  };
452 
453 
454  // STL const_reverse_iterator
455 
456  //- A const_reverse_iterator, for LListBase classes that support
457  //- reverse iteration
459  :
460  public const_base_iterator
461  {
462  public:
463 
464  //- Construct from base iterator
466  :
467  const_base_iterator(iter)
468  {}
469 
470  const_reference operator*() const
471  {
472  return link::ref(this->get_node());
473  }
474 
475  const_pointer operator->() const
476  {
477  return link::ptr(this->get_node());
478  }
479 
481  {
482  this->prev(); // Only if base iterator is bidirectional
483  return *this;
484  }
485 
487  {
488  this->next();
489  return *this;
490  }
491  };
492 
493 
494  //- Iterator to first item in list with non-const access
495  inline iterator begin()
496  {
497  return LListBase::template iterator_first<base_iterator>();
498  }
499 
500  //- Iterator to first item in list with const access
501  inline const_iterator cbegin() const
502  {
503  return LListBase::template iterator_first<const_base_iterator>();
504  }
505 
506  //- Iterator to last item in list with non-const access
507  inline reverse_iterator rbegin()
508  {
509  return LListBase::template iterator_last<base_iterator>();
510  }
511 
512  //- Iterator to last item in list with const access
513  inline const_reverse_iterator crbegin() const
514  {
515  return LListBase::template iterator_last<const_base_iterator>();
516  }
517 
518  //- Iterator to first item in list with const access
519  inline const_iterator begin() const
520  {
521  return LListBase::cbegin();
522  }
523 
524  //- Iterator to last item in list with const access
525  inline const_reverse_iterator rbegin() const
526  {
527  return crbegin();
528  }
529 
530 
531  //- End of list for forward iterators
532  inline const iterator& end()
533  {
534  return LListBase::template iterator_end<iterator>();
535  }
536 
537  //- End of list for forward iterators
538  inline const const_iterator& cend() const
539  {
540  return LListBase::template iterator_end<const_iterator>();
541  }
542 
543  //- End of list for reverse iterators
544  inline const reverse_iterator& rend()
545  {
546  return LListBase::template iterator_rend<reverse_iterator>();
547  }
548 
549  //- End of list for reverse iterators
550  inline const const_reverse_iterator& crend() const
551  {
552  return LListBase::template iterator_rend<const_reverse_iterator>();
553  }
554 
555  //- End of list for forward iterators
556  inline const const_iterator& end() const
557  {
558  return cend();
559  }
560 
561  //- End of list for reverse iterators
562  inline const const_reverse_iterator& rend() const
563  {
564  return crend();
565  }
566 
567 };
568 
569 
570 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
571 
572 } // End namespace Foam
573 
574 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
575 
576 #ifdef NoRepository
577  #include "LList.C"
578  #include "LListIO.C"
579 #endif
580 
581 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
582 
583 #endif
584 
585 // ************************************************************************* //
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:384
Foam::LList::reverse_iterator
Definition: LList.H:417
LList.C
Foam::LList::rbegin
reverse_iterator rbegin()
Iterator to last item in list with non-const access.
Definition: LList.H:506
Foam::LList::reverse_iterator::operator*
reference operator*() const
Definition: LList.H:429
Foam::LList::reverse_iterator::operator->
pointer operator->() const
Definition: LList.H:434
Foam::LList::iterator::operator--
iterator & operator--()
Definition: LList.H:355
Foam::LList::const_reverse_iterator
Definition: LList.H:457
Foam::LList::iterator
An STL-conforming iterator.
Definition: LList.H:322
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:555
Foam::LList::const_reverse_iterator::operator++
const_reverse_iterator & operator++()
Definition: LList.H:479
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:518
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::string >::base_iterator
typename Foam::string ::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:561
Foam::LList::end
const iterator & end()
End of list for forward iterators.
Definition: LList.H:531
Foam::LList
Template class for non-intrusive linked lists.
Definition: LList.H:54
Foam::LList::const_iterator::operator--
const_iterator & operator--()
Definition: LList.H:405
Foam::LList< Foam::string >::const_base_iterator
typename Foam::string ::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:334
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:464
Foam::LList::begin
iterator begin()
Iterator to first item in list with non-const access.
Definition: LList.H:494
Foam::LList::iterator::operator->
pointer operator->() const
Definition: LList.H:339
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:344
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:389
Foam::LList::const_reverse_iterator::operator--
const_reverse_iterator & operator--()
Definition: LList.H:485
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:445
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:379
Foam::LList::cend
const const_iterator & cend() const
End of list for forward iterators.
Definition: LList.H:537
label.H
Foam::LList::crend
const const_reverse_iterator & crend() const
End of list for reverse iterators.
Definition: LList.H:549
Foam::LList::iterator::iterator
iterator(base_iterator iter)
Construct from base iterator.
Definition: LList.H:329
Foam::LList::rbegin
const_reverse_iterator rbegin() const
Iterator to last item in list with const access.
Definition: LList.H:524
Foam::LList::reverse_iterator::reverse_iterator
reverse_iterator(base_iterator iter)
Construct from base iterator.
Definition: LList.H:424
Foam::LList::crbegin
const_reverse_iterator crbegin() const
Iterator to last item in list with const access.
Definition: LList.H:512
Foam::LList::reverse_iterator::operator++
reverse_iterator & operator++()
Definition: LList.H:439
Foam::LList::const_reverse_iterator::operator->
const_pointer operator->() const
Definition: LList.H:474
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:500
Foam::LList::rend
const reverse_iterator & rend()
End of list for reverse iterators.
Definition: LList.H:543
Foam::LList::LList
LList()=default
Null 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:349
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:373
Foam::LList::const_reverse_iterator::operator*
const_reference operator*() const
Definition: LList.H:469
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:366
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:394
Foam::LList::const_iterator::operator++
const_iterator & operator++()
Definition: LList.H:399
Foam::LList::writeList
Ostream & writeList(Ostream &os, const label shortLen=0) const
Write LList with line-breaks when length exceeds shortLen.
Definition: LListIO.C:134