Loading...
Searching...
No Matches
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{
50// Forward Declarations
51template<class LListBase, class T> class LList;
52
53template<class LListBase, class T>
54Istream& operator>>
55(
58);
59
60template<class LListBase, class T>
61Ostream& operator<<
62(
63 Ostream& os,
64 const LList<LListBase, T>& lst
65);
66
67
68/*---------------------------------------------------------------------------*\
69 Class LList Declaration
70\*---------------------------------------------------------------------------*/
71
72template<class LListBase, class T>
73class LList
74:
75 public LListBase
76{
77public:
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;
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)
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_;
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
193
194 //- Copy construct from an initializer list
195 LList(std::initializer_list<T> lst);
196
197
198 //- Destructor. Calls clear()
199 ~LList();
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
218 {
219 return link::ref(LListBase::back());
220 }
222 //- The last entry in the list (const access)
223 const_reference back() const
224 {
225 return link::ref(LListBase::back());
227
228
229 //- Add copy at front of list
230 void push_front(const T& elem)
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
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
313 (
314 Ostream& os,
316 );
317
318
319 // STL iterator
321 //- An STL-conforming iterator
322 class iterator
323 :
324 public base_iterator
326 public:
327
328 //- Construct from base iterator
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
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 :
369 {
370 public:
371
372 //- Construct from base iterator
374 :
376 {}
377
378 //- Construct from base iterator
379 const_iterator(base_iterator iter)
380 :
381 const_base_iterator(iter)
382 {}
385 {
386 return link::ref(this->get_node());
387 }
390 {
391 return link::ptr(this->get_node());
392 }
393
395 {
396 return operator*();
397 }
399 const_iterator& operator++()
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
460 {
461 public:
462
463 //- Construct from base iterator
465 :
467 {}
468
470 {
471 return link::ref(this->get_node());
472 }
473
476 return link::ptr(this->get_node());
477 }
478
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()
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
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>();
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>();
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)); }
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// ************************************************************************* //
label n
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition Istream.H:60
An STL-conforming const_iterator.
Definition LList.H:461
const_pointer operator->() const
Definition LList.H:485
const_reference operator()() const
Definition LList.H:490
const_iterator(base_iterator iter)
Construct from base iterator.
Definition LList.H:475
const_iterator & operator++()
Definition LList.H:495
const_iterator(const_base_iterator iter)
Construct from base iterator.
Definition LList.H:467
const_reference operator*() const
Definition LList.H:480
const_iterator & operator--()
Definition LList.H:501
A const_reverse_iterator, for LListBase classes that support reverse iteration.
Definition LList.H:562
const_pointer operator->() const
Definition LList.H:578
const_reverse_iterator(const_base_iterator iter)
Construct from base iterator.
Definition LList.H:568
const_reverse_iterator & operator--()
Definition LList.H:589
const_reverse_iterator & operator++()
Definition LList.H:583
const_reference operator*() const
Definition LList.H:573
An STL-conforming iterator.
Definition LList.H:413
reference operator()() const
Definition LList.H:434
iterator & operator--()
Definition LList.H:445
reference operator*() const
Definition LList.H:424
iterator & operator++()
Definition LList.H:439
iterator(base_iterator iter)
Construct from base iterator.
Definition LList.H:419
pointer operator->() const
Definition LList.H:429
A reverse_iterator, for LListBase classes that support reverse iteration.
Definition LList.H:518
reverse_iterator & operator++()
Definition LList.H:539
reference operator*() const
Definition LList.H:529
reverse_iterator(base_iterator iter)
Construct from base iterator.
Definition LList.H:524
reverse_iterator & operator--()
Definition LList.H:545
pointer operator->() const
Definition LList.H:534
Template class for non-intrusive linked lists.
Definition LList.H:71
typename LListBase::iterator base_iterator
Definition LList.H:117
label size_type
The type that can represent the container size.
Definition LList.H:104
void pop_front(label n=1)
Remove first element(s) from the list (deletes pointers).
Definition LList.C:71
const_reference front() const
The first entry in the list (const access).
Definition LList.H:258
void push_front(const T &elem)
Add copy at front of list.
Definition LList.H:283
const iterator & end()
End of list for forward iterators.
Definition LList.H:649
label difference_type
The difference between iterators.
Definition LList.H:109
void prepend(const T &elem)
Add copy at front of list.
Definition LList.H:730
T value_type
Type of values stored.
Definition LList.H:79
const_iterator begin() const
Iterator to first item in list with const access.
Definition LList.H:632
const const_iterator & end() const
End of list for forward iterators.
Definition LList.H:681
typename LListBase::const_iterator const_base_iterator
Definition LList.H:118
LList()=default
Default construct.
const_iterator cbegin() const
Iterator to first item in list with const access.
Definition LList.H:608
void transfer(LList< LListBase, T > &lst)
Transfer the contents of the argument into this List and annul the argument list.
Definition LList.C:96
LList(T &&elem)
Construct and move add initial item.
Definition LList.H:213
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
void append(T &&elem)
Move construct at back of list.
Definition LList.H:751
LList(std::initializer_list< T > lst)
Copy construct from an initializer list.
Definition LList.C:48
const_reference back() const
The last entry in the list (const access).
Definition LList.H:274
LList(const T &elem)
Construct and copy add initial item.
Definition LList.H:205
LList(LList< LListBase, T > &&lst)
Move construct.
Definition LList.C:39
T * pointer
Pointer for value_type.
Definition LList.H:84
reference front()
The first entry in the list.
Definition LList.H:250
void operator=(std::initializer_list< T > lst)
Copy assignment from an initializer list.
Definition LList.C:127
const_reverse_iterator crbegin() const
Iterator to last item in list with const access.
Definition LList.H:624
LList(Istream &is)
Construct from Istream.
Definition LListIO.C:29
T remove(iterator &iter)
Remove and return element specified by iterator.
Definition LList.H:341
const reverse_iterator & rend()
End of list for reverse iterators.
Definition LList.H:665
T removeHead()
Remove and return first entry.
Definition LList.H:325
T & reference
Reference for value_type.
Definition LList.H:94
reference last()
The last entry in the list.
Definition LList.H:716
void push_front(T &&elem)
Move construct at front of list.
Definition LList.H:291
void insert(const T &elem)
Add copy at front of list. Same as push_front().
Definition LList.H:758
friend Ostream & operator(Ostream &os, const LList< LListBase, T > &lst)
Write LList to Ostream with line breaks, as per writeList with shortLen=-1.
void insert(T &&elem)
Move construct at front of list. Same as push_front().
Definition LList.H:765
const const_reverse_iterator & crend() const
End of list for reverse iterators.
Definition LList.H:673
const_reference first() const
The first entry in the list (const access).
Definition LList.H:709
void push_back(const T &elem)
Add copy at back of list.
Definition LList.H:299
const_reference last() const
The last entry in the list (const access).
Definition LList.H:723
Ostream & writeList(Ostream &os, const label shortLen=0) const
Write LList with line-breaks when length exceeds shortLen.
Definition LListIO.C:118
void prepend(T &&elem)
Move construct at front of list.
Definition LList.H:737
void clear()
Delete contents of list.
Definition LList.C:88
reverse_iterator rbegin()
Iterator to last item in list with non-const access.
Definition LList.H:616
const const_reverse_iterator & rend() const
End of list for reverse iterators.
Definition LList.H:689
Istream & readList(Istream &is)
Read list from Istream.
Definition LListIO.C:38
iterator begin()
Iterator to first item in list with non-const access.
Definition LList.H:600
void append(const T &elem)
Add copy at back of list.
Definition LList.H:744
LList(const LList< LListBase, T > &lst)
Copy construct.
Definition LList.C:27
~LList()
Destructor. Calls clear().
Definition LList.C:62
reference first()
The first entry in the list.
Definition LList.H:702
void operator=(LList< LListBase, T > &&lst)
Move assignment.
Definition LList.C:118
void operator=(const LList< LListBase, T > &lst)
Copy assignment.
Definition LList.C:106
const_reverse_iterator rbegin() const
Iterator to last item in list with const access.
Definition LList.H:640
void push_back(T &&elem)
Move construct at back of list.
Definition LList.H:307
reference back()
The last entry in the list.
Definition LList.H:266
const T & const_reference
Const reference for value_type.
Definition LList.H:99
T remove(link *item)
Remove and return element.
Definition LList.H:333
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition Ostream.H:59
volScalarField & p
rDeltaT ref()
OBJstream os(runTime.globalPath()/outputName)
Namespace for OpenFOAM.
void T(FieldField< Field, Type > &f1, const FieldField< Field, Type > &f2)
nonInt insert("surfaceSum(((S|magSf)*S)")
Includes some common C++ headers, defines global macros and templates used in multiple places by Open...