5 #ifndef CMR_HMAP_INCLUDED
6 #define CMR_HMAP_INCLUDED
30 char purify_unary_function;
57 char purify_binary_function;
64 : purify_equal_to(0) {}
66 : purify_equal_to(0) {}
86 char purify_not_equal_to;
89 : purify_not_equal_to(0) {}
91 : purify_not_equal_to(0) {}
108 : purify_stl_select1st(0) {}
110 : purify_stl_select1st(0) {}
123 char purify_stl_select1st;
126 template<
class T1,
class T2 >
class pair
147 template<
class T1,
class T2 >
154 template<
class T1,
class T2 >
159 || (!(y.first < x.first) && x.second < y.second);
163 template<
class T1,
class T2 >
187 template <
class Key,
class T,
class KeyOfValue,
class Hasher,
class KeyEqual>
212 bool insert_always =
false) :
219 insert_always_(insert_always)
222 buckets_ = allocate_node_ptr(length_);
233 bool insert_always =
false) :
240 insert_always_(insert_always)
243 buckets_ = allocate_node_ptr(length_);
256 deallocate_node_ptr(buckets_);
265 deallocate_node_ptr(buckets_);
273 swap(insert_always_, other.insert_always_);
274 swap(size_, other.size_);
275 swap(buckets_, other.buckets_);
276 swap(length_, other.length_);
277 swap(limit_, other.limit_);
278 swap(ratio_, other.ratio_);
279 swap(hasher_, other.hasher_);
280 swap(key_equal_, other.key_equal_);
281 swap(key_of_value_, other.key_of_value_);
297 return iterator(buckets_, length_, first());
316 return iterator(buckets_, length_, 0);
346 return 1 < s ? s : 1;
382 return (
size_type)(hasher_(key) & 0x7FFFFFFF);
384 bool hash_key_equal(
size_type hash,
const key_type& key, hash_node< T >* n)
const
386 return n->hash_ == hash && key_equal_(key, key_of_value_(n->data_));
389 hash_node< T >* allocate_node()
392 z =
new hash_node<T>();
397 void deallocate_node(hash_node< T >* node)
402 hash_node< T >** allocate_node_ptr(
size_t n);
404 void deallocate_node_ptr(hash_node< T >** node)
409 hash_node< T >* first()
const;
410 void copy(
const hash_table < Key, T, KeyOfValue, Hasher, KeyEqual>& other);
411 void expand(
size_t new_length = 0);
412 hash_node< T >* next(hash_node< T >* node)
const;
417 hash_node< T >** buckets_;
423 KeyOfValue key_of_value_;
429 class hash_table_iterator
480 return node_ == other.node_;
486 return node_ != other.node_;
568 template<
class Key,
class Value,
592 hash_table_(table_size, Hasher(), KeyEqual(), 0.75f)
600 float load_ratio = 0.75f) :
610 hash_table_(first, last, table_size, Hasher(), KeyEqual(), 0.75f)
620 float load_ratio = 0.75f) :
621 hash_table_(first, last, table_size, hf, ke, load_ratio)
726 return (
const Value&)(*(
const Value*)0);
797 template <
class Key,
class Value,
class Hasher,
class KeyEqual>
806 template <
class Key,
class Value,
class Hasher,
class KeyEqual>
815 template <
class Key,
class Value,
class Hasher,
class KeyEqual>
823 template <
class Key,
class Value,
class Hasher = hash< Key >,
824 class KeyEqual = equal_to< Key > >
842 hash_table_(table_size, Hasher(), KeyEqual(), 0.75f, true) { }
848 float load_ratio = 0.75f) :
849 hash_table_(table_size, hf, ke, load_ratio, true) {}
853 hash_table_(first, last, table_size, Hasher(), KeyEqual(), 0.75f, true)
863 float load_ratio = 0.75f) :
864 hash_table_(first, last, table_size, hf, ke, load_ratio, true)
1022 template <
class Key,
class Value,
class Hasher,
class KeyEqual>
1024 < Key, Value, Hasher, KeyEqual>& x,
1026 < Key, Value, Hasher, KeyEqual>& y)
1028 return x.size() == y.size() && equal(x.begin(), x.end(), y.begin()) ;
1031 template <
class Key,
class Value,
class Hasher,
class KeyEqual>
1042 for (
size_t i = (node->
hash_ % length_) + 1; i < length_; i++)
1045 return buckets_[ i ];
1054 for (
size_t i = (node->
hash_ % length_) + 1; i < length_; i++)
1057 return buckets_[ i ];
1063 template <
class Key,
class Value,
class Hasher,
class KeyEqual>
1072 template <
class Key,
class T,
class KeyOfValue,
class Hasher,
class KeyEqual>
1076 for (
size_t i = 0; i < length_; i++)
1079 return buckets_[ i ];
1085 template <
class Key,
class T,
class KeyOfValue,
class Hasher,
class KeyEqual>
1088 for (
size_t i = 0; i < length_; i++)
1106 template <
class Key,
class T,
class KeyOfValue,
class Hasher,
class KeyEqual>
1119 template <
class Key,
class T,
class KeyOfValue,
class Hasher,
class KeyEqual>
1122 size_t hash = hash_value(key);
1123 size_t probe = hash % length_;
1127 for (; node; previous = node, node = node->
next_)
1128 if (hash_key_equal(hash, key, node))
1136 while (end && hash_key_equal(hash, key, end))
1145 previous->
next_ = end;
1149 buckets_[ probe ] = end;
1159 template <
class Key,
class T,
class KeyOfValue,
class Hasher,
class KeyEqual>
1164 size_t probe = target->
hash_ % length_;
1169 buckets_[ probe ] = target->
next_;
1173 while (node->
next_ != target)
1186 template <
class Key,
class T,
class KeyOfValue,
class Hasher,
class KeyEqual>
1199 template <
class Key,
class T,
class KeyOfValue,
class Hasher,
class KeyEqual>
1204 new_length = length_ * 2 + 1;
1207 hash_node< T >** new_buckets = allocate_node_ptr(new_length);
1209 for (
size_t i = 0; i < length_; i++)
1211 hash_node< T >* node = buckets_[ i ];
1215 hash_node< T >* current = node;
1217 size_t probe = current->hash_ % new_length;
1218 current->next_ = new_buckets[ probe ];
1219 new_buckets[ probe ] = current;
1223 deallocate_node_ptr(buckets_);
1224 buckets_ = new_buckets;
1225 length_ = new_length;
1226 limit_ = (size_type)(length_ * ratio_);
1230 template <
class Key,
class T,
class KeyOfValue,
class Hasher,
class KeyEqual>
1231 hash_node< T >** hash_table < Key, T, KeyOfValue, Hasher, KeyEqual>::allocate_node_ptr(
size_t n)
1233 hash_node< T >** z =
new hash_node< T >* [n];
1234 hash_node< T >** y = z + n;
1244 template <
class Key,
class T,
class KeyOfValue,
class Hasher,
class KeyEqual>
1247 size_t hash = hash_value(key_of_value_(value));
1248 size_t probe = hash % length_;
1251 for (node = buckets_[ probe ]; node != 0; node = node->
next_)
1252 if (hash_key_equal(hash, key_of_value_(value), node))
1257 new_node->
hash_ = hash;
1259 node->
next_ = new_node;
1261 if (++size_ > limit_)
1275 node->
next_ = buckets_[ probe ];
1277 buckets_[ probe ] = node;
1278 if (++size_ > limit_)
1286 template <
class Key,
class T,
class KeyOfValue,
class Hasher,
class KeyEqual>
1289 size_t hash = hash_value(key);
1292 for (node = buckets_[ hash % length_ ]; node != 0; node = node->
next_)
1293 if (hash_key_equal(hash, key, node))
1295 return iterator(buckets_, length_, node);
T1 first
Definition: HMap.h:141
size_type erase(const key_type &key)
Definition: HMap.h:963
void insert(const pair< Key, Value > *first, const pair< Key, Value > *last)
Definition: HMap.h:704
hash_table(size_type table_size, const Hasher &hf, const KeyEqual &ke, float load_ratio, bool insert_always=false)
Definition: HMap.h:207
hash_table_const_iterator(const hash_table_const_iterator< T > &other)
Definition: HMap.h:509
pair(const pair< T1, T2 > &p)
Definition: HMap.h:136
const_iterator begin() const
Definition: HMap.h:300
hash_node(const T &t)
Definition: HMap.h:173
equal_to(const equal_to< T > &)
Definition: HMap.h:65
pair< hash_table_iterator< T >, bool > insert(const T &value)
Definition: HMap.h:1245
hash_multimap(size_type table_size=203)
Definition: HMap.h:841
void clear()
Definition: HMap.h:784
size_t size_type
Definition: HMap.h:832
hash_node< T > * next(hash_node< T > *node)
Definition: HMap.h:1052
hash_table< Key, T, KeyOfValue, Hasher, KeyEqual > & operator=(const hash_table< Key, T, KeyOfValue, Hasher, KeyEqual > &other)
Definition: HMap.h:259
pair< iterator, bool > insert(const pair< Key, Value > &p)
Definition: HMap.h:693
void swap(HMap< Key, Value, Hasher, KeyEqual > &other)
Definition: HMap.h:643
KeyEqual key_equal
Definition: HMap.h:578
hash_node< T > * next_
Definition: HMap.h:180
bool empty() const
Definition: HMap.h:922
hash_node< T > * next(hash_node< T > *node)
Definition: HMap.h:1040
Arg2 second_argument_type
Definition: HMap.h:44
size_t hash_
Definition: HMap.h:179
hash_table_const_iterator< T > const_iterator
Definition: HMap.h:203
Hasher hasher
Definition: HMap.h:830
hasher hash_funct() const
Definition: HMap.h:892
Key key_type
Definition: HMap.h:828
Result result_type
Definition: HMap.h:19
equal_to()
Definition: HMap.h:63
size_t size_type
Definition: HMap.h:579
hash_multimap(const hash_multimap< Key, Value, Hasher, KeyEqual > &other)
Definition: HMap.h:869
hash_table(const hash_table< Key, T, KeyOfValue, Hasher, KeyEqual > &other)
Definition: HMap.h:247
Arg argument_type
Definition: HMap.h:18
hash_table_const_iterator< pair< Key, Value > > const_iterator
Definition: HMap.h:838
ptrdiff_t difference_type
Definition: HMap.h:833
T * pointer
Definition: HMap.h:197
binary_function(const binary_function< Arg1, Arg2, Result > &)
Definition: HMap.h:49
hash_table_const_iterator< T > operator++(int)
Definition: HMap.h:539
pair< T1, T2 > make_pair(const T1 &x, const T2 &y)
Definition: HMap.h:164
~pair()
Definition: HMap.h:138
stl_select1st()
Definition: HMap.h:107
hash_multimap(iterator first, iterator last, size_type table_size, const Hasher &hf, const KeyEqual &ke, float load_ratio=0.75f)
Definition: HMap.h:857
const pair< Key, Value > & const_reference
Definition: HMap.h:582
KeyEqual key_equal
Definition: HMap.h:831
iterator begin()
Definition: HMap.h:295
hash_node< T > * node_
Definition: HMap.h:562
hash_table_iterator< pair< Key, Value > > iterator
Definition: HMap.h:584
pair< hash_table_iterator< T >, hash_table_iterator< T > > equal_range(const key_type &key)
const U & operator()(const T &value) const
Definition: HMap.h:117
pair< Key, Value > value_type
Definition: HMap.h:829
T1 first_type
Definition: HMap.h:129
pair< iterator, iterator > equal_range(const key_type &key)
Definition: HMap.h:764
iterator end()
Definition: HMap.h:912
unary_function(const unary_function< Arg, Result > &)
Definition: HMap.h:23
HMap(iterator first, iterator last, size_type table_size=203)
Definition: HMap.h:606
size_type count(const key_type &key) const
size_type size() const
Definition: HMap.h:683
void erase(iterator pos)
Definition: HMap.h:958
not_equal_to(const not_equal_to< T > &)
Definition: HMap.h:90
pair< iterator, bool > insert(const Key &key, const Value &value)
Definition: HMap.h:698
void clear()
Definition: HMap.h:1008
void swap(C &c1, C &c2)
Definition: Commore.h:29
pair< Key, Value > & reference
Definition: HMap.h:834
size_type bucket_count() const
Definition: HMap.h:774
pair< const_iterator, const_iterator > equal_range(const key_type &key) const
Definition: HMap.h:769
pair(const T1 &x, const T2 &y)
Definition: HMap.h:134
bool operator!=(const hash_table_iterator< T > &other) const
Definition: HMap.h:484
size_type size() const
Definition: HMap.h:338
const T & operator*() const
Definition: HMap.h:528
hash_table_const_iterator()
Definition: HMap.h:503
~hash_table()
Definition: HMap.h:253
hash_table_iterator(const hash_table_iterator< T > &other)
Definition: HMap.h:440
not_equal_to< T > & operator=(const not_equal_to< T > &)
Definition: HMap.h:92
size_type bucket_count() const
Definition: HMap.h:998
hash_table_const_iterator(const hash_table_iterator< T > &other)
Definition: HMap.h:512
HMap(iterator first, iterator last, size_type table_size, const Hasher &hf, const KeyEqual &ke, float load_ratio=0.75f)
Definition: HMap.h:614
T2 second
Definition: HMap.h:142
const pair< Key, Value > & const_reference
Definition: HMap.h:835
const_iterator cbegin() const
Definition: HMap.h:309
hasher hash_funct() const
Definition: HMap.h:285
bool operator!=(const hash_table_const_iterator< T > &other) const
Definition: HMap.h:553
size_t length_
Definition: HMap.h:495
size_type count(const key_type &key) const
Definition: HMap.h:983
void clear()
Definition: HMap.h:1086
#define CMREXD
Definition: Compiler.h:22
hash_table< Key, pair< Key, Value >, stl_select1st< pair< Key, Value >, Key >, Hasher, KeyEqual > hash_table_
Definition: HMap.h:1019
iterator end()
Definition: HMap.h:668
Hasher hasher
Definition: HMap.h:193
hash_table(iterator first, iterator last, size_type table_size, const Hasher &hf, const KeyEqual &ke, float load_ratio, bool insert_always=false)
Definition: HMap.h:226
hash_node()
Definition: HMap.h:172
size_type max_size() const
Definition: HMap.h:688
iterator find(const key_type &key)
Definition: HMap.h:1287
Key key_type
Definition: HMap.h:191
binary_function()
Definition: HMap.h:48
hash_table_iterator< pair< Key, Value > > iterator
Definition: HMap.h:837
HMap< Key, Value, Hasher, KeyEqual > & operator=(const HMap< Key, Value, Hasher, KeyEqual > &other)
Definition: HMap.h:633
const T & const_reference
Definition: HMap.h:196
HMap(size_type table_size=203)
Definition: HMap.h:591
T & reference
Definition: HMap.h:195
HMap(const HMap< Key, Value, Hasher, KeyEqual > &other)
Definition: HMap.h:628
bool operator==(const pair< T1, T2 > &x, const pair< T1, T2 > &y)
Definition: HMap.h:148
key_equal key_eq() const
Definition: HMap.h:897
const T * const_pointer
Definition: HMap.h:198
hash_table_iterator< T > & operator=(const hash_table_iterator< T > &other)
Definition: HMap.h:448
size_t size_type
Definition: HMap.h:199
hash_node< T > ** buckets_
Definition: HMap.h:561
pair< iterator, iterator > equal_range(const key_type &key)
Definition: HMap.h:988
Value & operator[](const key_type &key)
Definition: HMap.h:716
hash_table_const_iterator< T > & operator++()
Definition: HMap.h:533
ptrdiff_t difference_type
Definition: HMap.h:200
bool operator()(const T &x, const T &y) const
Definition: HMap.h:98
KeyEqual key_equal
Definition: HMap.h:194
hasher hash_funct() const
Definition: HMap.h:648
T & operator*() const
Definition: HMap.h:458
binary_function< Arg1, Arg2, Result > & operator=(const binary_function< Arg1, Arg2, Result > &)
Definition: HMap.h:51
hash_multimap(iterator first, iterator last, size_type table_size=203)
Definition: HMap.h:852
key_equal key_eq() const
Definition: HMap.h:290
bool empty() const
Definition: HMap.h:333
const_iterator end() const
Definition: HMap.h:673
void erase(iterator first, iterator last)
Definition: HMap.h:744
hash_node< T > * node_
Definition: HMap.h:494
size_type size() const
Definition: HMap.h:927
iterator insert(const pair< Key, Value > &x)
Definition: HMap.h:937
stl_select1st(const stl_select1st< T, U > &)
Definition: HMap.h:109
stl_select1st< T, U > & operator=(const stl_select1st< T, U > &)
Definition: HMap.h:111
hash_table_const_iterator< pair< Key, Value > > const_iterator
Definition: HMap.h:585
size_type max_size() const
Definition: HMap.h:932
not_equal_to()
Definition: HMap.h:88
void erase(iterator first, iterator last)
Definition: HMap.h:968
hash_table_iterator< T > iterator
Definition: HMap.h:202
iterator begin()
Definition: HMap.h:902
bool operator<(const pair< T1, T2 > &x, const pair< T1, T2 > &y)
Definition: HMap.h:155
key_equal key_eq() const
Definition: HMap.h:653
pair< Key, Value > value_type
Definition: HMap.h:576
pair()
Definition: HMap.h:133
pair< const_iterator, const_iterator > equal_range(const key_type &key) const
Definition: HMap.h:993
iterator begin()
Definition: HMap.h:658
hash_table_iterator(hash_node< T > **buckets, size_t length, hash_node< T > *node)
Definition: HMap.h:436
hash_table_const_iterator< T > & operator=(const hash_table_const_iterator< T > &other)
Definition: HMap.h:518
iterator end()
Definition: HMap.h:314
hash_multimap< Key, Value, Hasher, KeyEqual > & operator=(const hash_multimap< Key, Value, Hasher, KeyEqual > &other)
Definition: HMap.h:876
iterator find(const key_type &key)
Definition: HMap.h:973
const_iterator cend() const
Definition: HMap.h:328
size_t length_
Definition: HMap.h:563
size_t CMREXD operator()(const T &key) const
void resize(size_type buckets)
Definition: HMap.h:779
void erase(iterator pos)
Definition: HMap.h:734
const_iterator end() const
Definition: HMap.h:319
size_type max_size() const
Definition: HMap.h:343
bool operator==(const hash_table_iterator< T > &other) const
Definition: HMap.h:478
HMap(size_type table_size, const Hasher &hf, const KeyEqual &ke, float load_ratio=0.75f)
Definition: HMap.h:596
const Value & operator[](const key_type &key) const
Definition: HMap.h:721
equal_to< T > & operator=(const equal_to< T > &)
Definition: HMap.h:67
hash_node< T > ** buckets_
Definition: HMap.h:493
Key key_type
Definition: HMap.h:575
hash_table_iterator< T > operator++(int)
Definition: HMap.h:470
hash_table_const_iterator(hash_node< T > **buckets, size_t length, hash_node< T > *node)
Definition: HMap.h:505
~hash_multimap()
Definition: HMap.h:874
void swap(hash_multimap< Key, Value, Hasher, KeyEqual > &other)
Definition: HMap.h:886
Hasher hasher
Definition: HMap.h:577
Arg1 first_argument_type
Definition: HMap.h:43
size_type erase(const key_type &key)
Definition: HMap.h:739
void swap(hash_table< Key, T, KeyOfValue, Hasher, KeyEqual > &other)
Definition: HMap.h:271
bool operator()(const T &x, const T &y) const
Definition: HMap.h:73
hash_table_iterator< T > & operator++()
Definition: HMap.h:463
void resize(size_type buckets)
Definition: HMap.h:1003
ptrdiff_t difference_type
Definition: HMap.h:580
const_iterator begin() const
Definition: HMap.h:907
T value_type
Definition: HMap.h:192
T data_
Definition: HMap.h:178
void resize(size_type buckets)
Definition: HMap.h:373
~hash_table_iterator()
Definition: HMap.h:444
size_type bucket_count() const
Definition: HMap.h:368
void insert(const pair< Key, Value > *first, const pair< Key, Value > *last)
Definition: HMap.h:948
iterator find(const key_type &key)
Definition: HMap.h:749
void insert(const_iterator first, const_iterator last)
Definition: HMap.h:953
hash_table< Key, pair< Key, Value >, stl_select1st< pair< Key, Value >, Key >, Hasher, KeyEqual > hash_table_
Definition: HMap.h:794
pair< Key, Value > & reference
Definition: HMap.h:581
bool operator==(const hash_table_const_iterator< T > &other) const
Definition: HMap.h:547
size_type count(const key_type &key) const
Definition: HMap.h:759
const_iterator find(const key_type &key) const
Definition: HMap.h:978
Result result_type
Definition: HMap.h:45
bool empty() const
Definition: HMap.h:678
void erase(iterator pos)
Definition: HMap.h:1160
~hash_table_const_iterator()
Definition: HMap.h:515
void insert(const_iterator first, const_iterator last)
Definition: HMap.h:711
unary_function()
Definition: HMap.h:22
hash_table_iterator()
Definition: HMap.h:432
unary_function< Arg, Result > & operator=(const unary_function< Arg, Result > &)
Definition: HMap.h:24
T2 second_type
Definition: HMap.h:130
const_iterator begin() const
Definition: HMap.h:663
~HMap()
Definition: HMap.h:631
const_iterator find(const key_type &key) const
Definition: HMap.h:754
iterator insert(const Key &key, const Value &value)
Definition: HMap.h:942
hash_multimap(size_type table_size, const Hasher &hf, const KeyEqual &ke, float load_ratio=0.75f)
Definition: HMap.h:844
const_iterator end() const
Definition: HMap.h:917