00001 #ifndef STATIC_CONTAINER_HASH_H
00002
00003 #define STATIC_CONTAINER_HASH_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 <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