FreeFOAM The Cross-Platform CFD Toolkit
Hosted by SourceForge:
Get FreeFOAM at SourceForge.net.
            Fast, secure and Free Open Source software downloads

PackedList< nBits > Class Template Reference

A dynamically allocatable list of packed unsigned integers. More...

#include <OpenFOAM/PackedList.H>


Detailed Description

template<unsigned nBits = 1>
class Foam::PackedList< nBits >

A dynamically allocatable list of packed unsigned integers.

The list resizing is similar to DynamicList, thus the methods clear() and setSize() behave like their DynamicList counterparts and the methods reserve() and setCapacity() can be used to influence the allocation.

The number of bits per item is specified by the template parameter nBits.

Note:
In a const context, the '[]' operator simply returns the stored value, with out-of-range elements returned as zero. In a non-const context, the '[]' operator returns an iteratorBase, which might not have a valid reference for out-of-range elements. The iteratorBase class handles the assignment of new values.

Using the iteratorBase as a proxy allows assignment of values between list elements. Thus the following bit of code works as expected:

list[1] = list[5];  // value assignment, not iterator position
list[2] = list[5] = 4;  // propagates value
list[1] = list[5] = list[6];  // propagates value

Using get() or the '[]' operator are similarly fast. Looping and reading via an iterator is approx. 15% slower, but can be more flexible.

Using the set() operator (and the '[]' operator) are marginally slower (approx. 5%) than using an iterator, but the set() method has the advantage of also returning a bool if the value changed. This can be useful for branching on changed values.

list[5] = 4;
changed = list.set(5, 8);
if (changed) ...

The lazy evaluation used means that reading an out-of-range element returns zero, but does not affect the list size. Even in a non-const context, only the assigment itself causes the element to be created. For example,

list.resize(4);
Info<< list[10] << "\n";  // print zero, but doesn't adjust list
list[8] = 1;
See also:
Foam::DynamicList
Source files

Definition at line 113 of file PackedList.H.

Inheritance diagram for PackedList< nBits >:
Collaboration diagram for PackedList< nBits >:

List of all members.

Classes

class  const_iterator
 The const_iterator for PackedList. More...
class  iterator
 The iterator class used for PackedList. More...
class  iteratorBase
 The iterator base for PackedList. More...

Public Member Functions

 PackedList ()
 Null constructor.
 PackedList (const label size)
 Construct with given size, initializes list to 0.
 PackedList (const label size, const unsigned val)
 Construct with given size and value for all elements.
 PackedList (const PackedList< nBits > &)
 Copy constructor.
 PackedList (const Xfer< PackedList< nBits > > &)
 Construct by transferring the parameter contents.
 PackedList (const UList< label > &)
 Construct from a list of labels.
autoPtr< PackedList< nBits > >  clone () const
 Clone.
label  capacity () const
 The number of elements that can be stored before reallocating.
label  size () const
 Number of entries.
bool  empty () const
 Return true if the list is empty (ie, size() is zero).
unsigned int  get (const label) const
 Get value at index I.
bool  set (const label, const unsigned int val=~0u)
 Set value at index I. Return true if value changed.
bool  unset (const label)
 Unset the entry at index I. Return true if value changed.
List< unsigned int > &  storage ()
 Return the underlying packed storage.
const List< unsigned int > &  storage () const
 Return the underlying packed storage.
unsigned int  count () const
 Count number of bits set, O(log(n))
labelList  values () const
 Return the values as a labelList.
Ostream &  print (Ostream &) const
 Print values and information.
bool  trim ()
 Trim any trailing zero elements.
void  flip ()
 Invert the bits in the addressable region.
void  setCapacity (const label)
 Alter the size of the underlying storage.
void  resize (const label, const unsigned int &val=0)
 Reset addressable list size, does not shrink the allocated size.
void  setSize (const label, const unsigned int &val=0)
 Alias for resize()
void  reserve (const label)
 Reserve allocation space for at least this size.
void  clear ()
 Clear the list, i.e. set addressable size to zero.
void  clearStorage ()
 Clear the list and delete storage.
void  shrink ()
 Shrink the allocated space to what is actually used.
void  transfer (PackedList< nBits > &)
 Transfer the contents of the argument list into this list.
Xfer< PackedList< nBits > >  xfer ()
 Transfer contents to the Xfer container.
void  append (const unsigned int val)
 Append a value at the end of the list.
unsigned int  remove ()
 Remove and return the last element.
unsigned int  operator[] (const label) const
 Get value at index I.
iteratorBase  operator[] (const label)
 Set value at index I.
void  operator= (const unsigned int val)
 Assignment of all entries to the given value. Takes linear time.
void  operator= (const PackedList< nBits > &)
 Assignment operator. Takes linear time.
void  operator= (const UList< label > &)
 Assignment operator. Takes linear time.
iterator  begin ()
 iterator set to the beginning of the PackedList
iterator  end ()
 iterator set to beyond the end of the PackedList
const_iterator  cbegin () const
 const_iterator set to the beginning of the PackedList
const_iterator  cend () const
 const_iterator set to beyond the end of the PackedList
