00001 #ifndef STATIC_CONTAINER_VECTOR_H
00002
00003 #define STATIC_CONTAINER_VECTOR_H
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057 #include <algorithm>
00058 #include <boost/assert.hpp>
00059 #include <gslib/rotate.h>
00060 #include <gslib/static_container/STATIC_CONTAINER_MEMBERTYPEDEF.h>
00061 #include <gslib/static_container/destruct.h>
00062 #include <gslib/static_container/compare_methods.h>
00063 #include <gslib/numeric.h>
00064 #include <algorithm>
00065 #include <boost/call_traits.hpp>
00066
00067 namespace gslib {
00068 namespace static_container {
00069
00071 #define STATIC_VECTOR_FOREACH( op ) \
00072 for ( iterator it = begin(); it != end(); ++it ) {\
00073 op;\
00074 }
00075
00077
00084 template< typename Value, size_t MaxSize >
00085 class vector : public compare_methods< vector< Value, MaxSize > > {
00086 public:
00087 STATIC_CONTAINER_MEMBERTYPEDEF( Value )
00088 typedef typename boost::call_traits< Value >::param_type param_type;
00089 typedef pointer iterator;
00090 typedef const_pointer const_iterator;
00091 private:
00092 size_type size_;
00093 uint8_t buffer_[ MaxSize * sizeof( Value ) ];
00094
00095 static void destruct( reference v ) {
00096 static_container::destruct< Value >( v );
00097 }
00098
00100 pointer top() {
00101 return reinterpret_cast< pointer >( buffer_ );
00102 }
00103
00105 const_pointer top() const {
00106 return reinterpret_cast< const_pointer >( buffer_ );
00107 }
00108 public:
00110 BOOST_STATIC_CONSTANT( size_type, const_max = MaxSize );
00111
00113 static size_type max_size() { return const_max; }
00114
00116 static size_type capaciry() { return const_max; }
00117
00119 size_type size() const { return size_; }
00120
00121 iterator begin() { return reinterpret_cast< iterator >( top() ); }
00122 const_iterator begin() const { return reinterpret_cast< const_iterator >( top() ); }
00123 iterator end() { return begin() + size(); }
00124 const_iterator end() const { return begin() + size(); }
00125
00127 void push_back( param_type v ) {
00128 BOOST_ASSERT( size() < max_size() );
00129 new( &top()[ size() ] ) Value( v );
00130 ++size_;
00131 }
00132
00134 void pop_back() {
00135 BOOST_ASSERT( false == empty() );
00136 --size_;
00137 destruct( top()[ size() ] );
00138 }
00139
00141 vector() : size_( 0 ) {
00142 }
00143
00145
00148 vector( size_type s ) : size_( s ) {
00149 BOOST_ASSERT( s <= max_size() );
00150 STATIC_VECTOR_FOREACH( new( it ) Value() )
00151 }
00153
00156 vector( size_type s, param_type v ) : size_( s ) {
00157 BOOST_ASSERT( s <= max_size() );
00158 STATIC_VECTOR_FOREACH( new( it ) Value( v ) )
00159 }
00161 vector( const vector& other ) : size_( other.size_ ) {
00162 const_iterator otherIt = other.begin();
00163 STATIC_VECTOR_FOREACH( new( it ) Value( *( otherIt++ ) ) )
00164 }
00165
00166 ~vector() {
00167 clear();
00168 }
00170 vector& operator = ( const vector& other ) {
00171 if ( this != &other ) {
00172 assign( other.begin(), other.end() );
00173 }
00174 return *this;
00175 }
00176
00178 template < typename InputIt >
00179 void assign( InputIt first, InputIt last ) {
00180 clear();
00181 iterator dest = begin();
00182 for ( InputIt src = first; src != last; ++src, ++dest ) {
00183 new( dest ) Value( *src );
00184 ++size_;
00185 }
00186 }
00187
00189 void erase( iterator it ) {
00190 rotate( it, it + 1, end() );
00191 pop_back();
00192 }
00193
00195
00199 void erase( iterator first, iterator last ) {
00200 difference_type sizeDiff = std::distance( first, last );
00201 rotate( first, last, end() );
00202 for ( size_type i = 0; i < sizeDiff; ++i ) {
00203 pop_back();
00204 }
00205 }
00206
00208 void clear() {
00209 for ( iterator it = begin(); it != end(); ++it ) {
00210 destruct( *it );
00211 }
00212 size_ = 0;
00213 }
00214
00215 Value& operator [] ( size_type i ) {
00216 PYD_ASSERT( i < size() );
00217 return *( begin() + i );
00218 }
00219 const Value& operator [] ( size_type i ) const {
00220 PYD_ASSERT( i < size() );
00221 return *( begin() + i );
00222 }
00223 bool empty() const {
00224 return 0 == size();
00225 }
00226 reference at( size_type i ) { return operator [] ( i ); }
00227 param_type at( size_type i ) const { return operator [] ( i ); }
00228
00229
00230 reference front() { return *begin(); }
00231 param_type front() const { return *begin(); }
00232 reference back() { return *( end() - 1 ); }
00233 param_type back() const { return *( end() - 1 ); }
00234
00236 void swap( vector& other ) {
00237 std::swap_ranges( begin(), end(), other.begin() );
00238 }
00239
00240
00241 void assign ( param_type value ) {
00242 std::fill_n( begin(), size(), value );
00243 }
00244 };
00245 #undef STATIC_VECTOR_FOREACH
00246 }
00247 }
00248
00249 #endif