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-2019 OpenCFD Ltd.
10 -------------------------------------------------------------------------------
11 License
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 // * * * * * * * * * * * * Protected Member Functions * * * * * * * * * * * //
32 
33 template<class T, int SizeMin>
34 template<class ListType>
36 (
37  const ListType& lst
38 )
39 {
40  const label newSize = lst.size();
41 
42  if (capacity_ >= newSize)
43  {
44  // Can copy w/o reallocating - adjust addressable size accordingly.
45  List<T>::size(newSize);
46  List<T>::operator=(lst);
47  }
48  else
49  {
50  // Ensure list size consistency prior to copying.
51  List<T>::size(capacity_);
52 
53  List<T>::operator=(lst);
54  capacity_ = List<T>::size();
55  }
56 }
57 
58 
59 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
60 
61 template<class T, int SizeMin>
62 inline constexpr Foam::DynamicList<T, SizeMin>::DynamicList() noexcept
63 :
64  capacity_(0)
65 {}
66 
67 
68 template<class T, int SizeMin>
70 :
71  capacity_(0)
72 {
73  reserve(nElem);
74 }
75 
76 
77 template<class T, int SizeMin>
79 (
80  const label nElem,
81  const T& val
82 )
83 :
84  List<T>(nElem, val),
85  capacity_(List<T>::size())
86 {}
87 
88 
89 template<class T, int SizeMin>
91 (
92  const label nElem,
93  const zero
94 )
95 :
96  List<T>(nElem, Zero),
97  capacity_(List<T>::size())
98 {}
99 
100 
101 template<class T, int SizeMin>
103 (
104  const DynamicList<T, SizeMin>& lst
105 )
106 :
107  List<T>(lst),
108  capacity_(List<T>::size())
109 {}
110 
111 
112 template<class T, int SizeMin>
113 template<int AnySizeMin>
115 (
116  const DynamicList<T, AnySizeMin>& lst
117 )
118 :
119  List<T>(lst),
120  capacity_(List<T>::size())
121 {}
122 
123 
124 template<class T, int SizeMin>
126 (
127  const UList<T>& lst
128 )
129 :
130  List<T>(lst),
131  capacity_(List<T>::size())
132 {}
133 
134 
135 template<class T, int SizeMin>
136 template<unsigned N>
138 (
139  const FixedList<T, N>& lst
140 )
141 :
142  capacity_(0)
143 {
144  this->operator=(lst);
145 }
146 
147 
148 template<class T, int SizeMin>
149 template<class InputIterator>
151 (
152  InputIterator begIter,
153  InputIterator endIter
154 )
155 :
156  List<T>(begIter, endIter),
157  capacity_(List<T>::size())
158 {}
159 
160 
161 template<class T, int SizeMin>
163 (
164  std::initializer_list<T> lst
165 )
166 :
167  List<T>(lst),
168  capacity_(List<T>::size())
169 {}
170 
171 
172 template<class T, int SizeMin>
173 template<class Addr>
175 (
176  const IndirectListBase<T, Addr>& lst
177 )
178 :
179  List<T>(lst),
180  capacity_(List<T>::size())
181 {}
182 
183 
184 template<class T, int SizeMin>
186 (
188 )
189 :
190  capacity_(0)
191 {
192  transfer(lst);
193 }
194 
195 
196 template<class T, int SizeMin>
197 template<int AnySizeMin>
199 (
201 )
202 :
203  capacity_(0)
204 {
205  transfer(lst);
206 }
207 
208 
209 template<class T, int SizeMin>
211 (
212  List<T>&& lst
213 )
214 :
215  capacity_(0)
216 {
217  transfer(lst);
218 }
219 
220 
221 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
222 
223 template<class T, int SizeMin>
225 {
226  return capacity_;
227 }
228 
229 
230 template<class T, int SizeMin>
232 (
233  const label nElem
234 )
235 {
236  label nextFree = List<T>::size();
237  capacity_ = nElem;
238 
239  if (nextFree > capacity_)
240  {
241  // Truncate addressed sizes too
242  nextFree = capacity_;
243  }
244 
245  // We could also enforce sizing granularity
246 
247  List<T>::setSize(capacity_);
248  List<T>::size(nextFree);
249 }
250 
251 
252 template<class T, int SizeMin>
254 (
255  const label nElem
256 )
257 {
258  // Allocate more capacity if necessary
259  if (nElem > capacity_)
260  {
261  capacity_ = max
262  (
263  SizeMin,
264  max
265  (
266  nElem,
267  // label(SizeInc + capacity_ * SizeMult / SizeDiv)
268  label(2 * capacity_)
269  )
270  );
271 
272  // Adjust allocated size, leave addressed size untouched
273  const label nextFree = List<T>::size();
274  List<T>::setSize(capacity_);
275  List<T>::size(nextFree);
276  }
277 }
278 
279 
280 template<class T, int SizeMin>
282 (
283  const label nElem
284 )
285 {
286  // Allocate more capacity if necessary
287  if (nElem > capacity_)
288  {
289  capacity_ = max
290  (
291  SizeMin,
292  max
293  (
294  nElem,
295  // label(SizeInc + capacity_ * SizeMult / SizeDiv)
296  label(2 * capacity_)
297  )
298  );
299 
300  List<T>::setSize(capacity_);
301  }
302 
303  // Adjust addressed size
304  List<T>::size(nElem);
305 }
306 
307 
308 template<class T, int SizeMin>
310 (
311  const label nElem,
312  const T& val
313 )
314 {
315  label nextFree = List<T>::size();
316  setSize(nElem);
317 
318  // Set new elements to constant value
319  while (nextFree < nElem)
320  {
321  this->operator[](nextFree++) = val;
322  }
323 }
324 
325 
326 template<class T, int SizeMin>
328 (
329  const label nElem
330 )
331 {
332  this->setSize(nElem);
333 }
334 
335 
336 template<class T, int SizeMin>
338 (
339  const label nElem,
340  const T& val
341 )
342 {
343  this->setSize(nElem, val);
344 }
345 
346 
347 template<class T, int SizeMin>
349 {
350  List<T>::size(0);
351 }
352 
353 
354 template<class T, int SizeMin>
356 {
357  List<T>::clear();
358  capacity_ = 0;
359 }
360 
361 
362 template<class T, int SizeMin>
364 {
365  const label nextFree = List<T>::size();
366 
367  // Allow addressing into the entire list
368  List<T>::size(capacity_);
369 
370  return nextFree;
371 }
372 
373 
374 template<class T, int SizeMin>
377 {
378  const label nextFree = List<T>::size();
379  if (capacity_ > nextFree)
380  {
381  // Use the full list when resizing
382  List<T>::size(capacity_);
383 
384  // The new size
385  capacity_ = nextFree;
386  List<T>::setSize(capacity_);
387  List<T>::size(nextFree);
388  }
389  return *this;
390 }
391 
392 
393 template<class T, int SizeMin>
394 template<int AnySizeMin>
396 (
398 )
399 {
400  if (this == &lst)
401  {
402  return; // Self-swap is a no-op
403  }
404 
405  DynamicList<T, SizeMin>& cur = *this;
406 
407  // Make addressable size identical to the allocated capacity
408  const label oldSize1 = cur.expandStorage();
409  const label oldSize2 = lst.expandStorage();
410 
411  // Swap storage
412  UList<T>::swap(lst);
413 
414  // Match capacity to the underlying allocated list size
415  cur.setCapacity(cur.size());
416  lst.setCapacity(lst.size());
417 
418  // Set addressable size
419  cur.setSize(oldSize2);
420  lst.setSize(oldSize1);
421 }
422 
423 
424 template<class T, int SizeMin>
425 inline void
427 {
428  // Take over storage, clear addressing for lst.
429  capacity_ = lst.size();
430  List<T>::transfer(lst);
431 }
432 
433 
434 template<class T, int SizeMin>
435 template<int AnySizeMin>
436 inline void
438 (
440 )
441 {
442  if (this == &lst)
443  {
444  return; // Self-assignment is a no-op
445  }
446 
447  // Take over storage as-is (without shrink, without using SizeMin)
448  // clear addressing and storage for old lst.
449  capacity_ = lst.capacity();
450 
451  List<T>::transfer(static_cast<List<T>&>(lst));
452  lst.clearStorage(); // Ensure capacity=0
453 }
454 
455 
456 template<class T, int SizeMin>
457 inline void
459 (
460  SortableList<T>& lst
461 )
462 {
463  lst.shrink(); // Shrink away sort indices
464  capacity_ = lst.size(); // Capacity after transfer == list size
465  List<T>::transfer(lst);
466 }
467 
468 
469 template<class T, int SizeMin>
472 (
473  const T& val
474 )
475 {
476  const label idx = List<T>::size();
477  setSize(idx + 1);
478 
479  this->operator[](idx) = val; // copy element
480  return *this;
481 }
482 
483 
484 template<class T, int SizeMin>
487 (
488  T&& val
489 )
490 {
491  const label idx = List<T>::size();
492  setSize(idx + 1);
493 
494  this->operator[](idx) = std::move(val); // move assign element
495  return *this;
496 }
497 
498 
499 template<class T, int SizeMin>
502 (
503  const UList<T>& lst
504 )
505 {
506  if (this == &lst)
507  {
509  << "Attempted appending to self" << abort(FatalError);
510  }
511 
512  label idx = List<T>::size();
513 
514  setSize(idx + lst.size());
515 
516  for (const T& val : lst)
517  {
518  this->operator[](idx++) = val; // copy element
519  }
520  return *this;
521 }
522 
523 
524 template<class T, int SizeMin>
525 template<unsigned N>
528 (
529  const FixedList<T, N>& lst
530 )
531 {
532  label idx = List<T>::size();
533  setSize(idx + lst.size());
534 
535  for (const T& val : lst)
536  {
537  this->operator[](idx++) = val; // copy element
538  }
539  return *this;
540 }
541 
542 
543 template<class T, int SizeMin>
546 (
547  std::initializer_list<T> lst
548 )
549 {
550  label idx = List<T>::size();
551 
552  setSize(idx + lst.size());
553 
554  for (const T& val : lst)
555  {
556  this->operator[](idx++) = val; // copy element
557  }
558  return *this;
559 }
560 
561 
562 template<class T, int SizeMin>
563 template<class Addr>
566 (
567  const IndirectListBase<T, Addr>& lst
568 )
569 {
570  label idx = List<T>::size();
571  const label n = lst.size();
572 
573  setSize(idx + n);
574 
575  for (label i=0; i<n; ++i)
576  {
577  this->operator[](idx++) = lst[i]; // copy element
578  }
579  return *this;
580 }
581 
582 
583 template<class T, int SizeMin>
586 (
587  List<T>&& lst
588 )
589 {
590  if (this == &lst)
591  {
593  << "Attempted appending to self" << abort(FatalError);
594  }
595 
596  label idx = List<T>::size();
597 
598  setSize(idx + lst.size());
599 
600  for (T& val : lst)
601  {
602  Foam::Swap(this->operator[](idx++), val); // moved content
603  }
604 
605  lst.clear();
606  return *this;
607 }
608 
609 
610 template<class T, int SizeMin>
613 (
615 )
616 {
617  append(std::move(static_cast<List<T>&>(lst)));
618  lst.clearStorage(); // Ensure capacity=0
619  return *this;
620 }
621 
622 
623 template<class T, int SizeMin>
624 template<int AnySizeMin>
627 (
629 )
630 {
631  append(std::move(static_cast<List<T>&>(lst)));
632  lst.clearStorage(); // Ensure capacity=0
633  return *this;
634 }
635 
636 
637 template<class T, int SizeMin>
640 (
641  SortableList<T>&& lst
642 )
643 {
644  lst.shrink(); // Shrink away sort indices
645  append(std::move(static_cast<List<T>&>(lst)));
646  return *this;
647 }
648 
649 
650 template<class T, int SizeMin>
652 {
653  // Location of last element and simultaneously the new size
654  const label idx = List<T>::size() - 1;
655 
656  if (idx < 0)
657  {
659  << "List is empty" << abort(FatalError);
660  }
661 
662  const T& val = List<T>::operator[](idx);
663 
664  List<T>::size(idx);
665 
666  return val;
667 }
668 
669 
670 template<class T, int SizeMin>
672 (
673  const label idx,
674  const bool fast
675 )
676 {
677  if (fast)
678  {
679  // Simply swap idx <=> last
680  this->swapLast(idx);
681  }
682  else
683  {
684  // Move element to the end and move everything down
685  this->moveLast(idx);
686  }
687 
688  // Element to remove is now at the end
689  return this->remove();
690 }
691 
692 
693 template<class T, int SizeMin>
695 (
696  const labelRange& range
697 )
698 {
699  return this->removeElements(this->validateRange(range));
700 }
701 
702 
703 template<class T, int SizeMin>
705 (
706  std::initializer_list<label> start_size
707 )
708 {
709  return this->removeElements(this->validateRange(start_size));
710 }
711 
712 
713 template<class T, int SizeMin>
715 (
716  const labelRange& range
717 )
718 {
719  return this->subsetElements(this->validateRange(range));
720 }
721 
722 
723 template<class T, int SizeMin>
725 (
726  std::initializer_list<label> start_size
727 )
728 {
729  return this->subsetElements(this->validateRange(start_size));
730 }
731 
732 
733 // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
734 
735 template<class T, int SizeMin>
737 (
738  const label i
739 )
740 {
741  if (i >= List<T>::size())
742  {
743  setSize(i + 1);
744  }
745 
746  return this->operator[](i);
747 }
748 
749 
750 template<class T, int SizeMin>
752 (
753  const T& val
754 )
755 {
757 }
758 
759 
760 template<class T, int SizeMin>
762 (
763  const zero
764 )
765 {
767 }
768 
769 
770 template<class T, int SizeMin>
772 (
773  const UList<T>& lst
774 )
775 {
776  assignDynList(lst);
777 }
778 
779 
780 template<class T, int SizeMin>
781 template<unsigned N>
783 (
784  const FixedList<T, N>& lst
785 )
786 {
787  const label n = lst.size();
788 
789  setSize(n);
790 
791  for (label i=0; i<n; ++i)
792  {
793  this->operator[](i) = lst[i]; // copy element
794  }
795 }
796 
797 
798 template<class T, int SizeMin>
800 (
801  const DynamicList<T, SizeMin>& lst
802 )
803 {
804  if (this == &lst)
805  {
806  return; // Self-assignment is a no-op
807  }
808 
809  assignDynList(lst);
810 }
811 
812 
813 template<class T, int SizeMin>
814 template<int SizeMin2>
816 (
817  const DynamicList<T, SizeMin2>& lst
818 )
819 {
820  if (this == &lst)
821  {
822  return; // Self-assignment is a no-op
823  }
824 
825  assignDynList(lst);
826 }
827 
828 
829 template<class T, int SizeMin>
831 (
832  std::initializer_list<T> lst
833 )
834 {
835  assignDynList(lst);
836 }
837 
838 
839 template<class T, int SizeMin>
840 template<class Addr>
842 (
843  const IndirectListBase<T, Addr>& lst
844 )
845 {
846  assignDynList(lst);
847 }
848 
849 
850 template<class T, int SizeMin>
852 (
853  List<T>&& lst
854 )
855 {
856  clear();
857  transfer(lst);
858 }
859 
860 
861 template<class T, int SizeMin>
863 (
865 )
866 {
867  if (this == &lst)
868  {
869  return; // Self-assignment is a no-op
870  }
871 
872  clear();
873  transfer(lst);
874 }
875 
876 
877 template<class T, int SizeMin>
878 template<int SizeMin2>
880 (
882 )
883 {
884  if (this == &lst)
885  {
886  return; // Self-assignment is a no-op
887  }
888 
889  clear();
890  transfer(lst);
891 }
892 
893 
894 template<class T, int SizeMin>
896 (
897  SortableList<T>&& lst
898 )
899 {
900  clear();
901  transfer(lst);
902 }
903 
904 
905 // * * * * * * * * * * * * * * * Global Functions * * * * * * * * * * * * * //
906 
907 template<class T, int SizeMin1, int SizeMin2>
908 inline void Foam::Swap
909 (
912 )
913 {
914  a.swap(b);
915 }
916 
917 
918 // ************************************************************************* //
setSize
points setSize(newPointi)
Foam::DynamicList::subset
label subset(const labelRange &range)
Retain a (start,size) subset from the list.
Definition: DynamicListI.H:715
Foam::val
label ListType::const_reference val
Definition: ListOps.H:407
Foam::DynamicList::DynamicList
constexpr DynamicList() noexcept
Construct null.
Definition: DynamicListI.H:62
Foam::DynamicList::assignDynList
void assignDynList(const ListType &lst)
Copy assignment from another list.
Definition: DynamicListI.H:36
Foam::Zero
static constexpr const zero Zero
Global zero.
Definition: zero.H:128
Foam::DynamicList
A 1D vector of objects of type <T> that resizes itself as necessary to accept the new objects.
Definition: DynamicList.H:57
Foam::DynamicList::expandStorage
label expandStorage()
Expand the addressable size to fit the allocated capacity.
Definition: DynamicListI.H:363
Foam::FixedList::size
static constexpr label size() noexcept
Return the number of elements in the FixedList.
Definition: FixedList.H:368
Foam::DynamicList::swap
void swap(DynamicList< T, AnySizeMin > &lst)
Swap content with any sized DynamicList.
Definition: DynamicListI.H:396
Foam::DynamicList::capacity
label capacity() const noexcept
Size of the underlying storage.
Definition: DynamicListI.H:224
Foam::DynamicList::shrink
DynamicList< T, SizeMin > & shrink()
Shrink the allocated space to the number of elements used.
Definition: DynamicListI.H:376
Foam::Swap
void Swap(DynamicList< T, SizeMin1 > &a, DynamicList< T, SizeMin2 > &b)
Definition: DynamicListI.H:909
Foam::DynamicList::reserve
void reserve(const label nElem)
Reserve allocation space for at least this size.
Definition: DynamicListI.H:254
n
label n
Definition: TABSMDCalcMethod2.H:31
Foam::label
intWM_LABEL_SIZE_t label
A label is an int32_t or int64_t as specified by the pre-processor macro WM_LABEL_SIZE.
Definition: label.H:62
Foam::constant::physicoChemical::b
const dimensionedScalar b
Wien displacement law constant: default SI units: [m.K].
Definition: createFields.H:27
Foam::DynamicList::append
DynamicList< T, SizeMin > & append(const T &val)
Append an element to the end of this list.
Definition: DynamicListI.H:472
Foam::DynamicList::clear
void clear()
Clear the addressed list, i.e. set the size to zero.
Definition: DynamicListI.H:348
Foam::labelRange
A range or interval of labels defined by a start and a size.
Definition: labelRange.H:58
Foam::max
label max(const labelHashSet &set, label maxValue=labelMin)
Find the max value in labelHashSet, optionally limited by second argument.
Definition: hashSets.C:47
Foam::SortableList::shrink
List< T > & shrink()
Clear the indices and return a reference to the underlying List.
Definition: SortableList.C:130
Foam::FatalError
error FatalError
Foam::SortableList
A list that is sorted upon construction or when explicitly requested with the sort() method.
Definition: List.H:66
Foam::abort
errorManip< error > abort(error &err)
Definition: errorManip.H:137
T
const volScalarField & T
Definition: createFieldRefs.H:2
Foam::DynamicList::setSize
void setSize(const label nElem)
Alter addressable list size.
Definition: DynamicListI.H:282
Foam::DynamicList::setCapacity
void setCapacity(const label nElem)
Alter the size of the underlying storage.
Definition: DynamicListI.H:232
Foam::DynamicList::resize
void resize(const label nElem)
Alter addressable list size.
Definition: DynamicListI.H:328
FatalErrorInFunction
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:355
range
scalar range
Definition: LISASMDCalcMethod1.H:12
clear
patchWriters clear()
Foam::List
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition: HashTable.H:102
Foam::FixedList
A 1D vector of objects of type <T> with a fixed length <N>.
Definition: HashTable.H:104
Foam::UList
A 1D vector of objects of type <T>, where the size of the vector is known and can be used for subscri...
Definition: HashTable.H:103
Foam::DynamicList::transfer
void transfer(List< T > &lst)
Transfer contents of the argument List into this.
Definition: DynamicListI.H:426
Foam::DynamicList::clearStorage
void clearStorage()
Clear the list and delete storage.
Definition: DynamicListI.H:355
Foam::DynamicList::remove
T remove()
Remove and return the last element. Fatal on an empty list.
Definition: DynamicListI.H:651
Foam::UList::size
void size(const label n) noexcept
Override size to be inconsistent with allocated storage.
Definition: UListI.H:360
Foam::DynamicList::operator
friend Ostream & operator(Ostream &os, const DynamicList< T, SizeMin > &lst)
append
rAUs append(new volScalarField(IOobject::groupName("rAU", phase1.name()), 1.0/(U1Eqn.A()+byDt(max(phase1.residualAlpha() - alpha1, scalar(0)) *rho1))))
Foam::IndirectListBase
Base for lists with indirect addressing, templated on the list contents type and the addressing type....
Definition: IndirectListBase.H:57
FixedList.H
Foam::IndirectListBase::size
label size() const
The number of elements in the list.
Definition: IndirectListBase.H:126
Foam::zero
A class representing the concept of 0 (zero), which can be used to avoid manipulating objects that ar...
Definition: zero.H:61