TPIE

2362a60
tpie::packed_array< T, B > Class Template Reference

An array storring elements of type T using B bits to to store a element. More...

#include <tpie/packed_array.h>

Inherits tpie::linear_memory_base< packed_array< T, B > >.

Public Types

typedef size_t storage_type
 
typedef T value_type
 Type of values containd in the array. More...
 
typedef const_iter_base< true > const_iterator
 Iterator over a const array. More...
 
typedef const_iter_base< false > const_reverse_iterator
 Reverse iterator over a const array. More...
 
typedef iter_base< true > iterator
 Iterator over an array. More...
 
typedef iter_base< false > reverse_iterator
 Reverse iterator over an array. More...
 

Public Member Functions

 packed_array (size_t s=0)
 Construct array of given size. More...
 
 packed_array (size_t s, T value)
 Construct array of given size. More...
 
 packed_array (const packed_array &a)
 Construct a copy of another array. More...
 
 packed_array (packed_array &&a)
 Move another aray into me. More...
 
 ~packed_array ()
 Free up all memory used by the array. More...
 
packed_arrayoperator= (const packed_array &a)
 Copy elements from another array into this. More...
 
packed_arrayoperator= (packed_array &&a)
 Move another array. More...
 
void resize (size_t s)
 Change the size of the array. More...
 
void fill (T value)
 Fill the entier array with the given value. More...
 
void resize (size_t s, T value)
 Change the size of the array. More...
 
size_t size () const
 Return the size of the array. More...
 
bool empty () const
 Check if the array is empty. More...
 
operator[] (size_t t) const
 Return an array entry. More...
 
return_type operator[] (size_t t)
 Return a object behaving like a reference to an array entry. More...
 
iterator find (size_type i)
 Return an iterator to the i'th element of the array. More...
 
const_iterator find (size_type i) const
 Return a const iterator to the i'th element of the array. More...
 
iterator begin ()
 Return an iterator to the beginning of the array. More...
 
const_iterator begin () const
 Return a const iterator to the beginning of the array. More...
 
iterator end ()
 Return an iterator to the end of the array. More...
 
const_iterator end () const
 Return a const iterator to the end of the array. More...
 
reverse_iterator rbegin ()
 
const_reverse_iterator rbegin () const
 
reverse_iterator rend ()
 
const_reverse_iterator rend () const
 
void swap (packed_array &o)
 Swap two arrays. More...
 

Static Public Member Functions

static double memory_coefficient ()
 Return the memory coefficient of the structure. More...
 
static double memory_overhead ()
 Return the memory overhead of the structure. More...
 
static memory_size_type memory_usage (memory_size_type size)
 Return the number of bytes required to create a data structure supporting a given number of elements. More...
 
static memory_size_type memory_fits (memory_size_type memory)
 Return the maximum number of elements that can be contained in in the structure when it is allowed to fill a given number of bytes. More...
 

Detailed Description

template<typename T, int B>
class tpie::packed_array< T, B >

An array storring elements of type T using B bits to to store a element.

T must be either bool or int. B must devide the word size, (XXX why?) in reality only 1, 2 or 4 seems usamle

Template Parameters
TThe type of elements to store in the array
BThe number of bits used to store a single element

Definition at line 89 of file packed_array.h.

Member Typedef Documentation

template<typename T, int B>
typedef const_iter_base<true> tpie::packed_array< T, B >::const_iterator

Iterator over a const array.

Definition at line 251 of file packed_array.h.

template<typename T, int B>
typedef const_iter_base<false> tpie::packed_array< T, B >::const_reverse_iterator

Reverse iterator over a const array.

Definition at line 256 of file packed_array.h.

template<typename T, int B>
typedef iter_base<true> tpie::packed_array< T, B >::iterator

Iterator over an array.

Definition at line 261 of file packed_array.h.

template<typename T, int B>
typedef iter_base<false> tpie::packed_array< T, B >::reverse_iterator

Reverse iterator over an array.

Definition at line 266 of file packed_array.h.

template<typename T, int B>
typedef T tpie::packed_array< T, B >::value_type

Type of values containd in the array.

Definition at line 246 of file packed_array.h.

Constructor & Destructor Documentation

template<typename T, int B>
tpie::packed_array< T, B >::packed_array ( size_t  s = 0)
inline

Construct array of given size.

The elements have undefined values

Parameters
sThe number of elements in the array

Definition at line 290 of file packed_array.h.

References tpie::packed_array< T, B >::resize().

Referenced by tpie::packed_array< T, B >::memory_overhead().

290 : m_elements(nullptr), m_size(0) {resize(s);}
void resize(size_t s)
Change the size of the array.
Definition: packed_array.h:353
template<typename T, int B>
tpie::packed_array< T, B >::packed_array ( size_t  s,
value 
)
inline

Construct array of given size.

Parameters
sThe number of elements in the array
valueEach entry of the array is initialized with this value

Definition at line 298 of file packed_array.h.

References tpie::packed_array< T, B >::resize().

