00001 #ifndef STATIC_CONTAINER_HASH_H
00002
00003 #define STATIC_CONTAINER_HASH_H
00004
00005 #include <boost/call_traits.hpp>
00006 #include <iterator>
00007 #include "static_container/STATIC_CONTAINER_MEMBERTYPEDEF.h"
00008
00009 namespace static_container {
00011
00020 template < typename Key, typename Value, typename Cont, typename KeyEqual = std::equal_to< Key > >
00021 class hash {
00022 public:
00023 typedef std::pair< Key, Value > pair_type;
00024 STATIC_CONTAINER_MEMBERTYPEDEF( pair_type );
00025 typedef typename Cont::iterator iterator;
00026 typedef typename Cont::const_iterator const_iterator;
00027 typedef typename boost::call_traits< KeyEqual >::param_type key_equal;
00028
00029 private:
00030 Cont cont_;
00031 KeyEqual equal_;
00032
00033 struct key_comp {
00034 typedef typename boost::call_traits< Key >::param_type key_param;
00035 key_param key;
00036 key_equal keyEqual;
00037 key_comp( key_param k, key_equal equal ) : key( k ), keyEqual( equal ) {}
00038 bool operator () ( typename boost::call_traits< value_type >::param_type pair ) const {
00039 return keyEqual( key, pair.first );
00040 }
00041 };
00042
00043 iterator push_back( const_reference v ) {
00044 cont_.push_back( v );
00045 iterator it = end();
00046 --it;
00047 return it;
00048 }
00049 public:
00050 hash( key_equal equal = KeyEqual() ) : cont_(), equal_( equal ) {}
00051 hash( const Cont& cont, key_equal equal = KeyEqual() ) : cont_( cont ), equal_( equal ) {}
00052
00053 iterator begin() {
00054 return cont_.begin();
00055 }
00056
00057 iterator end() {
00058 return cont_.end();
00059 }
00060
00061 const_iterator begin() const {
00062 return cont_.begin();
00063 }
00064
00065 const_iterator end() const {
00066 return cont_.end();
00067 }
00068
00069 const_iterator find( typename boost::call_traits< Key >::param_type key ) const {
00070 return find_if( begin(), end(), key_comp( key, equal_ ) );
00071 }
00072
00073 iterator find( typename boost::call_traits< Key >::param_type key ) {
00074 return find_if( begin(), end(), key_comp( key, equal_ ) );
00075 }
00076
00077 bool empty() const {
00078 return begin() == end();
00079 }
00080
00081 size_type size() const {
00082 return std::distance( begin(), end() );
00083 }
00084
00085 const Value& search( typename boost::call_traits< Key >::param_type key ) const {
00086 return find( key )->second;
00087 }
00088
00089 Value& search( typename boost::call_traits< Key >::param_type key ) {
00090 return find( key )->second;
00091 }
00092
00094
00097 Value& operator [] ( typename boost::call_traits< Key >::param_type key ) {
00098 iterator it = find( key );
00099 if ( it != end() ) {
00100 return it->second;
00101 } else {
00102 return push_back( value_type( key, Value() ) )->second;
00103 }
00104 }
00105
00107
00110 std::pair< iterator, bool > insert( const_reference v ) {
00111 iterator it = find( v.first );
00112 if ( it != end() ) {
00113
00114 return std::make_pair(
00115 it,
00116 false );
00117 } else {
00118
00119 return std::make_pair(
00120 push_back( v ),
00121 false );
00122 }
00123 }
00124 void erase( iterator it ) {
00125 cont_.erase( it );
00126 }
00127 void clear() {
00128 cont_.clear();
00129 }
00130
00132 Cont& get_container() {
00133 return cont_;
00134 }
00135
00137 const Cont& get_container() const {
00138 return cont_;
00139 }
00140 };
00141 }
00142
00143 #endif