Click
Loading...
Searching...
No Matches
Public Types | Public Member Functions | List of all members
Vector< T, ALIGNMENT > Class Template Reference

Vector template. More...

#include <vector.hh>

Public Types

enum  { RESERVE_GROW = (size_type) -1 }
 Constant passed to reserve() to grow the Vector. More...
 
typedef T value_type
 Value type.
 
typedef T & reference
 Reference to value type.
 
typedef const T & const_reference
 Const reference to value type.
 
typedef T * pointer
 Pointer to value type.
 
typedef const T * const_pointer
 Pointer to const value type.
 
typedef fast_argument< T >::type value_argument_type
 Type used for value arguments (either T or const T &).
 
typedef const T & const_access_type
 
typedef int size_type
 Type of sizes (size()).
 
typedef T * iterator
 Iterator type.
 
typedef const T * const_iterator
 Const iterator type.
 

Public Member Functions

 Vector ()
 Construct an empty vector.
 
 Vector (size_type n, value_argument_type v)
 Construct a vector containing n copies of v.
 
 Vector (const Vector< T, ALIGNMENT > &x)
 Construct a vector as a copy of x.
 
Vector< T, ALIGNMENT > & operator= (const Vector< T, ALIGNMENT > &x)
 Replace this vector's contents with a copy of x.
 
Vector< T, ALIGNMENT > & assign (size_type n, value_argument_type v=T())
 Replace this vector's contents with n copies of v.
 
iterator begin ()
 Return an iterator for the first element in the vector.
 
iterator end ()
 Return an iterator for the end of the vector.
 
const_iterator begin () const
 
const_iterator end () const
 
const_iterator cbegin () const
 Return a const_iterator for the beginning of the vector.
 
const_iterator cend () const
 Return a const_iterator for the end of the vector.
 
size_type size () const
 Return the number of elements.
 
size_type capacity () const
 Return the vector's capacity.
 
bool empty () const
 Test if the vector is empty (size() == 0).
 
void resize (size_type n, value_argument_type v=T())
 Resize the vector to contain n elements.
 
bool reserve (size_type n)
 Reserve space for at least n more elements.
 
T & operator[] (size_type i)
 Return a reference to the ith element.
 
const T & operator[] (size_type i) const
 
T & at (size_type i)
 Return a reference to the ith element.
 
const T & at (size_type i) const
 
T & front ()
 Return a reference to the first element.
 
const T & front () const
 
T & back ()
 Return a reference to the last element (number size()-1).
 
const T & back () const
 
T & unchecked_at (size_type i)
 Return a reference to the ith element.
 
const T & unchecked_at (size_type i) const
 
T & at_u (size_type i)
 
const T & at_u (size_type i) const
 
T * data ()
 Return a pointer to the vector's data.
 
const T * data () const
 
void push_back (value_argument_type v)
 Append element v.
 
void pop_back ()
 Remove the last element.
 
void push_front (value_argument_type v)
 Prepend element v.
 
void pop_front ()
 Remove the first element.
 
iterator insert (iterator it, value_argument_type v)
 Insert v before position it.
 
iterator erase (iterator it)
 Remove the element at position it.
 
iterator erase (iterator a, iterator b)
 Remove the elements in [a, b).
 
void fill (T value)
 
void clear ()
 Remove all elements.
 
Vector< T, ALIGNMENT > sub (size_type begin, size_type sz)
 Swap the contents of this vector and x.
 
void swap (Vector< T, ALIGNMENT > &x)
 Swap the contents of this vector and x.
 
void shuffle (int n)
 

Detailed Description

template<typename T, size_t ALIGNMENT = CLICK_DEFAULT_ALIGNMENT>
class Vector< T, ALIGNMENT >

Vector template.

never

Vector implements a vector, or growable array, suitable for use in the kernel or at user level. Its interface should be compatible with C++'s std::vector, although that type has more methods. Vector elements are accessed with operator[] like arrays, and can be resized and expanded through append operations (see push_back() and resize()). A common (and efficient) usage pattern grows a Vector through repeated push_back() calls.

