OpenFOAM logo
The Open Source CFD Toolbox
  Source Guide OpenCFD Solutions Contact OpenFOAM

PackedList< nBits > Class Template Reference

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

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.
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 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

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.

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 108 of file PackedList.H.


Constructor & Destructor Documentation

PackedList (  )  [inline]

Null constructor.

Definition at line 65 of file PackedListI.H.

References PackedList< nBits >::packing().

Here is the call graph for this function:

PackedList ( const label  size  )  [inline]

Construct with given size, initializes list to 0.

Definition at line 73 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 81 of file PackedListI.H.

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

Construct by transferring the parameter contents.

Definition at line 89 of file PackedListI.H.

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

Construct from a list of labels.

Definition at line 33 of file PackedList.C.


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 25 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 32 of file PackedListI.H.

unsigned int packing (  )  [inline, static]

The number of entries per packed storage element.

Definition at line 39 of file PackedListI.H.

Referenced by PackedList< nBits >::count(), PackedList< nBits >::PackedList(), and PackedList< nBits >::setCapacity().

Here is the caller graph for this function:

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

Masking for all bits below the offset.

Definition at line 46 of file PackedListI.H.

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

Here is the caller graph for this function:

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

Clone.

Reimplemented from List< unsigned int >.

Definition at line 97 of file PackedListI.H.

References PackedList< nBits >::transfer().

Here is the call graph for this function:

Foam::label capacity (  )  const [inline]

The number of elements that can be stored before reallocating.

Definition at line 634 of file PackedListI.H.

References PackedList< nBits >::resize().

Here is the call graph for this function:

Foam::label size (  )  const [inline]

Number of entries.

Reimplemented from List< unsigned int >.

Definition at line 545 of file PackedListI.H.

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

Here is the caller graph for this function:

bool empty (  )  const [inline]

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

Reimplemented from UList< unsigned int >.

Definition at line 552 of file PackedListI.H.

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

Get value at index I.

Never auto-vivify entries.

Definition at line 731 of file PackedListI.H.

References Foam::xferMove().

Referenced by syncTools::syncEdgeList(), syncTools::syncFaceList(), and syncTools::syncPointList().

Here is the call graph for this function:

Here is the caller graph for this function:

bool set ( const label  i,
const unsigned int  val = ~0u 
) [inline]

Set value at index I. Return true if value changed.

Does auto-vivify for non-existent entries. Default value set is the max_value.

Definition at line 771 of file PackedListI.H.

Referenced by syncTools::syncEdgeList(), syncTools::syncFaceList(), and syncTools::syncPointList().

Here is the caller graph for this function:

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

Return the underlying packed storage.

Definition at line 698 of file PackedListI.H.

References List< T >::setSize().

Here is the call graph for this function:

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

Return the underlying packed storage.

Definition at line 706 of file PackedListI.H.

unsigned int count (  )  const [inline]

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 71 of file PackedList.C.

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

Here is the call graph for this function:

Foam::labelList values (  )  const [inline]

Return the values as a labelList.

Definition at line 169 of file PackedList.C.

References UList< unsigned int >::operator, and UList< T >::operator[]().

Here is the call graph for this function:

Foam::Ostream & print ( Ostream &  os  )  const [inline]

Print values and information.

Definition at line 196 of file PackedList.C.

bool trim (  )  [inline]

Trim any trailing zero elements.

Definition at line 105 of file PackedList.C.

void flip (  )  [inline]

Invert the bits in the addressable region.

Definition at line 157 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 641 of file PackedListI.H.

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

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

Here is the call graph for this function:

Here is the caller graph for this function:

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 560 of file PackedListI.H.

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

Here is the caller graph for this function:

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

Alias for resize().

Reimplemented from List< unsigned int >.

Definition at line 623 of file PackedListI.H.

void reserve ( const label  nElem  )  [inline]

Reserve allocation space for at least this size.

Never shrinks the allocated size.

Definition at line 655 of file PackedListI.H.

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 670 of file PackedListI.H.

References List< T >::setSize().

Here is the call graph for this function:

void clearStorage (  )  [inline]

Clear the list and delete storage.

Definition at line 677 of file PackedListI.H.

void shrink (  )  [inline]

Shrink the allocated space to what is used.

Definition at line 685 of file PackedListI.H.

References List< T >::clear().

Here is the call graph for this function:

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

Transfer the contents of the argument list into this list.

and annull the argument list.

Definition at line 713 of file PackedListI.H.

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

Here is the caller graph for this function:

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

Transfer contents to the Xfer container.

Reimplemented from List< unsigned int >.

Definition at line 724 of file PackedListI.H.

void append ( const unsigned int  val  )  [inline]

Append a value at the end of the list.

Definition at line 796 of file PackedListI.H.

References PackedList< nBits >::resize().

Here is the call graph for this function:

unsigned int remove (  )  [inline]

Remove and return the last element.

Definition at line 807 of file PackedListI.H.

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 755 of file PackedListI.H.

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 827 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 835 of file PackedListI.H.

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

Assignment operator. Takes linear time.

Definition at line 259 of file PackedList.C.

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

Assignment operator. Takes linear time.

Definition at line 267 of file PackedList.C.

References List< T >::operator=(), PackedList< nBits >::setCapacity(), and PackedList< nBits >::size().

Here is the call graph for this function:

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

iterator set to the beginning of the PackedList

Reimplemented from UList< unsigned int >.

Definition at line 496 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 520 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 512 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 536 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 504 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 528 of file PackedListI.H.


The documentation for this class was generated from the following files:
Copyright © 2000-2009 OpenCFD Ltd