メインページ | ネームスペース一覧 | クラス階層 | 構成 | ファイル一覧 | ネームスペースメンバ | 構成メンバ | ファイルメンバ | 関連ページ

vector.h

説明を見る。
00001 #ifndef STATIC_CONTAINER_VECTOR_H
00002 
00003 #define STATIC_CONTAINER_VECTOR_H
00004 
00005 /*
00006 zlib/libpng license
00007 -------------------
00008 
00009 Copyright (C) 2004 &o
00010 
00011 This software is provided 'as-is', without any express or implied warranty. In n
00012 o event will the authors be held liable for any damages arising from the use of 
00013 this software.
00014 
00015 Permission is granted to anyone to use this software for any purpose, including 
00016 commercial applications, and to alter it and redistribute it freely, subject to 
00017 the following restrictions:
00018 
00019 The origin of this software must not be misrepresented; you must not claim that 
00020 you wrote the original software. If you use this software in a product, an ackno
00021 wledgment in the product documentation would be appreciated but is not required.
00022 
00023 Altered source versions must be plainly marked as such, and must not be misrepre
00024 sented as being the original software.
00025 This notice may not be removed or altered from any source distribution.
00026 
00027 project site : https://sourceforge.jp/projects/gslib/
00028 my site : http://www.game-syokunin.com/
00029 --------------------------------------------------------------------------------
00030 
00031 法的には、上記の原文のほうが有効なので、より厳密には日本語訳よりも原文を参考にし
00032 てください。日本語訳は、http://opensource.jp/licenses/zlib-license.html から頂い
00033 てきました。
00034 
00035 zlib/libpngライセンス ( 日本語訳 )
00036 
00037 Copyright (C) 2004 &o
00038 
00039 本ソフトウェアは「現状のまま」で、明示であるか暗黙であるかを問わず、何らの保証も
00040 なく提供されます。本ソフトウェアの使用によって生じるいかなる損害についても、作者
00041 は一切の責任を負わないものとします。 以下の制限に従う限り、商用アプリケーション
00042 を含めて、本ソフトウェアを任意の目的に使用し、自由に改変して再頒布することをすべ
00043 ての人に許可します。
00044 
00045 本ソフトウェアの出自について虚偽の表示をしてはなりません。あなたがオリジナルのソ
00046 フトウェアを作成したと主張してはなりません。あなたが本ソフトウェアを製品内で使用
00047 する場合、製品の文書に謝辞をれていただければ幸いですが、必須ではありません。
00048 ソースを変更した場合は、そのことを明示しなければなりません。オリジナルのソフトウ
00049 ェアであるという虚偽の表示をしてはなりません。
00050 ソースの頒布物から、この表示を削除したり、表示の内容を変更したりしてはなりません
00051 
00052 
00053 project site : https://sourceforge.jp/projects/gslib/
00054 my site : http://www.game-syokunin.com/
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             // front() and back()
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             // assign one value to all elements
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

static_containerに対してSat Nov 27 15:03:13 2004に生成されました。 doxygen 1.3.6