Vector iterators are pointers into the underlying array. This can simplify interactions between Vector and code that expects conventional C arrays.

Vector's push_front() and pop_front() operations are quite expensive (O(size()) complexity). For fast push_front() and pop_front() operations, use Deque.

Example code:

printf("%d\n", v.size()); // prints "0"
v.push_back(1);
v.push_back(2);
printf("%d\n", v.size()); // prints "2"
printf("%d %d\n", v[0], v[1]); // prints "1 2"
int *ip = it; // Vector iterators are pointers
assert(it == v.end() - 2);
v.erase(v.begin());
printf("%d\n", v.size()); // prints "1"
printf("%d\n", v[0]); // prints "2"
Vector template.
Definition vector.hh:111
T * iterator
Iterator type.
Definition vector.hh:130
iterator erase(iterator it)
Remove the element at position it.
Definition vector.hh:451
iterator end()
Return an iterator for the end of the vector.
Definition vector.hh:266
iterator begin()
Return an iterator for the first element in the vector.
Definition vector.hh:253
size_type size() const
Return the number of elements.
Definition vector.hh:231
void push_back(value_argument_type v)
Append element v.
Definition vector.hh:399

Member Typedef Documentation

◆ value_type

template<typename T , size_t ALIGNMENT = CLICK_DEFAULT_ALIGNMENT>
typedef T Vector< T, ALIGNMENT >::value_type

Value type.

◆ reference

template<typename T , size_t ALIGNMENT = CLICK_DEFAULT_ALIGNMENT>
typedef T& Vector< T, ALIGNMENT >::reference

Reference to value type.

◆ const_reference

template<typename T , size_t ALIGNMENT = CLICK_DEFAULT_ALIGNMENT>
typedef const T& Vector< T, ALIGNMENT >::const_reference

Const reference to value type.

◆ pointer

template<typename T , size_t ALIGNMENT = CLICK_DEFAULT_ALIGNMENT>
typedef T* Vector< T, ALIGNMENT >::pointer

Pointer to value type.

◆ const_pointer

template<typename T , size_t ALIGNMENT = CLICK_DEFAULT_ALIGNMENT>
typedef const T* Vector< T, ALIGNMENT >::const_pointer

Pointer to const value type.

◆ value_argument_type

template<typename T , size_t ALIGNMENT = CLICK_DEFAULT_ALIGNMENT>
typedef fast_argument<T>::type Vector< T, ALIGNMENT >::value_argument_type

Type used for value arguments (either T or const T &).

◆ size_type

template<typename T , size_t ALIGNMENT = CLICK_DEFAULT_ALIGNMENT>
typedef int Vector< T, ALIGNMENT >::size_type

Type of sizes (size()).

◆ iterator

template<typename T , size_t ALIGNMENT = CLICK_DEFAULT_ALIGNMENT>
typedef T* Vector< T, ALIGNMENT >::iterator

Iterator type.

◆ const_iterator

template<typename T , size_t ALIGNMENT = CLICK_DEFAULT_ALIGNMENT>
typedef const T* Vector< T, ALIGNMENT >::const_iterator

Const iterator type.

Member Enumeration Documentation

◆ anonymous enum

template<typename T , size_t ALIGNMENT = CLICK_DEFAULT_ALIGNMENT>
anonymous enum

Constant passed to reserve() to grow the Vector.

Constructor & Destructor Documentation

◆ Vector() [1/3]

template<typename T , size_t ALIGNMENT>
Vector< T, ALIGNMENT >::Vector ( )
inlineexplicit

Construct an empty vector.

◆ Vector() [2/3]

template<typename T , size_t ALIGNMENT>
Vector< T, ALIGNMENT >::Vector ( size_type  n,
value_argument_type  v 
)
inlineexplicit

Construct a vector containing n copies of v.

◆ Vector() [3/3]

template<typename T , size_t ALIGNMENT>
Vector< T, ALIGNMENT >::Vector ( const Vector< T, ALIGNMENT > &  x)
inline

Construct a vector as a copy of x.

Member Function Documentation

◆ operator=()

template<typename T , size_t ALIGNMENT>
Vector< T, ALIGNMENT > & Vector< T, ALIGNMENT >::operator= ( const Vector< T, ALIGNMENT > &  x)
inline

Replace this vector's contents with a copy of x.

◆ assign()

template<typename T , size_t ALIGNMENT>
Vector< T, ALIGNMENT > & Vector< T, ALIGNMENT >::assign ( size_type  n,
value_argument_type  v = T() 
)
inline

Replace this vector's contents with n copies of v.

Postcondition
size() == n

◆ begin() [1/2]

template<typename T , size_t ALIGNMENT>
Vector< T, ALIGNMENT >::iterator Vector< T, ALIGNMENT >::begin ( )
inline

Return an iterator for the first element in the vector.

◆ end() [1/2]

template<typename T , size_t ALIGNMENT>
Vector< T, ALIGNMENT >::iterator Vector< T, ALIGNMENT >::end ( )
inline

Return an iterator for the end of the vector.

Invariant
end() == begin() + size()

◆ begin() [2/2]

template<typename T , size_t ALIGNMENT>
Vector< T, ALIGNMENT >::const_iterator Vector< T, ALIGNMENT >::begin ( ) const
inline

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

◆ end() [2/2]

template<typename T , size_t ALIGNMENT>
Vector< T, ALIGNMENT >::const_iterator Vector< T, ALIGNMENT >::end ( ) const
inline

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

◆ cbegin()

template<typename T , size_t ALIGNMENT>
Vector< T, ALIGNMENT >::const_iterator Vector< T, ALIGNMENT >::cbegin ( ) const
inline

Return a const_iterator for the beginning of the vector.

◆ cend()

template<typename T , size_t ALIGNMENT>
Vector< T, ALIGNMENT >::const_iterator Vector< T, ALIGNMENT >::cend ( ) const
inline

Return a const_iterator for the end of the vector.

Invariant
cend() == cbegin() + size()

◆ size()

template<typename T , size_t ALIGNMENT>
Vector< T, ALIGNMENT >::size_type Vector< T, ALIGNMENT >::size ( ) const
inline

Return the number of elements.

◆ capacity()

template<typename T , size_t ALIGNMENT>
Vector< T, ALIGNMENT >::size_type Vector< T, ALIGNMENT >::capacity ( ) const
inline

Return the vector's capacity.

The capacity is greater than or equal to the size(). Functions such as resize(n) will not allocate new memory for the vector if n <= capacity().

◆ empty()

template<typename T , size_t ALIGNMENT>
bool Vector< T, ALIGNMENT >::empty ( ) const
inline

Test if the vector is empty (size() == 0).

◆ resize()

template<typename T , size_t ALIGNMENT>
void Vector< T, ALIGNMENT >::resize ( size_type  n,
value_argument_type  v = T() 
)
inline

Resize the vector to contain n elements.

Parameters
nnew size
vvalue used to fill new elements

◆ reserve()

template<typename T , size_t ALIGNMENT>
bool Vector< T, ALIGNMENT >::reserve ( size_type  n)
inline

Reserve space for at least n more elements.

Returns
true iff reserve succeeded.

This function changes the vector's capacity(), not its size(). If reserve(n) succeeds, then any succeeding call to resize(m) with m < n will succeed without allocating vector memory.

◆ operator[]() [1/2]

template<typename T , size_t ALIGNMENT>
T & Vector< T, ALIGNMENT >::operator[] ( size_type  i)
inline

Return a reference to the ith element.

Precondition
0 <= i < size()

◆ operator[]() [2/2]

template<typename T , size_t ALIGNMENT>
const T & Vector< T, ALIGNMENT >::operator[] ( size_type  i) const
inline

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

◆ at() [1/2]

template<typename T , size_t ALIGNMENT>
T & Vector< T, ALIGNMENT >::at ( size_type  i)
inline

Return a reference to the ith element.

Precondition
0 <= i < size()
See also
operator[]()

◆ at() [2/2]

template<typename T , size_t ALIGNMENT>
const T & Vector< T, ALIGNMENT >::at ( size_type  i) const
inline

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