298 : m_elements(nullptr), m_size(0) {resize(s,value);}
void resize(size_t s)
Change the size of the array.
Definition: packed_array.h:353
template<typename T, int B>
tpie::packed_array< T, B >::packed_array ( const packed_array< T, B > &  a)
inline

Construct a copy of another array.

Parameters
otherThe array to copy

Definition at line 304 of file packed_array.h.

304 : m_elements(nullptr), m_size(0) {*this=a;}
template<typename T, int B>
tpie::packed_array< T, B >::packed_array ( packed_array< T, B > &&  a)
inline

Move another aray into me.

Parameters
otherThe array to copy

Definition at line 310 of file packed_array.h.

310  : m_elements(a.m_elements), m_size(a.m_size) {
311  a.m_elements = nullptr;
312  a.m_size = 0;
313  }
template<typename T, int B>
tpie::packed_array< T, B >::~packed_array ( )
inline

Free up all memory used by the array.

Definition at line 318 of file packed_array.h.

References tpie::packed_array< T, B >::resize().

318 {resize(0);}
void resize(size_t s)
Change the size of the array.
Definition: packed_array.h:353

Member Function Documentation

template<typename T, int B>
iterator tpie::packed_array< T, B >::begin ( )
inline

Return an iterator to the beginning of the array.

Returns
an iterator tho the beginning of the array

Definition at line 444 of file packed_array.h.

444 {return iterator(m_elements,0);}
iter_base< true > iterator
Iterator over an array.
Definition: packed_array.h:261
template<typename T, int B>
const_iterator tpie::packed_array< T, B >::begin ( ) const
inline

Return a const iterator to the beginning of the array.

Returns
a const iterator tho the beginning of the array

Definition at line 451 of file packed_array.h.

451 {return const_iterator(m_elements,0);}
const_iter_base< true > const_iterator
Iterator over a const array.
Definition: packed_array.h:251
template<typename T, int B>
bool tpie::packed_array< T, B >::empty ( ) const
inline

Check if the array is empty.

Returns
true if and only if size is 0

Definition at line 399 of file packed_array.h.

399 {return m_size ==0;}
template<typename T, int B>
iterator tpie::packed_array< T, B >::end ( )
inline

Return an iterator to the end of the array.

Returns
an iterator tho the end of the array

Definition at line 458 of file packed_array.h.

458 {return iterator(m_elements,m_size);}
iter_base< true > iterator
Iterator over an array.
Definition: packed_array.h:261
template<typename T, int B>
const_iterator tpie::packed_array< T, B >::end ( ) const
inline

Return a const iterator to the end of the array.

Returns
a const iterator tho the end of the array

Definition at line 465 of file packed_array.h.

465 {return const_iterator(m_elements,m_size);}
const_iter_base< true > const_iterator
Iterator over a const array.
Definition: packed_array.h:251
template<typename T, int B>
void tpie::packed_array< T, B >::fill ( value)
inline

Fill the entier array with the given value.

Parameters
elmthe initialization element

Definition at line 365 of file packed_array.h.

Referenced by tpie::packed_array< T, B >::resize().

365  {
366  storage_type x=0;
367  for (size_t i=0; i < perword(); ++i)
368  x = (x << B) + ((storage_type)value & mask());
369  for (size_t i=0; i < words(m_size); ++i)
370  m_elements[i] = x;
371  }
template<typename T, int B>
iterator tpie::packed_array< T, B >::find ( size_type  i)
inline

Return an iterator to the i'th element of the array.

Parameters
ithe index of the element we want an iterator to
Returns
an iterator to the i'th element

Definition at line 429 of file packed_array.h.

429 {return iterator(m_elements,i);}
iter_base< true > iterator
Iterator over an array.
Definition: packed_array.h:261
template<typename T, int B>
const_iterator tpie::packed_array< T, B >::find ( size_type  i) const
inline

Return a const iterator to the i'th element of the array.

Parameters
ithe index of the element we want an iterator to
Returns
a const iterator to the i'th element

Definition at line 437 of file packed_array.h.

437 {return const_iterator(m_elements,i);}
const_iter_base< true > const_iterator
Iterator over a const array.
Definition: packed_array.h:251
template<typename T, int B>
static double tpie::packed_array< T, B >::memory_coefficient ( )
inlinestatic

Return the memory coefficient of the structure.

Allocating a structure with n elements will use at most $ \lfloor \mathrm{memory\_coefficient} \cdot n + \mathrm{memory\_overhead} \rfloor $ bytes. This does not include memory overhead incurred if the structure is allocated using new.

Returns
The memory coefficient of the structure.

Definition at line 272 of file packed_array.h.

272  {
273  return B/8.0;
274  }
static memory_size_type tpie::linear_memory_base< packed_array< T, B > >::memory_fits ( memory_size_type  memory)
inlinestaticinherited

Return the maximum number of elements that can be contained in in the structure when it is allowed to fill a given number of bytes.

Parameters
memoryThe number of bytes the structure is allowed to occupy
Returns
The number of elements that will fit in the structure

