DynamicListI.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) 2016-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
27\*---------------------------------------------------------------------------*/
28
29#include "FixedList.H"
30
31// * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
32
33template<class T, int SizeMin>
34template<class ListType>
36(
37 const ListType& list
38)
39{
40 const label len = list.size();
41
42 if (capacity_ < len)
43 {
44 // Needs more space for the copy operation
45 List<T>::setAddressableSize(capacity_); // Use entire space
46 List<T>::resize_nocopy(len);
47 capacity_ = List<T>::size();
48 }
49
50 // Perform copy into addressable portion
51 List<T>::setAddressableSize(len);
52 List<T>::operator=(list);
53}
54
55
56template<class T, int SizeMin>
58(
59 const bool nocopy,
60 const label newCapacity
61)
62{
63 if (newCapacity == capacity_)
64 {
65 return;
66 }
67
68 // Addressable length, possibly truncated by new capacity
69 const label currLen = min(List<T>::size(), newCapacity);
70
71 // Corner case...
72 if (List<T>::size() == newCapacity)
73 {
74 // Adjust addressable size to trigger proper resizing.
75 // Using (old size+1) is safe since it does not affect the 'overlap'
76 // of old and new addressable regions, but incurs fewew copy
77 // operations than extending to use the current capacity would.
78 List<T>::setAddressableSize(currLen+1);
79 }
80
81 if (nocopy)
82 {
83 List<T>::resize_nocopy(newCapacity);
84 }
85 else
86 {
87 List<T>::resize(newCapacity);
88 }
89
90 capacity_ = List<T>::size();
91 List<T>::setAddressableSize(currLen);
92}
93
94
95template<class T, int SizeMin>
97(
98 const bool nocopy,
99 const label len
100)
101{
102 if (capacity_ < len)
103 {
104 // Preserve addressed size
105 const label currLen = List<T>::size();
106
107 // Increase capacity (doubling)
108 capacity_ = max(SizeMin, max(len, label(2*capacity_)));
109
110 if (nocopy)
111 {
112 List<T>::resize_nocopy(capacity_);
114 else
115 {
116 List<T>::resize(capacity_);
117 }
120}
121
123template<class T, int SizeMin>
126 const bool nocopy,
127 const label len
128)
130 this->doReserve(nocopy, len);
132}
134
135// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
136
137template<class T, int SizeMin>
139:
140 List<T>(),
141 capacity_(0)
142{}
143
145template<class T, int SizeMin>
148 List<T>(),
149 capacity_(0)
150{
151 reserve_nocopy(len);
152}
153
155template<class T, int SizeMin>
157(
158 const label len,
159 const T& val
160)
161:
162 List<T>(len, val),
163 capacity_(List<T>::size())
164{}
165
166
167template<class T, int SizeMin>
169(
170 const label len,
171 const Foam::zero
173:
174 List<T>(len, Zero),
175 capacity_(List<T>::size())
176{}
177
178
179template<class T, int SizeMin>
182 const DynamicList<T, SizeMin>& list
183)
184:
185 List<T>(list),
186 capacity_(List<T>::size())
188
189
190template<class T, int SizeMin>
191template<int AnySizeMin>
193(
195)
197 List<T>(list),
198 capacity_(List<T>::size())
199{}
200
202template<class T, int SizeMin>
204(
205 const UList<T>& list
206)
207:
208 List<T>(list),
209 capacity_(List<T>::size())
210{}
211
213template<class T, int SizeMin>
214template<unsigned N>
216(
217 const FixedList<T, N>& list
219:
220 List<T>(list),
221 capacity_(List<T>::size())
222{}
223
224
225template<class T, int SizeMin>
227(
228 std::initializer_list<T> lst
229)
230:
231 List<T>(lst),
232 capacity_(List<T>::size())
233{}
234
236template<class T, int SizeMin>
237template<class Addr>
239(
241)
243 List<T>(lst),
244 capacity_(List<T>::size())
245{}
246
247
248template<class T, int SizeMin>
250(
253:
254 capacity_(0)
255{
257}
258
260template<class T, int SizeMin>
261template<int AnySizeMin>
263(
266:
267 capacity_(0)
268{
269 transfer(lst);
270}
271
273template<class T, int SizeMin>
275(
277)
278:
279 List<T>(std::move(lst)),
280 capacity_(List<T>::size())
281{}
282
284// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
285
286template<class T, int SizeMin>
287inline Foam::label Foam::DynamicList<T, SizeMin>::capacity() const noexcept
288{
289 return capacity_;
291
292
293template<class T, int SizeMin>
294inline std::streamsize
296{
297 return std::streamsize(capacity_)*sizeof(T);
299
300
301template<class T, int SizeMin>
303(
304 const label len
305)
306{
307 this->doCapacity(false, len); // nocopy = false
308}
309
310
311template<class T, int SizeMin>
314 const label len
315)
317 this->doCapacity(true, len); // nocopy = true
318}
319
320
321template<class T, int SizeMin>
323(
324 const label len
325) noexcept
326{
327 capacity_ = len;
329
330
331template<class T, int SizeMin>
333(
334 const label len
336{
337 this->doReserve(false, len); // nocopy = false
339
340
341template<class T, int SizeMin>
343(
344 const label len
346{
347 this->doReserve(true, len); // nocopy = true
348}
350
351template<class T, int SizeMin>
353(
354 const label len
356{
357 this->doResize(false, len); // nocopy = false
358}
360
361template<class T, int SizeMin>
363(
364 const label len
365)
366{
367 this->doResize(true, len); // nocopy = true
368}
369
370
371template<class T, int SizeMin>
373(
374 const label len,
375 const T& val
376)
377{
378 label idx = List<T>::size();
379 resize(len);
380
381 // Fill newly exposed with constant value
382 while (idx < len)
383 {
384 this->operator[](idx) = val;
385 ++idx;
386 }
387}
388
389
390template<class T, int SizeMin>
392{
394}
395
396
397template<class T, int SizeMin>
399{
401 capacity_ = 0;
402}
403
404
405template<class T, int SizeMin>
407{
408 const label currLen = List<T>::size();
409
410 // Address into the entire list
412
413 return currLen;
414}
415
416
417template<class T, int SizeMin>
419{
420 const label currLen = List<T>::size();
421 if (currLen < capacity_)
422 {
423 // Adjust addressable size to trigger proper resizing
425
426 List<T>::resize(currLen);
427 capacity_ = List<T>::size();
428 }
429}
430
431
432template<class T, int SizeMin>
435{
436 this->shrinkStorage();
437 return *this;
438}
439
440
441template<class T, int SizeMin>
442template<int AnySizeMin>
444(
446)
447{
448 if
449 (
450 static_cast<const List<T>*>(this)
451 == static_cast<const List<T>*>(&other)
452 )
453 {
454 return; // Self-swap is a no-op
455 }
456
457 // Swap storage and addressable size
458 UList<T>::swap(other);
459
460 // Swap capacity
461 std::swap(this->capacity_, other.capacity_);
462}
463
464
465template<class T, int SizeMin>
466inline void
468{
469 // Take over storage, clear addressing for list
470 capacity_ = list.size();
471 List<T>::transfer(list);
472}
473
474
475template<class T, int SizeMin>
476template<int AnySizeMin>
477inline void
479(
481)
482{
483 if
484 (
485 static_cast<const List<T>*>(this)
486 == static_cast<const List<T>*>(&list)
487 )
488 {
489 return; // Self-assignment is a no-op
490 }
491
492 // Take over storage as-is (without shrink, without using SizeMin)
493 // clear addressing and storage for old lst.
494 capacity_ = list.capacity();
495
496 List<T>::transfer(static_cast<List<T>&>(list));
497 list.clearStorage(); // Ensure capacity=0
498}
499
500
501template<class T, int SizeMin>
503(
504 const T& val
505)
506{
507 const label idx = List<T>::size();
508 resize(idx + 1);
509
510 this->operator[](idx) = val; // copy element
511}
512
513
514template<class T, int SizeMin>
516(
517 T&& val
518)
519{
520 const label idx = List<T>::size();
521 resize(idx + 1);
522
523 this->operator[](idx) = std::move(val); // move assign element
524}
525
526
527template<class T, int SizeMin>
529(
530 const UList<T>& lst
531)
532{
533 if (this == &lst)
534 {
536 << "Attempted appending to self"
537 << abort(FatalError);
538 }
539
540 label idx = List<T>::size();
541 resize(idx + lst.size());
542
543 for (const T& val : lst)
544 {
545 this->operator[](idx++) = val; // copy element
546 }
547}
548
549
550template<class T, int SizeMin>
551template<unsigned N>
553(
554 const FixedList<T, N>& lst
555)
556{
557 label idx = List<T>::size();
558 resize(idx + lst.size());
559
560 for (const T& val : lst)
561 {
562 this->operator[](idx++) = val; // copy element
563 }
564}
565
566
567template<class T, int SizeMin>
569(
570 std::initializer_list<T> lst
571)
572{
573 label idx = List<T>::size();
574 resize(idx + lst.size());
575
576 for (const T& val : lst)
577 {
578 this->operator[](idx++) = val; // copy element
579 }
580}
581
582
583template<class T, int SizeMin>
584template<class Addr>
586(
588)
589{
590 label idx = List<T>::size();
591 const label n = lst.size();
592
593 resize(idx + n);
594
595 for (label i=0; i<n; ++i)
596 {
597 this->operator[](idx++) = lst[i]; // copy element
598 }
599}
600
601
602template<class T, int SizeMin>
604(
605 List<T>&& list
606)
607{
608 if (this == &list)
609 {
611 << "Attempted appending to self"
612 << abort(FatalError);
613 }
614
615 label idx = List<T>::size();
616 resize(idx + list.size());
617
618 for (T& val : list)
619 {
620 Foam::Swap(this->operator[](idx++), val); // moved content
621 }
622
623 list.clear();
624}
625
626
627template<class T, int SizeMin>
628template<int AnySizeMin>
630(
632)
633{
634 append(std::move(static_cast<List<T>&>(list)));
635 list.clearStorage(); // Ensure capacity=0
636}
637
638
639template<class T, int SizeMin>
640inline Foam::label Foam::DynamicList<T, SizeMin>::appendUniq(const T& val)
641{
642 if (this->found(val))
643 {
644 return 0;
645 }
646 else
647 {
648 this->append(val);
649 return 1; // Increased list length by one
650 }
651}
652
653
654template<class T, int SizeMin>
656{
657 // Location of last element and simultaneously the new size
658 const label idx = List<T>::size() - 1;
659
660 if (idx < 0)
661 {
663 << "List is empty" << abort(FatalError);
664 }
665
666 const T& val = List<T>::operator[](idx);
667
669
670 return val;
671}
672
673
674template<class T, int SizeMin>
676(
677 const label idx,
678 const bool fast
679)
680{
681 if (fast)
682 {
683 // Simply swap idx <=> last
684 this->swapLast(idx);
685 }
686 else
687 {
688 // Move element to the end and move everything down
689 this->moveLast(idx);
690 }
691
692 // Element to remove is now at the end
693 return this->remove();
694}
695
696
697template<class T, int SizeMin>
699(
700 const labelRange& range
701)
702{
703 return this->removeElements(this->validateRange(range));
704}
705
706
707template<class T, int SizeMin>
709(
710 std::initializer_list<label> start_size
711)
712{
713 return this->removeElements(this->validateRange(start_size));
714}
715
716
717template<class T, int SizeMin>
719(
720 const labelRange& range
721)
722{
723 return this->subsetElements(this->validateRange(range));
724}
725
726
727template<class T, int SizeMin>
729(
730 std::initializer_list<label> start_size
731)
732{
733 return this->subsetElements(this->validateRange(start_size));
734}
735
736
737// * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
738
739template<class T, int SizeMin>
741(
742 const label i
743)
744{
745 if (i >= List<T>::size())
746 {
747 resize(i + 1);
748 }
749
750 return this->operator[](i);
751}
752
753
754template<class T, int SizeMin>
756(
757 const T& val
758)
759{
761}
762
763
764template<class T, int SizeMin>
766(
767 const Foam::zero
768)
769{
771}
772
773
774template<class T, int SizeMin>
776(
777 const UList<T>& lst
778)
779{
780 assignDynList(lst);
781}
782
783
784template<class T, int SizeMin>
785template<unsigned N>
787(
788 const FixedList<T, N>& lst
789)
790{
791 assignDynList(lst);
792}
793
794
795template<class T, int SizeMin>
797(
798 const DynamicList<T, SizeMin>& lst
799)
800{
801 if (this == &lst)
802 {
803 return; // Self-assignment is a no-op
804 }
805
806 assignDynList(lst);
807}
808
809
810template<class T, int SizeMin>
811template<int AnySizeMin>
813(
815)
816{
817 if
818 (
819 static_cast<const List<T>*>(this)
820 == static_cast<const List<T>*>(&list)
821 )
822 {
823 return; // Self-assignment is a no-op
824 }
825
826 assignDynList(list);
827}
828
829
830template<class T, int SizeMin>
832(
833 std::initializer_list<T> lst
834)
835{
836 assignDynList(lst);
837}
838
839
840template<class T, int SizeMin>
841template<class Addr>
843(
845)
846{
847 assignDynList(lst);
848}
849
850
851template<class T, int SizeMin>
853(
854 List<T>&& lst
855)
856{
857 clear();
858 transfer(lst);
859}
860
861
862template<class T, int SizeMin>
864(
866)
867{
868 if (this == &lst)
869 {
870 return; // Self-assignment is a no-op
871 }
872
873 clear();
874 transfer(lst);
875}
876
877
878template<class T, int SizeMin>
879template<int AnySizeMin>
881(
883)
884{
885 if
886 (
887 static_cast<const List<T>*>(this)
888 == static_cast<const List<T>*>(&list)
889 )
890 {
891 return; // Self-assignment is a no-op
892 }
893
894 clear();
895 transfer(list);
896}
897
898
899// ************************************************************************* //
scalar range
bool found
label n
Y[inertIndex] max(0.0)
A 1D vector of objects of type <T> that resizes itself as necessary to accept the new objects.
Definition: DynamicList.H:72
void clear() noexcept
Clear the addressed list, i.e. set the size to zero.
Definition: DynamicListI.H:391
constexpr DynamicList() noexcept
Default construct, an empty list without allocation.
Definition: DynamicListI.H:138
void swap(DynamicList< T, AnySizeMin > &other)
Swap content, independent of sizing parameter.
Definition: DynamicListI.H:444
DynamicList< T, SizeMin > & shrink()
Shrink the allocated space to the number of elements used.
Definition: DynamicListI.H:434
T remove()
Remove and return the last element. Fatal on an empty list.
Definition: DynamicListI.H:655
void resize_nocopy(const label len)
Definition: DynamicListI.H:363
void setCapacity_nocopy(const label len)
Definition: DynamicListI.H:313
void reserve_nocopy(const label len)
Definition: DynamicListI.H:343
void setCapacity_unsafe(const label len) noexcept
Definition: DynamicListI.H:323
void transfer(DynamicList< T, AnySizeMin > &list)
Transfer contents of any sized DynamicList into this.
Definition: DynamicListI.H:479
std::streamsize capacity_bytes() const noexcept
Number of contiguous bytes of the underlying storage.
Definition: DynamicListI.H:295
void clearStorage()
Clear the list and delete storage.
Definition: DynamicListI.H:398
label appendUniq(const T &val)
Append an element if not already in the list.
Definition: DynamicListI.H:640
label capacity() const noexcept
Size of the underlying storage.
Definition: DynamicListI.H:287
void shrinkStorage()
Shrink the allocated space to the number of elements used.
Definition: DynamicListI.H:418
label expandStorage() noexcept
Expand the addressable size to fit the allocated capacity.
Definition: DynamicListI.H:406
friend Ostream & operator(Ostream &os, const DynamicList< T, SizeMin > &list)
Write to Ostream.
void reserve(const label len)
Definition: DynamicListI.H:333
label subset(const labelRange &range)
Retain a (start,size) subset from the list.
Definition: DynamicListI.H:719
void setCapacity(const label len)
Alter the size of the underlying storage.
Definition: DynamicListI.H:303
virtual bool resize()
Resize the ODE solver.
Definition: Euler.C:53
A 1D vector of objects of type <T> with a fixed length <N>.
Definition: FixedList.H:81
static constexpr label size() noexcept
Return the number of elements in the FixedList.
Definition: FixedList.H:416
Base for lists with indirect addressing, templated on the list contents type and the addressing type....
label size() const noexcept
The number of elements in the list.
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition: List.H:77
A 1D vector of objects of type <T>, where the size of the vector is known and can be used for subscri...
Definition: UList.H:94
void size(const label n)
Older name for setAddressableSize.
Definition: UList.H:114
A range or interval of labels defined by a start and a size.
Definition: labelRange.H:58
bool append() const noexcept
True if output format uses an append mode.
A class representing the concept of 0 (zero) that can be used to avoid manipulating objects known to ...
Definition: zero.H:63
const volScalarField & T
patchWriters resize(patchIds.size())
patchWriters clear()
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:453
rAUs append(new volScalarField(IOobject::groupName("rAU", phase1.name()), 1.0/(U1Eqn.A()+byDt(max(phase1.residualAlpha() - alpha1, scalar(0)) *rho1))))
void Swap(DynamicList< T, SizeMinA > &a, DynamicList< T, SizeMinB > &b)
Definition: DynamicList.H:408
label min(const labelHashSet &set, label minValue=labelMax)
Find the min value in labelHashSet, optionally limited by second argument.
Definition: hashSets.C:33
errorManip< error > abort(error &err)
Definition: errorManip.H:144
static constexpr const zero Zero
Global zero (0)
Definition: zero.H:131
error FatalError