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 | Copyright (C) 2011-2016 OpenFOAM Foundation
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8 License
9  This file is part of OpenFOAM.
10 
11  OpenFOAM is free software: you can redistribute it and/or modify it
12  under the terms of the GNU General Public License as published by
13  the Free Software Foundation, either version 3 of the License, or
14  (at your option) any later version.
15 
16  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
17  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19  for more details.
20 
21  You should have received a copy of the GNU General Public License
22  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
23 
24 \*---------------------------------------------------------------------------*/
25 
26 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
27 
28 template<class T, unsigned SizeInc, unsigned SizeMult, unsigned SizeDiv>
30 :
31  capacity_(0)
32 {}
33 
34 
35 template<class T, unsigned SizeInc, unsigned SizeMult, unsigned SizeDiv>
37 (
38  const label nElem
39 )
40 :
41  List<T>(nElem),
42  capacity_(nElem)
43 {
44  // We could also enforce SizeInc granularity when (!SizeMult || !SizeDiv)
45  List<T>::size(0);
46 }
47 
48 
49 template<class T, unsigned SizeInc, unsigned SizeMult, unsigned SizeDiv>
51 (
52  const label nElem,
53  const T& a
54 )
55 :
56  List<T>(nElem, a),
57  capacity_(nElem)
58 {}
59 
60 
61 template<class T, unsigned SizeInc, unsigned SizeMult, unsigned SizeDiv>
63 (
65 )
66 :
67  List<T>(lst),
68  capacity_(lst.size())
69 {}
70 
71 
72 template<class T, unsigned SizeInc, unsigned SizeMult, unsigned SizeDiv>
74 (
75  const UList<T>& lst
76 )
77 :
78  List<T>(lst),
79  capacity_(lst.size())
80 {}
81 
82 
83 template<class T, unsigned SizeInc, unsigned SizeMult, unsigned SizeDiv>
85 (
86  const UIndirectList<T>& lst
87 )
88 :
89  List<T>(lst),
90  capacity_(lst.size())
91 {}
92 
93 
94 template<class T, unsigned SizeInc, unsigned SizeMult, unsigned SizeDiv>
96 (
97  const Xfer<List<T>>& lst
98 )
99 :
100  List<T>(lst),
101  capacity_(List<T>::size())
102 {}
103 
104 
105 
106 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
107 
108 template<class T, unsigned SizeInc, unsigned SizeMult, unsigned SizeDiv>
110 const
111 {
112  return capacity_;
113 }
114 
115 
116 template<class T, unsigned SizeInc, unsigned SizeMult, unsigned SizeDiv>
118 (
119  const label nElem
120 )
121 {
122  label nextFree = List<T>::size();
123  capacity_ = nElem;
124 
125  if (nextFree > capacity_)
126  {
127  // Truncate addressed sizes too
128  nextFree = capacity_;
129  }
130 
131  // We could also enforce SizeInc granularity when (!SizeMult || !SizeDiv)
132 
133  List<T>::setSize(capacity_);
134  List<T>::size(nextFree);
135 }
136 
137 
138 template<class T, unsigned SizeInc, unsigned SizeMult, unsigned SizeDiv>
140 (
141  const label nElem
142 )
143 {
144  // Allocate more capacity if necessary
145  if (nElem > capacity_)
146  {
147  capacity_ = max
148  (
149  nElem,
150  label(SizeInc + capacity_ * SizeMult / SizeDiv)
151  );
152 
153  // Adjust allocated size, leave addressed size untouched
154  label nextFree = List<T>::size();
155  List<T>::setSize(capacity_);
156  List<T>::size(nextFree);
157  }
158 }
159 
160 
161 template<class T, unsigned SizeInc, unsigned SizeMult, unsigned SizeDiv>
163 (
164  const label nElem
165 )
166 {
167  // Allocate more capacity if necessary
168  if (nElem > capacity_)
169  {
170  capacity_ = max
171  (
172  nElem,
173  label(SizeInc + capacity_ * SizeMult / SizeDiv)
174  );
175 
176  List<T>::setSize(capacity_);
177  }
178 
179  // Adjust addressed size
180  List<T>::size(nElem);
181 }
182 
183 
184 template<class T, unsigned SizeInc, unsigned SizeMult, unsigned SizeDiv>
186 (
187  const label nElem,
188  const T& t
189 )
190 {
191  label nextFree = List<T>::size();
192  setSize(nElem);
193 
194  // Set new elements to constant value
195  while (nextFree < nElem)
196  {
197  this->operator[](nextFree++) = t;
198  }
199 }
200 
201 
202 template<class T, unsigned SizeInc, unsigned SizeMult, unsigned SizeDiv>
204 (
205  const label nElem
206 )
207 {
208  this->setSize(nElem);
209 }
210 
211 
212 template<class T, unsigned SizeInc, unsigned SizeMult, unsigned SizeDiv>
214 (
215  const label nElem,
216  const T& t
217 )
218 {
219  this->setSize(nElem, t);
220 }
221 
222 
223 template<class T, unsigned SizeInc, unsigned SizeMult, unsigned SizeDiv>
225 {
226  List<T>::size(0);
227 }
228 
229 
230 template<class T, unsigned SizeInc, unsigned SizeMult, unsigned SizeDiv>
232 {
233  List<T>::clear();
234  capacity_ = 0;
235 }
236 
237 
238 template<class T, unsigned SizeInc, unsigned SizeMult, unsigned SizeDiv>
241 {
242  label nextFree = List<T>::size();
243  if (capacity_ > nextFree)
244  {
245  // Use the full list when resizing
246  List<T>::size(capacity_);
247 
248  // The new size
249  capacity_ = nextFree;
250  List<T>::setSize(capacity_);
251  List<T>::size(nextFree);
252  }
253  return *this;
254 }
255 
256 
257 template<class T, unsigned SizeInc, unsigned SizeMult, unsigned SizeDiv>
258 inline void
260 {
261  // Take over storage, clear addressing for lst.
262  capacity_ = lst.size();
263  List<T>::transfer(lst);
264 }
265 
266 
267 template<class T, unsigned SizeInc, unsigned SizeMult, unsigned SizeDiv>
268 inline void
270 (
272 )
273 {
274  // Take over storage as-is (without shrink), clear addressing for lst.
275  capacity_ = lst.capacity_;
276  lst.capacity_ = 0;
277  List<T>::transfer(static_cast<List<T>&>(lst));
278 }
279 
280 
281 template<class T, unsigned SizeInc, unsigned SizeMult, unsigned SizeDiv>
284 {
285  return xferMoveTo<List<T>>(*this);
286 }
287 
288 
289 template<class T, unsigned SizeInc, unsigned SizeMult, unsigned SizeDiv>
292 (
293  const T& t
294 )
295 {
296  const label elemI = List<T>::size();
297  setSize(elemI + 1);
298 
299  this->operator[](elemI) = t;
300  return *this;
301 }
302 
303 
304 template<class T, unsigned SizeInc, unsigned SizeMult, unsigned SizeDiv>
307 (
308  const UList<T>& lst
309 )
310 {
311  if (this == &lst)
312  {
314  << "Attempted appending to self" << abort(FatalError);
315  }
316 
317  label nextFree = List<T>::size();
318  setSize(nextFree + lst.size());
319 
320  forAll(lst, elemI)
321  {
322  this->operator[](nextFree++) = lst[elemI];
323  }
324  return *this;
325 }
326 
327 
328 template<class T, unsigned SizeInc, unsigned SizeMult, unsigned SizeDiv>
331 (
332  const UIndirectList<T>& lst
333 )
334 {
335  label nextFree = List<T>::size();
336  setSize(nextFree + lst.size());
337 
338  forAll(lst, elemI)
339  {
340  this->operator[](nextFree++) = lst[elemI];
341  }
342  return *this;
343 }
344 
345 
346 template<class T, unsigned SizeInc, unsigned SizeMult, unsigned SizeDiv>
348 {
349  const label elemI = List<T>::size() - 1;
350 
351  if (elemI < 0)
352  {
354  << "List is empty" << abort(FatalError);
355  }
356 
357  const T& val = List<T>::operator[](elemI);
358 
359  List<T>::size(elemI);
360 
361  return val;
362 }
363 
364 
365 // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
366 
367 template<class T, unsigned SizeInc, unsigned SizeMult, unsigned SizeDiv>
369 (
370  const label elemI
371 )
372 {
373  if (elemI >= List<T>::size())
374  {
375  setSize(elemI + 1);
376  }
377 
378  return this->operator[](elemI);
379 }
380 
381 
382 template<class T, unsigned SizeInc, unsigned SizeMult, unsigned SizeDiv>
383 inline void Foam::DynamicList<T, SizeInc, SizeMult, SizeDiv>::operator=
384 (
385  const T& t
386 )
387 {
389 }
390 
391 
392 template<class T, unsigned SizeInc, unsigned SizeMult, unsigned SizeDiv>
393 inline void Foam::DynamicList<T, SizeInc, SizeMult, SizeDiv>::operator=
394 (
396 )
397 {
398  if (this == &lst)
399  {
401  << "Attempted assignment to self" << abort(FatalError);
402  }
403 
404  if (capacity_ >= lst.size())
405  {
406  // Can copy w/o reallocating, match initial size to avoid reallocation
407  List<T>::size(lst.size());
408  List<T>::operator=(lst);
409  }
410  else
411  {
412  // Make everything available for the copy operation
413  List<T>::size(capacity_);
414 
415  List<T>::operator=(lst);
416  capacity_ = List<T>::size();
417  }
418 }
419 
420 
421 template<class T, unsigned SizeInc, unsigned SizeMult, unsigned SizeDiv>
422 inline void Foam::DynamicList<T, SizeInc, SizeMult, SizeDiv>::operator=
423 (
424  const UList<T>& lst
425 )
426 {
427  if (capacity_ >= lst.size())
428  {
429  // Can copy w/o reallocating, match initial size to avoid reallocation
430  List<T>::size(lst.size());
431  List<T>::operator=(lst);
432  }
433  else
434  {
435  // Make everything available for the copy operation
436  List<T>::size(capacity_);
437 
438  List<T>::operator=(lst);
439  capacity_ = List<T>::size();
440  }
441 }
442 
443 
444 template<class T, unsigned SizeInc, unsigned SizeMult, unsigned SizeDiv>
445 inline void Foam::DynamicList<T, SizeInc, SizeMult, SizeDiv>::operator=
446 (
447  const UIndirectList<T>& lst
448 )
449 {
450  if (capacity_ >= lst.size())
451  {
452  // Can copy w/o reallocating, match initial size to avoid reallocation
453  List<T>::size(lst.size());
454  List<T>::operator=(lst);
455  }
456  else
457  {
458  // Make everything available for the copy operation
459  List<T>::size(capacity_);
460 
461  List<T>::operator=(lst);
462  capacity_ = List<T>::size();
463  }
464 }
465 
466 
467 // * * * * * * * * * * * * * * STL Member Functions * * * * * * * * * * * * //
468 
469 template<class T, unsigned SizeInc, unsigned SizeMult, unsigned SizeDiv>
472 (
473  typename UList<T>::iterator curIter
474 )
475 {
476  typename Foam::UList<T>::iterator iter = curIter;
477  typename Foam::UList<T>::iterator nextIter = curIter;
478 
479  if (iter != this->end())
480  {
481  ++iter;
482 
483  while (iter != this->end())
484  {
485  *nextIter++ = *iter++;
486  }
487 
488  this->setSize(this->size() - 1);
489  }
490 
491  return curIter;
492 }
493 
494 
495 // ************************************************************************* //
A simple container for copying or transferring objects of type <T>.
Definition: Xfer.H:85
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:428
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:59
void resize(const label)
Alter the addressed list size.
Definition: DynamicListI.H:204
error FatalError
dimensioned< Type > max(const dimensioned< Type > &, const dimensioned< Type > &)
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:319
tUEqn clear()
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition: HashTable.H:60
UList< T >::iterator erase(typename UList< T >::iterator)
Erase an element, move the remaining elements to fill the gap.
Definition: DynamicListI.H:472
void size(const label)
Override size to be inconsistent with allocated storage.
Definition: ListI.H:163
T * iterator
Random access iterator for traversing UList.
Definition: UList.H:272
void reserve(const label)
Reserve allocation space for at least this size.
Definition: DynamicListI.H:140
points setSize(newPointi)
void setSize(const label)
Alter the addressed list size.
Definition: DynamicListI.H:163
A 1D vector of objects of type <T> that resizes itself as necessary to accept the new objects...
Definition: DynamicList.H:56
DynamicList()
Construct null.
Definition: DynamicListI.H:29
label size() const
Return the number of elements in the list.
void setCapacity(const label)
Alter the size of the underlying storage.
Definition: DynamicListI.H:118
Xfer< List< T > > xfer()
Transfer contents to the Xfer container as a plain List.
Definition: DynamicListI.H:283
DynamicList< T, SizeInc, SizeMult, SizeDiv > & append(const T &)
Append an element at the end of the list.
Definition: DynamicListI.H:292
errorManip< error > abort(error &err)
Definition: errorManip.H:131
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:61
DynamicList< T, SizeInc, SizeMult, SizeDiv > & shrink()
Shrink the allocated space to the number of elements used.
Definition: DynamicListI.H:240
const volScalarField & T
T remove()
Remove and return the top element.
Definition: DynamicListI.H:347
A List with indirect addressing.
Definition: fvMatrix.H:106
void clearStorage()
Clear the list and delete storage.
Definition: DynamicListI.H:231
label capacity() const
Size of the underlying storage.
Definition: DynamicListI.H:109
label size() const
Return the number of elements in the UList.
Definition: UListI.H:299
void clear()
Clear the addressed list, i.e. set the size to zero.
Definition: DynamicListI.H:224
void transfer(List< T > &)
Transfer contents of the argument List into this.
Definition: DynamicListI.H:259
friend Ostream & operator(Ostream &, const DynamicList< T, SizeInc, SizeMult, SizeDiv > &)