FreeFOAM The Cross-Platform CFD Toolkit
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.
Ostreamprint (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.

Additional Inherited Members

- Private Types inherited from List< unsigned int >
typedef SubList< unsigned int > subList
 Declare type of subList.
- Private Types inherited from UList< unsigned int >
typedef unsigned int value_type
 Type of values the UList contains.
typedef unsigned int & reference
 Type that can be used for storing into.
typedef const unsigned int & const_reference
 Type that can be used for storing into.
typedef label difference_type
 The type that can represent the difference between any two.
typedef label size_type
 The type that can represent the size of a UList.
typedef unsigned int * iterator
 Random access iterator for traversing UList.
typedef const unsigned int * const_iterator
 Random access iterator for traversing UList.
typedef unsigned int * reverse_iterator
 Reverse iterator for reverse traversal of UList.
typedef const unsigned int * const_reverse_iterator
 Reverse iterator for reverse traversal of constant UList.
- Private Member Functions inherited from List< unsigned int >
 List ()
 Null constructor.
 List (const label)
 Construct with given size.
 List (const label, const unsigned int &)
 Construct with given size and value for all elements.
 List (const List< unsigned int > &)
 Copy constructor.
 List (const Xfer< List< unsigned int > > &)
 Construct by transferring the parameter contents.
 List (List< unsigned int > &, bool reUse)
 Construct as copy or re-use as specified.
 List (const UList< unsigned int > &, const unallocLabelList &mapAddressing)
 Construct as subset.
 List (InputIterator first, InputIterator last)
 Construct given start and end iterators.
 List (const FixedList< unsigned int, Size > &)
 Construct as copy of FixedList<T, Size>
 List (const PtrList< unsigned int > &)
 Construct as copy of PtrList<T>
 List (const SLList< unsigned int > &)
 Construct as copy of SLList<T>
 List (const IndirectList< unsigned int > &)
 Construct as copy of IndirectList<T>
 List (const UIndirectList< unsigned int > &)
 Construct as copy of UIndirectList<T>
 List (const BiIndirectList< unsigned int > &)
 Construct as copy of BiIndirectList<T>
 List (Istream &)
 Construct from Istream.
 ~List ()
void resize (const label)
 Reset size of List.
void setSize (const label)
 Reset size of List.
void append (const UList< unsigned int > &)
 Append a List at the end of this list.
void append (const UIndirectList< unsigned int > &)
 Append a UIndirectList at the end of this list.
void transfer (List< unsigned int > &)
 Transfer the contents of the argument List into this list.
void transfer (DynamicList< unsigned int, SizeInc, SizeMult, SizeDiv > &)
 Transfer the contents of the argument List into this list.
void transfer (SortableList< unsigned int > &)
 Transfer the contents of the argument List into this list.
unsigned int & newElmt (const label)
 Return subscript-checked element of UList.
void operator= (const UList< unsigned int > &)
 Assignment from UList operator. Takes linear time.
void operator= (const List< unsigned int > &)
 Assignment operator. Takes linear time.
void operator= (const SLList< unsigned int > &)
 Assignment from SLList operator. Takes linear time.
void operator= (const IndirectList< unsigned int > &)
 Assignment from IndirectList operator. Takes linear time.
void operator= (const UIndirectList< unsigned int > &)
 Assignment from UIndirectList operator. Takes linear time.
void operator= (const BiIndirectList< unsigned int > &)
 Assignment from BiIndirectList operator. Takes linear time.
void operator= (const unsigned int &)
 Assignment of all entries to the given value.
void size (const label)
 Override size to be inconsistent with allocated storage.
- Private Member Functions inherited from UList< unsigned int >
 UList ()
 Null constructor.
 UList (unsigned int *__restrict__ v, label size)
 Construct from components.
label fcIndex (const label i) const
 Return the forward circular index, i.e. the next index.
label rcIndex (const label i) const
 Return the reverse circular index, i.e. the previous index.
label byteSize () const
 Return the binary size in number of characters of the UList.
const unsigned int * cdata () const
 Return a const pointer to the first data element,.
unsigned int * data ()
 Return a pointer to the first data element,.
void checkStart (const label start) const
 Check start is within valid range (0 ... size-1).
void checkSize (const label size) const
 Check size is within valid range (0 ... size).
void checkIndex (const label i) const
 Check index i is within valid range (0 ... size-1).
void writeEntry (Ostream &) const
 Write the UList as a dictionary entry.
void writeEntry (const word &keyword, Ostream &) const
 Write the UList as a dictionary entry with keyword.
void assign (const UList< unsigned int > &)
 Assign elements to those from UList.
 operator const Foam::List< unsigned int > & () const
 Allow cast to a const List<T>&.
reverse_iterator rbegin ()
 Return reverse_iterator to begin reverse traversing the UList.
const_reverse_iterator rbegin () const
 Return const_reverse_iterator to begin reverse traversing the UList.
reverse_iterator rend ()
 Return reverse_iterator to end reverse traversing the UList.
const_reverse_iterator rend () const
 Return const_reverse_iterator to end reverse traversing the UList.
const_reverse_iterator crbegin () const
 Return const_reverse_iterator to begin reverse traversing the UList.
const_reverse_iterator crend () const
 Return const_reverse_iterator to end reverse traversing the UList.
label max_size () const
 Return size of the largest possible UList.
void swap (UList< unsigned int > &)
 Swap two ULists of the same type in constant time.
bool operator== (const UList< unsigned int > &) const
 Equality operation on ULists of the same type.
bool operator!= (const UList< unsigned int > &) const
 The opposite of the equality operation. Takes linear time.
bool operator< (const UList< unsigned int > &) const
 Compare two ULists lexicographically. Takes linear time.
bool operator> (const UList< unsigned int > &) const
 Compare two ULists lexicographically. Takes linear time.
bool operator<= (const UList< unsigned int > &) const
 Return true if !(a > b). Takes linear time.
bool operator>= (const UList< unsigned int > &) const
 Return true if !(a < b). Takes linear time.
- Static Private Member Functions inherited from List< unsigned int >
static const List< unsigned int > & null ()
 Return a null List.

Constructor & Destructor Documentation

PackedList ( )
inline

Null constructor.

Definition at line 73 of file PackedListI.H.

PackedList ( const label  size)
inlineexplicit

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 ( )
inlinestatic

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 ( )
inlinestatic

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 ( )
inlinestatic
unsigned int maskLower ( unsigned  offset)
inlinestatic

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: