TPIE

v1.1rc1-6-g0c97303
tpie::array< T, Allocator > Class Template Reference

A generic array with a fixed size. More...

#include <tpie/array.h>

Inherits tpie::linear_memory_base< array< T > >.

Public Types

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

Public Member Functions

iterator find (size_t idx) throw ()
 Return an iterator to the i'th element of the array. More...
 
const_iterator find (size_t idx) const throw ()
 Return a const iterator to the i'th element of the array. More...
 
T & at (size_t i) throw ()
 Return the element located at the given index. More...
 
const T & at (size_t i) const throw ()
 
template<typename OtherAllocator >
arrayoperator= (const array< T, OtherAllocator > &other)
 Copy elements from another array into this. More...
 
bool empty () const
 Check if the array is empty. More...
 
const T & operator[] (size_t i) const
 Return a const reference to an array entry. More...
 
T & operator[] (size_t i)
 Return a reference to an array entry. More...
 
bool operator== (const array &other) const
 Compare if the other array has the same elements in the same order as this. More...
 
bool operator!= (const array &other) const
 Check if two arrays differ. 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...
 
const T & front () const
 Return the first element in the array. More...
 
T & front ()
 Return the first element in the array. More...
 
const T & back () const
 Return the last element in the array. More...
 
T & back ()
 Return the last element in the array. More...
 
reverse_iterator rbegin ()
 Reverse iterator to beginning of reverse sequence. More...
 
const_reverse_iterator rbegin () const
 Const reverse iterator to beginning of reverse sequence. More...
 
reverse_iterator rend ()
 Reverse iterator to end of reverse sequence. More...
 
const_reverse_iterator rend () const
 Const reverse iterator to end of reverse sequence. More...
 
 array (size_type s, const T &value, const Allocator &alloc=Allocator())
 Construct array of given size. More...
 
 array (size_type s=0, const Allocator &alloc=Allocator())
 Construct array of given size. More...
 
 array (const array &other)
 Construct a copy of another array. More...
 
 array (const array_view_base< T > &view)
 
 array (const array_view_base< const T > &view)
 
 ~array ()
 Free up all memory used by the array. More...
 
void resize (size_t size, const T &elm)
 Change the size of the array. More...
 
void swap (array &other)
 Swap two arrays. More...
 
void resize (size_t s)
 Change the size of the array. More...
 
size_type size () const
 Return the size of the array. More...
 
T * get ()
 Return a raw pointer to the array content. More...
 
