| Classes | Job Modules | Data Objects | Services | Algorithms | Tools | Packages | Directories | Tracs |

In This Package:

StaticArray< T, N > Class Template Reference

Simple implementation of an array with static allocation. More...

#include <DetDesc/StaticArray.h>

Collaboration diagram for StaticArray< T, N >:

[legend]
List of all members.

Public Types

typedef T value_type
 types
typedef T * iterator
typedef const T * const_iterator
typedef std::reverse_iterator<
iterator
reverse_iterator
typedef std::reverse_iterator<
const_iterator
const_reverse_iterator
typedef T & reference
typedef const T & const_reference
typedef std::size_t size_type
typedef std::ptrdiff_t difference_type

Public Member Functions

 StaticArray ()
 constructor
void push_back (const_reference x)
 add a single element
void clear ()
 clear all entries.
void erase (iterator first, iterator last)
 erase a range
void erase (iterator pos)
 erase a single element
void insert (iterator pos, const_reference x)
 insert a single element
void swap (StaticArray< T, N > &rhs)
 swap the contents of this array with that of another one
iterator begin ()
 iterator support
const_iterator begin () const
iterator end ()
const_iterator end () const
reverse_iterator rbegin ()
 reverse iterator support
const_reverse_iterator rbegin () const
reverse_iterator rend ()
const_reverse_iterator rend () const
size_type size () const
 size and capacity
bool empty () const
size_type max_size () const
size_type capacity () const
void reserve (size_type n)
reference operator[] (size_type i)
 element access
const_reference operator[] (size_type i) const
reference at (size_type i)
const_reference at (size_type i) const
reference front ()
const_reference front () const
reference back ()
const_reference back () const

Private Member Functions

void testcapacity (size_type newsize) const
 throw an exception if the capacity is not large enough to hold 'newsize' elements

Private Attributes

m_data [N]
size_type m_size
 elements

Detailed Description

template<typename T, std::size_t N>
class StaticArray< T, N >

Simple implementation of an array with static allocation.

It has a bit the look and feel of a std::vector, but with fixed capacity. Note that it does not call destructors or placement new, so it probably only works for basic types. The interface is also not entirely complete (e.g. no assignment, insert, or resize), but that can easily be changed when needed.

Note that there is also a more official implementation of this idea, namely the "alps/fixed_capacity_vector.h". I hope that that will make it to boost one day.

Author:
Wouter Hulsbergen
Date:
20/09/2007

Definition at line 25 of file StaticArray.h.


Member Typedef Documentation

template<typename T, std::size_t N>
typedef T StaticArray< T, N >::value_type

types

Definition at line 30 of file StaticArray.h.

template<typename T, std::size_t N>
typedef T* StaticArray< T, N >::iterator

Definition at line 31 of file StaticArray.h.

template<typename T, std::size_t N>
typedef const T* StaticArray< T, N >::const_iterator

Definition at line 32 of file StaticArray.h.

template<typename T, std::size_t N>
typedef std::reverse_iterator<iterator> StaticArray< T, N >::reverse_iterator

Definition at line 33 of file StaticArray.h.

template<typename T, std::size_t N>
typedef std::reverse_iterator<const_iterator> StaticArray< T, N >::const_reverse_iterator

Definition at line 34 of file StaticArray.h.

template<typename T, std::size_t N>
typedef T& StaticArray< T, N >::reference

Definition at line 35 of file StaticArray.h.

template<typename T, std::size_t N>
typedef const T& StaticArray< T, N >::const_reference

Definition at line 36 of file StaticArray.h.

template<typename T, std::size_t N>
typedef std::size_t StaticArray< T, N >::size_type

Definition at line 37 of file StaticArray.h.

template<typename T, std::size_t N>
typedef std::ptrdiff_t StaticArray< T, N >::difference_type

Definition at line 38 of file StaticArray.h.


Constructor & Destructor Documentation

template<typename T, std::size_t N>
StaticArray< T, N >::StaticArray (  )  [inline]

constructor

Definition at line 42 of file StaticArray.h.

00042 : m_size(0) {}


Member Function Documentation

template<typename T, std::size_t N>
iterator StaticArray< T, N >::begin (  )  [inline]

iterator support

Definition at line 48 of file StaticArray.h.

00048 { return m_data ; }

template<typename T, std::size_t N>
const_iterator StaticArray< T, N >::begin (  )  const [inline]

Definition at line 49 of file StaticArray.h.

00049 { return m_data ; }

template<typename T, std::size_t N>
iterator StaticArray< T, N >::end (  )  [inline]

Definition at line 50 of file StaticArray.h.

00050 { return m_data + m_size ; }

template<typename T, std::size_t N>
const_iterator StaticArray< T, N >::end (  )  const [inline]

Definition at line 51 of file StaticArray.h.

00051 { return m_data + m_size ; }

template<typename T, std::size_t N>
reverse_iterator StaticArray< T, N >::rbegin (  )  [inline]

reverse iterator support

Definition at line 56 of file StaticArray.h.

00056 { return reverse_iterator(end()) ; }

template<typename T, std::size_t N>
const_reverse_iterator StaticArray< T, N >::rbegin (  )  const [inline]

Definition at line 57 of file StaticArray.h.

00057 { return const_reverse_iterator(end()) ; }

template<typename T, std::size_t N>
reverse_iterator StaticArray< T, N >::rend (  )  [inline]

Definition at line 58 of file StaticArray.h.

00058 { return reverse_iterator(begin()) ; }

template<typename T, std::size_t N>
const_reverse_iterator StaticArray< T, N >::rend (  )  const [inline]

Definition at line 59 of file StaticArray.h.

