PLearn 0.1
Public Types | Public Member Functions | Private Attributes
PLearn::TinyVector< T, N, TTrait > Class Template Reference

Compile-time fixed-size vector with interface close to std::vector. More...

#include <TinyVector.h>

Inheritance diagram for PLearn::TinyVector< T, N, TTrait >:
Inheritance graph
[legend]
Collaboration diagram for PLearn::TinyVector< T, N, TTrait >:
Collaboration graph
[legend]

List of all members.

Public Types

typedef T value_type
 Typedefs.
typedef size_t size_type
typedef ptrdiff_t difference_type
typedef T * iterator
typedef const T * const_iterator
typedef T * pointer
typedef const T * const_pointer
typedef T & reference
typedef const T & const_reference

Public Member Functions

iterator begin ()
 Iterators.
const_iterator begin () const
iterator end ()
const_iterator end () const
reference operator[] (size_type n)
 Unchecked element access.
const_reference operator[] (size_type n) const
reference at (size_type n)
 Checked element access.
const_reference at (size_type n) const
reference front ()
 first element
const_reference front () const
reference back ()
 last element
const_reference back () const
 TinyVector ()
 Constructors, etc.
 TinyVector (size_type n, const T &val=T())
template<class In >
void assign (In first, In last)
 Use default copy constructor, destructor, assignment operator.
void assign (size_type n, const T &val)
 n copies of val
void push_back (const T &x)
 Stack operations.
void pop_back ()
 remove last element
size_type size () const
 (list operations are NOT provided at the moment)
bool empty () const
size_type max_size ()
void resize (size_type sz, const T &val=T())
 added elts init by val
void reserve (size_type n)
 make room for total of n elts
void swap (TinyVector &)
 Swap this with the argument.
void fill (const T &)
 Fill with a constant (keeping the size constant)
TinyVector copy () const
 For compatibility with TVec, copy() simply returns a copy of ourselves.

Private Attributes

arr [N]

Detailed Description

template<class T, unsigned N, class TTrait = TinyVectorTrait<T>>
class PLearn::TinyVector< T, N, TTrait >

Compile-time fixed-size vector with interface close to std::vector.

A tiny vector tries to mimic (a small subset of) the interface of a built-in standard std::vector<>, but it has an additional restriction: it can hold a compile-time maximum number of elements. In exchange for this restriction, it allocates the elements of the vector within the class, which completely eliminates the need for dynamic allocation. In addition, the size of the vector is not stored explicitly, but rather determined from the number of "missing values". What exactly should be considered a missing value differs for each type T, which is why a TinyVectorTrait::Missing is defined for useful types. You can also define one yourself.

Definition at line 73 of file TinyVector.h.


Member Typedef Documentation

template<class T, unsigned N, class TTrait = TinyVectorTrait<T>>
typedef const T* PLearn::TinyVector< T, N, TTrait >::const_iterator

Definition at line 82 of file TinyVector.h.

template<class T, unsigned N, class TTrait = TinyVectorTrait<T>>
typedef const T* PLearn::TinyVector< T, N, TTrait >::const_pointer

Definition at line 85 of file TinyVector.h.

template<class T, unsigned N, class TTrait = TinyVectorTrait<T>>
typedef const T& PLearn::TinyVector< T, N, TTrait >::const_reference

Definition at line 87 of file TinyVector.h.

template<class T, unsigned N, class TTrait = TinyVectorTrait<T>>
typedef ptrdiff_t PLearn::TinyVector< T, N, TTrait >::difference_type

Definition at line 79 of file TinyVector.h.

template<class T, unsigned N, class TTrait = TinyVectorTrait<T>>
typedef T* PLearn::TinyVector< T, N, TTrait >::iterator

Definition at line 81 of file TinyVector.h.

template<class T, unsigned N, class TTrait = TinyVectorTrait<T>>
typedef T* PLearn::TinyVector< T, N, TTrait >::pointer