◆ front() [1/2]

template<typename T , size_t ALIGNMENT>
T & Vector< T, ALIGNMENT >::front ( )
inline

Return a reference to the first element.

Precondition
!empty()

◆ front() [2/2]

template<typename T , size_t ALIGNMENT>
const T & Vector< T, ALIGNMENT >::front ( ) const
inline

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

◆ back() [1/2]

template<typename T , size_t ALIGNMENT>
T & Vector< T, ALIGNMENT >::back ( )
inline

Return a reference to the last element (number size()-1).

Precondition
!empty()

◆ back() [2/2]

template<typename T , size_t ALIGNMENT>
const T & Vector< T, ALIGNMENT >::back ( ) const
inline

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

◆ unchecked_at() [1/2]

template<typename T , size_t ALIGNMENT>
T & Vector< T, ALIGNMENT >::unchecked_at ( size_type  i)
inline

Return a reference to the ith element.

Precondition
0 <= i < size()

Unlike operator[]() and at(), this function does not check bounds, even if assertions are enabled. Use with caution.

◆ unchecked_at() [2/2]

template<typename T , size_t ALIGNMENT>
const T & Vector< T, ALIGNMENT >::unchecked_at ( size_type  i) const
inline

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

◆ data() [1/2]

template<typename T , size_t ALIGNMENT>
T * Vector< T, ALIGNMENT >::data ( )
inline

Return a pointer to the vector's data.

never

May be null if empty().

◆ data() [2/2]

template<typename T , size_t ALIGNMENT>
const T * Vector< T, ALIGNMENT >::data ( ) const
inline

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

◆ push_back()

template<typename T , size_t ALIGNMENT>
void Vector< T, ALIGNMENT >::push_back ( value_argument_type  v)
inline

Append element v.

A copy of v is inserted at position size(). Takes amortized O(1) time.

◆ pop_back()

template<typename T , size_t ALIGNMENT>
void Vector< T, ALIGNMENT >::pop_back ( )
inline

Remove the last element.

Takes O(1) time.

◆ push_front()

template<typename T , size_t ALIGNMENT>
void Vector< T, ALIGNMENT >::push_front ( value_argument_type  v)
inline

Prepend element v.

A copy of v is added to position 0. Other elements are shifted one position forward. Takes O(size()) time.

◆ pop_front()

template<typename T , size_t ALIGNMENT>
void Vector< T, ALIGNMENT >::pop_front ( )
inline

Remove the first element.

Other elements are shifted one position backward. Takes O(size()) time.

◆ insert()

template<typename T , size_t ALIGNMENT>
Vector< T, ALIGNMENT >::iterator Vector< T, ALIGNMENT >::insert ( iterator  it,
value_argument_type  v 
)
inline

Insert v before position it.

Returns
An iterator pointing at the new element.

◆ erase() [1/2]

template<typename T , size_t ALIGNMENT>
Vector< T, ALIGNMENT >::iterator Vector< T, ALIGNMENT >::erase ( iterator  it)
inline

Remove the element at position it.

Returns
An iterator pointing at the element following it.

◆ erase() [2/2]

template<typename T , size_t ALIGNMENT>
Vector< T, ALIGNMENT >::iterator Vector< T, ALIGNMENT >::erase ( iterator  a,
iterator  b 
)
inline

Remove the elements in [a, b).

Returns
An iterator corresponding to b.

◆ clear()

template<typename T , size_t ALIGNMENT>
void Vector< T, ALIGNMENT >::clear ( )
inline

Remove all elements.

Postcondition
size() == 0

◆ sub()

template<typename T , size_t ALIGNMENT>
Vector< T, ALIGNMENT > Vector< T, ALIGNMENT >::sub ( size_type  begin,
size_type  sz 
)
inline

Swap the contents of this vector and x.

◆ swap()

template<typename T , size_t ALIGNMENT>
void Vector< T, ALIGNMENT >::swap ( Vector< T, ALIGNMENT > &  x)
inline

Swap the contents of this vector and x.


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