google_hash 0.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (85) hide show
  1. data/README +21 -0
  2. data/Rakefile +11 -0
  3. data/VERSION +1 -0
  4. data/ext/extconf.rb +15 -0
  5. data/ext/go.cpp +109 -0
  6. data/ext/sparsehash-1.5.2/AUTHORS +2 -0
  7. data/ext/sparsehash-1.5.2/COPYING +28 -0
  8. data/ext/sparsehash-1.5.2/ChangeLog +167 -0
  9. data/ext/sparsehash-1.5.2/INSTALL +236 -0
  10. data/ext/sparsehash-1.5.2/Makefile.am +157 -0
  11. data/ext/sparsehash-1.5.2/Makefile.in +1019 -0
  12. data/ext/sparsehash-1.5.2/NEWS +0 -0
  13. data/ext/sparsehash-1.5.2/README +149 -0
  14. data/ext/sparsehash-1.5.2/README.windows +25 -0
  15. data/ext/sparsehash-1.5.2/TODO +28 -0
  16. data/ext/sparsehash-1.5.2/aclocal.m4 +868 -0
  17. data/ext/sparsehash-1.5.2/compile +99 -0
  18. data/ext/sparsehash-1.5.2/config.guess +1516 -0
  19. data/ext/sparsehash-1.5.2/config.sub +1626 -0
  20. data/ext/sparsehash-1.5.2/configure +8054 -0
  21. data/ext/sparsehash-1.5.2/configure.ac +74 -0
  22. data/ext/sparsehash-1.5.2/depcomp +530 -0
  23. data/ext/sparsehash-1.5.2/doc/dense_hash_map.html +1591 -0
  24. data/ext/sparsehash-1.5.2/doc/dense_hash_set.html +1445 -0
  25. data/ext/sparsehash-1.5.2/doc/designstyle.css +115 -0
  26. data/ext/sparsehash-1.5.2/doc/implementation.html +365 -0
  27. data/ext/sparsehash-1.5.2/doc/index.html +69 -0
  28. data/ext/sparsehash-1.5.2/doc/performance.html +96 -0
  29. data/ext/sparsehash-1.5.2/doc/sparse_hash_map.html +1527 -0
  30. data/ext/sparsehash-1.5.2/doc/sparse_hash_set.html +1376 -0
  31. data/ext/sparsehash-1.5.2/doc/sparsetable.html +1393 -0
  32. data/ext/sparsehash-1.5.2/experimental/Makefile +9 -0
  33. data/ext/sparsehash-1.5.2/experimental/README +14 -0
  34. data/ext/sparsehash-1.5.2/experimental/example.c +54 -0
  35. data/ext/sparsehash-1.5.2/experimental/libchash.c +1537 -0
  36. data/ext/sparsehash-1.5.2/experimental/libchash.h +252 -0
  37. data/ext/sparsehash-1.5.2/google-sparsehash.sln +47 -0
  38. data/ext/sparsehash-1.5.2/install-sh +323 -0
  39. data/ext/sparsehash-1.5.2/m4/acx_pthread.m4 +363 -0
  40. data/ext/sparsehash-1.5.2/m4/google_namespace.m4 +42 -0
  41. data/ext/sparsehash-1.5.2/m4/namespaces.m4 +15 -0
  42. data/ext/sparsehash-1.5.2/m4/stl_hash.m4 +70 -0
  43. data/ext/sparsehash-1.5.2/m4/stl_hash_fun.m4 +36 -0
  44. data/ext/sparsehash-1.5.2/m4/stl_namespace.m4 +25 -0
  45. data/ext/sparsehash-1.5.2/missing +360 -0
  46. data/ext/sparsehash-1.5.2/mkinstalldirs +158 -0
  47. data/ext/sparsehash-1.5.2/packages/deb.sh +74 -0
  48. data/ext/sparsehash-1.5.2/packages/deb/README +7 -0
  49. data/ext/sparsehash-1.5.2/packages/deb/changelog +107 -0
  50. data/ext/sparsehash-1.5.2/packages/deb/compat +1 -0
  51. data/ext/sparsehash-1.5.2/packages/deb/control +17 -0
  52. data/ext/sparsehash-1.5.2/packages/deb/copyright +35 -0
  53. data/ext/sparsehash-1.5.2/packages/deb/docs +16 -0
  54. data/ext/sparsehash-1.5.2/packages/deb/rules +117 -0
  55. data/ext/sparsehash-1.5.2/packages/deb/sparsehash.dirs +2 -0
  56. data/ext/sparsehash-1.5.2/packages/deb/sparsehash.install +2 -0
  57. data/ext/sparsehash-1.5.2/packages/rpm.sh +86 -0
  58. data/ext/sparsehash-1.5.2/packages/rpm/rpm.spec +61 -0
  59. data/ext/sparsehash-1.5.2/src/config.h.in +131 -0
  60. data/ext/sparsehash-1.5.2/src/config.h.include +23 -0
  61. data/ext/sparsehash-1.5.2/src/google/dense_hash_map +310 -0
  62. data/ext/sparsehash-1.5.2/src/google/dense_hash_set +287 -0
  63. data/ext/sparsehash-1.5.2/src/google/sparse_hash_map +294 -0
  64. data/ext/sparsehash-1.5.2/src/google/sparse_hash_set +275 -0
  65. data/ext/sparsehash-1.5.2/src/google/sparsehash/densehashtable.h +1062 -0
  66. data/ext/sparsehash-1.5.2/src/google/sparsehash/sparsehashtable.h +1015 -0
  67. data/ext/sparsehash-1.5.2/src/google/sparsetable +1468 -0
  68. data/ext/sparsehash-1.5.2/src/google/type_traits.h +250 -0
  69. data/ext/sparsehash-1.5.2/src/hashtable_unittest.cc +1375 -0
  70. data/ext/sparsehash-1.5.2/src/simple_test.cc +103 -0
  71. data/ext/sparsehash-1.5.2/src/sparsetable_unittest.cc +696 -0
  72. data/ext/sparsehash-1.5.2/src/time_hash_map.cc +488 -0
  73. data/ext/sparsehash-1.5.2/src/type_traits_unittest.cc +492 -0
  74. data/ext/sparsehash-1.5.2/src/windows/config.h +149 -0
  75. data/ext/sparsehash-1.5.2/src/windows/google/sparsehash/sparseconfig.h +32 -0
  76. data/ext/sparsehash-1.5.2/src/windows/port.cc +63 -0
  77. data/ext/sparsehash-1.5.2/src/windows/port.h +81 -0
  78. data/ext/sparsehash-1.5.2/src/words +8944 -0
  79. data/ext/sparsehash-1.5.2/vsprojects/hashtable_unittest/hashtable_unittest.vcproj +187 -0
  80. data/ext/sparsehash-1.5.2/vsprojects/sparsetable_unittest/sparsetable_unittest.vcproj +172 -0
  81. data/ext/sparsehash-1.5.2/vsprojects/time_hash_map/time_hash_map.vcproj +187 -0
  82. data/ext/sparsehash-1.5.2/vsprojects/type_traits_unittest/type_traits_unittest.vcproj +169 -0
  83. data/ext/test.rb +10 -0
  84. data/test/spec.go +70 -0
  85. metadata +147 -0
