LList.H
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration | Website: https://openfoam.org
5  \\ / A nd | Copyright (C) 2011-2018 OpenFOAM Foundation
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8 License
9  This file is part of OpenFOAM.
10 
11  OpenFOAM is free software: you can redistribute it and/or modify it
12  under the terms of the GNU General Public License as published by
13  the Free Software Foundation, either version 3 of the License, or
14  (at your option) any later version.
15 
16  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
17  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19  for more details.
20 
21  You should have received a copy of the GNU General Public License
22  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
23 
24 Class
25  Foam::LList
26 
27 Description
28  Template class for non-intrusive linked lists.
29 
30 SourceFiles
31  LList.C
32  LListIO.C
33 
34 \*---------------------------------------------------------------------------*/
35 
36 #ifndef LList_H
37 #define LList_H
38 
39 #include "label.H"
40 #include <initializer_list>
41 
42 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
43 
44 namespace Foam
45 {
46 
47 class Istream;
48 class Ostream;
49 
50 // Forward declaration of friend functions and operators
51 
52 template<class LListBase, class T> class LList;
53 
54 template<class LListBase, class T>
55 Istream& operator>>
56 (
57  Istream&,
59 );
60 
61 template<class LListBase, class T>
62 Ostream& operator<<
63 (
64  Ostream&,
65  const LList<LListBase, T>&
66 );
67 
68 
69 /*---------------------------------------------------------------------------*\
70  Class LList Declaration
71 \*---------------------------------------------------------------------------*/
72 
73 template<class LListBase, class T>
74 class LList
75 :
76  public LListBase
77 {
78 
79 public:
80 
81  // Forward declaration of STL iterators
82 
83  class iterator;
84  friend class iterator;
85 
86  class const_iterator;
87  friend class const_iterator;
88 
89 
90  //- Link structure
91  struct link
92  :
93  public LListBase::link
94  {
95  //- Stored object
96  T obj_;
97 
98  //- Construct given object
99  link(T a)
100  :
101  obj_(a)
102  {}
103  };
104 
105 
106  // Constructors
107 
108  //- Null construct
109  LList()
110  {}
111 
112  //- Construct given initial T
113  LList(T a)
114  :
115  LListBase(new link(a))
116  {}
117 
118  //- Construct from Istream
119  LList(Istream&);
120 
121  //- Construct as copy
122  LList(const LList<LListBase, T>&);
123 
124  //- Construct from an initializer list
125  LList(std::initializer_list<T>);
126 
127 
128  //- Destructor
129  ~LList();
130 
131 
132  // Member Functions
133 
134  // Access
135 
136  //- Return the first entry added
137  T& first()
138  {
139  return static_cast<link*>(LListBase::first())->obj_;
140  }
141 
142  //- Return const access to the first entry added
143  const T& first() const
144  {
145  return static_cast<const link*>(LListBase::first())->obj_;
146  }
147 
148  //- Return the last entry added
149  T& last()
150  {
151  return static_cast<link*>(LListBase::last())->obj_;
152  }
153 
154  //- Return const access to the last entry added
155  const T& last() const
156  {
157  return static_cast<const link*>(LListBase::last())->obj_;
158  }
159 
160 
161  // Edit
162 
163  //- Add at head of list
164  void insert(const T& a)
165  {
166  LListBase::insert(new link(a));
167  }
168 
169  //- Add at tail of list
170  void append(const T& a)
171  {
172  LListBase::append(new link(a));
173  }
174 
175  //- Remove and return head
176  T removeHead()
177  {
178  link* elmtPtr = static_cast<link*>(LListBase::removeHead());
179  T data = elmtPtr->obj_;
180  delete elmtPtr;
181  return data;
182  }
183 
184  //- Remove and return element
185  T remove(link* l)
186  {
187  link* elmtPtr = static_cast<link*>(LListBase::remove(l));
188  T data = elmtPtr->obj_;
189  delete elmtPtr;
190  return data;
191  }
192 
193  //- Remove and return element specified by iterator
194  T remove(iterator& it)
195  {
196  link* elmtPtr = static_cast<link*>(LListBase::remove(it));
197  T data = elmtPtr->obj_;
198  delete elmtPtr;
199  return data;
200  }
201 
202  //- Delete contents of list
203  void clear();
204 
205  //- Transfer the contents of the argument into this List
206  // and annul the argument list.
208 
209 
210  // Member operators
211 
212  //- Assignment operator
213  void operator=(const LList<LListBase, T>&);
214 
215  //- Assignment to an initializer list
216  void operator=(std::initializer_list<T>);
217 
218 
219  // STL type definitions
220 
221  //- Type of values the LList contains.
222  typedef T value_type;
223 
224  //- Type that can be used for storing into value_type
225  // objects.
226  typedef T& reference;
227 
228  //- Type that can be used for storing into constant
229  // LList::value_type objects.
230  typedef const T& const_reference;
231 
232  //- The type that can represent the size of a LList.
233  typedef label size_type;
234 
235 
236  // STL iterator
238  typedef typename LListBase::iterator LListBase_iterator;
239 
240  //- An STL-conforming iterator
241  class iterator
242  :
243  public LListBase_iterator
244  {
245 
246  public:
247 
248  //- Construct from base iterator
249  iterator(LListBase_iterator baseIter)
250  :
251  LListBase_iterator(baseIter)
252  {}
253 
254 
255  // Member operators
257  T& operator*()
258  {
259  return
260  static_cast<link&>
262  }
264  T& operator()()
265  {
266  return operator*();
267  }
269  iterator& operator++()
270  {
271  LListBase_iterator::operator++();
272  return *this;
273  }
274  };
276  inline iterator begin()
277  {
278  return LListBase::begin();
279  }
281  inline const iterator& end()
282  {
283  return static_cast<const iterator&>(LListBase::end());
284  }
285 
286 
287  // STL const_iterator
289  typedef typename LListBase::const_iterator LListBase_const_iterator;
290 
291  //- An STL-conforming const_iterator
292  class const_iterator
293  :
294  public LListBase_const_iterator
295  {
296 
297  public:
298 
299  //- Construct from base const_iterator
300  const_iterator(LListBase_const_iterator baseIter)
301  :
302  LListBase_const_iterator(baseIter)
303  {}
304 
305 
306  //- Construct from base iterator
307  const_iterator(LListBase_iterator baseIter)
308  :
309  LListBase_const_iterator(baseIter)
310  {}
311 
312 
313  // Member operators
315  const T& operator*()
316  {
317  return
318  static_cast<const link&>
320  }
322  const T& operator()()
323  {
324  return operator*();
325  }
327  const_iterator& operator++()
328  {
329  LListBase_const_iterator::operator++();
330  return *this;
331  }
332  };
334  inline const_iterator cbegin() const
335  {
336  return LListBase::cbegin();
337  }
339  inline const const_iterator& cend() const
340  {
341  return static_cast<const const_iterator&>(LListBase::cend());
342  }
344  inline const_iterator begin() const
345  {
346  return LListBase::begin();
347  }
349  inline const const_iterator& end() const
350  {
351  return static_cast<const const_iterator&>(LListBase::end());
352  }
353 
354 
355  // IOstream operators
356 
357  friend Istream& operator>> <LListBase, T>
358  (
359  Istream&,
361  );
362 
363  friend Ostream& operator<< <LListBase, T>
364  (
365  Ostream&,
366  const LList<LListBase, T>&
367  );
368 };
369 
370 
371 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
372 
373 } // End namespace Foam
374 
375 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
376 
377 #ifdef NoRepository
378  #include "LList.C"
379 #endif
380 
381 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
382 
383 #endif
384 
385 // ************************************************************************* //
intWM_LABEL_SIZE_t label
A label is an int32_t or int64_t as specified by the pre-processor macro WM_LABEL_SIZE.
Definition: label.H:59
LListBase::const_iterator LListBase_const_iterator
Definition: LList.H:288
LList()
Null construct.
Definition: LList.H:108
const iterator & end()
Definition: LList.H:280
const const_iterator & cend() const
Definition: LList.H:338
Template class for non-intrusive linked lists.
Definition: LList.H:51
T & first()
Return the first entry added.
Definition: LList.H:136
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:291
An STL-conforming iterator.
Definition: LList.H:240
friend class iterator
Definition: LList.H:82
T removeHead()
Remove and return head.
Definition: LList.H:175
tmp< fvMatrix< Type > > operator*(const volScalarField::Internal &, const fvMatrix< Type > &)
timeIndices insert(timeIndex, timeDirs[timeI].value())
void transfer(LList< LListBase, T > &)
Transfer the contents of the argument into this List.
Definition: LList.C:77
void operator=(const LList< LListBase, T > &)
Assignment operator.
Definition: LList.C:87
rAUs append(new volScalarField(IOobject::groupName("rAU", phase1.name()), 1.0/(U1Eqn.A()+byDt(max(phase1.residualAlpha() - alpha1, scalar(0)) *rho1))))
void insert(const T &a)
Add at head of list.
Definition: LList.H:163
~LList()
Destructor.
Definition: LList.C:55
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:53
void clear()
Delete contents of list.
Definition: LList.C:64
Database for solution data, solver performance and other reduced data.
Definition: data.H:52
T & last()
Return the last entry added.
Definition: LList.H:148
iterator begin()
Definition: LList.H:275
void T(FieldField< Field, Type > &f1, const FieldField< Field, Type > &f2)
T & reference
Type that can be used for storing into value_type.
Definition: LList.H:225
void append(const T &a)
Add at tail of list.
Definition: LList.H:169
const_iterator cbegin() const
Definition: LList.H:333
friend class const_iterator
Definition: LList.H:85
label size_type
The type that can represent the size of a LList.
Definition: LList.H:232
const T & const_reference
Type that can be used for storing into constant.
Definition: LList.H:229
LListBase::iterator LListBase_iterator
Definition: LList.H:237
T value_type
Type of values the LList contains.
Definition: LList.H:221
Namespace for OpenFOAM.