00001 #ifndef STATIC_CONTAINER_VECTOR_H
00002
00003 #define STATIC_CONTAINER_VECTOR_H
00004
00005 #include <algorithm>
00006 #include <boost/assert.hpp>
00007 #include "static_container/rotate.h"
00008 #include "static_container/STATIC_CONTAINER_MEMBERTYPEDEF.h"
00009 #include "static_container/destruct.h"
00010 #include "static_container/compare_methods.h"
00011 #include "static_container/integer.h"
00012 #include <algorithm>
00013 #include <boost/call_traits.hpp>
00014
00015 namespace static_container {
00016
00018 #define STATIC_VECTOR_FOREACH( op ) \
00019 for ( iterator it = begin(); it != end(); ++it ) {\
00020 op;\
00021 }
00022
00024
00031 template< typename Value, size_type MaxSize >
00032 class vector : public compare_methods< vector< Value, MaxSize > > {
00033 public:
00034 STATIC_CONTAINER_MEMBERTYPEDEF( Value )
00035 typedef typename boost::call_traits< Value >::param_type param_type;
00036 typedef pointer iterator;
00037 typedef const_pointer const_iterator;
00038 private:
00039 size_type size_;
00040 uint8_t buffer_[ MaxSize * sizeof( Value ) ];
00041
00042 static void destruct( reference v ) {
00043 static_container::destruct< Value >( v );
00044 }
00045
00047 pointer top() {
00048 return reinterpret_cast< pointer >( buffer_ );
00049 }
00050
00052 const_pointer top() const {
00053 return reinterpret_cast< const_pointer >( buffer_ );
00054 }
00055 public:
00057 BOOST_STATIC_CONSTANT( size_type, const_max = MaxSize );
00058
00060 static size_type max_size() { return const_max; }
00061
00063 static size_type capaciry() { return const_max; }
00064
00066 size_type size() const { return size_; }
00067
00068 iterator begin() { return reinterpret_cast< iterator >( top() ); }
00069 const_iterator begin() const { return reinterpret_cast< const_iterator >( top() ); }
00070 iterator end() { return begin() + size(); }
00071 const_iterator end() const { return begin() + size(); }
00072
00074 void push_back( param_type v ) {
00075 BOOST_ASSERT( size() < max_size() );
00076 new( &top()[ size() ] ) Value( v );
00077 ++size_;
00078 }
00079
00081 void pop_back() {
00082 BOOST_ASSERT( false == empty() );
00083 --size_;
00084 destruct( top()[ size() ] );
00085 }
00086
00088 vector() : size_( 0 ) {
00089 }
00090
00092
00095 vector( size_type s ) : size_( s ) {
00096 BOOST_ASSERT( s <= max_size() );
00097 STATIC_VECTOR_FOREACH( new( it ) Value() )
00098 }
00100
00103 vector( size_type s, param_type v ) : size_( s ) {
00104 BOOST_ASSERT( s <= max_size() );
00105 STATIC_VECTOR_FOREACH( new( it ) Value( v ) )
00106 }
00108 vector( const vector& other ) : size_( other.size_ ) {
00109 const_iterator otherIt = other.begin();
00110 STATIC_VECTOR_FOREACH( new( it ) Value( *( otherIt++ ) ) )
00111 }
00112
00113 ~vector() {
00114 clear();
00115 }
00117 vector& operator = ( const vector& other ) {
00118 if ( this != &other ) {
00119 assign( other.begin(), other.end() );
00120 }
00121 return *this;
00122 }
00123
00125 template < typename InputIt >
00126 void assign( InputIt first, InputIt last ) {
00127 clear();
00128 iterator dest = begin();
00129 for ( InputIt src = first; src != last; ++src ) {
00130 new( dest ) Value( *src );
00131 ++size_;
00132 }
00133 }
00134
00136 void erase( iterator it ) {
00137 rotate( it, it + 1, end() );
00138 pop_back();
00139 }
00140
00142
00146 void erase( iterator first, iterator last ) {
00147 difference_type sizeDiff = std::distance( first, last );
00148 rotate( first, last, end() );
00149 for ( size_type i = 0; i < sizeDiff; ++i ) {
00150 pop_back();
00151 }
00152 }
00153
00155 void clear() {
00156 for ( iterator it = begin(); it != end(); ++it ) {
00157 destruct( *it );
00158 }
00159 size_ = 0;
00160 }
00161
00162 Value& operator [] ( size_type i ) {
00163 PYD_ASSERT( i < size() );
00164 return *( begin() + i );
00165 }
00166 const Value& operator [] ( size_type i ) const {
00167 PYD_ASSERT( i < size() );
00168 return *( begin() + i );
00169 }
00170 bool empty() const {
00171 return 0 == size();
00172 }
00173 reference at( size_type i ) { return operator [] ( i ); }
00174 param_type at( size_type i ) const { return operator [] ( i ); }
00175
00176
00177 reference front() { return *begin(); }
00178 param_type front() const { return *begin(); }
00179 reference back() { return *( end() - 1 ); }
00180 param_type back() const { return *( end() - 1 ); }
00181
00183 void swap( vector& other ) {
00184 std::swap_ranges( begin(), end(), other.begin() );
00185 }
00186
00187
00188 void assign ( param_type value ) {
00189 std::fill_n( begin(), size(), value );
00190 }
00191 };
00192 #undef STATIC_VECTOR_FOREACH
00193 }
00194
00195 #endif