TPIE

2362a60
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 ()
 
arrayoperator= (const array &other)
 Copy elements from another array into this. More...
 
arrayoperator= (array &&other)
 Move elements from another array into this. More...
 
template<typename OtherAllocator >
arrayoperator= (const array< T, OtherAllocator > &other)
 Copy elements from another array with any allocator 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, memory_bucket_ref bucket)
 
 array (memory_bucket_ref bucket)
 
 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 (array &&other)
 Move construct from 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...
 
Allocator get_allocator () const
 Return copy of the allocator. 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...
 

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 144 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 147 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 150 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 153 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 156 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 159 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 408 of file array.h.

Referenced by tpie::array< push_type< dest_t >::type >::memory_overhead().

410  : m_elements(0), m_size(0), m_tss_used(false), m_allocator(alloc)
411  {resize(s, value);}
void resize(size_t size, const T &elm)
Change the size of the array.
Definition: array.h:485
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 425 of file array.h.

426  : m_elements(0), m_size(0), m_tss_used(false),
427  m_allocator(alloc) {resize(s);}
void resize(size_t size, const T &elm)
Change the size of the array.
Definition: array.h:485
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 433 of file array.h.

433  : m_elements(0), m_size(other.m_size), m_tss_used(false), m_allocator(other.m_allocator) {
434  if (other.size() == 0) return;
435  alloc_copy(other.m_elements);
436  }
template<typename T, typename Allocator = allocator<T>>
tpie::array< T, Allocator >::array ( array< T, Allocator > &&  other)
inline

Move construct from another array.

Parameters
otherThe array to move.

Definition at line 442 of file array.h.

443  : m_elements(other.m_elements)
444  , m_size(other.m_size)
445  , m_tss_used(other.m_tss_used)
446  , m_allocator(other.m_allocator) {
447  other.m_elements = nullptr;
448  other.m_size = 0;
449  other.m_tss_used = false;
450  }
template<typename T, typename Allocator = allocator<T>>
tpie::array< T, Allocator >::~array ( )
inline

Free up all memory used by the array.

Definition at line 473 of file array.h.

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

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 188 of file array.h.

Referenced by tpie::array< push_type< dest_t >::type >::back(), tpie::array< push_type< dest_t >::type >::front(), and tpie::array< push_type< dest_t >::type >::operator[]().

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

Definition at line 196 of file array.h.

196  {
197  assert(i < size());
198  return m_elements[i];
199  }
size_type size() const
Return the size of the array.
Definition: array.h:526
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 343 of file array.h.

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

Return the last element in the array.

Definition at line 348 of file array.h.

348 {return at(size()-1);}
T & at(size_t i)
Return the element located at the given index.
Definition: array.h:188
size_type size() const
Return the size of the array.
Definition: array.h:526
template<typename T, typename Allocator = allocator<T>>
iterator tpie::array< T, Allocator >::begin ( )
inline
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 314 of file array.h.