Definition at line 84 of file TinyVector.h.

template<class T, unsigned N, class TTrait = TinyVectorTrait<T>>
typedef T& PLearn::TinyVector< T, N, TTrait >::reference

Definition at line 86 of file TinyVector.h.

template<class T, unsigned N, class TTrait = TinyVectorTrait<T>>
typedef size_t PLearn::TinyVector< T, N, TTrait >::size_type

Definition at line 78 of file TinyVector.h.

template<class T, unsigned N, class TTrait = TinyVectorTrait<T>>
typedef T PLearn::TinyVector< T, N, TTrait >::value_type

Typedefs.

Definition at line 77 of file TinyVector.h.


Constructor & Destructor Documentation

template<class T , unsigned N, class TTrait >
PLearn::TinyVector< T, N, TTrait >::TinyVector ( ) [inline]

Constructors, etc.

Definition at line 413 of file TinyVector.h.

References N.

{
    assign(static_cast<size_type>(N), 
           static_cast<const T&>(TTrait::Missing));
}
template<class T, unsigned N, class TTrait >
PLearn::TinyVector< T, N, TTrait >::TinyVector ( size_type  n,
const T &  val = T() 
) [inline, explicit]

Definition at line 420 of file TinyVector.h.

References i, and N.

{
    assign(n, val);
    if (n<N)
        for (size_type i=n; i<N; ++i)
            arr[i] = TTrait::Missing;
}

Member Function Documentation

template<class T, unsigned N, class TTrait = TinyVectorTrait<T>>
template<class In >
void PLearn::TinyVector< T, N, TTrait >::assign ( In  first,
In  last 
) [inline]

Use default copy constructor, destructor, assignment operator.

could not define it out-of-line; bug in gcc?

Definition at line 122 of file TinyVector.h.

                                   {
        resize(last-first);
        for (size_type i=0; first != last && i < N; ++i, ++first)
            arr[i] = *first;
    }
template<class T, unsigned N, class TTrait >
void PLearn::TinyVector< T, N, TTrait >::assign ( size_type  n,
const T &  val 
) [inline]

n copies of val

Definition at line 402 of file TinyVector.h.

References i, N, n, and PLERROR.

{
    if (n > N)
        PLERROR("%s: out-of-range.",typeid(*this).name());

    resize(n);
    for (size_type i=0; i<n; ++i)
        arr[i] = val;
}
template<class T , unsigned N, class TTrait >
TinyVector< T, N, TTrait >::const_reference PLearn::TinyVector< T, N, TTrait >::at ( size_type  n) const

n cannot be less than zero, because size_type is usually unsigned

Definition at line 349 of file TinyVector.h.

References n, and PLERROR.

{
    if (n >= size())
        PLERROR("%s: out-of-range.",typeid(*this).name());
        
    return arr[n];
}
template<class T , unsigned N, class TTrait >
TinyVector< T, N, TTrait >::reference PLearn::TinyVector< T, N, TTrait >::at ( size_type  n)

Checked element access.

n cannot be less than zero, because size_type is usually unsigned

Definition at line 338 of file TinyVector.h.

References n, and PLERROR.

{
    if (n >= size())
        PLERROR("%s: out-of-range.",typeid(*this).name());
        
    return arr[n];
}
template<class T , unsigned N, class TTrait >
TinyVector< T, N, TTrait >::reference PLearn::TinyVector< T, N, TTrait >::back ( ) [inline]

last element

Definition at line 380 of file TinyVector.h.

References PLERROR.

{
    if (empty())
        PLERROR("%s: out-of-range.",typeid(*this).name());
        
    return *(end()-1);
}
template<class T , unsigned N, class TTrait >
TinyVector< T, N, TTrait >::const_reference PLearn::TinyVector< T, N, TTrait >::back ( ) const [inline]

Definition at line 390 of file TinyVector.h.

References PLERROR.