const_iterator  begin () const
 const_iterator set to the beginning of the PackedList
const_iterator  end () const
 const_iterator set to beyond the end of the PackedList
template<>
void  operator= (const unsigned int val)

Static Public Member Functions

static unsigned int  max_bits ()
 The max. number of bits that can be templated.
static unsigned int  max_value ()
 The max. value for an entry, which simultaneously the bit-mask.
static unsigned int  packing ()
 The number of entries per packed storage element.
static unsigned int  maskLower (unsigned offset)
 Masking for all bits below the offset.

Constructor & Destructor Documentation

PackedList (  ) [inline]

Null constructor.

Definition at line 73 of file PackedListI.H.

PackedList ( const label   size  ) [inline, explicit]

Construct with given size, initializes list to 0.

Definition at line 81 of file PackedListI.H.

PackedList ( const label   size,
const unsigned   val  
)

Construct with given size and value for all elements.

PackedList ( const PackedList< nBits > &   lst  ) [inline]

Copy constructor.

Definition at line 89 of file PackedListI.H.

PackedList ( const Xfer< PackedList< nBits > > &   lst  ) [inline]

Construct by transferring the parameter contents.

Definition at line 97 of file PackedListI.H.

PackedList ( const UList< label > &   lst  ) [explicit]

Construct from a list of labels.

Definition at line 41 of file PackedList.C.

References forAll.


Member Function Documentation

unsigned int max_bits (  ) [inline, static]

The max. number of bits that can be templated.

Might someday be useful for a template assert.

Definition at line 33 of file PackedListI.H.

unsigned int max_value (  ) [inline, static]

The max. value for an entry, which simultaneously the bit-mask.

eg, ((1 << 2) - 1) yields 0b0011

Definition at line 40 of file PackedListI.H.

Referenced by PackedList< nBits >::iteratorBase::get(), PackedList< nBits >::operator=(), PackedList< nBits >::resize(), and PackedList< nBits >::iteratorBase::set().

unsigned int packing (  ) [inline, static]
unsigned int maskLower ( unsigned   offset  ) [inline, static]

Masking for all bits below the offset.

Definition at line 54 of file PackedListI.H.

Referenced by PackedList< nBits >::resize().

Foam::autoPtr< Foam::PackedList< nBits > > clone (  ) const [inline]

Clone.

Reimplemented from List< unsigned int >.

Definition at line 105 of file PackedListI.H.

Foam::label capacity (  ) const [inline]

The number of elements that can be stored before reallocating.

Definition at line 642 of file PackedListI.H.

References PackedList< nBits >::packing(), and List< T >::size().

Foam::label size (  ) const [inline]

Number of entries.

Reimplemented from List< unsigned int >.

Definition at line 553 of file PackedListI.H.

Referenced by PackedList< nBits >::operator=(), syncTools::syncEdgeList(), syncTools::syncFaceList(), and syncTools::syncPointList().

bool empty (  ) const [inline]

Return true if the list is empty (ie, size() is zero).

Reimplemented from UList< unsigned int >.

Definition at line 560 of file PackedListI.H.

Referenced by dynamicRefineFvMesh::selectRefineCells().

bool unset ( const label   i  ) [inline]

Unset the entry at index I. Return true if value changed.

Never auto-vivify entries.

Definition at line 813 of file PackedListI.H.

References PackedList< nBits >::iteratorBase::set().

Foam::List< unsigned int > & storage (  ) [inline]

Return the underlying packed storage.

Definition at line 715 of file PackedListI.H.

const Foam::List< unsigned int > & storage (  ) const [inline]

Return the underlying packed storage.

Definition at line 723 of file PackedListI.H.

unsigned int count (  ) const

Count number of bits set, O(log(n))

Uses the Hamming weight (population count) method http://en.wikipedia.org/wiki/Hamming_weight

Definition at line 79 of file PackedList.C.

References COUNT_PACKEDBITS.

Foam::labelList values (  ) const

Return the values as a labelList.

Definition at line 177 of file PackedList.C.

References forAll.

Foam::Ostream & print ( Ostream &   os  ) const

Print values and information.

Definition at line 204 of file PackedList.C.

References Foam::nl.

bool trim (  )

Trim any trailing zero elements.

Definition at line 113 of file PackedList.C.

void flip (  )

Invert the bits in the addressable region.

Definition at line 165 of file PackedList.C.

void setCapacity ( const label   nElem  ) [inline]

Alter the size of the underlying storage.

The addressed size will be truncated if needed to fit, but will remain otherwise untouched.

Definition at line 649 of file PackedListI.H.

References List< T >::setSize().

void resize ( const label   nElem,
const unsigned int &   val = 0  
) [inline]

Reset addressable list size, does not shrink the allocated size.

Optionally specify a value for new elements.

Reimplemented from List< unsigned int >.

Definition at line 568 of file PackedListI.H.

References PackedList< nBits >::maskLower(), PackedList< nBits >::max_value(), PackedList< nBits >::operator=(), UList< T >::operator[](), PackedList< nBits >::packing(), and PackedList< nBits >::reserve().

