commore  1.0.6-SNAPSHOT
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
HMap.h
Go to the documentation of this file.
1 //
2 // Copyright (c) 2006-2007 Raphael David / CANTOR
3 //
4 
5 #ifndef CMR_HMAP_INCLUDED
6 #define CMR_HMAP_INCLUDED
7 
8 #include <string.h>
9 #include <stddef.h>
10 #include "commore/Commore.h"
11 
12 namespace commore
13 {
14  template<class Arg, class Result> class unary_function
15  {
16  public:
17  //
18  typedef Arg argument_type;
19  typedef Result result_type;
20 
21  public:
22  unary_function() : purify_unary_function(0) {}
23  unary_function(const unary_function< Arg, Result >&) : purify_unary_function(0) {}
25  {
26  return *this;
27  }
28 
29  private:
30  char purify_unary_function;
31 
32  };
33 
34  template< class T > class hash : public unary_function< T, size_t >
35  {
36  public:
37  size_t CMREXD operator()(const T& key) const; // { return (size_t) key; }
38  };
39 
40  template<class Arg1, class Arg2, class Result> class binary_function
41  {
42  public:
43  typedef Arg1 first_argument_type;
44  typedef Arg2 second_argument_type;
45  typedef Result result_type;
46 
47  public:
48  binary_function() : purify_binary_function(0) {}
49  binary_function(const binary_function< Arg1, Arg2, Result >&) : purify_binary_function(0) {}
52  {
53  return *this;
54  }
55 
56  private:
57  char purify_binary_function;
58  };
59 
60  template< class T > class equal_to : public binary_function< T, T, bool >
61  {
62  public:
64  : purify_equal_to(0) {}
66  : purify_equal_to(0) {}
68  {
69  return *this;
70  }
71 
72  public:
73  bool operator()(const T& x, const T& y) const
74  {
75  return x == y;
76  }
77 
78  private:
79  char purify_equal_to;
80  };
81 
82 
83  template<class T> class not_equal_to : public binary_function< T, T, bool >
84  {
85  private:
86  char purify_not_equal_to;
87  public:
89  : purify_not_equal_to(0) {}
91  : purify_not_equal_to(0) {}
93  {
94  return *this;
95  }
96 
97  public:
98  bool operator()(const T& x, const T& y) const
99  {
100  return x != y;
101  }
102  };
103 
104  template< class T, class U > class stl_select1st : public unary_function< T, const U& >
105  {
106  public:
108  : purify_stl_select1st(0) {}
110  : purify_stl_select1st(0) {}
112  {
113  return *this;
114  }
115 
116  public:
117  const U& operator()(const T& value) const
118  {
119  return value.first;
120  }
121 
122  private:
123  char purify_stl_select1st;
124  };
125 
126  template< class T1, class T2 > class pair
127  {
128  public:
129  typedef T1 first_type;
130  typedef T2 second_type;
131 
132  public:
133  pair() {}
134  pair(const T1& x, const T2& y)
135  : first(x), second(y) { }
137  : first(p.first), second(p.second) { }
138  ~pair() {}
139 
140  public:
141  T1 first;
142  T2 second;
143  };
144 
145 
146 
147  template< class T1, class T2 >
148  inline bool operator==(const pair< T1, T2 >& x, const pair< T1, T2 >& y)
149  {
150  return x.first == y.first && x.second == y.second;
151  }
152 
153 
154  template< class T1, class T2 >
155  inline bool operator<(const pair< T1, T2 >& x, const pair< T1, T2 >& y)
156  {
157  return
158  x.first < y.first
159  || (!(y.first < x.first) && x.second < y.second);
160  }
161 
162 
163  template< class T1, class T2 >
164  inline pair< T1, T2 > make_pair(const T1& x, const T2& y)
165  {
166  return pair< T1, T2 >(x, y);
167  }
168 
169  template< class T > struct hash_node
170  {
171  public:
173  hash_node(const T& t)
174  : data_(t) {}
175 
176  public:
177  //
178  T data_;
179  size_t hash_;
181  };
182 
183 
184  template< class T > class hash_table_iterator;
185  template< class T > class hash_table_const_iterator;
186 
187  template < class Key, class T, class KeyOfValue, class Hasher, class KeyEqual>
189  {
190  public:
191  typedef Key key_type;
192  typedef T value_type;
193  typedef Hasher hasher;
194  typedef KeyEqual key_equal;
195  typedef T& reference;
196  typedef const T& const_reference;
197  typedef T* pointer;
198  typedef const T* const_pointer;
199  typedef size_t size_type;
200  typedef ptrdiff_t difference_type;
201 
204 
205  public:
206 
208  size_type table_size,
209  const Hasher& hf,
210  const KeyEqual& ke,
211  float load_ratio ,
212  bool insert_always = false) :
213  size_(0),
214  length_(table_size),
215  hasher_(hf),
216  key_equal_(ke),
217  ratio_(load_ratio),
218  buckets_(0),
219  insert_always_(insert_always)
220  {
221  limit_ = (size_type)(length_ * ratio_);
222  buckets_ = allocate_node_ptr(length_);
223  }
224 
225 
227  iterator first,
228  iterator last,
229  size_type table_size ,
230  const Hasher& hf ,
231  const KeyEqual& ke ,
232  float load_ratio ,
233  bool insert_always = false) :
234  size_(0),
235  length_(table_size),
236  hasher_(hf),
237  key_equal_(ke),
238  ratio_(load_ratio),
239  buckets_(0),
240  insert_always_(insert_always)
241  {
242  limit_ = (size_type)(length_ * ratio_);
243  buckets_ = allocate_node_ptr(length_);
244  insert(first, last);
245  }
246 
249  {
250  copy(other);
251  }
252 
254  {
255  clear();
256  deallocate_node_ptr(buckets_);
257  }
258 
261  {
262  if (this != &other)
263  {
264  clear();
265  deallocate_node_ptr(buckets_);
266  copy(other);
267  }
268  return *this;
269  }
270 
272  {
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_);
282  }
283 
284  public:
286  {
287  return hasher_;
288  }
289 
291  {
292  return key_equal_;
293  }
294 
296  {
297  return iterator(buckets_, length_, first());
298  }
299 
301  {
302  return const_iterator(buckets_, length_, first());
303  }
304 
310  {
311  return begin();
312  }
313 
315  {
316  return iterator(buckets_, length_, 0);
317  }
318 
320  {
321  return const_iterator(buckets_, length_, 0);
322  }
323 
329  {
330  return end();
331  }
332 
333  bool empty() const
334  {
335  return size_ == 0;
336  }
337 
338  size_type size() const
339  {
340  return size_;
341  }
342 
344  {
345  const size_type s = (size_type)(0xffffffff / sizeof(T));
346  return 1 < s ? s : 1;
347  }
348 
349  pair< hash_table_iterator< T >, bool > insert(const T& value);
350 
351  void insert(const T* first, const T* last);
352 
354  void erase(iterator pos);
355 
356  size_type erase(const key_type& key);
357  void erase(iterator first, iterator last);
358 
359  iterator find(const key_type& key);
360  const_iterator find(const key_type& key) const;
361 
362  size_type count(const key_type& key) const;
363 
365 
367 
369  {
370  return limit_;
371  }
372 
373  void resize(size_type buckets)
374  {
375  expand(buckets);
376  }
377  void clear();
378 
379  private:
380  size_type hash_value(const key_type& key) const
381  {
382  return (size_type)(hasher_(key) & 0x7FFFFFFF);
383  }
384  bool hash_key_equal(size_type hash, const key_type& key, hash_node< T >* n) const
385  {
386  return n->hash_ == hash && key_equal_(key, key_of_value_(n->data_));
387  }
388 
389  hash_node< T >* allocate_node()
390  {
391  hash_node< T >* z;
392  z = new hash_node<T>();
393  z->next_ = 0;
394  return z;
395  }
396 
397  void deallocate_node(hash_node< T >* node)
398  {
399  delete node;
400  }
401 
402  hash_node< T >** allocate_node_ptr(size_t n);
403 
404  void deallocate_node_ptr(hash_node< T >** node)
405  {
406  delete [] node;
407  }
408 
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;
413 
414  private:
415  bool insert_always_;
416  size_type size_;
417  hash_node< T >** buckets_;
418  size_type length_;
419  size_type limit_;
420  float ratio_;
421  Hasher hasher_;
422  KeyEqual key_equal_;
423  KeyOfValue key_of_value_;
424  };
425 
426 
427 
428  template< class T >
429  class hash_table_iterator // : public forward_iterator< T, Distance >
430  {
431  public:
433  : buckets_(0), node_(0), length_(0) { }
434 
435 
436  hash_table_iterator(hash_node< T >** buckets, size_t length, hash_node< T >* node)
437  : buckets_(buckets), node_(node), length_(length) { }
438 
439 
441  : buckets_(other. buckets_), node_(other. node_), length_(other. length_) { }
442 
443 
445 
446 
449  {
450  buckets_ = other. buckets_;
451  node_ = other. node_;
452  length_ = other. length_;
453  return *this;
454  }
455 
456  public:
457 
458  T& operator*() const
459  {
460  return node_->data_;
461  }
462 
464  {
465  node_ = node_->next_ ? node_->next_ : next(node_);
466  return *this;
467  }
468 
469 
471  {
472  hash_table_iterator< T > tmp = *this;
473  ++*this;
474  return tmp;
475  }
476 
477 
478  bool operator==(const hash_table_iterator< T >& other) const
479  {
480  return node_ == other.node_;
481  }
482 
483 
484  bool operator!=(const hash_table_iterator< T >& other) const
485  {
486  return node_ != other.node_;
487  }
488 
489 
491 
492  public:
495  size_t length_;
496  };
497 
498 
499  template< class T >
500  class hash_table_const_iterator //: public forward_iterator< T, Distance >
501  {
502  public:
504 
505  hash_table_const_iterator(hash_node< T >** buckets, size_t length, hash_node< T >* node)
506  : buckets_(buckets), node_(node), length_(length) { }
507 
508 
510  : buckets_(other. buckets_), node_(other. node_), length_(other. length_) { }
511 
513  : buckets_(other. buckets_), node_(other. node_), length_(other. length_) { }
514 
516 
517 
519  {
520  buckets_ = other. buckets_;
521  node_ = other. node_;
522  length_ = other. length_;
523  return *this;
524  }
525 
526  public:
527 
528  const T& operator*() const
529  {
530  return node_->data_;
531  }
532 
534  {
535  node_ = node_->next_ ? node_->next_ : next(node_);
536  return *this;
537  }
538 
540  {
541  hash_table_const_iterator< T > tmp = *this;
542  ++*this;
543  return tmp;
544  }
545 
546 
548  {
549  return node_ == other.node_;
550  }
551 
552 
554  {
555  return node_ != other.node_;
556  }
557 
559 
560  public:
563  size_t length_;
564  };
565 
566 
567 
568  template<class Key, class Value,
569  class Hasher = hash< Key >,
570  class KeyEqual = equal_to< Key >
571  >
572  class HMap
573  {
574  public:
575  typedef Key key_type;
577  typedef Hasher hasher;
578  typedef KeyEqual key_equal;
579  typedef size_t size_type;
580  typedef ptrdiff_t difference_type;
583 
586 
587  public:
588 
589 
590 
591  explicit HMap(size_type table_size = 203) :
592  hash_table_(table_size, Hasher(), KeyEqual(), 0.75f)
593  {
594  }
595 
597  size_type table_size,
598  const Hasher& hf,
599  const KeyEqual& ke,
600  float load_ratio = 0.75f) :
601  hash_table_(table_size, hf, ke, load_ratio)
602  {
603  }
604 
605 
607  iterator first,
608  iterator last,
609  size_type table_size = 203) :
610  hash_table_(first, last, table_size, Hasher(), KeyEqual(), 0.75f)
611  {
612  }
613 
615  iterator first,
616  iterator last,
617  size_type table_size,
618  const Hasher& hf,
619  const KeyEqual& ke,
620  float load_ratio = 0.75f) :
621  hash_table_(first, last, table_size, hf, ke, load_ratio)
622  {
623  }
624 
625 
626 
627 
629  hash_table_(other.hash_table_) {}
630 
631  ~HMap() {}
632 
634  {
635  if (this != &other)
636  {
637  hash_table_ = other.hash_table_;
638  }
639  return *this;
640  }
641 
642  public:
644  {
645  hash_table_.swap(other.hash_table_);
646  }
647 
649  {
650  return hash_table_.hash_funct();
651  }
652 
654  {
655  return hash_table_.key_eq();
656  }
657 
659  {
660  return hash_table_.begin();
661  }
662 
664  {
665  return hash_table_.begin();
666  }
667 
669  {
670  return hash_table_.end();
671  }
672 
674  {
675  return hash_table_.end();
676  }
677 
678  bool empty() const
679  {
680  return hash_table_.empty();
681  }
682 
683  size_type size() const
684  {
685  return hash_table_.size();
686  }
687 
689  {
690  return hash_table_.max_size();
691  }
692 
694  {
695  return hash_table_.insert(p);
696  }
697 
698  pair< iterator, bool > insert(const Key& key, const Value& value)
699  {
700  pair< Key, Value > p(key, value);
701  return hash_table_.insert(p);
702  }
703 
704  void insert(
705  const pair< Key, Value >* first,
706  const pair< Key, Value >* last)
707  {
708  hash_table_.insert(first, last);
709  }
710 
712  {
713  hash_table_.insert(first, last);
714  }
715 
716  Value& operator[](const key_type& key)
717  {
718  return (*(insert(pair< Key, Value >(key, Value())).first)).second;
719  }
720 
721  const Value& operator[](const key_type& key) const
722  {
723  const_iterator i = find(key);
724  if (i == end())
725  {
726  return (const Value&)(*(const Value*)0);
727  }
728  else
729  {
730  return (*i).second;
731  }
732  }
733 
734  void erase(iterator pos)
735  {
736  hash_table_.erase(pos);
737  }
738 
740  {
741  return hash_table_.erase(key);
742  }
743 
745  {
746  hash_table_.erase(first, last);
747  }
748 
749  iterator find(const key_type& key)
750  {
751  return hash_table_.find(key);
752  }
753 
754  const_iterator find(const key_type& key) const
755  {
756  return hash_table_.find(key);
757  }
758 
759  size_type count(const key_type& key) const
760  {
761  return hash_table_.count(key);
762  }
763 
765  {
766  return hash_table_.equal_range(key);
767  }
768 
770  {
771  return hash_table_.equal_range(key);
772  }
773 
775  {
776  return hash_table_.bucket_count();
777  }
778 
779  void resize(size_type buckets)
780  {
781  hash_table_.resize(buckets);
782  }
783 
784  void clear()
785  {
786  hash_table_.clear();
787  }
788 
789  protected:
792  Hasher,
793  KeyEqual
795  };
796 
797  template < class Key, class Value, class Hasher, class KeyEqual>
798  inline bool operator==(
801  {
802  return x.size() == y.size() && equal(x.begin(), x.end(), y.begin()) ;
803  }
804 
805 
806  template < class Key, class Value, class Hasher, class KeyEqual>
807  inline bool operator < (
810  {
811  return lexicographical_compare(x.begin(), x.end(), y.begin(), y.end());
812  }
813 
814 
815  template < class Key, class Value, class Hasher, class KeyEqual>
816  inline void swap(
819  {
820  x.swap(y);
821  }
822 
823  template < class Key, class Value, class Hasher = hash< Key >,
824  class KeyEqual = equal_to< Key > >
826  {
827  public:
828  typedef Key key_type;
830  typedef Hasher hasher;
831  typedef KeyEqual key_equal;
832  typedef size_t size_type;
833  typedef ptrdiff_t difference_type;
836 
839 
840  public:
841  explicit hash_multimap(size_type table_size = 203) :
842  hash_table_(table_size, Hasher(), KeyEqual(), 0.75f, true) { }
843 
845  size_type table_size,
846  const Hasher& hf,
847  const KeyEqual& ke,
848  float load_ratio = 0.75f) :
849  hash_table_(table_size, hf, ke, load_ratio, true) {}
850 
851 
853  hash_table_(first, last, table_size, Hasher(), KeyEqual(), 0.75f, true)
854  {
855  }
856 
858  iterator first,
859  iterator last,
860  size_type table_size,
861  const Hasher& hf,
862  const KeyEqual& ke,
863  float load_ratio = 0.75f) :
864  hash_table_(first, last, table_size, hf, ke, load_ratio, true)
865  {
866  }
867 
868 
870  hash_table_(other.hash_table_)
871  {
872  }
873 
875 
878  {
879  if (this != &other)
880  {
881  hash_table_ = other.hash_table_;
882  }
883  return *this;
884  }
885 
887  {
888  hash_table_.swap(other.hash_table_);
889  }
890 
891  public:
893  {
894  return hash_table_.hash_funct();
895  }
896 
898  {
899  return hash_table_.key_eq();
900  }
901 
903  {
904  return hash_table_.begin();
905  }
906 
908  {
909  return hash_table_.begin();
910  }
911 
913  {
914  return hash_table_.end();
915  }
916 
918  {
919  return hash_table_.end();
920  }
921 
922  bool empty() const
923  {
924  return hash_table_.empty();
925  }
926 
927  size_type size() const
928  {
929  return hash_table_.size();
930  }
931 
933  {
934  return hash_table_.max_size();
935  }
936 
938  {
939  return hash_table_.insert(x).first;
940  }
941 
942  iterator insert(const Key& key, const Value& value)
943  {
944  pair< Key, Value > p(key, value);
945  return hash_table_.insert(p).first;
946  }
947 
949  {
950  hash_table_.insert(first, last);
951  }
952 
954  {
955  hash_table_.insert(first, last);
956  }
957 
958  void erase(iterator pos)
959  {
960  hash_table_.erase(pos);
961  }
962 
964  {
965  return hash_table_.erase(key);
966  }
967 
969  {
970  hash_table_.erase(first, last);
971  }
972 
973  iterator find(const key_type& key)
974  {
975  return hash_table_.find(key);
976  }
977 
978  const_iterator find(const key_type& key) const
979  {
980  return hash_table_.find(key);
981  }
982 
983  size_type count(const key_type& key) const
984  {
985  return hash_table_.count(key);
986  }
987 
989  {
990  return hash_table_.equal_range(key);
991  }
992 
994  {
995  return hash_table_.equal_range(key);
996  }
997 
999  {
1000  return hash_table_.bucket_count();
1001  }
1002 
1003  void resize(size_type buckets)
1004  {
1005  hash_table_.resize(buckets);
1006  }
1007 
1008  void clear()
1009  {
1010  hash_table_.clear();
1011  }
1012 
1013  protected:
1014 
1017  Hasher,
1018  KeyEqual
1020  };
1021 
1022  template < class Key, class Value, class Hasher, class KeyEqual>
1023  inline bool operator==(const hash_multimap
1024  < Key, Value, Hasher, KeyEqual>& x,
1025  const hash_multimap
1026  < Key, Value, Hasher, KeyEqual>& y)
1027  {
1028  return x.size() == y.size() && equal(x.begin(), x.end(), y.begin()) ;
1029  }
1030 
1031  template < class Key, class Value, class Hasher, class KeyEqual>
1032  inline bool operator < (
1035  {
1036  return lexicographical_compare(x.begin(), x.end(), y.begin(), y.end());
1037  }
1038 
1039  template< class T>
1041  {
1042  for (size_t i = (node->hash_ % length_) + 1; i < length_; i++)
1043  if (buckets_[ i ])
1044  {
1045  return buckets_[ i ];
1046  }
1047  return 0;
1048  }
1049 
1050 
1051  template< class T>
1053  {
1054  for (size_t i = (node->hash_ % length_) + 1; i < length_; i++)
1055  if (buckets_[ i ])
1056  {
1057  return buckets_[ i ];
1058  }
1059  return 0;
1060  }
1061 
1062 
1063  template < class Key, class Value, class Hasher, class KeyEqual>
1064  inline void swap(
1067  {
1068  x.swap(y);
1069  }
1070 
1071 
1072  template < class Key, class T, class KeyOfValue, class Hasher, class KeyEqual>
1074  {
1075  if (size_ > 0)
1076  for (size_t i = 0; i < length_; i++)
1077  if (buckets_[ i ])
1078  {
1079  return buckets_[ i ];
1080  }
1081  return 0;
1082  }
1083 
1084 
1085  template < class Key, class T, class KeyOfValue, class Hasher, class KeyEqual>
1087  {
1088  for (size_t i = 0; i < length_; i++)
1089  {
1090  hash_node< T >* node = buckets_[ i ];
1091 
1092  while (node)
1093  {
1094  hash_node< T >* next = node->next_;
1095 
1096  delete node;
1097  node = next;
1098  }
1099 
1100  buckets_[ i ] = 0;
1101  }
1102 
1103  size_ = 0;
1104  }
1105 
1106  template < class Key, class T, class KeyOfValue, class Hasher, class KeyEqual>
1108  const T* first,
1109  const T* last)
1110  {
1111  while (first != last)
1112  {
1113  insert(*first);
1114  first++;
1115  }
1116  }
1117 
1118 
1119  template < class Key, class T, class KeyOfValue, class Hasher, class KeyEqual>
1121  {
1122  size_t hash = hash_value(key);
1123  size_t probe = hash % length_;
1124 
1125  hash_node< T >* node = buckets_[ probe ];
1126  hash_node< T >* previous = 0;
1127  for (; node; previous = node, node = node->next_)
1128  if (hash_key_equal(hash, key, node))
1129  {
1130  size_type count = 1;
1131  hash_node< T >* tmp = node;
1132  hash_node< T >* end = node->next_;
1133 
1134  delete tmp;
1135 
1136  while (end && hash_key_equal(hash, key, end))
1137  {
1138  ++count;
1139  tmp = end;
1140  end = end->next_;
1141  delete tmp;
1142  }
1143  if (previous)
1144  {
1145  previous->next_ = end;
1146  }
1147  else
1148  {
1149  buckets_[ probe ] = end;
1150  }
1151 
1152  size_ -= count;
1153  return count;
1154  }
1155  return 0;
1156  }
1157 
1158 
1159  template < class Key, class T, class KeyOfValue, class Hasher, class KeyEqual>
1162  {
1163  hash_node< T >* target = pos.node_;
1164  size_t probe = target->hash_ % length_;
1165  hash_node< T >* node = buckets_[ probe ];
1166 
1167  if (target == node)
1168  {
1169  buckets_[ probe ] = target->next_;
1170  }
1171  else
1172  {
1173  while (node->next_ != target)
1174  {
1175  node = node->next_;
1176  }
1177 
1178  node->next_ = target->next_;
1179  }
1180 
1181  delete target;
1182  --size_;
1183  }
1184 
1185 
1186  template < class Key, class T, class KeyOfValue, class Hasher, class KeyEqual>
1190  {
1191  while (first != last)
1192  {
1193  insert(*first);
1194  first++;
1195  }
1196  }
1197 
1198 
1199  template < class Key, class T, class KeyOfValue, class Hasher, class KeyEqual>
1201  {
1202  if (!new_length)
1203  {
1204  new_length = length_ * 2 + 1;
1205  }
1206 
1207  hash_node< T >** new_buckets = allocate_node_ptr(new_length);
1208 
1209  for (size_t i = 0; i < length_; i++)
1210  {
1211  hash_node< T >* node = buckets_[ i ];
1212 
1213  while (node)
1214  {
1215  hash_node< T >* current = node;
1216  node = node->next_;
1217  size_t probe = current->hash_ % new_length;
1218  current->next_ = new_buckets[ probe ];
1219  new_buckets[ probe ] = current;
1220  }
1221  }
1222 
1223  deallocate_node_ptr(buckets_);
1224  buckets_ = new_buckets;
1225  length_ = new_length;
1226  limit_ = (size_type)(length_ * ratio_);
1227  }
1228 
1229 
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)
1232  {
1233  hash_node< T >** z = new hash_node< T >* [n];
1234  hash_node< T >** y = z + n;
1235 
1236  while (y != z)
1237  {
1238  *--y = 0;
1239  }
1240  return z;
1241  }
1242 
1243 
1244  template < class Key, class T, class KeyOfValue, class Hasher, class KeyEqual>
1246  {
1247  size_t hash = hash_value(key_of_value_(value));
1248  size_t probe = hash % length_;
1249  hash_node< T >* node;
1250 
1251  for (node = buckets_[ probe ]; node != 0; node = node->next_)
1252  if (hash_key_equal(hash, key_of_value_(value), node))
1253  {
1254  if (insert_always_)
1255  {
1256  hash_node< T >* new_node = new hash_node< T >(value);
1257  new_node->hash_ = hash;
1258  new_node->next_ = node->next_;
1259  node->next_ = new_node;
1260 
1261  if (++size_ > limit_)
1262  {
1263  expand();
1264  }
1265 
1266  return pair< iterator, bool >(iterator(buckets_, length_, new_node), true);
1267  }
1268  else
1269  {
1270  return pair< iterator, bool >(iterator(buckets_, length_, node), false);
1271  }
1272  }
1273  node = new hash_node< T >(value);
1274  node->hash_ = hash;
1275  node->next_ = buckets_[ probe ];
1276 
1277  buckets_[ probe ] = node;
1278  if (++size_ > limit_)
1279  {
1280  expand();
1281  }
1282 
1283  return pair< iterator, bool >(iterator(buckets_, length_, node), true);
1284  }
1285 
1286  template < class Key, class T, class KeyOfValue, class Hasher, class KeyEqual>
1288  {
1289  size_t hash = hash_value(key);
1290  hash_node< T >* node;
1291 
1292  for (node = buckets_[ hash % length_ ]; node != 0; node = node->next_)
1293  if (hash_key_equal(hash, key, node))
1294  {
1295  return iterator(buckets_, length_, node);
1296  }
1297 
1298  return end();
1299  }
1300 }
1301 
1302 
1303 #endif
T1 first
Definition: HMap.h:141
Definition: Time.h:246
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
Definition: HMap.h:40
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
Definition: HMap.h:83
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
Definition: HMap.h:104
#define CMREXD
Definition: Compiler.h:22
Definition: HMap.h:126
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
Definition: Time.h:244
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
Definition: HMap.h:572
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
Definition: HMap.h:825
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
Definition: HMap.h:60
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
Definition: HMap.h:169
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
Definition: HMap.h:188
bool empty() const
Definition: HMap.h:678
Definition: HMap.h:14
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
Definition: HMap.h:34
unary_function()
Definition: HMap.h:22
hash_table_iterator()
Definition: HMap.h:432
Definition: HMap.h:184
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