Definition at line 93 of file util.h.

93  {
94  return static_cast<memory_size_type>(
95  floor((memory - child_t::memory_overhead()) / child_t::memory_coefficient()));
96  }
template<typename T, int B>
static double tpie::packed_array< T, B >::memory_overhead ( )
inlinestatic

Return the memory overhead of the structure.

See also
memory_coefficient()
Returns
The memory overhead.

Definition at line 280 of file packed_array.h.

References tpie::packed_array< T, B >::packed_array().

280  {
281  return (double)sizeof(packed_array)+(double)sizeof(storage_type);
282  }
packed_array(size_t s=0)
Construct array of given size.
Definition: packed_array.h:290
static memory_size_type tpie::linear_memory_base< packed_array< T, B > >::memory_usage ( memory_size_type  size)
inlinestaticinherited

Return the number of bytes required to create a data structure supporting a given number of elements.

Parameters
sizeThe number of elements to support
Returns
The amount of memory required in bytes

Definition at line 81 of file util.h.

81  {
82  return static_cast<memory_size_type>(
83  floor(static_cast<double>(size) * child_t::memory_coefficient() + child_t::memory_overhead()));
84  }
template<typename T, int B>
packed_array& tpie::packed_array< T, B >::operator= ( const packed_array< T, B > &  a)
inline

Copy elements from another array into this.

Note this array is resized to the size of other

Parameters
otherThe array to copy from
Returns
a reference to this array

Definition at line 327 of file packed_array.h.

References tpie::packed_array< T, B >::resize().

327  {
328  resize(a.m_size);
329  for(size_t i=0;i<words(m_size);++i)
330  m_elements[i] = a.m_elements[i];
331  return *this;
332  }
void resize(size_t s)
Change the size of the array.
Definition: packed_array.h:353
template<typename T, int B>
packed_array& tpie::packed_array< T, B >::operator= ( packed_array< T, B > &&  a)
inline

Move another array.

Definition at line 338 of file packed_array.h.

References tpie::packed_array< T, B >::resize().

338  {
339  resize(0);
340  std::swap(m_elements, a.m_elements);
341  std::swap(m_size, a.m_size);
342  return *this;
343  }
void resize(size_t s)
Change the size of the array.
Definition: packed_array.h:353
template<typename T, int B>
T tpie::packed_array< T, B >::operator[] ( size_t  t) const
inline

Return an array entry.

Parameters
ithe index of the entry to return
Returns
the array entry

Definition at line 407 of file packed_array.h.

407  {
408  assert(t < m_size);
409  return static_cast<T>((m_elements[high(t)] >> low(t))&mask());
410  }
template<typename T, int B>
return_type tpie::packed_array< T, B >::operator[] ( size_t  t)
inline

Return a object behaving like a reference to an array entry.

Parameters
ithe index of the entry to return
Returns
The object behaving like a reference

Definition at line 418 of file packed_array.h.

418  {
419  assert(t < m_size);
420  return return_type(m_elements+high(t), low(t));
421  }
template<typename T, int B>
void tpie::packed_array< T, B >::resize ( size_t  s)
inline

Change the size of the array.

All elements are lost, after resize the value of the entries is undefined

Parameters
sthe new size of the array

Definition at line 353 of file packed_array.h.

References tpie::tpie_delete_array().

Referenced by tpie::packed_array< T, B >::operator=(), tpie::packed_array< T, B >::packed_array(), tpie::packed_array< T, B >::resize(), and tpie::packed_array< T, B >::~packed_array().

353  {
354  if (s == m_size) return;
355  tpie_delete_array(m_elements, words(m_size));
356  m_size = s;
357  m_elements = m_size?tpie_new_array<storage_type>(words(m_size)):nullptr;
358  }
void tpie_delete_array(T *a, size_t size)
Delete an array allocated with tpie_new_array.
Definition: memory.h:319
template<typename T, int B>
void tpie::packed_array< T, B >::resize ( size_t  s,
value 
)
inline

Change the size of the array.

All elements are lost, after resize the value of the entries is initialized by a copy of elm

Parameters
sthe new size of the array
valuethe initialization element

Definition at line 382 of file packed_array.h.

References tpie::packed_array< T, B >::fill(), and tpie::packed_array< T, B >::resize().

382  {
383  resize(s);
384  fill(value);
385  }
void resize(size_t s)
Change the size of the array.
Definition: packed_array.h:353
void fill(T value)
Fill the entier array with the given value.
Definition: packed_array.h:365
template<typename T, int B>
size_t tpie::packed_array< T, B >::size ( ) const
inline

Return the size of the array.

Returns
the size of the array

Definition at line 392 of file packed_array.h.

392 {return m_size;}
template<typename T, int B>
void tpie::packed_array< T, B >::swap ( packed_array< T, B > &  o)
inline

Swap two arrays.

Definition at line 477 of file packed_array.h.

477  {
478  std::swap(m_elements, o.m_elements);
479  std::swap(m_size, o.m_size);
480  }

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