314 {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 253 of file array.h.

253 {return size() == 0;}
size_type size() const
Return the size of the array.
Definition: array.h:526
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 321 of file array.h.

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

321 {return get_iter(size());}
size_type size() const
Return the size of the array.
Definition: array.h:526
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 328 of file array.h.

328 {return get_iter(size());}
size_type size() const
Return the size of the array.
Definition: array.h:526
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 167 of file array.h.

Referenced by tpie::internal_vector< T >::end(), tpie::internal_priority_queue< tpie::ami::heap_ptr< REC >, comp >::insert(), and tpie::internal_priority_queue< tpie::ami::heap_ptr< REC >, comp >::make_safe().

167  {
168  assert(idx <= size());
169  return get_iter(idx);
170  }
size_type size() const
Return the size of the array.
Definition: array.h:526
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 178 of file array.h.

178  {
179  assert(idx <= size());
180  return get_iter(idx);
181  }
size_type size() const
Return the size of the array.
Definition: array.h:526
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 333 of file array.h.

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

Return the first element in the array.

Definition at line 338 of file array.h.

338 {return at(0);}
T & at(size_t i)
Return the element located at the given index.
Definition: array.h:188
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 531 of file array.h.

Referenced by tpie::compressor_buffer::get(), and tpie::bits::serialization_reader_base::read().

531 {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 536 of file array.h.

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

Return copy of the allocator.

Definition at line 541 of file array.h.

541 {return m_allocator;}
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 memory_size_type tpie::linear_memory_base< array< T > >::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.

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

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, 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 296 of file array.h.

296  {
297  if (size() != other.size()) return true;
298  for (size_t i=0; i<size(); ++i) if (*get_iter(i) != *other.get_iter(i)) return true;
299  return false;
300  }
size_type size() const
Return the size of the array.
Definition: array.h:526
template<typename T, typename Allocator = allocator<T>>
array& tpie::array< T, Allocator >::operator= ( const array< T, Allocator > &  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:485
size_type size() const
Return the size of the array.
Definition: array.h:526
template<typename T, typename Allocator = allocator<T>>
array& tpie::array< T, Allocator >::operator= ( array< T, Allocator > &&  other)
inline

Move 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 223 of file array.h.

223  {
224  resize(0);
225  std::swap(m_allocator, other.m_allocator);
226  std::swap(m_elements, other.m_elements);
227  std::swap(m_size, other.m_size);
228  std::swap(m_tss_used, other.m_tss_used);
229  return *this;
230  }
void resize(size_t size, const T &elm)
Change the size of the array.
Definition: array.h:485
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 with any allocator into this.

Note: This array is resized to the size of other.

Parameters
otherThe array to copy from.
Template Parameters
OtherAllocatorThe allocator used by the other array.
Returns
A reference to this array.

Definition at line 242 of file array.h.

242  {
243  resize(other.size());
244  for (size_t i=0; i < size(); ++i) m_elements[i] = other[i];
245  return *this;
246  }
void resize(size_t size, const T &elm)
Change the size of the array.
Definition: array.h:485
size_type size() const
Return the size of the array.
Definition: array.h:526
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 284 of file array.h.

284  {
285  if (size() != other.size()) return false;
286  for (size_t i=0;i<size();++i) if (*get_iter(i) != *other.get_iter(i)) return false;
287  return true;
288  }
size_type size() const
Return the size of the array.
Definition: array.h:526
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 261 of file array.h.

261  {
262  assert(i < size());
263  return at(i);
264  }
T & at(size_t i)
Return the element located at the given index.
Definition: array.h:188
size_type size() const
Return the size of the array.
Definition: array.h:526
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 272 of file array.h.

272  {
273  assert(i < size());
274  return at(i);
275  }
T & at(size_t i)
Return the element located at the given index.
Definition: array.h:188
size_type size() const
Return the size of the array.
Definition: array.h:526
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 353 of file array.h.

353 {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 358 of file array.h.

358 {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 363 of file array.h.

363 {return get_rev_iter(size());}
size_type size() const
Return the size of the array.
Definition: array.h:526
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 368 of file array.h.

368 {return get_rev_iter(size());}
size_type size() const
Return the size of the array.
Definition: array.h:526
template<typename T, typename Allocator = allocator<T>>
void tpie::array< T, Allocator >::resize ( size_t  size,
const T &  elm 
)
inline
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 515 of file array.h.

515  {
516  destruct_and_dealloc();
517  m_size = s;
518  alloc_dfl();
519  }
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 526 of file array.h.

Referenced by tpie::array< push_type< dest_t >::type >::array(), tpie::array< push_type< dest_t >::type >::at(), tpie::internal_queue< item_type >::back(), tpie::array< push_type< dest_t >::type >::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::compressor_buffer::capacity(), tpie::chaining_hash_table< value_t, hash_t, equal_t, index_t >::clear(), tpie::array< push_type< dest_t >::type >::empty(), tpie::chaining_hash_table< value_t, hash_t, equal_t, index_t >::end(), tpie::linear_probing_hash_table< value_t, hash_t, equal_t, index_t >::end(), tpie::array< push_type< dest_t >::type >::end(), tpie::merge_sorter< T, UseProgress, pred_t, store_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::chaining_hash_table< value_t, hash_t, equal_t, index_t >::find(), tpie::array< push_type< dest_t >::type >::find(), tpie::linear_probing_hash_table< value_t, hash_t, equal_t, index_t >::find(), tpie::internal_queue< item_type >::front(), tpie::internal_queue< item_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::array< push_type< dest_t >::type >::operator!=(), tpie::array< push_type< dest_t >::type >::operator=(), tpie::array< push_type< dest_t >::type >::operator==(), tpie::array< push_type< dest_t >::type >::operator[](), tpie::internal_queue< item_type >::push(), tpie::stack< item_type >::push(), tpie::internal_priority_queue< tpie::ami::heap_ptr< REC >, comp >::push(), tpie::serialization_bits::internal_sort< T, pred_t >::push(), tpie::array< push_type< dest_t >::type >::rend(), and tpie::array< push_type< dest_t >::type >::resize().

526 {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 499 of file array.h.

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

499  {
500  std::swap(m_allocator, other.m_allocator);
501  std::swap(m_elements, other.m_elements);
502  std::swap(m_size, other.m_size);
503  std::swap(m_tss_used, other.m_tss_used);
504  }

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