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

hash.h

説明を見る。
00001 #ifndef STATIC_CONTAINER_HASH_H
00002 
00003 #define STATIC_CONTAINER_HASH_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 <boost/call_traits.hpp>
00058 #include <iterator>
00059 #include <gslib/static_container/STATIC_CONTAINER_MEMBERTYPEDEF.h>
00060 
00061 namespace gslib {
00062     namespace static_container {
00064 
00073         template < typename Key, typename Value, typename Cont, typename KeyEqual = std::equal_to< Key > >
00074         class hash {
00075         public:
00076             typedef std::pair< Key, Value >         pair_type;
00077             STATIC_CONTAINER_MEMBERTYPEDEF( pair_type );
00078             typedef typename Cont::iterator         iterator;
00079             typedef typename Cont::const_iterator   const_iterator;
00080             typedef typename boost::call_traits< KeyEqual >::param_type key_equal;
00081         
00082         private:
00083             Cont        cont_;
00084             KeyEqual    equal_;
00085 
00086             struct key_comp {
00087                 typedef typename boost::call_traits< Key >::param_type  key_param;
00088                 key_param   key;
00089                 key_equal   keyEqual;
00090                 key_comp( key_param k, key_equal equal ) : key( k ), keyEqual( equal ) {}
00091                 bool operator () ( typename boost::call_traits< value_type >::param_type pair ) const {
00092                     return keyEqual( key, pair.first );
00093                 }
00094             };
00095 
00096             iterator push_back( const_reference v ) {
00097                 cont_.push_back( v );
00098                 iterator it = end();
00099                 --it;
00100                 return it;
00101             }
00102         public:
00103             hash( key_equal equal = KeyEqual() ) : cont_(), equal_( equal ) {}
00104             hash( const Cont& cont, key_equal equal = KeyEqual() ) : cont_( cont ), equal_( equal ) {}
00105 
00106             iterator begin() {
00107                 return cont_.begin();
00108             }
00109 
00110             iterator end() {
00111                 return cont_.end();
00112             }
00113 
00114             const_iterator begin() const {
00115                 return cont_.begin();
00116             }
00117 
00118             const_iterator end() const {
00119                 return cont_.end();
00120             }
00121 
00122             const_iterator find( typename boost::call_traits< Key >::param_type key ) const {
00123                 return find_if( begin(), end(), key_comp( key, equal_ ) );
00124             }
00125 
00126             iterator find( typename boost::call_traits< Key >::param_type key ) {
00127                 return find_if( begin(), end(), key_comp( key, equal_ ) );
00128             }
00129 
00130             bool empty() const {
00131                 return begin() == end();
00132             }
00133 
00134             size_type size() const {
00135                 return std::distance( begin(), end() );
00136             }
00137             
00138             const Value& search( typename boost::call_traits< Key >::param_type key ) const {
00139                 return find( key )->second;
00140             }
00141 
00142             Value& search( typename boost::call_traits< Key >::param_type key ) {
00143                 return find( key )->second;
00144             }
00145             
00147 
00150             Value& operator [] ( typename boost::call_traits< Key >::param_type key ) {
00151                 iterator it = find( key );
00152                 if ( it != end() ) {
00153                     return it->second;
00154                 } else {
00155                     return push_back( value_type( key, Value() ) )->second;
00156                 }
00157             }
00158 
00160 
00163             std::pair< iterator, bool > insert( const_reference v ) {
00164                 iterator it = find( v.first );
00165                 if ( it != end() ) {
00166                     //  すでに同じキーのペアが存在したので、それを返す
00167                     return std::make_pair(
00168                         it,
00169                         false );
00170                 } else {
00171                     //  存在しなかったので挿入
00172                     return std::make_pair(
00173                         push_back( v ),
00174                         false );
00175                 }
00176             }
00177             void erase( iterator it ) {
00178                 cont_.erase( it );
00179             }
00180             void clear() {
00181                 cont_.clear();
00182             }
00183             
00185             Cont& get_container() {
00186                 return cont_;
00187             }
00188             
00190             const Cont& get_container() const {
00191                 return cont_;
00192             }
00193         };
00194     }
00195 }
00196 
00197 #endif

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