00059 { return const_reverse_iterator(begin()) ; }

template<typename T, std::size_t N>
size_type StaticArray< T, N >::size (  )  const [inline]

size and capacity

Definition at line 64 of file StaticArray.h.

00064 { return m_size ; }

template<typename T, std::size_t N>
bool StaticArray< T, N >::empty (  )  const [inline]

Definition at line 65 of file StaticArray.h.

00065 { return m_size==0 ; }

template<typename T, std::size_t N>
size_type StaticArray< T, N >::max_size (  )  const [inline]

Definition at line 66 of file StaticArray.h.

00066 { return N ; }

template<typename T, std::size_t N>
size_type StaticArray< T, N >::capacity (  )  const [inline]

Definition at line 67 of file StaticArray.h.

00067 { return N ; }

template<typename T, std::size_t N>
void StaticArray< T, N >::reserve ( size_type  n  )  [inline]

Definition at line 68 of file StaticArray.h.

00068 { testcapacity(n) ; }

template<typename T, std::size_t N>
reference StaticArray< T, N >::operator[] ( size_type  i  )  [inline]

element access

Definition at line 73 of file StaticArray.h.

00073 { return m_data[i] ; }

template<typename T, std::size_t N>
const_reference StaticArray< T, N >::operator[] ( size_type  i  )  const [inline]

Definition at line 74 of file StaticArray.h.

00074 { return m_data[i] ; }

template<typename T, std::size_t N>
reference StaticArray< T, N >::at ( size_type  i  )  [inline]

Definition at line 75 of file StaticArray.h.

00075 { return m_data[i] ; }

template<typename T, std::size_t N>
const_reference StaticArray< T, N >::at ( size_type  i  )  const [inline]

Definition at line 76 of file StaticArray.h.

00076 { return m_data[i] ; }

template<typename T, std::size_t N>
reference StaticArray< T, N >::front (  )  [inline]

Definition at line 77 of file StaticArray.h.

00077 { return m_data[0] ; }

template<typename T, std::size_t N>
const_reference StaticArray< T, N >::front (  )  const [inline]

Definition at line 78 of file StaticArray.h.

00078 { return m_data[0] ; }

template<typename T, std::size_t N>
reference StaticArray< T, N >::back (  )  [inline]

Definition at line 79 of file StaticArray.h.

00079 { return m_data[m_size-1] ; }

template<typename T, std::size_t N>
const_reference StaticArray< T, N >::back (  )  const [inline]

Definition at line 80 of file StaticArray.h.

00080 { return m_data[m_size-1] ; }

template<typename T, std::size_t N>
void StaticArray< T, N >::push_back ( const_reference  x  )  [inline]

add a single element

Definition at line 84 of file StaticArray.h.

00084 { testcapacity(m_size+1) ; at(m_size++) = x ; }

template<typename T, std::size_t N>
void StaticArray< T, N >::clear (  )  [inline]

clear all entries.

note: no desctrutors called.

Definition at line 87 of file StaticArray.h.

00087 { m_size = 0 ; }

template<typename T, std::size_t N>
void StaticArray< T, N >::erase ( iterator  first,
iterator  last 
) [inline]

erase a range

Definition at line 90 of file StaticArray.h.

00090                                             {
00091     if( last>first ) {
00092       if(last < end() ) {
00093         int n = end() - last ;
00094         for(int i=0; i<n; ++i) *(first + i) = *(last+i) ;
00095       }
00096       m_size -= last - first ;
00097     }
00098   }

template<typename T, std::size_t N>
void StaticArray< T, N >::erase ( iterator  pos  )  [inline]

erase a single element

Definition at line 101 of file StaticArray.h.

00101 { return erase(pos,pos+1) ; }

template<typename T, std::size_t N>
void StaticArray< T, N >::insert ( iterator  pos,
const_reference  x 
) [inline]

insert a single element

Definition at line 104 of file StaticArray.h.

00104                                                {
00105     testcapacity(m_size+1) ;
00106     for(iterator next = pos; next<end(); ++next) *(next+1) = *(next) ;
00107     ++m_size ;
00108     *pos = x ;
00109   }

template<typename T, std::size_t N>
void StaticArray< T, N >::swap ( StaticArray< T, N > &  rhs  )  [inline]

swap the contents of this array with that of another one

Definition at line 112 of file StaticArray.h.

00112                                      { 
00113     std::swap_ranges(m_data,m_data+std::max(m_size,rhs.m_size),rhs.m_data) ; 
00114     std::swap( m_size, rhs.m_size ) ;
00115   }

template<typename T, std::size_t N>
void StaticArray< T, N >::testcapacity ( size_type  newsize  )  const [inline, private]

throw an exception if the capacity is not large enough to hold 'newsize' elements

Definition at line 119 of file StaticArray.h.

00119                                                {
00120     if( newsize > N ) 
00121       throw GaudiException("DetDesc/StaticArray: trying to add data beyond maximum capacity of this container.\n Please increase the max_size template parameter.","DetDesc/StaticArray overflow", StatusCode::FAILURE) ;
00122   }


Member Data Documentation

template<typename T, std::size_t N>
T StaticArray< T, N >::m_data[N] [private]

Definition at line 125 of file StaticArray.h.

template<typename T, std::size_t N>
size_type StaticArray< T, N >::m_size [private]

elements

Definition at line 126 of file StaticArray.h.


The documentation for this class was generated from the following file:
| Classes | Job Modules | Data Objects | Services | Algorithms | Tools | Packages | Directories | Tracs |

Generated on Mon Apr 11 20:00:22 2011 for DetDesc by doxygen 1.4.7