PackedList.C
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-2015 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 #include "PackedList.H"
27 #include "IOstreams.H"
28 
29 
30 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
31 
32 #if (UINT_MAX == 0xFFFFFFFF)
33 // 32-bit counting, Hamming weight method
34 # define COUNT_PACKEDBITS(sum, x) \
35 { \
36  x -= (x >> 1) & 0x55555555; \
37  x = (x & 0x33333333) + ((x >> 2) & 0x33333333); \
38  sum += (((x + (x >> 4)) & 0x0F0F0F0F) * 0x01010101) >> 24; \
39 }
40 #elif (UINT_MAX == 0xFFFFFFFFFFFFFFFF)
41 // 64-bit counting, Hamming weight method
42 # define COUNT_PACKEDBITS(sum, x) \
43 { \
44  x -= (x >> 1) & 0x5555555555555555; \
45  x = (x & 0x3333333333333333) + ((x >> 2) & 0x3333333333333333); \
46  sum += (((x + (x >> 4)) & 0x0F0F0F0F0F0F0F0F) * 0x0101010101010101) >> 56;\
47 }
48 #else
49 // Arbitrary number of bits, Brian Kernighan's method
50 # define COUNT_PACKEDBITS(sum, x) for (; x; ++sum) { x &= x - 1; }
51 #endif
52 
53 
54 template<unsigned nBits>
55 unsigned int Foam::PackedList<nBits>::count() const
56 {
57  unsigned int c = 0;
58 
59  if (size_)
60  {
61  const label packLen = packedLength();
62  for (label i = 0; i < packLen; ++i)
63  {
64  unsigned int bits = StorageList::operator[](i);
65  COUNT_PACKEDBITS(c, bits);
66  }
67  }
68 
69  return c;
70 }
71 
72 
73 template<unsigned nBits>
75 {
76  if (!size_)
77  {
78  return false;
79  }
80 
81  const label oldSize = size_;
82  for (label storeI = packedLength()-1; storeI >= 0; --storeI)
83  {
84  size_ = storeI * packing();
85  unsigned int bits = StorageList::operator[](storeI);
86 
87  // found some bits
88  if (bits)
89  {
90  while (bits)
91  {
92  bits >>= nBits;
93  ++size_;
94  }
95  break;
96  }
97  }
98 
99  return (size_ != oldSize);
100 }
101 
102 
103 template<unsigned nBits>
105 {
106  if (!size_)
107  {
108  return;
109  }
110 
111  // mask value for complete segments
112  const unsigned int mask = maskLower(packing());
113 
114  const label packLen = packedLength();
115  for (label i=0; i < packLen; ++i)
116  {
117  StorageList::operator[](i) = mask & ~StorageList::operator[](i);
118  }
119 
120  // mask off the final partial segment
121  {
122  const unsigned int off = size_ % packing();
123  if (off)
124  {
125  const unsigned int seg = size_ / packing();
126 
127  StorageList::operator[](seg) &= maskLower(off);
128  }
129  }
130 }
131 
132 
133 template<unsigned nBits>
135 {
136  labelList elems(size_);
137 
138  forAll(*this, i)
139  {
140  elems[i] = get(i);
141  }
142 
143  return elems.xfer();
144 }
145 
146 
147 template<unsigned nBits>
149 (
150  Ostream& os
151 ) const
152 {
153  os << "iterator<" << label(nBits) << "> ["
154  << this->index_ << "]"
155  << " segment:" << label(this->index_ / packing())
156  << " offset:" << label(this->index_ % packing())
157  << " value:" << this->get()
158  << nl;
159 
160  return os;
161 }
162 
163 
164 template<unsigned nBits>
166 (
167  Ostream& os,
168  const bool fullOutput
169 ) const
170 {
171  const label packLen = packedLength();
172 
173  // mask value for complete segments
174  unsigned int mask = maskLower(packing());
175  const label outputLen = fullOutput ? StorageList::size() : packLen;
176 
177  os << "(\n";
178  for (label i=0; i < outputLen; ++i)
179  {
180  const StorageType& rawBits = StorageList::operator[](i);
181 
182  // the final segment may not be full, modify mask accordingly
183  if (i == packLen-1)
184  {
185  const unsigned int off = size_ % packing();
186 
187  if (off)
188  {
189  mask = maskLower(off);
190  }
191  }
192  else if (i == packLen)
193  {
194  // no mask for unaddressed bit
195  mask = 0u;
196  }
197 
198 
199  for (unsigned int testBit = (1u << max_bits()); testBit; testBit >>= 1)
200  {
201  if (mask & testBit)
202  {
203  // addressable region
204  if (rawBits & testBit)
205  {
206  os << '1';
207  }
208  else
209  {
210  os << '-';
211  }
212  }
213  else
214  {
215  if (rawBits & testBit)
216  {
217  os << '!';
218  }
219  else
220  {
221  os << '.';
222  }
223  }
224  }
225  os << '\n';
226  }
227  os << ")\n";
228 
229  return os;
230 }
231 
232 
233 template<unsigned nBits>
235 (
236  Ostream& os,
237  const bool fullOutput
238 ) const
239 {
240  os << "PackedList<" << nBits << ">"
241  << " max_value:" << max_value()
242  << " packing:" << packing() << nl
243  << " count: " << count() << nl
244  << " size/capacity: " << size_ << "/" << capacity() << nl
245  << " storage/capacity: "
246  << packedLength() << "/" << StorageList::size()
247  << "\n";
248 
249  return printBits(os, fullOutput);
250 }
251 
252 
253 template<unsigned nBits>
255 {
256  PackedList<nBits>& lst = *this;
257 
258  lst.clear();
259  is.fatalCheck("PackedList<nBits>::read(Istream&)");
260 
261  token firstTok(is);
262  is.fatalCheck
263  (
264  "PackedList<nBits>::read(Istream&) : "
265  "reading first token"
266  );
267 
268  if (firstTok.isLabel())
269  {
270  const label sz = firstTok.labelToken();
271 
272  // Set list length to that read
273  lst.resize(sz);
274 
275  // Read list contents depending on data format
276  if (is.format() == IOstream::ASCII)
277  {
278  // Read beginning of contents
279  const char delimiter = is.readBeginList("PackedList<nBits>");
280 
281  if (sz)
282  {
283  if (delimiter == token::BEGIN_LIST)
284  {
285  for (label i=0; i<sz; ++i)
286  {
287  lst[i] = lst.readValue(is);
288 
289  is.fatalCheck
290  (
291  "PackedList<nBits>::read(Istream&) : "
292  "reading entry"
293  );
294  }
295  }
296  else if (delimiter == token::BEGIN_BLOCK)
297  {
298  // assign for all entries
299  lst = lst.readValue(is);
300 
301  is.fatalCheck
302  (
303  "PackedList<nBits>::read(Istream&) : "
304  "reading the single entry"
305  );
306  }
307  else
308  {
310  (
311  "PackedList<nBits>::read(Istream&)",
312  is
313  )
314  << "incorrect list token, expected '(' or '{', found "
315  << firstTok.info()
316  << exit(FatalIOError);
317  }
318  }
319 
320  // Read end of contents
321  is.readEndList("PackedList<nBits>");
322  }
323  else
324  {
325  if (sz)
326  {
327  is.read
328  (
329  reinterpret_cast<char*>(lst.storage().data()),
330  lst.byteSize()
331  );
332 
333  is.fatalCheck
334  (
335  "PackedList<nBits>::read(Istream&) : "
336  "reading the binary block"
337  );
338  }
339  }
340  }
341  else if (firstTok.isPunctuation())
342  {
343  if (firstTok.pToken() == token::BEGIN_LIST)
344  {
345  token nextTok(is);
346  is.fatalCheck("PackedList<nBits>::read(Istream&)");
347 
348  while
349  (
350  !( nextTok.isPunctuation()
351  && nextTok.pToken() == token::END_LIST
352  )
353  )
354  {
355  is.putBack(nextTok);
356  lst.append(lst.readValue(is));
357 
358  is >> nextTok;
359  is.fatalCheck("PackedList<nBits>::read(Istream&)");
360  }
361  }
362  else if (firstTok.pToken() == token::BEGIN_BLOCK)
363  {
364  token nextTok(is);
365  is.fatalCheck("PackedList<nBits>::read(Istream&)");
366 
367  while
368  (
369  !( nextTok.isPunctuation()
370  && nextTok.pToken() == token::END_BLOCK
371  )
372  )
373  {
374  is.putBack(nextTok);
375  lst.setPair(is);
376 
377  is >> nextTok;
378  is.fatalCheck("PackedList<nBits>::read(Istream&)");
379  }
380  }
381  else
382  {
384  (
385  "PackedList<nBits>::read(Istream&)",
386  is
387  )
388  << "incorrect first token, expected '(', found "
389  << firstTok.info()
390  << exit(FatalIOError);
391  }
392  }
393  else
394  {
396  (
397  "PackedList<nBits>::read(Istream&)",
398  is
399  )
400  << "incorrect first token, expected <int>, '(' or '{', found "
401  << firstTok.info()
402  << exit(FatalIOError);
403  }
404 
405  return is;
406 }
407 
408 
409 template<unsigned nBits>
411 (
412  Ostream& os,
413  const bool indexedOutput
414 ) const
415 {
416  const PackedList<nBits>& lst = *this;
417  const label sz = lst.size();
418 
419  // Write list contents depending on data format
420  if (os.format() == IOstream::ASCII)
421  {
422  bool uniform = false;
423 
424  if (sz > 1 && !indexedOutput)
425  {
426  uniform = true;
427 
428  forAll(lst, i)
429  {
430  if (lst[i] != lst[0])
431  {
432  uniform = false;
433  break;
434  }
435  }
436  }
437 
438  if (uniform)
439  {
440  // uniform values:
441  os << sz << token::BEGIN_BLOCK << lst[0] << token::END_BLOCK;
442  }
443  else if (indexedOutput)
444  {
445  // indexed output
446  os << nl << token::BEGIN_BLOCK << nl;
447 
448  for
449  (
450  typename PackedList<nBits>::const_iterator iter = lst.cbegin();
451  iter != lst.cend();
452  ++iter
453  )
454  {
455  if (iter.writeIfSet(os))
456  {
457  os << nl;
458  }
459  }
460 
461  os << token::END_BLOCK << nl;
462  }
463  else if (sz < 11)
464  {
465  // short list:
466  os << sz << token::BEGIN_LIST;
467  forAll(lst, i)
468  {
469  if (i)
470  {
471  os << token::SPACE;
472  }
473  os << lst[i];
474  }
475  os << token::END_LIST;
476  }
477  else
478  {
479  // longer list:
480  os << nl << sz << nl << token::BEGIN_LIST;
481  forAll(lst, i)
482  {
483  os << nl << lst[i];
484  }
485  os << nl << token::END_LIST << nl;
486  }
487  }
488  else
489  {
490  os << nl << sz << nl;
491  if (sz)
492  {
493  os.write
494  (
495  reinterpret_cast<const char*>(lst.storage().cdata()),
496  lst.byteSize()
497  );
498  }
499  }
500 
501  return os;
502 }
503 
504 
505 template<unsigned nBits>
507 {
508  os << *this;
509 }
510 
511 
512 template<unsigned nBits>
514 (
515  const word& keyword,
516  Ostream& os
517 ) const
518 {
519  os.writeKeyword(keyword);
520  writeEntry(os);
521  os << token::END_STATEMENT << endl;
522 }
523 
524 
525 // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
526 
527 template<unsigned nBits>
530 {
531  StorageList::operator=(lst);
532  size_ = lst.size();
533  return *this;
534 }
535 
536 
537 template<unsigned nBits>
540 {
541  setCapacity(lst.size());
542  size_ = lst.size();
543 
544  forAll(lst, i)
545  {
546  set(i, lst[i]);
547  }
548  return *this;
549 }
550 
551 
552 template<unsigned nBits>
555 {
556  setCapacity(lst.size());
557  size_ = lst.size();
558 
559  forAll(lst, i)
560  {
561  set(i, lst[i]);
562  }
563  return *this;
564 }
565 
566 
567 // * * * * * * * * * * * * * * Friend Operators * * * * * * * * * * * * * * //
568 
569 template<unsigned nBits>
571 {
572  return lst.read(is);
573 }
574 
575 
576 template<unsigned nBits>
577 Foam::Ostream& Foam::operator<<(Ostream& os, const PackedList<nBits>& lst)
578 {
579  return lst.write(os, false);
580 }
581 
582 
583 // ************************************************************************* //
void setPair(Istream &)
Read an index/value pair and set accordingly.
Definition: PackedListI.H:125
bool isLabel() const
Definition: tokenI.H:262
const_iterator cend() const
const_iterator set to beyond the end of the PackedList
Definition: PackedListI.H:713
Xfer< List< T > > xfer()
Transfer contents to the Xfer container.
Definition: ListI.H:90
bool trim()
Trim any trailing zero elements.
Definition: PackedList.C:74
void writeEntry(Ostream &) const
Write as a dictionary entry.
Definition: PackedList.C:506
An Istream is an abstract base class for all input systems (streams, files, token lists etc)...
Definition: Istream.H:57
char readBeginList(const char *funcName)
Definition: Istream.C:131
A simple container for copying or transferring objects of type <T>.
Definition: Xfer.H:85
virtual Istream & read(token &)=0
Return next token from stream.
Istream & read(Istream &)
Clear list and read from stream.
Definition: PackedList.C:254
A class for handling words, derived from string.
Definition: word.H:59
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
bool isPunctuation() const
Definition: tokenI.H:203
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:124
List< unsigned int > & storage()
Return the underlying packed storage.
Definition: PackedListI.H:927
label size() const
Number of entries.
Definition: PackedListI.H:722
Ostream & write(Ostream &, const bool indexedOutput=false) const
Write, optionally with indexedOutput.
Definition: PackedList.C:411
unsigned int count() const
Count number of bits set, O(log(n))
Definition: PackedList.C:55
PackedList< nBits > & append(const unsigned int val)
Append a value at the end of the list.
Definition: PackedListI.H:1040
PackedList< nBits > & operator=(const unsigned int val)
Assignment of all entries to the given value. Takes linear time.
Definition: PackedListI.H:1080
T * data()
Return a pointer to the first data element,.
Definition: UListI.H:152
void flip()
Invert the bits in the addressable region.
Definition: PackedList.C:104
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition: HashTable.H:59
Useful combination of include files which define Sin, Sout and Serr and the use of IO streams general...
static const char nl
Definition: Ostream.H:260
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:251
IOerror FatalIOError
void putBack(const token &)
Put back token.
Definition: Istream.C:30
A List with indirect addressing.
Definition: fvMatrix.H:106
InfoProxy< token > info() const
Return info proxy.
Definition: token.H:372
label size() const
Return the number of elements in the list.
char readEndList(const char *funcName)
Definition: Istream.C:152
void fatalCheck(const char *operation) const
Check IOstream status for given operation.
Definition: IOstream.C:107
static unsigned int readValue(Istream &)
Read a list entry (allows for specialization)
Definition: PackedListI.H:104
#define forAll(list, i)
Definition: UList.H:421
streamFormat format() const
Return current stream format.
Definition: IOstream.H:377
virtual Ostream & write(const token &)=0
Write next token to stream.
Ostream & printInfo(Ostream &, const bool fullOutput=false) const
Print information and bit patterns (with printBits)
Definition: PackedList.C:235
const_iterator cbegin() const
const_iterator set to the beginning of the PackedList
Definition: PackedListI.H:689
Ostream & printInfo(Ostream &) const
Print information and values.
Definition: PackedList.C:149
label size() const
Return the number of elements in the UList.
Definition: UListI.H:299
A token holds items read from Istream.
Definition: token.H:67
Ostream & writeKeyword(const keyType &)
Write the keyword followed by an appropriate indentation.
Definition: Ostream.C:59
label labelToken() const
Definition: tokenI.H:267
void clear()
Clear the list, i.e. set addressable size to zero.
Definition: PackedListI.H:900
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:60
const T * cdata() const
Return a const pointer to the first data element,.
Definition: UListI.H:145
Istream & operator>>(Istream &, edgeMesh &)
Definition: edgeMeshIO.C:144
std::streamsize byteSize() const
Return the binary size in number of characters.
Definition: PackedListI.H:948
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:53
#define COUNT_PACKEDBITS(sum, x)
Definition: PackedList.C:50
const dimensionedScalar c
Speed of light in a vacuum.
Ostream & printBits(Ostream &, const bool fullOutput=false) const
Print bit patterns, optionally output unused elements.
Definition: PackedList.C:166
Uniform/equally-weighted distribution model.
Definition: uniform.H:47
punctuationToken pToken() const
Definition: tokenI.H:208
A dynamically allocatable list of packed unsigned integers.
Definition: PackedList.H:117
unsigned int StorageType
Definition: PackedList.H:153
Xfer< labelList > values() const
Return the values as a list of labels.
Definition: PackedList.C:134
#define FatalIOErrorIn(functionName, ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:325
void resize(const label, const unsigned int &val=0u)
Reset addressable list size, does not shrink the allocated size.
Definition: PackedListI.H:737
The const_iterator for PackedList.
Definition: PackedList.H:554