LongListI.H
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | cfMesh: A library for mesh generation
4  \\ / O peration |
5  \\ / A nd | Author: Franjo Juretic (franjo.juretic@c-fields.com)
6  \\/ M anipulation | Copyright (C) Creative Fields, Ltd.
7 -------------------------------------------------------------------------------
8 License
9  This file is part of cfMesh.
10 
11  cfMesh is free software; you can redistribute it and/or modify it
12  under the terms of the GNU General Public License as published by the
13  Free Software Foundation; either version 3 of the License, or (at your
14  option) any later version.
15 
16  cfMesh 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 cfMesh. If not, see <http://www.gnu.org/licenses/>.
23 
24 \*---------------------------------------------------------------------------*/
25 
26 #include "contiguous.H"
27 
28 template<class T, Foam::label Offset>
30 {
31  if( (i < 0) || (i >= nextFree_) )
32  {
34  (
35  "void Foam::LongList<T, label>::"
36  "checkIndex(const label i) const"
37  ) << "Index " << i << " is not in range " << 0
38  << " and " << nextFree_ << abort(FatalError);
39  }
40 }
41 
42 template<class T, Foam::label Offset>
44 {
45  unsigned int t = sizeof(T);
46  label it(0);
47 
48  while( t > 1 )
49  {
50  t >>= 1;
51  ++it;
52  }
53 
54  shift_ = Foam::max(10, Offset - it);
55  mask_ = 1<<shift_;
56  mask_ -= 1;
57 }
58 
59 template<class T, Foam::label Offset>
61 {
62  if( s == 0 )
63  {
64  clearOut();
65  return;
66  }
67  else if( s < 0 )
68  {
70  (
71  "template<class T, Foam::label Offset>\n"
72  "inline void Foam::LongList<T, Offset>::allocateSize(const label)"
73  ) << "Negative size requested." << abort(FatalError);
74  }
75 
76  const label numblock1 = ((s-1)>>shift_) + 1;
77  const label blockSize = 1<<shift_;
78 
79  if( numblock1 < numBlocks_ )
80  {
81  for(label i=numblock1;i<numBlocks_;++i)
82  delete [] dataPtr_[i];
83  }
84  else if( numblock1 > numBlocks_ )
85  {
86  if( numblock1 >= numAllocatedBlocks_ )
87  {
88  do
89  {
90  numAllocatedBlocks_ += 64;
91  } while( numblock1 > numAllocatedBlocks_ );
92 
93  T** dataptr1 = new T*[numAllocatedBlocks_];
94 
95  for(label i=0;i<numBlocks_;++i)
96  dataptr1[i] = dataPtr_[i];
97 
98  if( dataPtr_ )
99  delete [] dataPtr_;
100  dataPtr_ = dataptr1;
101  }
102 
103  for(label i=numBlocks_;i<numblock1;++i)
104  dataPtr_[i] = new T[blockSize];
105  }
106 
107  numBlocks_ = numblock1;
108  N_ = numBlocks_ * blockSize;
109 }
110 
111 template<class T, Foam::label Offset>
113 {
114  for(label i=0;i<numBlocks_;++i)
115  delete [] dataPtr_[i];
116 
117  if( dataPtr_ )
118  {
119  delete [] dataPtr_;
120  dataPtr_ = NULL;
121  }
122 
123  N_ = 0;
124  numBlocks_ = 0;
125  numAllocatedBlocks_ = 0;
126  nextFree_ = 0;
127 }
128 
129 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
130 
131 //- Construct null
132 template<class T, Foam::label Offset>
134 :
135  N_(0),
136  nextFree_(0),
137  numBlocks_(0),
138  numAllocatedBlocks_(0),
139  shift_(),
140  mask_(),
141  dataPtr_(NULL)
142 {
143  initializeParameters();
144 }
145 
146 //- Construct given size
147 template<class T, Foam::label Offset>
149 :
150  N_(0),
151  nextFree_(0),
152  numBlocks_(0),
153  numAllocatedBlocks_(0),
154  shift_(),
155  mask_(),
156  dataPtr_(NULL)
157 {
158  initializeParameters();
159  setSize(s);
160 }
161 
162 
163 //- Construct given size
164 template<class T, Foam::label Offset>
166 :
167  N_(0),
168  nextFree_(0),
169  numBlocks_(0),
170  numAllocatedBlocks_(0),
171  shift_(),
172  mask_(),
173  dataPtr_(NULL)
174 {
175  initializeParameters();
176  setSize(s);
177  *this = t;
178 }
179 
180 template<class T, Foam::label Offset>
182 :
183  N_(0),
184  nextFree_(0),
185  numBlocks_(0),
186  numAllocatedBlocks_(0),
187  shift_(ol.shift_),
188  mask_(ol.mask_),
189  dataPtr_(NULL)
190 {
191  *this = ol;
192 }
193 
194 template<class T, Foam::label Offset>
196 {
197  clearOut();
198 }
199 
200 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
201 
202 template<class T, Foam::label Offset>
204 {
205  return nextFree_;
206 }
207 
208 template<class T, Foam::label Offset>
210 {
211  if( !contiguous<T>() )
212  {
213  FatalErrorIn("LongList<T, Offset>::byteSize()")
214  << "Cannot return the binary size of a list of "
215  "non-primitive elements"
216  << abort(FatalError);
217  }
218 
219  return nextFree_*sizeof(T);
220 }
221 
222 template<class T, Foam::label Offset>
224 {
225  allocateSize(i);
226  nextFree_ = i;
227 }
228 
229 template<class T, Foam::label Offset>
231 {
232  nextFree_ = 0;
233 }
234 
235 
236 template<class T, Foam::label Offset>
239 {
240  setSize(nextFree_);
241  return *this;
242 }
243 
244 template<class T, Foam::label Offset>
246 {
247  clearOut();
248  dataPtr_ = ol.dataPtr_;
249  N_ = ol.N_;
250  nextFree_ = ol.nextFree_;
251  numBlocks_ = ol.numBlocks_;
252  numAllocatedBlocks_ = ol.numAllocatedBlocks_;
253  shift_ = ol.shift_;
254  mask_ = ol.mask_;
255 
256  ol.dataPtr_ = NULL;
257  ol.N_ = 0;
258  ol.nextFree_ = 0;
259  ol.numBlocks_ = 0;
260  ol.numAllocatedBlocks_ = 0;
261 }
262 
263 
264 template<class T, Foam::label Offset>
266 {
267  if( nextFree_ >= N_ )
268  {
269  allocateSize(nextFree_+1);
270  }
271 
272  operator[](nextFree_++) = e;
273 }
274 
275 template<class T, Foam::label Offset>
277 {
278  if( !contains(e) )
279  append(e);
280 }
281 
282 template<class T, Foam::label Offset>
283 inline bool Foam::LongList<T, Offset>::contains(const T& e) const
284 {
285  for(label i=0;i<nextFree_;++i)
286  if( (*this)[i] == e )
287  return true;
288 
289  return false;
290 }
291 
292 template<class T, Foam::label Offset>
294 (
295  const T& e
296 ) const
297 {
298  for(label i=0;i<nextFree_;++i)
299  if( (*this)[i] == e )
300  return i;
301 
302  return -1;
303 }
304 
305 template<class T, Foam::label Offset>
307 {
308  if( nextFree_ == 0 )
309  {
311  (
312  "void Foam::LongList<T, Offset>::remove()"
313  ) << "List is empty" << abort(FatalError);
314  }
315 
316  T el = operator[](i);
317  operator[](i) = operator[](nextFree_-1);
318  --nextFree_;
319  return el;
320 }
321 
322 template<class T, Foam::label Offset>
324 {
325  if( nextFree_ == 0 )
326  {
328  (
329  "void Foam::LongList<T, Offset>::remove()"
330  ) << "List is empty" << abort(FatalError);
331  }
332 
333  T lastEl = operator[](nextFree_-1);
334  --nextFree_;
335  return lastEl;
336 }
337 
338 
339 // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
340 
341 template<class T, Foam::label Offset>
342 inline const T& Foam::LongList<T, Offset>::operator[](const label i) const
343 {
344  #ifdef FULLDEBUG
345  checkIndex(i);
346  #endif
347 
348  return dataPtr_[i>>shift_][i&mask_];
349 }
350 
351 template<class T, Foam::label Offset>
353 {
354  #ifdef FULLDEBUG
355  checkIndex(i);
356  #endif
357 
358  return dataPtr_[i>>shift_][i&mask_];
359 }
360 
361 template<class T, Foam::label Offset>
363 {
364  if( i >= nextFree_ )
365  setSize(i+1);
366 
367  return operator[](i);
368 }
369 
370 
371 template<class T, Foam::label Offset>
373 {
374  return operator()(i);
375 }
376 
377 template<class T, Foam::label Offset>
379 {
380  for(label i=0;i<nextFree_;++i)
381  operator[](i) = t;
382 }
383 
384 template<class T, Foam::label Offset>
386 {
387  setSize(l.size());
388 
389  for(label i=0;i<l.nextFree_;++i)
390  operator[](i) = l[i];
391 }
392 
393 
394 // ************************************************************************* //
Foam::LongList::newElmt
T & newElmt(const label)
return a non-const access to an element,
Definition: LongListI.H:372
setSize
points setSize(newPointi)
Foam::LongList::initializeParameters
void initializeParameters()
initialize width and mask
Definition: LongListI.H:43
Foam::LongList::append
void append(const T &e)
Append an element at the end of the list.
Definition: LongListI.H:265
Foam::LongList::operator()
T & operator()(const label)
Return non-const access to an element,.
Definition: LongListI.H:362
Foam::LongList::clear
void clear()
Clear the list, i.e. set next free to zero.
Definition: LongListI.H:230
Foam::LongList::shrink
LongList< T, Offset > & shrink()
Shrink the list to the number of elements used.
Definition: LongListI.H:238
Foam::LongList::clearOut
void clearOut()
delete all elements
Definition: LongListI.H:112
Foam::LongList::containsAtPosition
label containsAtPosition(const T &e) const
Definition: LongListI.H:294
Foam::LongList::shift_
label shift_
Definition: LongList.H:94
Foam::LongList::size
label size() const
Size of the active part of the list.
Definition: LongListI.H:203
Foam::LongList::checkIndex
void checkIndex(const label i) const
check index
Definition: LongListI.H:29
Foam::LongList::nextFree_
label nextFree_
number of elements in the list
Definition: LongList.H:82
Foam::LongList::setSize
void setSize(const label)
Reset size of List.
Definition: LongListI.H:223
Foam::LongList
Definition: LongList.H:55
Foam::LongList::N_
label N_
number of allocated elements
Definition: LongList.H:79
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:59
Foam::LongList::transfer
void transfer(LongList< T, Offset > &)
transfer the list from another one without allocating it
Definition: LongListI.H:245
Foam::LongList::~LongList
~LongList()
Definition: LongListI.H:195
Foam::LongList::operator[]
const T & operator[](const label i) const
get and set operators
Definition: LongListI.H:342
Foam::FatalError
error FatalError
Foam::LongList::LongList
LongList()
Construct null.
Definition: LongListI.H:133
Foam::LongList::operator=
void operator=(const T &)
Assignment of all entries to the given value.
Definition: LongListI.H:378
Foam::abort
errorManip< error > abort(error &err)
Definition: errorManip.H:131
s
gmvFile<< "tracers "<< particles.size()<< nl;forAllConstIter(Cloud< passiveParticle >, particles, iter){ gmvFile<< iter().position().x()<< " ";}gmvFile<< nl;forAllConstIter(Cloud< passiveParticle >, particles, iter){ gmvFile<< iter().position().y()<< " ";}gmvFile<< nl;forAllConstIter(Cloud< passiveParticle >, particles, iter){ gmvFile<< iter().position().z()<< " ";}gmvFile<< nl;forAll(lagrangianScalarNames, i){ word name=lagrangianScalarNames[i];IOField< scalar > s(IOobject(name, runTime.timeName(), cloud::prefix, mesh, IOobject::MUST_READ, IOobject::NO_WRITE))
Foam::LongList::numBlocks_
label numBlocks_
number of used blocks of data
Definition: LongList.H:85
Foam::LongList::remove
T remove(const label i)
Return and remove the element.
Definition: LongListI.H:306
Foam::max
dimensioned< Type > max(const dimensioned< Type > &, const dimensioned< Type > &)
Foam::LongList::mask_
label mask_
Definition: LongList.H:95
T
const volScalarField & T
Definition: createFields.H:25
Foam::LongList::appendIfNotIn
void appendIfNotIn(const T &e)
Definition: LongListI.H:276
Foam::LongList::byteSize
label byteSize() const
Return the binary size in number of characters of the UList.
Definition: LongListI.H:209
contiguous.H
Template function to specify if the data of a type are contiguous.
Foam::LongList::contains
bool contains(const T &e) const
check if the element is in the list (takes linear time)
Definition: LongListI.H:283
Foam::constant::electromagnetic::e
const dimensionedScalar e
Elementary charge.
Definition: doubleFloat.H:94
Foam::LongList::allocateSize
void allocateSize(const label)
Allocate memory for the list.
Definition: LongListI.H:60
FatalErrorIn
#define FatalErrorIn(functionName)
Report an error message using Foam::FatalError.
Definition: error.H:313
Foam::LongList::dataPtr_
T ** dataPtr_
array of pointers to the blocks of data, each of the size WIDTH
Definition: LongList.H:98
Foam::LongList::numAllocatedBlocks_
label numAllocatedBlocks_
Definition: LongList.H:90
Foam::LongList::removeLastElement
T removeLastElement()
Definition: LongListI.H:323