@@ -0,0 +1,287 @@
1
+ // Copyright (c) 2005, Google Inc.
2
+ // All rights reserved.
3
+ //
4
+ // Redistribution and use in source and binary forms, with or without
5
+ // modification, are permitted provided that the following conditions are
6
+ // met:
7
+ //
8
+ // * Redistributions of source code must retain the above copyright
9
+ // notice, this list of conditions and the following disclaimer.
10
+ // * Redistributions in binary form must reproduce the above
11
+ // copyright notice, this list of conditions and the following disclaimer
12
+ // in the documentation and/or other materials provided with the
13
+ // distribution.
14
+ // * Neither the name of Google Inc. nor the names of its
15
+ // contributors may be used to endorse or promote products derived from
16
+ // this software without specific prior written permission.
17
+ //
18
+ // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19
+ // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20
+ // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21
+ // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22
+ // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23
+ // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24
+ // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25
+ // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26
+ // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27
+ // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28
+ // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
+
30
+ // ---
31
+ // Author: Craig Silverstein
32
+ //
33
+ // This is just a very thin wrapper over densehashtable.h, just
34
+ // like sgi stl's stl_hash_set is a very thin wrapper over
35
+ // stl_hashtable. The major thing we define is operator[], because
36
+ // we have a concept of a data_type which stl_hashtable doesn't
37
+ // (it only has a key and a value).
38
+ //
39
+ // This is more different from dense_hash_map than you might think,
40
+ // because all iterators for sets are const (you obviously can't
41
+ // change the key, and for sets there is no value).
42
+ //
43
+ // NOTE: this is exactly like sparse_hash_set.h, with the word
44
+ // "sparse" replaced by "dense", except for the addition of
45
+ // set_empty_key().
46
+ //
47
+ // YOU MUST CALL SET_EMPTY_KEY() IMMEDIATELY AFTER CONSTRUCTION.
48
+ //
49
+ // Otherwise your program will die in mysterious ways.
50
+ //
51
+ // In other respects, we adhere mostly to the STL semantics for
52
+ // hash-set. One important exception is that insert() invalidates
53
+ // iterators entirely. On the plus side, though, erase() doesn't
54
+ // invalidate iterators at all, or even change the ordering of elements.
55
+ //
56
+ // Here are a few "power user" tips:
57
+ //
58
+ // 1) set_deleted_key():
59
+ // If you want to use erase() you must call set_deleted_key(),
60
+ // in addition to set_empty_key(), after construction.
61
+ // The deleted and empty keys must differ.
62
+ //
63
+ // 2) resize(0):
64
+ // When an item is deleted, its memory isn't freed right
65
+ // away. This allows you to iterate over a hashtable,
66
+ // and call erase(), without invalidating the iterator.
67
+ // To force the memory to be freed, call resize(0).
68
+ // For tr1 compatibility, this can also be called as rehash(0).
69
+ //
70
+ // 3) min_load_factor(0.0)
71
+ // Setting the minimum load factor to 0.0 guarantees that
72
+ // the hash table will never shrink.
73
+ //
74
+ // Guide to what kind of hash_set to use:
75
+ // (1) dense_hash_set: fastest, uses the most memory
76
+ // (2) sparse_hash_set: slowest, uses the least memory
77
+ // (3) hash_set (STL): in the middle
78
+ // Typically I use sparse_hash_set when I care about space and/or when
79
+ // I need to save the hashtable on disk. I use hash_set otherwise. I
80
+ // don't personally use dense_hash_set ever; some people use it for
81
+ // small sets with lots of lookups.
82
+ //
83
+ // - dense_hash_set has, typically, a factor of 2 memory overhead (if your
84
+ // data takes up X bytes, the hash_set uses X more bytes in overhead).
85
+ // - sparse_hash_set has about 2 bits overhead per entry.
86
+ // - sparse_hash_map can be 3-7 times slower than the others for lookup and,
87
+ // especially, inserts. See time_hash_map.cc for details.
88
+ //
89
+ // See /usr/(local/)?doc/sparsehash-*/dense_hash_set.html
90
+ // for information about how to use this class.
91
+
92
+ #ifndef _DENSE_HASH_SET_H_
93
+ #define _DENSE_HASH_SET_H_
94
+
95
+ #include <google/sparsehash/sparseconfig.h>
96
+ #include <stdio.h> // for FILE * in read()/write()
97
+ #include <algorithm> // for the default template args
98
+ #include <functional> // for equal_to
99
+ #include <memory> // for alloc<>
100
+ #include <utility> // for pair<>
101
+ #include HASH_FUN_H // defined in config.h
102
+ #include <google/sparsehash/densehashtable.h>
103
+
104
+
105
+ _START_GOOGLE_NAMESPACE_
106
+
107
+ using STL_NAMESPACE::pair;
108
+
109
+ template <class Value,
110
+ class HashFcn = SPARSEHASH_HASH<Value>, // defined in sparseconfig.h
111
+ class EqualKey = STL_NAMESPACE::equal_to<Value>,
112
+ class Alloc = STL_NAMESPACE::allocator<Value> >
113
+ class dense_hash_set {
114
+ private:
115
+ // Apparently identity is not stl-standard, so we define our own
116
+ struct Identity {
117
+ Value& operator()(Value& v) const { return v; }
118
+ const Value& operator()(const Value& v) const { return v; }
119
+ };
120
+ struct SetKey {
121
+ void operator()(Value* value, const Value& new_key) const {
122
+ *value = new_key;
123
+ }
124
+ };
125
+
126
+ // The actual data
127
+ typedef dense_hashtable<Value, Value, HashFcn,
128
+ Identity, SetKey, EqualKey, Alloc> ht;
129
+ ht rep;
130
+
131
+ public:
132
+ typedef typename ht::key_type key_type;
133
+ typedef typename ht::value_type value_type;
134
+ typedef typename ht::hasher hasher;
135
+ typedef typename ht::key_equal key_equal;
136
+ typedef Alloc allocator_type;
137
+
138
+ typedef typename ht::size_type size_type;
139
+ typedef typename ht::difference_type difference_type;
140
+ typedef typename ht::const_pointer pointer;
141
+ typedef typename ht::const_pointer const_pointer;
142
+ typedef typename ht::const_reference reference;
143
+ typedef typename ht::const_reference const_reference;
144
+
145
+ typedef typename ht::const_iterator iterator;
146
+ typedef typename ht::const_iterator const_iterator;
147
+ typedef typename ht::const_local_iterator local_iterator;
148
+ typedef typename ht::const_local_iterator const_local_iterator;
149
+
150
+
151
+ // Iterator functions -- recall all iterators are const
152
+ iterator begin() const { return rep.begin(); }
153
+ iterator end() const { return rep.end(); }
154
+
155
+ // These come from tr1's unordered_set. For us, a bucket has 0 or 1 elements.
156
+ local_iterator begin(size_type i) const { return rep.begin(i); }
157
+ local_iterator end(size_type i) const { return rep.end(i); }
158
+
159
+
160
+ // Accessor functions
161
+ hasher hash_funct() const { return rep.hash_funct(); }
162
+ key_equal key_eq() const { return rep.key_eq(); }
163
+
164
+
165
+ // Constructors
166
+ explicit dense_hash_set(size_type expected_max_items_in_table = 0,
167
+ const hasher& hf = hasher(),
168
+ const key_equal& eql = key_equal())
169
+ : rep(expected_max_items_in_table, hf, eql) { }
170
+
171
+ template <class InputIterator>
172
+ dense_hash_set(InputIterator f, InputIterator l,
173
+ size_type expected_max_items_in_table = 0,
174
+ const hasher& hf = hasher(),
175
+ const key_equal& eql = key_equal())
176
+ : rep(expected_max_items_in_table, hf, eql) {
177
+ rep.insert(f, l);
178
+ }
179
+ // We use the default copy constructor
180
+ // We use the default operator=()
181
+ // We use the default destructor
182
+
183
+ void clear() { rep.clear(); }
184
+ // This clears the hash set without resizing it down to the minimum
185
+ // bucket count, but rather keeps the number of buckets constant
186
+ void clear_no_resize() { rep.clear_no_resize(); }
187
+ void swap(dense_hash_set& hs) { rep.swap(hs.rep); }
188
+
189
+
190
+ // Functions concerning size
191
+ size_type size() const { return rep.size(); }
192
+ size_type max_size() const { return rep.max_size(); }
193
+ bool empty() const { return rep.empty(); }
194
+ size_type bucket_count() const { return rep.bucket_count(); }
195
+ size_type max_bucket_count() const { return rep.max_bucket_count(); }
196
+
197
+ // These are tr1 methods. bucket() is the bucket the key is or would be in.
198
+ size_type bucket_size(size_type i) const { return rep.bucket_size(i); }
199
+ size_type bucket(const key_type& key) const { return rep.bucket(key); }
200
+ float load_factor() const {
201
+ return size() * 1.0f / bucket_count();
202
+ }
203
+ float max_load_factor() const {
204
+ float shrink, grow;
205
+ rep.get_resizing_parameters(&shrink, &grow);
206
+ return grow;
207
+ }
208
+ void max_load_factor(float new_grow) {
209
+ float shrink, grow;
210
+ rep.get_resizing_parameters(&shrink, &grow);
211
+ rep.set_resizing_parameters(shrink, new_grow);
212
+ }
213
+ // These aren't tr1 methods but perhaps ought to be.
214
+ float min_load_factor() const {
215
+ float shrink, grow;
216
+ rep.get_resizing_parameters(&shrink, &grow);
217
+ return shrink;
218
+ }
219
+ void min_load_factor(float new_shrink) {
220
+ float shrink, grow;
221
+ rep.get_resizing_parameters(&shrink, &grow);
222
+ rep.set_resizing_parameters(new_shrink, grow);
223
+ }
224
+ // Deprecated; use min_load_factor() or max_load_factor() instead.
225
+ void set_resizing_parameters(float shrink, float grow) {
226
+ return rep.set_resizing_parameters(shrink, grow);
227
+ }
228
+
229
+ void resize(size_type hint) { rep.resize(hint); }
230
+ void rehash(size_type hint) { resize(hint); } // the tr1 name
231
+
232
+ // Lookup routines
233
+ iterator find(const key_type& key) const { return rep.find(key); }
234
+
235
+ size_type count(const key_type& key) const { return rep.count(key); }
236
+
237
+ pair<iterator, iterator> equal_range(const key_type& key) const {
238
+ return rep.equal_range(key);
239
+ }
240
+
241
+ // Insertion routines
242
+ pair<iterator, bool> insert(const value_type& obj) {
243
+ pair<typename ht::iterator, bool> p = rep.insert(obj);
244
+ return pair<iterator, bool>(p.first, p.second); // const to non-const
245
+ }
246
+ template <class InputIterator>
247
+ void insert(InputIterator f, InputIterator l) { rep.insert(f, l); }
248
+ void insert(const_iterator f, const_iterator l) { rep.insert(f, l); }
249
+ // required for std::insert_iterator; the passed-in iterator is ignored
250
+ iterator insert(iterator, const value_type& obj) { return insert(obj).first; }
251
+
252
+
253
+ // Deletion and empty routines
254
+ // THESE ARE NON-STANDARD! I make you specify an "impossible" key
255
+ // value to identify deleted and empty buckets. You can change the
256
+ // deleted key as time goes on, or get rid of it entirely to be insert-only.
257
+ void set_empty_key(const key_type& key) { rep.set_empty_key(key); }
258
+ void set_deleted_key(const key_type& key) { rep.set_deleted_key(key); }
259
+ void clear_deleted_key() { rep.clear_deleted_key(); }
260
+
261
+ // These are standard
262
+ size_type erase(const key_type& key) { return rep.erase(key); }
263
+ void erase(iterator it) { rep.erase(it); }
264
+ void erase(iterator f, iterator l) { rep.erase(f, l); }
265
+
266
+
267
+ // Comparison
268
+ bool operator==(const dense_hash_set& hs) const { return rep == hs.rep; }
269
+ bool operator!=(const dense_hash_set& hs) const { return rep != hs.rep; }
270
+
271
+
272
+ // I/O -- this is an add-on for writing metainformation to disk
273
+ bool write_metadata(FILE *fp) { return rep.write_metadata(fp); }
274
+ bool read_metadata(FILE *fp) { return rep.read_metadata(fp); }
275
+ bool write_nopointer_data(FILE *fp) { return rep.write_nopointer_data(fp); }
276
+ bool read_nopointer_data(FILE *fp) { return rep.read_nopointer_data(fp); }
277
+ };
278
+
279
+ template <class Val, class HashFcn, class EqualKey, class Alloc>
280
+ inline void swap(dense_hash_set<Val, HashFcn, EqualKey, Alloc>& hs1,
281
+ dense_hash_set<Val, HashFcn, EqualKey, Alloc>& hs2) {
282
+ hs1.swap(hs2);
283
+ }
284
+
285
+ _END_GOOGLE_NAMESPACE_
286
+
287
+ #endif /* _DENSE_HASH_SET_H_ */
@@ -0,0 +1,294 @@
1
+ // Copyright (c) 2005, Google Inc.
2
+ // All rights reserved.
3
+ //
4
+ // Redistribution and use in source and binary forms, with or without
5
+ // modification, are permitted provided that the following conditions are
6
+ // met:
7
+ //
8
+ // * Redistributions of source code must retain the above copyright
9
+ // notice, this list of conditions and the following disclaimer.
10
+ // * Redistributions in binary form must reproduce the above
11
+ // copyright notice, this list of conditions and the following disclaimer
12
+ // in the documentation and/or other materials provided with the
13
+ // distribution.
14
+ // * Neither the name of Google Inc. nor the names of its
15
+ // contributors may be used to endorse or promote products derived from
16
+ // this software without specific prior written permission.
17
+ //
18
+ // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19
+ // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20
+ // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21
+ // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22
+ // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23
+ // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24
+ // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25
+ // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26
+ // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27
+ // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28
+ // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
+
30
+ // ---
31
+ // Author: Craig Silverstein
32
+ //
33
+ // This is just a very thin wrapper over sparsehashtable.h, just
34
+ // like sgi stl's stl_hash_map is a very thin wrapper over
35
+ // stl_hashtable. The major thing we define is operator[], because
36
+ // we have a concept of a data_type which stl_hashtable doesn't
37
+ // (it only has a key and a value).
38
+ //
39
+ // We adhere mostly to the STL semantics for hash-map. One important
40
+ // exception is that insert() invalidates iterators entirely. On the
41
+ // plus side, though, delete() doesn't invalidate iterators at all, or
42
+ // even change the ordering of elements.
43
+ //
44
+ // Here are a few "power user" tips:
45
+ //
46
+ // 1) set_deleted_key():
47
+ // Unlike STL's hash_map, if you want to use erase() you
48
+ // *must* call set_deleted_key() after construction.
49
+ //
50
+ // 2) resize(0):
51
+ // When an item is deleted, its memory isn't freed right
52
+ // away. This is what allows you to iterate over a hashtable
53
+ // and call erase() without invalidating the iterator.
54
+ // To force the memory to be freed, call resize(0).
55
+ // For tr1 compatibility, this can also be called as rehash(0).
56
+ //
57
+ // 3) min_load_factor(0.0)
58
+ // Setting the minimum load factor to 0.0 guarantees that
59
+ // the hash table will never shrink.
60
+ //
61
+ // Guide to what kind of hash_map to use:
62
+ // (1) dense_hash_map: fastest, uses the most memory
63
+ // (2) sparse_hash_map: slowest, uses the least memory
64
+ // (3) hash_map / unordered_map (STL): in the middle
65
+ // Typically I use sparse_hash_map when I care about space and/or when
66
+ // I need to save the hashtable on disk. I use hash_map otherwise. I
67
+ // don't personally use dense_hash_map ever; some people use it for
68
+ // small maps with lots of lookups.
69
+ //
70
+ // - dense_hash_map has, typically, a factor of 2 memory overhead (if your
71
+ // data takes up X bytes, the hash_map uses X more bytes in overhead).
72
+ // - sparse_hash_map has about 2 bits overhead per entry.
73
+ // - sparse_hash_map can be 3-7 times slower than the others for lookup and,
74
+ // especially, inserts. See time_hash_map.cc for details.
75
+ //
76
+ // See /usr/(local/)?doc/sparsehash-*/sparse_hash_map.html
77
+ // for information about how to use this class.
78
+
79
+ #ifndef _SPARSE_HASH_MAP_H_
80
+ #define _SPARSE_HASH_MAP_H_
81
+
82
+ #include <google/sparsehash/sparseconfig.h>
83
+ #include <stdio.h> // for FILE * in read()/write()
84
+ #include <algorithm> // for the default template args
85
+ #include <functional> // for equal_to
86
+ #include <memory> // for alloc<>
87
+ #include <utility> // for pair<>
88
+ #include HASH_FUN_H // defined in config.h
89
+ #include <google/sparsehash/sparsehashtable.h>
90
+
91
+
92
+ _START_GOOGLE_NAMESPACE_
93
+
94
+ using STL_NAMESPACE::pair;
95
+
96
+ template <class Key, class T,
97
+ class HashFcn = SPARSEHASH_HASH<Key>, // defined in sparseconfig.h
98
+ class EqualKey = STL_NAMESPACE::equal_to<Key>,
99
+ class Alloc = STL_NAMESPACE::allocator<T> >
100
+ class sparse_hash_map {
101
+ private:
102
+ // Apparently select1st is not stl-standard, so we define our own
103
+ struct SelectKey {
104
+ const Key& operator()(const pair<const Key, T>& p) const {
105
+ return p.first;
106
+ }
107
+ };
108
+ struct SetKey {
109
+ void operator()(pair<const Key, T>* value, const Key& new_key) const {
110
+ *const_cast<Key*>(&value->first) = new_key;
111
+ // It would be nice to clear the rest of value here as well, in
112
+ // case it's taking up a lot of memory. We do this by clearing
113
+ // the value. This assumes T has a zero-arg constructor!
114
+ value->second = T();
115
+ }
116
+ };
117
+
118
+ // The actual data
119
+ typedef sparse_hashtable<pair<const Key, T>, Key, HashFcn,
120
+ SelectKey, SetKey, EqualKey, Alloc> ht;
121
+ ht rep;
122
+
123
+ public:
124
+ typedef typename ht::key_type key_type;
125
+ typedef T data_type;
126
+ typedef T mapped_type;
127
+ typedef typename ht::value_type value_type;
128
+ typedef typename ht::hasher hasher;
129
+ typedef typename ht::key_equal key_equal;
130
+ typedef Alloc allocator_type;
131
+
132
+ typedef typename ht::size_type size_type;
133
+ typedef typename ht::difference_type difference_type;
134
+ typedef typename ht::pointer pointer;
135
+ typedef typename ht::const_pointer const_pointer;
136
+ typedef typename ht::reference reference;
137
+ typedef typename ht::const_reference const_reference;
138
+
139
+ typedef typename ht::iterator iterator;
140
+ typedef typename ht::const_iterator const_iterator;
141
+ typedef typename ht::local_iterator local_iterator;
142
+ typedef typename ht::const_local_iterator const_local_iterator;
143
+
144
+ // Iterator functions
145
+ iterator begin() { return rep.begin(); }
146
+ iterator end() { return rep.end(); }
147
+ const_iterator begin() const { return rep.begin(); }
148
+ const_iterator end() const { return rep.end(); }
149
+
150
+ // These come from tr1's unordered_map. For us, a bucket has 0 or 1 elements.
151
+ local_iterator begin(size_type i) { return rep.begin(i); }
152
+ local_iterator end(size_type i) { return rep.end(i); }
153
+ const_local_iterator begin(size_type i) const { return rep.begin(i); }
154
+ const_local_iterator end(size_type i) const { return rep.end(i); }
155
+
156
+ // Accessor functions
157
+ // TODO(csilvers): implement Alloc get_allocator() const;
158
+ hasher hash_funct() const { return rep.hash_funct(); }
159
+ hasher hash_function() const { return hash_funct(); }
160
+ key_equal key_eq() const { return rep.key_eq(); }
161
+
162
+
163
+ // Constructors
164
+ explicit sparse_hash_map(size_type expected_max_items_in_table = 0,
165
+ const hasher& hf = hasher(),
166
+ const key_equal& eql = key_equal())
167
+ : rep(expected_max_items_in_table, hf, eql) { }
168
+
169
+ template <class InputIterator>
170
+ sparse_hash_map(InputIterator f, InputIterator l,
171
+ size_type expected_max_items_in_table = 0,
172
+ const hasher& hf = hasher(),
173
+ const key_equal& eql = key_equal())
174
+ : rep(expected_max_items_in_table, hf, eql) {
175
+ rep.insert(f, l);
176
+ }
177
+ // We use the default copy constructor
178
+ // We use the default operator=()
179
+ // We use the default destructor
180
+
181
+ void clear() { rep.clear(); }
182
+ void swap(sparse_hash_map& hs) { rep.swap(hs.rep); }
183
+
184
+
185
+ // Functions concerning size
186
+ size_type size() const { return rep.size(); }
187
+ size_type max_size() const { return rep.max_size(); }
188
+ bool empty() const { return rep.empty(); }
189
+ size_type bucket_count() const { return rep.bucket_count(); }
190
+ size_type max_bucket_count() const { return rep.max_bucket_count(); }
191
+
192
+ // These are tr1 methods. bucket() is the bucket the key is or would be in.
193
+ size_type bucket_size(size_type i) const { return rep.bucket_size(i); }
194
+ size_type bucket(const key_type& key) const { return rep.bucket(key); }
195
+ float load_factor() const {
196
+ return size() * 1.0f / bucket_count();
197
+ }
198
+ float max_load_factor() const {
199
+ float shrink, grow;
200
+ rep.get_resizing_parameters(&shrink, &grow);
201
+ return grow;
202
+ }
203
+ void max_load_factor(float new_grow) {
204
+ float shrink, grow;
205
+ rep.get_resizing_parameters(&shrink, &grow);
206
+ rep.set_resizing_parameters(shrink, new_grow);
207
+ }
208
+ // These aren't tr1 methods but perhaps ought to be.
209
+ float min_load_factor() const {
210
+ float shrink, grow;
211
+ rep.get_resizing_parameters(&shrink, &grow);
212
+ return shrink;
213
+ }
214
+ void min_load_factor(float new_shrink) {
215
+ float shrink, grow;
216
+ rep.get_resizing_parameters(&shrink, &grow);
217
+ rep.set_resizing_parameters(new_shrink, grow);
218
+ }
219
+ // Deprecated; use min_load_factor() or max_load_factor() instead.
220
+ void set_resizing_parameters(float shrink, float grow) {
221
+ return rep.set_resizing_parameters(shrink, grow);
222
+ }
223
+
224
+ void resize(size_type hint) { rep.resize(hint); }
225
+ void rehash(size_type hint) { resize(hint); } // the tr1 name
226
+
227
+ // Lookup routines
228
+ iterator find(const key_type& key) { return rep.find(key); }
229
+ const_iterator find(const key_type& key) const { return rep.find(key); }
230
+
231
+ data_type& operator[](const key_type& key) { // This is our value-add!
232
+ iterator it = find(key);
233
+ if (it != end()) {
234
+ return it->second;
235
+ } else {
236
+ return insert(value_type(key, data_type())).first->second;
237
+ }
238
+ }
239
+
240
+ size_type count(const key_type& key) const { return rep.count(key); }
241
+
242
+ pair<iterator, iterator> equal_range(const key_type& key) {
243
+ return rep.equal_range(key);
244
+ }
245
+ pair<const_iterator, const_iterator> equal_range(const key_type& key) const {
246
+ return rep.equal_range(key);
247
+ }
248
+
249
+ // Insertion routines
250
+ pair<iterator, bool> insert(const value_type& obj) { return rep.insert(obj); }
251
+ template <class InputIterator>
252
+ void insert(InputIterator f, InputIterator l) { rep.insert(f, l); }
253
+ void insert(const_iterator f, const_iterator l) { rep.insert(f, l); }
254
+ // required for std::insert_iterator; the passed-in iterator is ignored
255
+ iterator insert(iterator, const value_type& obj) { return insert(obj).first; }
256
+
257
+
258
+ // Deletion routines
259
+ // THESE ARE NON-STANDARD! I make you specify an "impossible" key
260
+ // value to identify deleted buckets. You can change the key as
261
+ // time goes on, or get rid of it entirely to be insert-only.
262
+ void set_deleted_key(const key_type& key) {
263
+ rep.set_deleted_key(key);
264
+ }
265
+ void clear_deleted_key() { rep.clear_deleted_key(); }
266
+
267
+ // These are standard
268
+ size_type erase(const key_type& key) { return rep.erase(key); }
269
+ void erase(iterator it) { rep.erase(it); }
270
+ void erase(iterator f, iterator l) { rep.erase(f, l); }
271
+
272
+
273
+ // Comparison
274
+ bool operator==(const sparse_hash_map& hs) const { return rep == hs.rep; }
275
+ bool operator!=(const sparse_hash_map& hs) const { return rep != hs.rep; }
276
+
277
+
278
+ // I/O -- this is an add-on for writing metainformation to disk
279
+ bool write_metadata(FILE *fp) { return rep.write_metadata(fp); }
280
+ bool read_metadata(FILE *fp) { return rep.read_metadata(fp); }
281
+ bool write_nopointer_data(FILE *fp) { return rep.write_nopointer_data(fp); }
282
+ bool read_nopointer_data(FILE *fp) { return rep.read_nopointer_data(fp); }
283
+ };
284
+
285
+ // We need a global swap as well
286
+ template <class Key, class T, class HashFcn, class EqualKey, class Alloc>
287
+ inline void swap(sparse_hash_map<Key, T, HashFcn, EqualKey, Alloc>& hm1,
288
+ sparse_hash_map<Key, T, HashFcn, EqualKey, Alloc>& hm2) {
289
+ hm1.swap(hm2);
290
+ }
291
+
292
+ _END_GOOGLE_NAMESPACE_
293
+
294
+ #endif /* _SPARSE_HASH_MAP_H_ */