{
    if (empty())
        PLERROR("%s: out-of-range.",typeid(*this).name());
        
    return *(end()-1);
}
template<class T , unsigned N, class TTrait >
TinyVector< T, N, TTrait >::const_iterator PLearn::TinyVector< T, N, TTrait >::begin ( ) const [inline]

This is always correct, even for zero-size vectors

Definition at line 291 of file TinyVector.h.

{
    return &arr[0];
}
template<class T , unsigned N, class TTrait >
TinyVector< T, N, TTrait >::iterator PLearn::TinyVector< T, N, TTrait >::begin ( ) [inline]

Iterators.

This is always correct, even for zero-size vectors

Definition at line 283 of file TinyVector.h.

Referenced by PLearn::operator==(), and PLearn::operator>>().

{
    return &arr[0];
}

Here is the caller graph for this function:

template<class T, unsigned N, class TTrait = TinyVectorTrait<T>>
TinyVector PLearn::TinyVector< T, N, TTrait >::copy ( ) const [inline]

For compatibility with TVec, copy() simply returns a copy of ourselves.

Definition at line 161 of file TinyVector.h.

{ return *this; }
template<class T, unsigned N, class TTrait = TinyVectorTrait<T>>
bool PLearn::TinyVector< T, N, TTrait >::empty ( ) const [inline]

Definition at line 142 of file TinyVector.h.

Referenced by PLearn::TTensor< T >::DEPRECATEDsubTensor(), and PLearn::TTensor< T >::subTensor().

                       {
        return size() == 0;
    }

Here is the caller graph for this function:

template<class T , unsigned N, class TTrait >
TinyVector< T, N, TTrait >::const_iterator PLearn::TinyVector< T, N, TTrait >::end ( ) const [inline]

Definition at line 306 of file TinyVector.h.

{
    return &arr[0] + size();
}
template<class T , unsigned N, class TTrait >
TinyVector< T, N, TTrait >::iterator PLearn::TinyVector< T, N, TTrait >::end ( ) [inline]

Definition at line 299 of file TinyVector.h.

Referenced by PLearn::operator==().

{
    return &arr[0] + size();
}

Here is the caller graph for this function:

template<class T, unsigned N, class TTrait >
void PLearn::TinyVector< T, N, TTrait >::fill ( const T &  value)

Fill with a constant (keeping the size constant)

Definition at line 499 of file TinyVector.h.

References i, and n.

{
    for (int i=0, n=size() ; i<n ; ++i)
        arr[i] = value;
}
template<class T , unsigned N, class TTrait >
TinyVector< T, N, TTrait >::reference PLearn::TinyVector< T, N, TTrait >::front ( ) [inline]

first element

Definition at line 360 of file TinyVector.h.

References PLERROR.

{
    if (empty())
        PLERROR("%s: out-of-range.",typeid(*this).name());
        
    return arr[0];
}
template<class T , unsigned N, class TTrait >
TinyVector< T, N, TTrait >::const_reference PLearn::TinyVector< T, N, TTrait >::front ( ) const [inline]

Definition at line 370 of file TinyVector.h.

References PLERROR.

{
    if (empty())
        PLERROR("%s: out-of-range.",typeid(*this).name());
        
    return arr[0];
}
template<class T, unsigned N, class TTrait = TinyVectorTrait<T>>
size_type PLearn::TinyVector< T, N, TTrait >::max_size ( ) [inline]

Definition at line 145 of file TinyVector.h.

                         {
        return N;
    }
template<class T , unsigned N, class TTrait >
TinyVector< T, N, TTrait >::reference PLearn::TinyVector< T, N, TTrait >::operator[] ( size_type  n) [inline]

Unchecked element access.

Definition at line 316 of file TinyVector.h.

References n, and PLERROR.

{
#ifdef BOUNDCHECK
    if (n >= size())
        PLERROR("%s: out-of-range.",typeid(*this).name());
#endif
    return arr[n];
}
template<class T , unsigned N, class TTrait >
TinyVector< T, N, TTrait >::const_reference PLearn::TinyVector< T, N, TTrait >::operator[] ( size_type  n) const [inline]

Definition at line 327 of file TinyVector.h.

References n, and PLERROR.

{
#ifdef BOUNDCHECK
    if (n >= size())
        PLERROR("%s: out-of-range.",typeid(*this).name());
#endif
    return arr[n];
}
template<class T , unsigned N, class TTrait >
void PLearn::TinyVector< T, N, TTrait >::pop_back ( )

remove last element

Definition at line 442 of file TinyVector.h.

References PLERROR.

{
    size_type s = size();
    if (s == 0)
        PLERROR("%s: out-of-range.",typeid(*this).name());
        
    arr[s-1] = TTrait::Missing;
}
template<class T, unsigned N, class TTrait >
void PLearn::TinyVector< T, N, TTrait >::push_back ( const T &  x)

Stack operations.

add to end

Definition at line 432 of file TinyVector.h.

References N, PLERROR, and x.

Referenced by PLearn::TTensor< T >::DEPRECATEDsubTensor(), PLearn::TTensor< T >::operator[](), PLearn::TTensor< T >::selectDimensions(), and PLearn::TTensor< T >::subTensor().

{
    size_type s = size();
    if (s >= N)
        PLERROR("%s: out-of-range.",typeid(*this).name());
        
    arr[s] = x;
}

Here is the caller graph for this function:

template<class T , unsigned N, class TTrait >
void PLearn::TinyVector< T, N, TTrait >::reserve ( size_type  n)

make room for total of n elts

Definition at line 479 of file TinyVector.h.

References PLERROR.

{
    if (n > max_size())
        PLERROR("%s: out-of-range.",typeid(*this).name());
        
}
template<class T, unsigned N, class TTrait >
void PLearn::TinyVector< T, N, TTrait >::resize ( size_type  sz,
const T &  val = T() 
)

added elts init by val

Definition at line 466 of file TinyVector.h.

References N, and PLERROR.

Referenced by PLearn::TTensor< T >::lastElementPos(), PLearn::operator>>(), and PLearn::TTensor< T >::resize().

{
    if (sz > max_size())
        PLERROR("%s: out-of-range.",typeid(*this).name());
        
    size_type s = size();
    while (s < sz)
        arr[s++] = val;
    while (sz < N)
        arr[sz++] = TTrait::Missing;
}

Here is the caller graph for this function:

template<class T , unsigned N, class TTrait >
TinyVector< T, N, TTrait >::size_type PLearn::TinyVector< T, N, TTrait >::size ( ) const

(list operations are NOT provided at the moment)

Size and capacity operations number of elements

Definition at line 456 of file TinyVector.h.

References N.

Referenced by PLearn::TTensor< T >::resize(), and PLearn::TTensor< T >::selectDimensions().

{
    difference_type p = N-1;

    while (p >= 0 && arr[p] == static_cast<T>(TTrait::Missing))
        p--;
    return p+1;
}

Here is the caller graph for this function:

template<class T , unsigned N, class TTrait >
void PLearn::TinyVector< T, N, TTrait >::swap ( TinyVector< T, N, TTrait > &  other)

Swap this with the argument.

< if necessary for swap; otherwise uses Koenig lookup

Definition at line 490 of file TinyVector.h.

References PLearn::TinyVector< T, N, TTrait >::arr, i, N, and PLearn::swap().

{
    using namespace std;                     

    for (size_type i=0; i<N; ++i)
        swap(arr[i], other.arr[i]);
}

Here is the call graph for this function:


Member Data Documentation

template<class T, unsigned N, class TTrait = TinyVectorTrait<T>>
T PLearn::TinyVector< T, N, TTrait >::arr[N] [private]

Definition at line 164 of file TinyVector.h.

Referenced by PLearn::TinyVector< T, N, TTrait >::swap().


The documentation for this class was generated from the following file:
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines