PLearn 0.1
Public Types | Public Member Functions | Static Public Member Functions | Private Attributes | Static Private Attributes
PLearn::SmallVector< T, SizeBits, Allocator > Class Template Reference

#include <SmallVector.h>

Collaboration diagram for PLearn::SmallVector< T, SizeBits, Allocator >:
Collaboration graph
[legend]

List of all members.

Public Types

typedef SmallVector< T, SizeBits > self_type
 Typedefs.
typedef Allocator alloc_type
typedef T value_type
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
 SmallVector ()
 Constructors, etc.
 SmallVector (size_type n, const T &val=T())
 ~SmallVector ()
 SmallVector (const self_type &)
self_typeoperator= (const self_type &)
template<class In >
 SmallVector (In first, In last)
 Construct/Copy from Input iterator.
template<class In >
void assign (In first, In last)
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 (SmallVector &)
 Other functions.

Static Public Member Functions

static void allocator (alloc_type *the_alloc)
 Set here the allocator you want (we capture the object)
static alloc_typeallocator ()

Private Attributes

alloc_type::index_type i
 index of vector in memory

Static Private Attributes

static alloc_typealloc
 underlying allocator

Detailed Description

template<class T, unsigned SizeBits, class Allocator = ArrayAllocatorTrivial<T,SizeBits>>
class PLearn::SmallVector< T, SizeBits, Allocator >

A small vector is designed to have a predetermined maximum fixed size, specified as a template arguments (with SizeBits; e.g. SizeBits=8 for maximum size=256). It is built over an ArrayAllocator. These vectors trade representational efficiency for resizing performance (i.e. they always require a copy to resize).

Definition at line 62 of file SmallVector.h.


Member Typedef Documentation

template<class T, unsigned SizeBits, class Allocator = ArrayAllocatorTrivial<T,SizeBits>>
typedef Allocator PLearn::SmallVector< T, SizeBits, Allocator >::alloc_type

Definition at line 67 of file SmallVector.h.

template<class T, unsigned SizeBits, class Allocator = ArrayAllocatorTrivial<T,SizeBits>>
typedef const T* PLearn::SmallVector< T, SizeBits, Allocator >::const_iterator

Definition at line 74 of file SmallVector.h.

template<class T, unsigned SizeBits, class Allocator = ArrayAllocatorTrivial<T,SizeBits>>
typedef const T* PLearn::SmallVector< T, SizeBits, Allocator >::const_pointer

Definition at line 77 of file SmallVector.h.

template<class T, unsigned SizeBits, class Allocator = ArrayAllocatorTrivial<T,SizeBits>>
typedef const T& PLearn::SmallVector< T, SizeBits, Allocator >::const_reference

Definition at line 79 of file SmallVector.h.

template<class T, unsigned SizeBits, class Allocator = ArrayAllocatorTrivial<T,SizeBits>>
typedef ptrdiff_t PLearn::SmallVector< T, SizeBits, Allocator >::difference_type

Definition at line 71 of file SmallVector.h.

template<class T, unsigned SizeBits, class Allocator = ArrayAllocatorTrivial<T,SizeBits>>
typedef T* PLearn::SmallVector< T, SizeBits, Allocator >::iterator

Definition at line 73 of file SmallVector.h.

template<class T, unsigned SizeBits, class Allocator = ArrayAllocatorTrivial<T,SizeBits>>
typedef T* PLearn::SmallVector< T, SizeBits, Allocator >::pointer

Definition at line 76 of file SmallVector.h.

template<class T, unsigned SizeBits, class Allocator = ArrayAllocatorTrivial<T,SizeBits>>
typedef T& PLearn::SmallVector< T, SizeBits, Allocator >::reference

Definition at line 78 of file SmallVector.h.

template<class T, unsigned SizeBits, class Allocator = ArrayAllocatorTrivial<T,SizeBits>>
typedef SmallVector<T,SizeBits> PLearn::SmallVector< T, SizeBits, Allocator >::self_type

Typedefs.

Definition at line 66 of file SmallVector.h.

template<class T, unsigned SizeBits, class Allocator = ArrayAllocatorTrivial<T,SizeBits>>
typedef size_t PLearn::SmallVector< T, SizeBits, Allocator >::size_type

Definition at line 70 of file SmallVector.h.

template<class T, unsigned SizeBits, class Allocator = ArrayAllocatorTrivial<T,SizeBits>>
typedef T PLearn::SmallVector< T, SizeBits, Allocator >::value_type

Definition at line 69 of file SmallVector.h.


Constructor & Destructor Documentation

template<class T , unsigned SizeBits, class Allocator >
PLearn::SmallVector< T, SizeBits, Allocator >::SmallVector ( ) [inline]

Constructors, etc.

Definition at line 350 of file SmallVector.h.

    : i(0,0)
{ }
template<class T , unsigned SizeBits, class Allocator >
PLearn::SmallVector< T, SizeBits, Allocator >::SmallVector ( size_type  n,
const T &  val = T() 
) [inline]

Definition at line 356 of file SmallVector.h.

References PLearn::SmallVector< T, SizeBits, Allocator >::assign().

    : i(0,0)
{
    assign(n, val);
}

Here is the call graph for this function:

template<class T , unsigned SizeBits, class Allocator >
PLearn::SmallVector< T, SizeBits, Allocator >::~SmallVector ( )

Definition at line 372 of file SmallVector.h.

References i.

{
    allocator().deallocate(i);
}
template<class T , unsigned SizeBits, class Allocator >
PLearn::SmallVector< T, SizeBits, Allocator >::SmallVector ( const self_type other)

Definition at line 364 of file SmallVector.h.

References PLearn::SmallVector< T, SizeBits, Allocator >::assign(), PLearn::SmallVector< T, SizeBits, Allocator >::begin(), and PLearn::SmallVector< T, SizeBits, Allocator >::end().

    : i(0)
{
    assign(other.begin(), other.end());
}

Here is the call graph for this function:

template<class T, unsigned SizeBits, class Allocator = ArrayAllocatorTrivial<T,SizeBits>>
template<class In >
PLearn::SmallVector< T, SizeBits, Allocator >::SmallVector ( In  first,
In  last 
) [inline]

Construct/Copy from Input iterator.

Definition at line 112 of file SmallVector.h.

                                   : i(0,0) {
        assign(first,last);
    }

Member Function Documentation

template<class T, unsigned SizeBits, class Allocator = ArrayAllocatorTrivial<T,SizeBits>>
static void PLearn::SmallVector< T, SizeBits, Allocator >::allocator ( alloc_type the_alloc) [inline, static]

Set here the allocator you want (we capture the object)

Definition at line 153 of file SmallVector.h.

References PLASSERT.

                                                 {
        PLASSERT(the_alloc);
        alloc = the_alloc;
    }
template<class T, unsigned SizeBits, class Allocator = ArrayAllocatorTrivial<T,SizeBits>>
static alloc_type& PLearn::SmallVector< T, SizeBits, Allocator >::allocator ( ) [inline, static]

Definition at line 157 of file SmallVector.h.

                                   {
        return *alloc;
    }
template<class T, unsigned SizeBits, class Allocator = ArrayAllocatorTrivial<T,SizeBits>>
template<class In >
void PLearn::SmallVector< T, SizeBits, Allocator >::assign ( In  first,
In  last 
) [inline]

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

Definition at line 117 of file SmallVector.h.

Referenced by PLearn::SmallVector< T, SizeBits, Allocator >::SmallVector().

                                   {
        resize(last-first);
        iterator dest = begin();
        while (first != last)
            *dest++ = *first++;
    }

Here is the caller graph for this function:

template<class T , unsigned SizeBits, class Allocator >
void PLearn::SmallVector< T, SizeBits, Allocator >::assign ( size_type  n,
const T &  val 
) [inline]

n copies of val

Definition at line 338 of file SmallVector.h.

References i, n, and PLERROR.

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

    resize(n);
    for (size_type i=0; i<n; ++i)
        (*this)[i] = val;
}
template<class T , unsigned SizeBits, class Allocator >
SmallVector< T, SizeBits, Allocator >::const_reference PLearn::SmallVector< T, SizeBits, Allocator >::at ( size_type  n) const

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

Definition at line 280 of file SmallVector.h.

References n, and PLERROR.

{
    if (n >= size())
        PLERROR("%s: out-of-range.",typeid(*this).name());
        
    return *(begin()+n);
}
template<class T , unsigned SizeBits, class Allocator >
SmallVector< T, SizeBits, Allocator >::reference PLearn::SmallVector< T, SizeBits, Allocator >::at ( size_type  n)

Checked element access.

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

Definition at line 268 of file SmallVector.h.

References n, and PLERROR.

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

last element

Definition at line 314 of file SmallVector.h.

References PLERROR.

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

Definition at line 325 of file SmallVector.h.

References PLERROR.

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

This is always correct, even for zero-size vectors

Definition at line 216 of file SmallVector.h.

References i.

{
    return allocator().toPointer(i);
}
template<class T , unsigned SizeBits, class Allocator >
SmallVector< T, SizeBits, Allocator >::iterator PLearn::SmallVector< T, SizeBits, Allocator >::begin ( ) [inline]

Iterators.

This is always correct, even for zero-size vectors

Definition at line 207 of file SmallVector.h.

References i.

Referenced by PLearn::SmallVector< T, SizeBits, Allocator >::operator=(), PLearn::operator==(), and PLearn::SmallVector< T, SizeBits, Allocator >::SmallVector().

{
    return allocator().toPointer(i);
}

Here is the caller graph for this function:

template<class T, unsigned SizeBits, class Allocator = ArrayAllocatorTrivial<T,SizeBits>>
bool PLearn::SmallVector< T, SizeBits, Allocator >::empty ( ) const [inline]

Definition at line 138 of file SmallVector.h.

                       {
        return size() == 0;
    }
template<class T , unsigned SizeBits, class Allocator >
SmallVector< T, SizeBits, Allocator >::const_iterator PLearn::SmallVector< T, SizeBits, Allocator >::end ( ) const [inline]

Definition at line 233 of file SmallVector.h.

{
    return begin() + size();
}
template<class T , unsigned SizeBits, class Allocator >
SmallVector< T, SizeBits, Allocator >::iterator PLearn::SmallVector< T, SizeBits, Allocator >::end ( ) [inline]

Definition at line 225 of file SmallVector.h.

Referenced by PLearn::SmallVector< T, SizeBits, Allocator >::operator=(), PLearn::operator==(), and PLearn::SmallVector< T, SizeBits, Allocator >::SmallVector().

{
    return begin() + size();
}

Here is the caller graph for this function:

template<class T , unsigned SizeBits, class Allocator >
SmallVector< T, SizeBits, Allocator >::reference PLearn::SmallVector< T, SizeBits, Allocator >::front ( ) [inline]

first element

Definition at line 292 of file SmallVector.h.

References PLERROR.

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

Definition at line 303 of file SmallVector.h.

References PLERROR.

{
    if (empty())
        PLERROR("%s: out-of-range.",typeid(*this).name());
        
    return *begin();
}
template<class T, unsigned SizeBits, class Allocator = ArrayAllocatorTrivial<T,SizeBits>>
size_type PLearn::SmallVector< T, SizeBits, Allocator >::max_size ( ) [inline]

Definition at line 141 of file SmallVector.h.

                         {
        typename alloc_type::index_type dummy(unsigned(-1), unsigned(-1));
        return dummy.size;
    }
template<class T , unsigned SizeBits, class Allocator >
SmallVector< T, SizeBits, Allocator >::self_type & PLearn::SmallVector< T, SizeBits, Allocator >::operator= ( const self_type other)

Definition at line 380 of file SmallVector.h.

References PLearn::SmallVector< T, SizeBits, Allocator >::begin(), PLearn::SmallVector< T, SizeBits, Allocator >::end(), PLearn::SmallVector< T, SizeBits, Allocator >::size(), and PLearn::swap().

