Wiselib
wiselib.stable/util/pstl/vector_static.h
Go to the documentation of this file.
00001 /***************************************************************************
00002  ** This file is part of the generic algorithm library Wiselib.           **
00003  ** Copyright (C) 2008,2009 by the Wisebed (www.wisebed.eu) project.      **
00004  **                                                                       **
00005  ** The Wiselib is free software: you can redistribute it and/or modify   **
00006  ** it under the terms of the GNU Lesser General Public License as        **
00007  ** published by the Free Software Foundation, either version 3 of the    **
00008  ** License, or (at your option) any later version.                       **
00009  **                                                                       **
00010  ** The Wiselib is distributed in the hope that it will be useful,        **
00011  ** but WITHOUT ANY WARRANTY; without even the implied warranty of        **
00012  ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         **
00013  ** GNU Lesser General Public License for more details.                   **
00014  **                                                                       **
00015  ** You should have received a copy of the GNU Lesser General Public      **
00016  ** License along with the Wiselib.                                       **
00017  ** If not, see <http://www.gnu.org/licenses/>.                           **
00018  ***************************************************************************/
00019 #ifndef __WISELIB_INTERNAL_INTERFACE_STL_VECTOR_STATIC_H
00020 #define __WISELIB_INTERNAL_INTERFACE_STL_VECTOR_STATIC_H
00021 
00022 #include "util/pstl/iterator.h"
00023 #include <string.h>
00024 
00025 namespace wiselib
00026 {
00027 
00028    template<typename OsModel_P,
00029             typename Value_P,
00030             int VECTOR_SIZE>
00031    class vector_static
00032    {
00033    public:
00034       typedef Value_P value_type;
00035       typedef value_type* pointer;
00036       typedef value_type& reference;
00037 
00038       typedef vector_static<OsModel_P, value_type, VECTOR_SIZE> vector_type;
00039 
00040       typedef normal_iterator<OsModel_P, pointer, vector_type> iterator;
00041 
00042       typedef typename OsModel_P::size_t size_type;
00043       // --------------------------------------------------------------------
00044       vector_static()
00045       {
00046          start_ = &vec_[0];
00047          finish_ = start_;
00048          end_of_storage_ = start_ + VECTOR_SIZE;
00049       }
00050       // --------------------------------------------------------------------
00051       vector_static( const vector_static& vec )
00052       { *this = vec; }
00053       // --------------------------------------------------------------------
00054       ~vector_static() {}
00055       // --------------------------------------------------------------------
00056       vector_static& operator=( const vector_static& vec )
00057       {
00058          memcpy( vec_, vec.vec_, sizeof(vec_) );
00059          start_ = &vec_[0];
00060          finish_ = start_ + (vec.finish_ - vec.start_);
00061          end_of_storage_ = start_ + VECTOR_SIZE;
00062          return *this;
00063       }
00064       // --------------------------------------------------------------------
00067       iterator begin()
00068       { return iterator( start_ ); }
00069       // --------------------------------------------------------------------
00070       iterator end()
00071       { return iterator( finish_ ); }
00073       // --------------------------------------------------------------------
00076       size_type size() const
00077       { return size_type(finish_ - start_); }
00078       // --------------------------------------------------------------------
00079       size_type max_size() const
00080       { return VECTOR_SIZE; }
00081       // --------------------------------------------------------------------
00082       size_type capacity() const
00083       { return VECTOR_SIZE; }
00084       // --------------------------------------------------------------------
00085       bool empty() const
00086       { return size() == 0; }
00088       // --------------------------------------------------------------------
00091       reference operator[](size_type n)
00092       {
00093          return *(this->start_ + n);
00094       }
00095       // --------------------------------------------------------------------
00096       reference at(size_type n)
00097       {
00098          return (*this)[n];
00099       }
00100       // --------------------------------------------------------------------
00101       reference front()
00102       {
00103          return *begin();
00104       }
00105       // --------------------------------------------------------------------
00106       reference back()
00107       {
00108          return *(end() - 1);
00109       }
00110       // --------------------------------------------------------------------
00111       pointer data()
00112       { return pointer(this->start_); }
00114       // --------------------------------------------------------------------
00117       template <class InputIterator>
00118       void assign ( InputIterator first, InputIterator last )
00119       {
00120          clear();
00121          for ( InputIterator it = first; it != last; ++it )
00122             push_back( *it );
00123       }
00124       // --------------------------------------------------------------------
00125       void assign( size_type n, const value_type& u )
00126       {
00127          clear();
00128          for ( unsigned int i = 0; i < n; ++i )
00129             push_back( u );
00130       }
00131       // --------------------------------------------------------------------
00132       void push_back( const value_type& x )
00133       {
00134          if ( finish_ != end_of_storage_ )
00135          {
00136             *finish_ = x;
00137             ++finish_;
00138          }
00139       }
00140       // --------------------------------------------------------------------
00141       void pop_back()
00142       {
00143          if ( finish_ != start_ )
00144             --finish_;
00145       }
00146       // --------------------------------------------------------------------
00147       iterator insert( iterator position, const value_type& x )
00148       {
00149          // no more elements can be inserted because vector is full
00150          if ( size() == max_size() )
00151             return iterator(finish_);
00152 
00153          value_type cur = x, temp;
00154          for ( iterator it = position; it != end(); ++it )
00155          {
00156             temp = *it;
00157             *it = cur;
00158             cur = temp;
00159          }
00160          push_back( cur );
00161 
00162          return position;
00163       }
00164       // --------------------------------------------------------------------
00165       void insert( iterator position, size_type n, const value_type& x )
00166       {
00167          for ( int i = 0; i < n; ++i )
00168             insert( position, x );
00169       }
00170       // --------------------------------------------------------------------
00171       iterator erase( iterator position )
00172       {
00173          if ( position == end() )
00174             return end();
00175 
00176          for ( iterator cur = position; cur != end(); cur++ )
00177             *cur = *(cur + 1);
00178 
00179          pop_back();
00180 
00181          return position;
00182       }
00183       // --------------------------------------------------------------------
00184       iterator erase( iterator first, iterator last )
00185       {
00186          if ( first == end() || first == last )
00187             return first;
00188 
00189          iterator ret = first;
00190 
00191          while ( last != end() )
00192          {
00193             *first = *last;
00194             first++;
00195             last++;
00196          }
00197 
00198          finish_ = &(*first);
00199          return ret;
00200       }
00201       // --------------------------------------------------------------------
00202       void swap( vector_type& vec )
00203       {
00204          vector_type tmp = *this;
00205          *this = vec;
00206          vec = tmp;
00207       }
00208       // --------------------------------------------------------------------
00209       void clear()
00210       {
00211          finish_ = start_;
00212       }
00214 
00215    protected:
00216       value_type vec_[VECTOR_SIZE];
00217 
00218       pointer start_, finish_, end_of_storage_;
00219    };
00220 
00221 }
00222 
00223 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines