TPIE

2362a60
tpie::array_view< T > Class Template Reference

Encapsulation of two pointers from any random access container. More...

#include <tpie/array_view.h>

Inherits tpie::array_view_base< T >.

Public Types

typedef T value_type
 Type of values contained in the array. More...
 

Public Member Functions

 array_view (const array_view &o)
 Copy constructor. More...
 
template<typename A >
 array_view (std::vector< T, A > &v)
 std::vector constructor. More...
 
 array_view (tpie::array< T > &v)
 tpie::array constructor. More...
 
 array_view (tpie::internal_vector< T > &v)
 tpie::internal_vector constructor. More...
 
template<typename A >
 array_view (std::vector< T, A > &v, size_t start, size_t end)
 std::vector subsequence. More...
 
 array_view (tpie::array< T > &v, size_t start, size_t end)
 tpie::array subsequence. More...
 
 array_view (T *start, T *end)
 Pointer constructor. More...
 
 array_view (T *start, size_t size)
 Pointer+offset constructor. More...
 
iterator find (size_t idx) const throw ()
 Return an iterator to the i'th element of the array. More...
 
T & at (size_t i) const throw ()
 Return the element located at the given index. More...
 
bool empty () const
 Check if the array is empty. More...
 
size_t size () const
 Get number of elements in the array. More...
 
T & operator[] (size_t i) const
 Return a reference to an array entry. More...
 
bool operator== (const array_view_base &other) const
 Check if the other array has the same elements in the same order as this. More...
 
bool operator!= (const array_view_base &other) const
 Check if the two arrays differ. More...
 
iterator begin () const
 Return an iterator to the beginning of the array. More...
 
iterator end () const
 Return an iterator to the end of the array. More...
 
T & front () const
 Return the first element in the array. More...
 
T & back () const
 Return the last element in the array. More...
 

Detailed Description

template<typename T>
class tpie::array_view< T >

Encapsulation of two pointers from any random access container.

Like std::vector, tpie::array and others, this class has begin(), end() iterators and front(), back() accessors. However, an array_view does not store data, it just stores pointers to data owned by other random access data structures. Passing around array_views, one avoids having the container type as a template parameter in one's code, but the code will still work with any supported random access container.

Definition at line 47 of file array_view.h.

Member Typedef Documentation

template<typename T>
typedef T tpie::array_view_base< T >::value_type
inherited

Type of values contained in the array.

Definition at line 73 of file array_view_base.h.

Constructor & Destructor Documentation

template<typename T>
tpie::array_view< T >::array_view ( const array_view< T > &  o)
inline

Copy constructor.

Definition at line 52 of file array_view.h.

52  :
53  array_view_base<T>(o) {}
template<typename T>
template<typename A >
tpie::array_view< T >::array_view ( std::vector< T, A > &  v)
inline

std::vector constructor.

Definition at line 59 of file array_view.h.

59  :
60  array_view_base<T>(&*(v.begin()), &(*v.end())) {}
template<typename T>
tpie::array_view< T >::array_view ( tpie::array< T > &  v)
inline

tpie::array constructor.

Definition at line 65 of file array_view.h.

65  :
66  array_view_base<T>(&*(v.begin()), &(*v.end())) {}
iterator end()
Return an iterator to the end of the array.
Definition: array.h:321
iterator begin()
Return an iterator to the beginning of the array.
Definition: array.h:307
template<typename T>
tpie::array_view< T >::array_view ( tpie::internal_vector< T > &  v)
inline

tpie::internal_vector constructor.

Definition at line 71 of file array_view.h.

71  :
72  array_view_base<T>(&*(v.begin()), &(*v.end())) {}
iterator begin()
Get an iterator to the beginning of the structure.
iterator end()
Get an iterator to the end of the structure.
template<typename T>
template<typename A >
tpie::array_view< T >::array_view ( std::vector< T, A > &  v,
size_t  start,
size_t  end 
)
inline

std::vector subsequence.

Parameters
vThe std::vector that stores the elements.
startThe index of the first element of the view (such that view.front() == v[start]).
endIndex past the last element of the view (such that view.back() == v[end-1]).

Definition at line 81 of file array_view.h.

81  :
82  array_view_base<T>(&v[start], &v[end]) {}
iterator end() const
Return an iterator to the end of the array.
template<typename T>
tpie::array_view< T >::array_view ( tpie::array< T > &  v,
size_t  start,
size_t  end 
)
inline

tpie::array subsequence.

Parameters
vThe tpie::array that stores the elements.
startThe index of the first element of the view (such that view.front() == v[start]).
endIndex past the last element of the view (such that view.back() == v[end-1]).

Definition at line 90 of file array_view.h.

90  :
91  array_view_base<T>(&v[start], &v[end]) {}
iterator end() const
Return an iterator to the end of the array.
template<typename T>
tpie::array_view< T >::array_view ( T *  start,
T *  end 
)
inline

Pointer constructor.

The structure will produce the elements in the memory range [start, end).

Parameters
startPointer to first element of the array_view.
endPointer past the last element of the array_view.

Definition at line 97 of file array_view.h.

97  :
98  array_view_base<T>(start, end) {}
iterator end() const
Return an iterator to the end of the array.
template<typename T>
tpie::array_view< T >::array_view ( T *  start,
size_t  size 
)
inline

Pointer+offset constructor.

The array_view will produce the elements in the memory range [start, start+size).

Parameters
startPointer to first element of the array_view.
sizeNumber of elements in the array_view.

Definition at line 106 of file array_view.h.

106  :
107  array_view_base<T>(start, start+size) {}
size_t size() const
Get number of elements in the array.

Member Function Documentation

template<typename T>
T& tpie::array_view_base< T >::at ( size_t  i) const
throw (
)
inlineinherited

Return the element located at the given index.

Parameters
iThe index of the element returned.

Definition at line 91 of file array_view_base.h.

Referenced by tpie::array_view_base< const T >::operator!=(), tpie::array_view_base< const T >::operator==(), and tpie::array_view_base< const T >::operator[]().

91  {
92  assert(i < size());
93  return *find(i);
94  }
iterator find(size_t idx) const
Return an iterator to the i'th element of the array.
size_t size() const
Get number of elements in the array.
template<typename T>
T& tpie::array_view_base< T >::back ( ) const
inlineinherited

Return the last element in the array.

Definition at line 167 of file array_view_base.h.

167 {return *(m_end-1);}
template<typename T>
iterator tpie::array_view_base< T >::begin ( ) const
inlineinherited

Return an iterator to the beginning of the array.

Returns
An iterator to the beginning of the array.

Definition at line 150 of file array_view_base.h.

150 {return iterator(m_start);}
template<typename T>
bool tpie::array_view_base< T >::empty ( ) const
inlineinherited

Check if the array is empty.

Returns
True if and only if size is 0.

Definition at line 101 of file array_view_base.h.

101 {return m_end == m_start;}
template<typename T>
iterator tpie::array_view_base< T >::end ( ) const
inlineinherited

Return an iterator to the end of the array.

Returns
An iterator to the end of the array.

Definition at line 157 of file array_view_base.h.

157 {return iterator(m_end);}
template<typename T>
iterator tpie::array_view_base< T >::find ( size_t  idx) const
throw (
)
inlineinherited

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 81 of file array_view_base.h.

Referenced by tpie::array_view_base< const T >::at().

81  {
82  assert(idx <= size());
83  return iterator(m_start + idx);
84  }
size_t size() const
Get number of elements in the array.
template<typename T>
T& tpie::array_view_base< T >::front ( ) const
inlineinherited

Return the first element in the array.

Definition at line 162 of file array_view_base.h.

162 {return *m_start;}
template<typename T>
bool tpie::array_view_base< T >::operator!= ( const array_view_base< T > &  other) const
inlineinherited

Check if the two arrays differ.

Parameters
otherThe array to compare against.
Returns
false If they are equal otherwise true.

Definition at line 139 of file array_view_base.h.

139  {
140  if (size() != other.size()) return true;
141  for (size_t i=0; i< size(); ++i) if (at(i) != other.at(i)) return true;
142  return false;
143  }
T & at(size_t i) const
Return the element located at the given index.
size_t size() const
Get number of elements in the array.
template<typename T>
bool tpie::array_view_base< T >::operator== ( const array_view_base< T > &  other) const
inlineinherited

Check 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 127 of file array_view_base.h.

127  {
128  if (size() != other.size()) return false;
129  for (size_t i=0; i < size(); ++i) if (at(i) != other.at(i)) return false;
130  return true;
131  }
T & at(size_t i) const
Return the element located at the given index.
size_t size() const
Get number of elements in the array.
template<typename T>
T& tpie::array_view_base< T >::operator[] ( size_t  i) const
inlineinherited

Return a reference to an array entry.

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

Definition at line 115 of file array_view_base.h.

115  {
116  assert(i < size());
117  return at(i);
118  }
T & at(size_t i) const
Return the element located at the given index.
size_t size() const
Get number of elements in the array.

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