{
    if (size() != other.size()) {
        self_type tmp(other);
        swap(tmp);
    }
    else {
        assign(other.begin(), other.end());
    }
    return *this;
}

Here is the call graph for this function:

template<class T , unsigned SizeBits, class Allocator >
SmallVector< T, SizeBits, Allocator >::reference PLearn::SmallVector< T, SizeBits, Allocator >::operator[] ( size_type  n) [inline]

Unchecked element access.

Definition at line 244 of file SmallVector.h.

References n, and PLERROR.

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

Definition at line 256 of file SmallVector.h.

References n, and PLERROR.

{
#ifdef BOUNDCHECK
    if (n >= size())
        PLERROR("%s: out-of-range.",typeid(*this).name());
#endif
    return *(begin()+n);
}
template<class T , unsigned SizeBits, class Allocator >
void PLearn::SmallVector< T, SizeBits, Allocator >::pop_back ( )

remove last element

Definition at line 406 of file SmallVector.h.

References PLERROR.

{
    size_type s = size();
    if (s == 0)
        PLERROR("%s: out-of-range.",typeid(*this).name());
    resize(s-1);
}
template<class T , unsigned SizeBits, class Allocator >
void PLearn::SmallVector< T, SizeBits, Allocator >::push_back ( const T &  x)

Stack operations.

add to end

Definition at line 397 of file SmallVector.h.

References x.

{
    size_type s = size();
    resize(s+1);
    (*this)[s] = x;
}
template<class T , unsigned SizeBits, class Allocator >
void PLearn::SmallVector< T, SizeBits, Allocator >::reserve ( size_type  n)

make room for total of n elts

Definition at line 457 of file SmallVector.h.

References PLERROR.

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

added elts init by val

Copy old data on new data

Initialize remaining of new data

make new size effective

Definition at line 430 of file SmallVector.h.

References i, PLearn::max(), PLearn::min(), and PLERROR.

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

    pointer newdata = allocator().allocate(sz);
    typename alloc_type::index_type newi = allocator().toIndex(newdata, sz);

    pointer olddata = allocator().toPointer(i);
    size_type n = std::min(int(sz), int(i.size));
    while (n--)
        *newdata++ = *olddata++;

    n = std::max(0, int(sz) - int(i.size));
    while (n--)
        *newdata++ = val;

    allocator().deallocate(i);
    i = newi;
}

Here is the call graph for this function:

template<class T , unsigned SizeBits, class Allocator >
SmallVector< T, SizeBits, Allocator >::size_type PLearn::SmallVector< T, SizeBits, Allocator >::size ( ) const

(list operations are NOT provided at the moment)

Size and capacity operations number of elements

Definition at line 420 of file SmallVector.h.

References i.

Referenced by PLearn::SmallVector< T, SizeBits, Allocator >::operator=().

{
    if (i.isNull())
        return 0;
    else
        return i.size;
}

Here is the caller graph for this function:

template<class T , unsigned SizeBits, class Allocator >
void PLearn::SmallVector< T, SizeBits, Allocator >::swap ( SmallVector< T, SizeBits, Allocator > &  other)

Other functions.

Definition at line 469 of file SmallVector.h.

References PLearn::SmallVector< T, SizeBits, Allocator >::i, i, and PLearn::swap().

{
    std::swap(i,other.i);
}

Here is the call graph for this function:


Member Data Documentation

template<class T, unsigned SizeBits, class Allocator = ArrayAllocatorTrivial<T,SizeBits>>
Allocator * PLearn::SmallVector< T, SizeBits, Allocator >::alloc [static, private]

underlying allocator

Definition at line 162 of file SmallVector.h.

template<class T, unsigned SizeBits, class Allocator = ArrayAllocatorTrivial<T,SizeBits>>
alloc_type::index_type PLearn::SmallVector< T, SizeBits, Allocator >::i [private]

index of vector in memory

Definition at line 163 of file SmallVector.h.

Referenced by PLearn::SmallVector< T, SizeBits, Allocator >::swap().


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