const T * get () const
 Return a raw pointer to the array content. 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 stream_size_type memory_usage (stream_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...
 

Friends

struct bits::allocator_usage< T, Allocator >
 

Detailed Description

template<typename T, typename Allocator = allocator<T>>
class tpie::array< T, Allocator >

A generic array with a fixed size.

This is almost the same as a real C-style T array but the memory management is better.

Template Parameters
TThe type of element to contain.
alloc_tAllocator.

Definition at line 143 of file array.h.

Member Typedef Documentation

template<typename T, typename Allocator = allocator<T>>
typedef array_iter_base<T const, true> tpie::array< T, Allocator >::const_iterator

Iterator over a const array.

Definition at line 146 of file array.h.

template<typename T, typename Allocator = allocator<T>>
typedef array_iter_base<T const, false> tpie::array< T, Allocator >::const_reverse_iterator

Reverse iterator over a const array.

Definition at line 149 of file array.h.

template<typename T, typename Allocator = allocator<T>>
typedef array_iter_base<T, true> tpie::array< T, Allocator >::iterator

Iterator over an array.

Definition at line 152 of file array.h.

template<typename T, typename Allocator = allocator<T>>
typedef array_iter_base<T, false> tpie::array< T, Allocator >::reverse_iterator

Reverse iterator over an array.

Definition at line 155 of file array.h.

template<typename T, typename Allocator = allocator<T>>
typedef T tpie::array< T, Allocator >::value_type

Type of values containd in the array.

Definition at line 158 of file array.h.

Constructor & Destructor Documentation

template<typename T, typename Allocator = allocator<T>>
tpie::array< T, Allocator >::array ( size_type  s,
const T &  value,
const Allocator &  alloc = Allocator() 
)
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 375 of file array.h.

Referenced by tpie::array< tpie::pipelining::parallel_bits::parallel_input_buffer< T > * >::memory_overhead().

377  : m_elements(0), m_size(0), m_tss_used(false), m_allocator(alloc)
378  {resize(s, value);}
void resize(size_t size, const T &elm)
Change the size of the array.
Definition: array.h:431
template<typename T, typename Allocator = allocator<T>>
tpie::array< T, Allocator >::array ( size_type  s = 0,
const Allocator &  alloc = Allocator() 
)
inline

Construct array of given size.

Parameters
sThe number of elements in the array.

Definition at line 385 of file array.h.

386  : m_elements(0), m_size(0), m_tss_used(false),
387  m_allocator(alloc) {resize(s);}
void resize(size_t size, const T &elm)
Change the size of the array.
Definition: array.h:431
template<typename T, typename Allocator = allocator<T>>
tpie::array< T, Allocator >::array ( const array< T, Allocator > &  other)
inline

Construct a copy of another array.

Parameters
otherThe array to copy.

Definition at line 393 of file array.h.

393  : m_elements(0), m_size(other.m_size), m_tss_used(false), m_allocator(other.m_allocator) {
394  if (other.size() == 0) return;
395  alloc_copy(other.m_elements);
396  }
template<typename T, typename Allocator = allocator<T>>
tpie::array< T, Allocator >::~array ( )
inline

Free up all memory used by the array.

Definition at line 419 of file array.h.

419 {resize(0);}
void resize(size_t size, const T &elm)
Change the size of the array.
Definition: array.h:431

Member Function Documentation

template<typename T, typename Allocator = allocator<T>>
T& tpie::array< T, Allocator >::at ( size_t  i)
throw (
)
inline

Return the element located at the given index.

Parameters
iThe index of the element returned.

Definition at line 187 of file array.h.

Referenced by tpie::array< tpie::pipelining::parallel_bits::parallel_input_buffer< T > * >::back(), tpie::array< tpie::pipelining::parallel_bits::parallel_input_buffer< T > * >::front(), and tpie::array< tpie::pipelining::parallel_bits::parallel_input_buffer< T > * >::operator[]().

187  {
188  assert(i < size());
189  return m_elements[i];
190  }
size_type size() const
Return the size of the array.
Definition: array.h:472
template<typename T, typename Allocator = allocator<T>>
const T& tpie::array< T, Allocator >::at ( size_t  i) const
throw (
)
inline

Definition at line 195 of file array.h.

195  {
196  assert(i < size());
197  return m_elements[i];
198  }
size_type size() const
Return the size of the array.
Definition: array.h:472
template<typename T, typename Allocator = allocator<T>>
const T& tpie::array< T, Allocator >::back ( ) const
inline

Return the last element in the array.

Definition at line 310 of file array.h.

310 {return at(size()-1);}
T & at(size_t i)
Return the element located at the given index.
Definition: array.h:187
size_type size() const
Return the size of the array.
Definition: array.h:472
template<typename T, typename Allocator = allocator<T>>
T& tpie::array< T, Allocator >::back ( )
inline

Return the last element in the array.

Definition at line 315 of file array.h.

315 {return at(size()-1);}
T & at(size_t i)
Return the element located at the given index.
Definition: array.h:187
size_type size() const
Return the size of the array.
Definition: array.h:472
template<typename T, typename Allocator = allocator<T>>
const_iterator tpie::array< T, Allocator >::begin ( ) const
inline

Return a const iterator to the beginning of the array.

Returns
A const iterator to the beginning of the array.

Definition at line 281 of file array.h.

281 {return get_iter(0);}
template<typename T, typename Allocator = allocator<T>>
bool tpie::array< T, Allocator >::empty ( ) const
inline

Check if the array is empty.

Returns
True if and only if size is 0.

Definition at line 220 of file array.h.

220 {return size() == 0;}
size_type size() const
Return the size of the array.
Definition: array.h:472
template<typename T, typename Allocator = allocator<T>>
iterator tpie::array< T, Allocator >::end ( )
inline

Return an iterator to the end of the array.

Returns
An iterator to the end of the array.

Definition at line 288 of file array.h.

Referenced by tpie::chaining_hash_table< value_t, hash_t, equal_t, index_t >::clear(), and tpie::linear_probing_hash_table< value_t, hash_t, equal_t, index_t >::clear().

288 {return get_iter(size());}
size_type size() const
Return the size of the array.
Definition: array.h:472
template<typename T, typename Allocator = allocator<T>>
const_iterator tpie::array< T, Allocator >::end ( ) const
inline

Return a const iterator to the end of the array.

Returns
A const iterator to the end of the array.

Definition at line 295 of file array.h.

295 {return get_iter(size());}
size_type size() const
Return the size of the array.
Definition: array.h:472
template<typename T, typename Allocator = allocator<T>>
iterator tpie::array< T, Allocator >::find ( size_t  idx)
throw (
)
inline

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

Parameters
idxThe index of the element we want an iterator to.
Returns
An iterator to the i'th element.

Definition at line 166 of file array.h.

Referenced by tpie::internal_vector< T >::end(), tpie::internal_priority_queue< std::pair< T, size_t >, tpie::merger::predwrap >::insert(), tpie::internal_priority_queue< std::pair< T, size_t >, tpie::merger::predwrap >::make_safe(), tpie::internal_priority_queue< std::pair< T, size_t >, tpie::merger::predwrap >::pop(), and tpie::internal_priority_queue< std::pair< T, size_t >, tpie::merger::predwrap >::pop_and_push().

166  {
167  assert(idx <= size());
168  return get_iter(idx);
169  }
size_type size() const
Return the size of the array.
Definition: array.h:472
template<typename T, typename Allocator = allocator<T>>
const_iterator tpie::array< T, Allocator >::find ( size_t  idx) const
throw (
)
inline

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

Parameters
idxThe index of the element we want an iterator to.
Returns
A const iterator to the i'th element.

Definition at line 177 of file array.h.

177  {
178  assert(idx <= size());
179  return get_iter(idx);
180  }
size_type size() const
Return the size of the array.
Definition: array.h:472
template<typename T, typename Allocator = allocator<T>>
const T& tpie::array< T, Allocator >::front ( ) const
inline

Return the first element in the array.

Definition at line 300 of file array.h.

300 {return at(0);}
T & at(size_t i)
Return the element located at the given index.
Definition: array.h:187
template<typename T, typename Allocator = allocator<T>>
T& tpie::array< T, Allocator >::front ( )
inline

Return the first element in the array.

Definition at line 305 of file array.h.

305 {return at(0);}
T & at(size_t i)
Return the element located at the given index.
Definition: array.h:187
template<typename T, typename Allocator = allocator<T>>
T* tpie::array< T, Allocator >::get ( )
inline

Return a raw pointer to the array content.

Definition at line 477 of file array.h.

Referenced by tpie::bits::serialization_reader_base::read().

477 {return m_elements;}
template<typename T, typename Allocator = allocator<T>>
const T* tpie::array< T, Allocator >::get ( ) const
inline

Return a raw pointer to the array content.

Definition at line 482 of file array.h.

482 {return m_elements;}
template<typename T, typename Allocator = allocator<T>>
static double tpie::array< T, Allocator >::memory_coefficient ( )
inlinestatic
static memory_size_type tpie::linear_memory_base< array< T > >::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, typename Allocator = allocator<T>>
static double tpie::array< T, Allocator >::memory_overhead ( )
inlinestatic
static stream_size_type tpie::linear_memory_base< array< T > >::memory_usage ( stream_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.

Referenced by tpie::stack< item_type >::memory_usage().

81  {
82  return static_cast<stream_size_type>(
83  floor(static_cast<double>(size) * child_t::memory_coefficient() + child_t::memory_overhead()));
84  }
template<typename T, typename Allocator = allocator<T>>
bool tpie::array< T, Allocator >::operator!= ( const array< T, Allocator > &  other) const
inline

Check if two arrays differ.

Parameters
otherThe array to compare against.
Returns
False if they are equal; otherwise true.

Definition at line 263 of file array.h.

263  {
264  if (size() != other.size()) return true;
265  for (size_t i=0; i<size(); ++i) if (*get_iter(i) != *other.get_iter(i)) return true;
266  return false;
267  }
size_type size() const
Return the size of the array.
Definition: array.h:472
template<typename T, typename Allocator = allocator<T>>
template<typename OtherAllocator >
array& tpie::array< T, Allocator >::operator= ( const array< T, OtherAllocator > &  other)
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 209 of file array.h.

209  {
210  resize(other.size());
211  for (size_t i=0; i < size(); ++i) m_elements[i] = other[i];
212  return *this;
213  }
void resize(size_t size, const T &elm)
Change the size of the array.
Definition: array.h:431
size_type size() const
Return the size of the array.
Definition: array.h:472
template<typename T, typename Allocator = allocator<T>>
bool tpie::array< T, Allocator >::operator== ( const array< T, Allocator > &  other) const
inline

Compare if the other array has the same elements in the same order as this.

Parameters
otherThe array to compare against.
Returns
True if they are equal otherwise false.

Definition at line 251 of file array.h.

251  {
252  if (size() != other.size()) return false;
253  for (size_t i=0;i<size();++i) if (*get_iter(i) != *other.get_iter(i)) return false;
254  return true;
255  }
size_type size() const
Return the size of the array.
Definition: array.h:472
template<typename T, typename Allocator = allocator<T>>
const T& tpie::array< T, Allocator >::operator[] ( size_t  i) const
inline

Return a const reference to an array entry.

Parameters
iThe index of the entry to return.
Returns
Const reference to the entry.

Definition at line 228 of file array.h.

228  {
229  assert(i < size());
230  return at(i);
231  }
T & at(size_t i)
Return the element located at the given index.
Definition: array.h:187
size_type size() const
Return the size of the array.
Definition: array.h:472
template<typename T, typename Allocator = allocator<T>>
T& tpie::array< T, Allocator >::operator[] ( size_t  i)
inline

Return a reference to an array entry.

Parameters
iThe index of the entry to return.
Returns
Reference to the entry.

Definition at line 239 of file array.h.

239  {
240  assert(i < size());
241  return at(i);
242  }
T & at(size_t i)
Return the element located at the given index.
Definition: array.h:187
size_type size() const
Return the size of the array.
Definition: array.h:472
template<typename T, typename Allocator = allocator<T>>
reverse_iterator tpie::array< T, Allocator >::rbegin ( )
inline

Reverse iterator to beginning of reverse sequence.

Definition at line 320 of file array.h.

320 {return get_rev_iter(0);}
template<typename T, typename Allocator = allocator<T>>
const_reverse_iterator tpie::array< T, Allocator >::rbegin ( ) const
inline

Const reverse iterator to beginning of reverse sequence.

Definition at line 325 of file array.h.

325 {return get_rev_iter(0);}
template<typename T, typename Allocator = allocator<T>>
reverse_iterator tpie::array< T, Allocator >::rend ( )
inline

Reverse iterator to end of reverse sequence.

Definition at line 330 of file array.h.

330 {return get_rev_iter(size());}
size_type size() const
Return the size of the array.
Definition: array.h:472
template<typename T, typename Allocator = allocator<T>>
const_reverse_iterator tpie::array< T, Allocator >::rend ( ) const
inline

Const reverse iterator to end of reverse sequence.

Definition at line 335 of file array.h.

335 {return get_rev_iter(size());}
size_type size() const
Return the size of the array.
Definition: array.h:472
template<typename T, typename Allocator = allocator<T>>
void tpie::array< T, Allocator >::resize ( size_t  size,
const T &  elm 
)
inline

Change the size of the array.

All elements are lost.

Memory manager MUST be initialized at this point unless s == 0.

Parameters
sizeThe new size of the array.
elmThe initialization element.

Definition at line 431 of file array.h.

Referenced by tpie::array< tpie::pipelining::parallel_bits::parallel_input_buffer< T > * >::array(), tpie::merge_sorter< T, UseProgress, pred_t >::begin(), tpie::pipelining::parallel_bits::producer< T1, T2 >::begin(), tpie::merge_sorter< T, UseProgress, pred_t >::end(), tpie::pipelining::parallel_bits::producer< T1, T2 >::end(), tpie::serialization_bits::internal_sort< T, pred_t >::free(), tpie::internal_queue< memory_size_type >::internal_queue(), tpie::internal_stack_vector_base< T, internal_stack< T > >::internal_stack_vector_base(), tpie::array< tpie::pipelining::parallel_bits::parallel_input_buffer< T > * >::operator=(), tpie::merge_sorter< T, UseProgress, pred_t >::pull(), tpie::internal_queue< memory_size_type >::resize(), tpie::internal_stack_vector_base< T, internal_stack< T > >::resize(), tpie::internal_priority_queue< std::pair< T, size_t >, tpie::merger::predwrap >::resize(), tpie::chaining_hash_table< value_t, hash_t, equal_t, index_t >::resize(), tpie::linear_probing_hash_table< value_t, hash_t, equal_t, index_t >::resize(), and tpie::array< tpie::pipelining::parallel_bits::parallel_input_buffer< T > * >::~array().

431  {
432  if (size != m_size) {
433  destruct_and_dealloc();
434  m_size = size;
435 
436  alloc_fill(elm);
437  } else {
438  std::fill(m_elements+0, m_elements+m_size, elm);
439  }
440  }
size_type size() const
Return the size of the array.
Definition: array.h:472
template<typename T, typename Allocator = allocator<T>>
void tpie::array< T, Allocator >::resize ( size_t  s)
inline

Change the size of the array.

All elements are lost.

Memory manager MUST be initialized at this point unless s == 0.

Parameters
sThe new size of the array.

Definition at line 461 of file array.h.

461  {
462  destruct_and_dealloc();
463  m_size = s;
464  alloc_dfl();
465  }
template<typename T, typename Allocator = allocator<T>>
size_type tpie::array< T, Allocator >::size ( ) const
inline

Return the size of the array.

Returns
The size of the array.

Definition at line 472 of file array.h.

Referenced by tpie::array< tpie::pipelining::parallel_bits::parallel_input_buffer< T > * >::array(), tpie::array< tpie::pipelining::parallel_bits::parallel_input_buffer< T > * >::at(), tpie::internal_queue< memory_size_type >::back(), tpie::array< tpie::pipelining::parallel_bits::parallel_input_buffer< T > * >::back(), tpie::chaining_hash_table< value_t, hash_t, equal_t, index_t >::begin(), tpie::linear_probing_hash_table< value_t, hash_t, equal_t, index_t >::begin(), tpie::chaining_hash_table< value_t, hash_t, equal_t, index_t >::clear(), tpie::array< tpie::pipelining::parallel_bits::parallel_input_buffer< T > * >::empty(), tpie::merge_sorter< T, UseProgress, pred_t >::end(), tpie::chaining_hash_table< value_t, hash_t, equal_t, index_t >::end(), tpie::array< tpie::pipelining::parallel_bits::parallel_input_buffer< T > * >::end(), tpie::linear_probing_hash_table< value_t, hash_t, equal_t, index_t >::end(), tpie::chaining_hash_table< value_t, hash_t, equal_t, index_t >::erase(), tpie::linear_probing_hash_table< value_t, hash_t, equal_t, index_t >::erase(), tpie::array< tpie::pipelining::parallel_bits::parallel_input_buffer< T > * >::find(), tpie::chaining_hash_table< value_t, hash_t, equal_t, index_t >::find(), tpie::linear_probing_hash_table< value_t, hash_t, equal_t, index_t >::find(), tpie::internal_queue< memory_size_type >::front(), tpie::internal_queue< memory_size_type >::full(), tpie::chaining_hash_table< value_t, hash_t, equal_t, index_t >::insert(), tpie::linear_probing_hash_table< value_t, hash_t, equal_t, index_t >::insert(), tpie::serialization_bits::internal_sort< T, pred_t >::memory_usage(), tpie::array< tpie::pipelining::parallel_bits::parallel_input_buffer< T > * >::operator!=(), tpie::array< tpie::pipelining::parallel_bits::parallel_input_buffer< T > * >::operator=(), tpie::array< tpie::pipelining::parallel_bits::parallel_input_buffer< T > * >::operator==(), tpie::array< tpie::pipelining::parallel_bits::parallel_input_buffer< T > * >::operator[](), tpie::internal_queue< memory_size_type >::push(), tpie::serialization_bits::internal_sort< T, pred_t >::push(), tpie::internal_priority_queue< std::pair< T, size_t >, tpie::merger::predwrap >::push(), tpie::stack< item_type >::push(), tpie::array< tpie::pipelining::parallel_bits::parallel_input_buffer< T > * >::rend(), and tpie::array< tpie::pipelining::parallel_bits::parallel_input_buffer< T > * >::resize().

472 {return m_size;}
template<typename T, typename Allocator = allocator<T>>
void tpie::array< T, Allocator >::swap ( array< T, Allocator > &  other)
inline

Swap two arrays.

Definition at line 445 of file array.h.

Referenced by tpie::merge_sorter< T, UseProgress, pred_t >::end().

445  {
446  std::swap(m_allocator, other.m_allocator);
447  std::swap(m_elements, other.m_elements);
448  std::swap(m_size, other.m_size);
449  std::swap(m_tss_used, other.m_tss_used);
450  }

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