Referenced by PackedList< nBits >::remove(), PackedList< nBits >::set(), and PackedList< nBits >::setSize().

void setSize ( const label   newSize,
const unsigned int &   val = 0  
) [inline]

Alias for resize()

Reimplemented from List< unsigned int >.

Definition at line 631 of file PackedListI.H.

References PackedList< nBits >::resize().

void reserve ( const label   nElem  ) [inline]

Reserve allocation space for at least this size.

Never shrinks the allocated size. The list size is adjusted as per DynamicList with SizeInc=0, SizeMult=2, SizeDiv=1

Definition at line 663 of file PackedListI.H.

References Foam::max(), List< T >::setSize(), and List< T >::size().

Referenced by PackedList< nBits >::append(), and PackedList< nBits >::resize().

void clear (  ) [inline]

Clear the list, i.e. set addressable size to zero.

Does not adjust the underlying storage

Reimplemented from List< unsigned int >.

Definition at line 687 of file PackedListI.H.

Referenced by dynamicRefineFvMesh::calculateProtectedCells(), and dynamicRefineFvMesh::dynamicRefineFvMesh().

void clearStorage (  ) [inline]

Clear the list and delete storage.

Definition at line 694 of file PackedListI.H.

References List< T >::clear().

void shrink (  ) [inline]

Shrink the allocated space to what is actually used.

Definition at line 702 of file PackedListI.H.

References List< T >::setSize(), and List< T >::size().

void transfer ( PackedList< nBits > &   lst  ) [inline]

Transfer the contents of the argument list into this list.

and annul the argument list.

Definition at line 730 of file PackedListI.H.

References List< T >::transfer().

Foam::Xfer< Foam::PackedList< nBits > > xfer (  ) [inline]

Transfer contents to the Xfer container.

Reimplemented from List< unsigned int >.

Definition at line 741 of file PackedListI.H.

References Foam::xferMove().

void append ( const unsigned int   val  ) [inline]

Append a value at the end of the list.

Definition at line 826 of file PackedListI.H.

References PackedList< nBits >::reserve(), and PackedList< nBits >::iteratorBase::set().

unsigned int remove (  ) [inline]

Remove and return the last element.

Definition at line 837 of file PackedListI.H.

References Foam::abort(), Foam::FatalError, FatalErrorIn, PackedList< nBits >::iteratorBase::get(), and PackedList< nBits >::resize().

unsigned int operator[] ( const label   i  ) const [inline]

Get value at index I.

Never auto-vivify entries.

Reimplemented from UList< unsigned int >.

Definition at line 772 of file PackedListI.H.

References PackedList< nBits >::iteratorBase::get().

Foam::PackedList< nBits >::iteratorBase operator[] ( const label   i  ) [inline]

Set value at index I.

Returns iterator to perform the actual operation. Does not auto-vivify entries, but will when assigned to.

Reimplemented from UList< unsigned int >.

Definition at line 857 of file PackedListI.H.

void operator= ( const unsigned int   val  ) [inline]

Assignment of all entries to the given value. Takes linear time.

Definition at line 882 of file PackedListI.H.

References PackedList< nBits >::max_value(), List< T >::operator=(), and PackedList< nBits >::packing().

Referenced by PackedList< nBits >::operator=(), and PackedList< nBits >::resize().

void operator= ( const PackedList< nBits > &   lst  )

Assignment operator. Takes linear time.

Definition at line 267 of file PackedList.C.

References PackedList< nBits >::size().

void operator= ( const UList< label > &   lst  )

Assignment operator. Takes linear time.

Definition at line 275 of file PackedList.C.

References forAll, and UList< T >::size().

Foam::PackedList< nBits >::iterator begin (  ) [inline]

iterator set to the beginning of the PackedList

Reimplemented from UList< unsigned int >.

Definition at line 504 of file PackedListI.H.

Foam::PackedList< nBits >::iterator end (  ) [inline]

iterator set to beyond the end of the PackedList

Reimplemented from UList< unsigned int >.

Definition at line 528 of file PackedListI.H.

Foam::PackedList< nBits >::const_iterator cbegin (  ) const [inline]

const_iterator set to the beginning of the PackedList

Reimplemented from UList< unsigned int >.

Definition at line 520 of file PackedListI.H.

Foam::PackedList< nBits >::const_iterator cend (  ) const [inline]

const_iterator set to beyond the end of the PackedList

Reimplemented from UList< unsigned int >.

Definition at line 544 of file PackedListI.H.

Foam::PackedList< nBits >::const_iterator begin (  ) const [inline]

const_iterator set to the beginning of the PackedList

Reimplemented from UList< unsigned int >.

Definition at line 512 of file PackedListI.H.

Foam::PackedList< nBits >::const_iterator end (  ) const [inline]

const_iterator set to beyond the end of the PackedList

Reimplemented from UList< unsigned int >.

Definition at line 536 of file PackedListI.H.

void operator= ( const unsigned int   val  ) [inline]

Definition at line 867 of file PackedListI.H.

References PackedList< nBits >::operator=().


The documentation for this class was generated from the following files: