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