UILList.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::UILList
29
30Description
31 Template class for intrusive linked lists.
32
33SourceFiles
34 UILList.C
35 UILListIO.C
36
37\*---------------------------------------------------------------------------*/
38
39#ifndef Foam_UILList_H
40#define Foam_UILList_H
41
42#include "label.H"
43#include "uLabel.H"
44
45// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
46
47namespace Foam
48{
49
50// Forward Declarations
51class Ostream;
52
53template<class LListBase, class T> class UILList;
54
55template<class LListBase, class T>
56Ostream& operator<<
57(
58 Ostream& os,
59 const UILList<LListBase, T>& lst
60);
61
62
63/*---------------------------------------------------------------------------*\
64 Class UILList Declaration
65\*---------------------------------------------------------------------------*/
66
67template<class LListBase, class T>
68class UILList
69:
70 public LListBase
71{
72public:
73
74 // STL type definitions
75
76 //- Type of values stored
77 typedef T value_type;
78
79 //- Pointer for value_type
80 typedef T* pointer;
81
82 //- Const pointer for value_type
83 typedef const T* const_pointer;
84
85 //- Reference for value_type
86 typedef T& reference;
87
88 //- Const reference for value_type
89 typedef const T& const_reference;
90
91 //- The type that can represent the container size
92 typedef label size_type;
93
94 //- The difference between iterator objects
95 typedef label difference_type;
96
97
98 // Forward Declarations (iterators)
99
100 class iterator;
101 class const_iterator;
105
106
107 // Constructors
108
109 //- Default construct
110 UILList() = default;
111
112 //- Construct and add initial item pointer
113 explicit UILList(T* item)
114 {
115 this->prepend(item);
116 }
117
118 //- Construct as copy
119 UILList(const UILList<LListBase, T>& lst);
120
121
122 // Member Functions
123
124 //- The first entry in the list
125 T* first()
126 {
127 return static_cast<T*>(LListBase::first());
128 }
129
130 //- The first entry in the list (const access)
131 const T* first() const
132 {
133 return static_cast<const T*>(LListBase::first());
134 }
135
136 //- The last entry in the list
137 T* last()
138 {
139 return static_cast<T*>(LListBase::last());
140 }
141
142 //- The last entry in the list (const access)
143 const T* last() const
144 {
145 return static_cast<const T*>(LListBase::last());
146 }
147
148
149 //- Remove and return head
150 T* removeHead()
151 {
152 return static_cast<T*>(LListBase::removeHead());
153 }
154
155 //- Remove and return element
156 T* remove(T* item)
157 {
158 return static_cast<T*>(LListBase::remove(item));
159 }
160
161 //- Remove and return item specified by iterator
162 T* remove(iterator& iter)
163 {
164 return static_cast<T*>(LListBase::remove(iter));
165 }
166
167
168 // Member operators
169
170 //- Copy assignment
171 void operator=(const UILList<LListBase, T>& lst);
172
173 //- Equality. True both lists are element-wise equal
174 // (using value_type::operator==). Takes linear time.
175 bool operator==(const UILList<LListBase, T>& lst) const;
176
177 //- The opposite of the equality operation. Takes linear time.
178 bool operator!=(const UILList<LListBase, T>& lst) const;
179
180
181 // IOstream Operators
182
183 //- Write UILList with line-breaks when length exceeds shortLen.
184 // Using '0' suppresses line-breaks entirely.
185 Ostream& writeList(Ostream& os, const label shortLen=0) const;
186
187 //- Write UILList to Ostream with line breaks,
188 //- as per writeList() with shortLen=-1
189 friend Ostream& operator<< <LListBase, T>
190 (
191 Ostream& os,
192 const UILList<LListBase, T>& lst
193 );
194
195
196 // STL iterator
197
198 //- A non-const iterator
199 class iterator
200 :
201 public base_iterator
202 {
203 public:
206 :
207 base_iterator(iter)
208 {}
209
210 //- Return the address of the object being referenced
211 pointer get() const
212 {
213 return static_cast<T*>(base_iterator::get_node());
214 }
216 reference operator*() const
217 {
218 return *(this->get());
219 }
221 pointer operator->() const
222 {
223 return this->get();
224 }
226 reference operator()() const
227 {
228 return operator*();
229 }
232 {
233 this->next();
234 return *this;
235 }
236 };
237
238
239 // STL const_iterator
240
241 //- A const_iterator
242 class const_iterator
243 :
245 {
246 public:
247
248 //- Construct from base const_iterator
250 :
252 {}
253
254 //- Construct from base iterator
256 :
258 {}
259
260 //- Return the address of the object being referenced
261 const_pointer get() const
262 {
263 return static_cast<const T*>(const_base_iterator::get_node());
264 }
267 {
268 return *(this->get());
269 }
271 const_pointer operator->() const
272 {
273 return this->get();
274 }
276 const_reference operator()() const
277 {
278 return operator*();
279 }
282 {
283 this->next();
284 return *this;
285 }
286 };
287
288
289 // STL reverse_iterator
290
291 //- A reverse_iterator, for LListBase classes that support
292 //- reverse iteration
293 class reverse_iterator
294 :
295 public base_iterator
296 {
297 public:
300 :
301 base_iterator(iter)
302 {}
303
304 //- Return the address of the object being referenced
305 pointer get() const
306 {
307 return static_cast<T*>(base_iterator::get_node());
308 }
310 reference operator*() const
311 {
312 return *(this->get());
313 }
315 pointer operator->() const
316 {
317 return this->get();
318 }
320 reference operator()() const
321 {
322 return operator*();
323 }
326 {
327 this->prev(); // Only if base iterator is bidirectional
328 return *this;
329 }
330 };
331
332
333 // STL const_reverse_iterator
334
335 //- A const_reverse_iterator, for LListBase classes that support
336 //- reverse iteration
338 :
340 {
341 public:
344 :
346 {}
347
348 //- Return the address of the object being referenced
349 const_pointer get() const
350 {
351 return static_cast<const T*>(const_base_iterator::get_node());
352 }
355 {
356 return *(this->get());
357 }
359 const_pointer operator->() const
360 {
361 return this->get();
362 }
364 const_reference operator()() const
365 {
366 return operator*();
367 }
370 {
371 this->prev(); // Only if base iterator is bidirectional
372 return *this;
373 }
374 };
375
376
377 //- Iterator to first item in list with non-const access
378 inline iterator begin()
379 {
380 return LListBase::template iterator_first<base_iterator>();
381 }
382
383 //- Iterator to first item in list with const access
384 inline const_iterator cbegin() const
385 {
386 return LListBase::template iterator_first<const_base_iterator>();
387 }
388
389 //- Iterator to last item in list with non-const access
390 inline reverse_iterator rbegin()
391 {
392 return LListBase::template iterator_last<base_iterator>();
393 }
394
395 //- Iterator to last item in list with const access
396 inline const_reverse_iterator crbegin() const
397 {
398 return LListBase::template iterator_last<const_base_iterator>();
399 }
400
401 //- Iterator to first item in list with const access
402 inline const_iterator begin() const
403 {
404 return LListBase::cbegin();
405 }
406
407 //- Iterator to last item in list with const access
408 inline const_reverse_iterator rbegin() const
409 {
410 return crbegin();
411 }
412
413
414 //- End of list for forward iterators
415 inline const iterator& end()
416 {
417 return LListBase::template iterator_end<iterator>();
418 }
419
420 //- End of list for forward iterators
421 inline const const_iterator& cend() const
422 {
423 return LListBase::template iterator_end<const_iterator>();
424 }
425
426 //- End of list for reverse iterators
427 inline const reverse_iterator& rend()
428 {
429 return LListBase::template iterator_rend<reverse_iterator>();
430 }
431
432 //- End of list for reverse iterators
433 inline const const_reverse_iterator& crend() const
434 {
435 return LListBase::template iterator_rend<const_reverse_iterator>();
436 }
437
438 //- End of list for forward iterators
439 inline const const_iterator& end() const
440 {
441 return cend();
442 }
443
444 //- End of list for reverse iterators
445 inline const const_reverse_iterator& rend() const
446 {
447 return crend();
448 }
449};
450
451
452// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
453
454} // End namespace Foam
455
456// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
457
458#ifdef NoRepository
459 #include "UILList.C"
460 #include "UILListIO.C"
461#endif
462
463// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
464
465#endif
466
467// ************************************************************************* //
Forward iterator with non-const access.
Definition: HashTable.H:720
T removeHead()
Remove and return first entry.
Definition: LList.H:265
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.
Definition: UILList.H:244
const_pointer operator->() const
Definition: UILList.H:270
const_reference operator()() const
Definition: UILList.H:275
const_pointer get() const
Return the address of the object being referenced.
Definition: UILList.H:260
const_iterator(base_iterator iter)
Construct from base iterator.
Definition: UILList.H:254
const_iterator & operator++()
Definition: UILList.H:280
const_iterator(const_base_iterator iter)
Construct from base const_iterator.
Definition: UILList.H:248
const_reference operator*() const
Definition: UILList.H:265
const_pointer operator->() const
Definition: UILList.H:358
const_reference operator()() const
Definition: UILList.H:363
const_reverse_iterator(const_base_iterator iter)
Definition: UILList.H:342
const_pointer get() const
Return the address of the object being referenced.
Definition: UILList.H:348
const_reverse_iterator & operator++()
Definition: UILList.H:368
const_reference operator*() const
Definition: UILList.H:353
A non-const iterator.
Definition: UILList.H:201
reference operator()() const
Definition: UILList.H:225
reference operator*() const
Definition: UILList.H:215
iterator & operator++()
Definition: UILList.H:230
pointer get() const
Return the address of the object being referenced.
Definition: UILList.H:210
iterator(base_iterator iter)
Definition: UILList.H:204
pointer operator->() const
Definition: UILList.H:220
reference operator()() const
Definition: UILList.H:319
reverse_iterator & operator++()
Definition: UILList.H:324
reference operator*() const
Definition: UILList.H:309
reverse_iterator(base_iterator iter)
Definition: UILList.H:298
pointer get() const
Return the address of the object being referenced.
Definition: UILList.H:304
pointer operator->() const
Definition: UILList.H:314
Template class for intrusive linked lists.
Definition: UILList.H:70
typename LListBase::iterator base_iterator
Definition: UILList.H:102
label size_type
The type that can represent the container size.
Definition: UILList.H:91
const iterator & end()
End of list for forward iterators.
Definition: UILList.H:414
label difference_type
The difference between iterator objects.
Definition: UILList.H:94
const T * last() const
The last entry in the list (const access)
Definition: UILList.H:142
UILList()=default
Default construct.
T value_type
Type of values stored.
Definition: UILList.H:76
const_iterator begin() const
Iterator to first item in list with const access.
Definition: UILList.H:401
const const_iterator & end() const
End of list for forward iterators.
Definition: UILList.H:438
typename LListBase::const_iterator const_base_iterator
Definition: UILList.H:103
const T * first() const
The first entry in the list (const access)
Definition: UILList.H:130
const_iterator cbegin() const
Iterator to first item in list with const access.
Definition: UILList.H:383
const const_iterator & cend() const
End of list for forward iterators.
Definition: UILList.H:420
const T * const_pointer
Const pointer for value_type.
Definition: UILList.H:82
void operator=(const UILList< LListBase, T > &lst)
Copy assignment.
Definition: UILList.C:46
T * pointer
Pointer for value_type.
Definition: UILList.H:79
const_reverse_iterator crbegin() const
Iterator to last item in list with const access.
Definition: UILList.H:395
const reverse_iterator & rend()
End of list for reverse iterators.
Definition: UILList.H:426
T & reference
Reference for value_type.
Definition: UILList.H:85
bool operator==(const UILList< LListBase, T > &lst) const
Equality. True both lists are element-wise equal.
Definition: UILList.C:59
const const_reverse_iterator & crend() const
End of list for reverse iterators.
Definition: UILList.H:432
T * remove(T *item)
Remove and return element.
Definition: UILList.H:155
T * first()
The first entry in the list.
Definition: UILList.H:124
bool operator!=(const UILList< LListBase, T > &lst) const
The opposite of the equality operation. Takes linear time.
Definition: UILList.C:84
Ostream & writeList(Ostream &os, const label shortLen=0) const
Write UILList with line-breaks when length exceeds shortLen.
Definition: UILListIO.C:37
reverse_iterator rbegin()
Iterator to last item in list with non-const access.
Definition: UILList.H:389
const const_reverse_iterator & rend() const
End of list for reverse iterators.
Definition: UILList.H:444
iterator begin()
Iterator to first item in list with non-const access.
Definition: UILList.H:377
T * last()
The last entry in the list.
Definition: UILList.H:136
T * removeHead()
Remove and return head.
Definition: UILList.H:149
UILList(T *item)
Construct and add initial item pointer.
Definition: UILList.H:112
T * remove(iterator &iter)
Remove and return item specified by iterator.
Definition: UILList.H:161
const_reverse_iterator rbegin() const
Iterator to last item in list with const access.
Definition: UILList.H:407
const T & const_reference
Const reference for value_type.
Definition: UILList.H:88
A const_iterator for iterating across on values.
Definition: bitSet.H:505
edgeFaceCirculator cbegin() const
const volScalarField & T
OBJstream os(runTime.globalPath()/outputName)
Namespace for OpenFOAM.