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 Foam::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>
224 inline Foam::label Foam::DynamicList<T, SizeMin>::capacity() const noexcept
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  // Cannot compare 'this' for different types, so use cdata()
401  if (this->cdata() == lst.cdata())
402  {
403  return; // Self-swap is a no-op
404  }
405 
406  DynamicList<T, SizeMin>& cur = *this;
407 
408  // Make addressable size identical to the allocated capacity
409  const label oldSize1 = cur.expandStorage();
410  const label oldSize2 = lst.expandStorage();
411 
412  // Swap storage
413  UList<T>::swap(lst);
414 
415  // Match capacity to the underlying allocated list size
416  cur.setCapacity(cur.size());
417  lst.setCapacity(lst.size());
418 
419  // Set addressable size
420  cur.setSize(oldSize2);
421  lst.setSize(oldSize1);
422 }
423 
424 
425 template<class T, int SizeMin>
426 inline void
428 {
429  // Take over storage, clear addressing for lst.
430  capacity_ = lst.size();
431  List<T>::transfer(lst);
432 }
433 
434 
435 template<class T, int SizeMin>
436 template<int AnySizeMin>
437 inline void
439 (
441 )
442 {
443  // Cannot compare 'this' for different types, so use cdata()
444  if (this->cdata() == lst.cdata())
445  {
446  return; // Self-assignment is a no-op
447  }
448 
449  // Take over storage as-is (without shrink, without using SizeMin)
450  // clear addressing and storage for old lst.
451  capacity_ = lst.capacity();
452 
453  List<T>::transfer(static_cast<List<T>&>(lst));
454  lst.clearStorage(); // Ensure capacity=0
455 }
456 
457 
458 template<class T, int SizeMin>
459 inline void
461 (
462  SortableList<T>& lst
463 )
464 {
465  lst.shrink(); // Shrink away sort indices
466  capacity_ = lst.size(); // Capacity after transfer == list size
467  List<T>::transfer(lst);
468 }
469 
470 
471 template<class T, int SizeMin>
474 (
475  const T& val
476 )
477 {
478  const label idx = List<T>::size();
479  setSize(idx + 1);
480 
481  this->operator[](idx) = val; // copy element
482  return *this;
483 }
484 
485 
486 template<class T, int SizeMin>
489 (
490  T&& val
491 )
492 {
493  const label idx = List<T>::size();
494  setSize(idx + 1);
495 
496  this->operator[](idx) = std::move(val); // move assign element
497  return *this;
498 }
499 
500 
501 template<class T, int SizeMin>
504 (
505  const UList<T>& lst
506 )
507 {
508  if (this == &lst)
509  {
511  << "Attempted appending to self" << abort(FatalError);
512  }
513 
514  label idx = List<T>::size();
515 
516  setSize(idx + lst.size());
517 
518  for (const T& val : lst)
519  {
520  this->operator[](idx++) = val; // copy element
521  }
522  return *this;
523 }
524 
525 
526 template<class T, int SizeMin>
527 template<unsigned N>
530 (
531  const FixedList<T, N>& lst
532 )
533 {
534  label idx = List<T>::size();
535  setSize(idx + lst.size());
536 
537  for (const T& val : lst)
538  {
539  this->operator[](idx++) = val; // copy element
540  }
541  return *this;
542 }
543 
544 
545 template<class T, int SizeMin>
548 (
549  std::initializer_list<T> lst
550 )
551 {
552  label idx = List<T>::size();
553 
554  setSize(idx + lst.size());
555 
556  for (const T& val : lst)
557  {
558  this->operator[](idx++) = val; // copy element
559  }
560  return *this;
561 }
562 
563 
564 template<class T, int SizeMin>
565 template<class Addr>
568 (
569  const IndirectListBase<T, Addr>& lst
570 )
571 {
572  label idx = List<T>::size();
573  const label n = lst.size();
574 
575  setSize(idx + n);
576 
577  for (label i=0; i<n; ++i)
578  {
579  this->operator[](idx++) = lst[i]; // copy element
580  }
581  return *this;
582 }
583 
584 
585 template<class T, int SizeMin>
588 (
589  List<T>&& lst
590 )
591 {
592  if (this == &lst)
593  {
595  << "Attempted appending to self" << abort(FatalError);
596  }
597 
598  label idx = List<T>::size();
599 
600  setSize(idx + lst.size());
601 
602  for (T& val : lst)
603  {
604  Foam::Swap(this->operator[](idx++), val); // moved content
605  }
606 
607  lst.clear();
608  return *this;
609 }
610 
611 
612 template<class T, int SizeMin>
615 (
617 )
618 {
619  append(std::move(static_cast<List<T>&>(lst)));
620  lst.clearStorage(); // Ensure capacity=0
621  return *this;
622 }
623 
624 
625 template<class T, int SizeMin>
626 template<int AnySizeMin>
629 (
631 )
632 {
633  append(std::move(static_cast<List<T>&>(lst)));
634  lst.clearStorage(); // Ensure capacity=0
635  return *this;
636 }
637 
638 
639 template<class T, int SizeMin>
642 (
643  SortableList<T>&& lst
644 )
645 {
646  lst.shrink(); // Shrink away sort indices
647  append(std::move(static_cast<List<T>&>(lst)));
648  return *this;
649 }
650 
651 
652 template<class T, int SizeMin>
654 {
655  // Location of last element and simultaneously the new size
656  const label idx = List<T>::size() - 1;
657 
658  if (idx < 0)
659  {
661  << "List is empty" << abort(FatalError);
662  }
663 
664  const T& val = List<T>::operator[](idx);
665 
666  List<T>::size(idx);
667 
668  return val;
669 }
670 
671 
672 template<class T, int SizeMin>
674 (
675  const label idx,
676  const bool fast
677 )
678 {
679  if (fast)
680  {
681  // Simply swap idx <=> last
682  this->swapLast(idx);
683  }
684  else
685  {
686  // Move element to the end and move everything down
687  this->moveLast(idx);
688  }
689 
690  // Element to remove is now at the end
691  return this->remove();
692 }
693 
694 
695 template<class T, int SizeMin>
696 inline Foam::label Foam::DynamicList<T, SizeMin>::remove
697 (
698  const labelRange& range
699 )
700 {
701  return this->removeElements(this->validateRange(range));
702 }
703 
704 
705 template<class T, int SizeMin>
706 inline Foam::label Foam::DynamicList<T, SizeMin>::remove
707 (
708  std::initializer_list<label> start_size
709 )
710 {
711  return this->removeElements(this->validateRange(start_size));
712 }
713 
714 
715 template<class T, int SizeMin>
716 inline Foam::label Foam::DynamicList<T, SizeMin>::subset
717 (
718  const labelRange& range
719 )
720 {
721  return this->subsetElements(this->validateRange(range));
722 }
723 
724 
725 template<class T, int SizeMin>
726 inline Foam::label Foam::DynamicList<T, SizeMin>::subset
727 (
728  std::initializer_list<label> start_size
729 )
730 {
731  return this->subsetElements(this->validateRange(start_size));
732 }
733 
734 
735 // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
736 
737 template<class T, int SizeMin>
739 (
740  const label i
741 )
742 {
743  if (i >= List<T>::size())
744  {
745  setSize(i + 1);
746  }
747 
748  return this->operator[](i);
749 }
750 
751 
752 template<class T, int SizeMin>
754 (
755  const T& val
756 )
757 {
758  UList<T>::operator=(val);
759 }
760 
761 
762 template<class T, int SizeMin>
764 (
765  const Foam::zero
766 )
767 {
769 }
770 
771 
772 template<class T, int SizeMin>
774 (
775  const UList<T>& lst
776 )
777 {
778  assignDynList(lst);
779 }
780 
781 
782 template<class T, int SizeMin>
783 template<unsigned N>
785 (
786  const FixedList<T, N>& lst
787 )
788 {
789  const label n = lst.size();
790 
791  setSize(n);
792 
793  for (label i=0; i<n; ++i)
794  {
795  this->operator[](i) = lst[i]; // copy element
796  }
797 }
798 
799 
800 template<class T, int SizeMin>
802 (
803  const DynamicList<T, SizeMin>& lst
804 )
805 {
806  if (this == &lst)
807  {
808  return; // Self-assignment is a no-op
809  }
810 
811  assignDynList(lst);
812 }
813 
814 
815 template<class T, int SizeMin>
816 template<int AnySizeMin>
818 (
819  const DynamicList<T, AnySizeMin>& list
820 )
821 {
822  // Cannot compare 'this' for different types, so use cdata()
823  if (this->cdata() == list.cdata())
824  {
825  return; // Self-assignment is a no-op
826  }
827 
828  assignDynList(list);
829 }
830 
831 
832 template<class T, int SizeMin>
834 (
835  std::initializer_list<T> lst
836 )
837 {
838  assignDynList(lst);
839 }
840 
841 
842 template<class T, int SizeMin>
843 template<class Addr>
845 (
846  const IndirectListBase<T, Addr>& lst
847 )
848 {
849  assignDynList(lst);
850 }
851 
852 
853 template<class T, int SizeMin>
855 (
856  List<T>&& lst
857 )
858 {
859  clear();
860  transfer(lst);
861 }
862 
863 
864 template<class T, int SizeMin>
866 (
868 )
869 {
870  if (this == &lst)
871  {
872  return; // Self-assignment is a no-op
873  }
874 
875  clear();
876  transfer(lst);
877 }
878 
879 
880 template<class T, int SizeMin>
881 template<int AnySizeMin>
883 (
885 )
886 {
887  // Cannot compare 'this' for different types, so use cdata()
888  if (this->cdata() == list.cdata())
889  {
890  return; // Self-assignment is a no-op
891  }
892 
893  clear();
894  transfer(list);
895 }
896 
897 
898 template<class T, int SizeMin>
900 (
901  SortableList<T>&& lst
902 )
903 {
904  clear();
905  transfer(lst);
906 }
907 
908 
909 // * * * * * * * * * * * * * * * Global Functions * * * * * * * * * * * * * //
910 
911 template<class T, int SizeMin1, int SizeMin2>
912 inline void Foam::Swap
913 (
916 )
917 {
918  a.swap(b);
919 }
920 
921 
922 // ************************************************************************* //
setSize
points setSize(newPointi)
Foam::DynamicList::subset
label subset(const labelRange &range)
Retain a (start,size) subset from the list.
Definition: DynamicListI.H:717
Foam::DynamicList::DynamicList
constexpr DynamicList() noexcept
Default construct, an empty list without allocation.
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 (0)
Definition: zero.H:131
Foam::DynamicList
A 1D vector of objects of type <T> that resizes itself as necessary to accept the new objects.
Definition: DynamicList.H:55
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:393
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
append
rAUs append(new volScalarField(IOobject::groupName("rAU", phase1.name()), 1.0/(U1Eqn.A()+byDt(max(phase1.residualAlpha() - alpha1, scalar(0)) *rho1))))
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:913
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::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:474
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:55
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:63
Foam::abort
errorManip< error > abort(error &err)
Definition: errorManip.H:144
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:381
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: BitOps.H:63
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:427
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:653
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)
Foam::IndirectListBase
Base for lists with indirect addressing, templated on the list contents type and the addressing type....
Definition: IndirectListBase.H:56
FixedList.H
Foam::IndirectListBase::size
label size() const
The number of elements in the list.
Definition: IndirectListBase.H:125
Foam::zero
A class representing the concept of 0 (zero) that can be used to avoid manipulating objects known to ...
Definition: zero.H:62