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-2021 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  //- Copy constructor
122  LList(const LList<LListBase, T>&);
123 
124  //- Move constructor
126 
127  //- Construct from an initialiser list
128  LList(std::initializer_list<T>);
129 
130 
131  //- Destructor
132  ~LList();
133 
134 
135  // Member Functions
136 
137  // Access
138 
139  //- Return the first entry added
140  T& first()
141  {
142  return static_cast<link*>(LListBase::first())->obj_;
143  }
144 
145  //- Return const access to the first entry added
146  const T& first() const
147  {
148  return static_cast<const link*>(LListBase::first())->obj_;
149  }
150 
151  //- Return the last entry added
152  T& last()
153  {
154  return static_cast<link*>(LListBase::last())->obj_;
155  }
156 
157  //- Return const access to the last entry added
158  const T& last() const
159  {
160  return static_cast<const link*>(LListBase::last())->obj_;
161  }
162 
163 
164  // Edit
165 
166  //- Add at head of list
167  void insert(const T& a)
168  {
169  LListBase::insert(new link(a));
170  }
171 
172  //- Add at tail of list
173  void append(const T& a)
174  {
175  LListBase::append(new link(a));
176  }
177 
178  //- Remove and return head
179  T removeHead()
180  {
181  link* elmtPtr = static_cast<link*>(LListBase::removeHead());
182  T data = elmtPtr->obj_;
183  delete elmtPtr;
184  return data;
185  }
186 
187  //- Remove and return element
188  T remove(link* l)
189  {
190  link* elmtPtr = static_cast<link*>(LListBase::remove(l));
191  T data = elmtPtr->obj_;
192  delete elmtPtr;
193  return data;
194  }
195 
196  //- Remove and return element specified by iterator
197  T remove(iterator& it)
198  {
199  link* elmtPtr = static_cast<link*>(LListBase::remove(it));
200  T data = elmtPtr->obj_;
201  delete elmtPtr;
202  return data;
203  }
204 
205  //- Delete contents of list
206  void clear();
207 
208  //- Transfer the contents of the argument into this List
209  // and annul the argument list.
211 
212 
213  // Member Operators
214 
215  //- Assignment operator
216  void operator=(const LList<LListBase, T>&);
217 
218  //- Move assignment operator
220 
221  //- Assignment to an initialiser list
222  void operator=(std::initializer_list<T>);
223 
224 
225  // STL type definitions
226 
227  //- Type of values the LList contains.
228  typedef T value_type;
229 
230  //- Type that can be used for storing into value_type
231  // objects.
232  typedef T& reference;
233 
234  //- Type that can be used for storing into constant
235  // LList::value_type objects.
236  typedef const T& const_reference;
237 
238  //- The type that can represent the size of a LList.
239  typedef label size_type;
240 
241 
242  // STL iterator
244  typedef typename LListBase::iterator LListBase_iterator;
245 
246  //- An STL-conforming iterator
247  class iterator
248  :
249  public LListBase_iterator
250  {
251 
252  public:
253 
254  //- Construct from base iterator
255  iterator(LListBase_iterator baseIter)
256  :
257  LListBase_iterator(baseIter)
258  {}
259 
260 
261  // Member Operators
263  T& operator*()
264  {
265  return
266  static_cast<link&>
268  }
270  T& operator()()
271  {
272  return operator*();
273  }
275  iterator& operator++()
276  {
277  LListBase_iterator::operator++();
278  return *this;
279  }
280  };
282  inline iterator begin()
283  {
284  return LListBase::begin();
285  }
287  inline const iterator& end()
288  {
289  return static_cast<const iterator&>(LListBase::end());
290  }
291 
292 
293  // STL const_iterator
295  typedef typename LListBase::const_iterator LListBase_const_iterator;
296 
297  //- An STL-conforming const_iterator
298  class const_iterator
299  :
300  public LListBase_const_iterator
301  {
302 
303  public:
304 
305  //- Construct from base const_iterator
306  const_iterator(LListBase_const_iterator baseIter)
307  :
308  LListBase_const_iterator(baseIter)
309  {}
310 
311 
312  //- Construct from base iterator
313  const_iterator(LListBase_iterator baseIter)
314  :
315  LListBase_const_iterator(baseIter)
316  {}
317 
318 
319  // Member Operators
321  const T& operator*()
322  {
323  return
324  static_cast<const link&>
326  }
328  const T& operator()()
329  {
330  return operator*();
331  }
333  const_iterator& operator++()
334  {
335  LListBase_const_iterator::operator++();
336  return *this;
337  }
338  };
340  inline const_iterator cbegin() const
341  {
342  return LListBase::cbegin();
343  }
345  inline const const_iterator& cend() const
346  {
347  return static_cast<const const_iterator&>(LListBase::cend());
348  }
350  inline const_iterator begin() const
351  {
352  return LListBase::begin();
353  }
355  inline const const_iterator& end() const
356  {
357  return static_cast<const const_iterator&>(LListBase::end());
358  }
359 
360 
361  // IOstream Operators
362 
363  friend Istream& operator>> <LListBase, T>
364  (
365  Istream&,
367  );
368 
369  friend Ostream& operator<< <LListBase, T>
370  (
371  Ostream&,
372  const LList<LListBase, T>&
373  );
374 };
375 
376 
377 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
378 
379 } // End namespace Foam
380 
381 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
382 
383 #ifdef NoRepository
384  #include "LList.C"
385 #endif
386 
387 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
388 
389 #endif
390 
391 // ************************************************************************* //
labelList first(const UList< labelPair > &p)
Definition: patchToPatch.C:38
tmp< fvMatrix< Type > > operator*(const volScalarField::Internal &, const fvMatrix< Type > &)
LListBase::const_iterator LListBase_const_iterator
Definition: LList.H:294
LList()
Null construct.
Definition: LList.H:108
const iterator & end()
Definition: LList.H:286
const const_iterator & cend() const
Definition: LList.H:344
Template class for non-intrusive linked lists.
Definition: LList.H:51
T & first()
Return the first entry added.
Definition: LList.H:139
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:297
An STL-conforming iterator.
Definition: LList.H:246
friend class iterator
Definition: LList.H:82
void insert(const scalar, DynamicList< floatScalar > &)
Append scalar to given DynamicList.
T removeHead()
Remove and return head.
Definition: LList.H:178
void transfer(LList< LListBase, T > &)
Transfer the contents of the argument into this List.
Definition: LList.C:86
void operator=(const LList< LListBase, T > &)
Assignment operator.
Definition: LList.C:96
void insert(const T &a)
Add at head of list.
Definition: LList.H:166
~LList()
Destructor.
Definition: LList.C:64
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:54
void clear()
Delete contents of list.
Definition: LList.C:73
Database for solution and other reduced data.
Definition: data.H:51
T & last()
Return the last entry added.
Definition: LList.H:151
iterator begin()
Definition: LList.H:281
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:231
void append(const T &a)
Add at tail of list.
Definition: LList.H:172
const_iterator cbegin() const
Definition: LList.H:339
friend class const_iterator
Definition: LList.H:85
label size_type
The type that can represent the size of a LList.
Definition: LList.H:238
const T & const_reference
Type that can be used for storing into constant.
Definition: LList.H:235
LListBase::iterator LListBase_iterator
Definition: LList.H:243
T value_type
Type of values the LList contains.
Definition: LList.H:227
Namespace for OpenFOAM.