/[pcsx2_0.9.7]/trunk/3rdparty/google/dense_hash_set
ViewVC logotype

Contents of /trunk/3rdparty/google/dense_hash_set

Parent Directory Parent Directory | Revision Log Revision Log


Revision 8 - (show annotations) (download)
Mon Sep 6 11:19:43 2010 UTC (9 years, 9 months ago) by william
File size: 10314 byte(s)
Exported ./upsream/trunk @r3730 from http://pcsx2.googlecode.com/svn/trunk/
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 //
69 // 3) set_resizing_parameters(0.0, 0.8):
70 // Setting the shrink_resize_percent to 0.0 guarantees
71 // that the hash table will never shrink.
72 //
73 // Guide to what kind of hash_set to use:
74 // (1) dense_hash_set: fastest, uses the most memory
75 // (2) sparse_hash_set: slowest, uses the least memory
76 // (3) hash_set (STL): in the middle
77 // Typically I use sparse_hash_set when I care about space and/or when
78 // I need to save the hashtable on disk. I use hash_set otherwise. I
79 // don't personally use dense_hash_set ever; the only use of
80 // dense_hash_set I know of is to work around malloc() bugs in some
81 // systems (dense_hash_set has a particularly simple allocation scheme).
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-0.1/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
121 // The actual data
122 typedef dense_hashtable<Value, Value, HashFcn, Identity, EqualKey, Alloc> ht;
123 ht rep;
124
125 public:
126 typedef typename ht::key_type key_type;
127 typedef typename ht::value_type value_type;
128 typedef typename ht::hasher hasher;
129 typedef typename ht::key_equal key_equal;
130
131 typedef typename ht::size_type size_type;
132 typedef typename ht::difference_type difference_type;
133 typedef typename ht::const_pointer pointer;
134 typedef typename ht::const_pointer const_pointer;
135 typedef typename ht::const_reference reference;
136 typedef typename ht::const_reference const_reference;
137
138 typedef typename ht::const_iterator iterator;
139 typedef typename ht::const_iterator const_iterator;
140
141
142 // Iterator functions -- recall all iterators are const
143 iterator begin() const { return rep.begin(); }
144 iterator end() const { return rep.end(); }
145
146
147 // Accessor functions
148 hasher hash_funct() const { return rep.hash_funct(); }
149 key_equal key_eq() const { return rep.key_eq(); }
150
151
152 // Constructors
153 explicit dense_hash_set(size_type expected_max_items_in_table = 0,
154 const hasher& hf = hasher(),
155 const key_equal& eql = key_equal())
156 : rep(expected_max_items_in_table, hf, eql) { }
157
158 template <class InputIterator>
159 dense_hash_set(InputIterator f, InputIterator l,
160 size_type expected_max_items_in_table = 0,
161 const hasher& hf = hasher(),
162 const key_equal& eql = key_equal())
163 : rep(expected_max_items_in_table, hf, eql) {
164 rep.insert(f, l);
165 }
166 // We use the default copy constructor
167 // We use the default operator=()
168 // We use the default destructor
169
170 void clear() { rep.clear(); }
171 // This clears the hash set without resizing it down to the minimum
172 // bucket count, but rather keeps the number of buckets constant
173 void clear_no_resize() { rep.clear_no_resize(); }
174 void swap(dense_hash_set& hs) { rep.swap(hs.rep); }
175
176
177 // Functions concerning size
178 size_type size() const { return rep.size(); }
179 size_type max_size() const { return rep.max_size(); }
180 bool empty() const { return rep.empty(); }
181 size_type bucket_count() const { return rep.bucket_count(); }
182 size_type max_bucket_count() const { return rep.max_bucket_count(); }
183
184 void resize(size_type hint) { rep.resize(hint); }
185
186 void set_resizing_parameters(float shrink, float grow) {
187 return rep.set_resizing_parameters(shrink, grow);
188 }
189
190 // Lookup routines
191 iterator find(const key_type& key) const { return rep.find(key); }
192
193 size_type count(const key_type& key) const { return rep.count(key); }
194
195 pair<iterator, iterator> equal_range(const key_type& key) const {
196 return rep.equal_range(key);
197 }
198
199 // Insertion routines
200 pair<iterator, bool> insert(const value_type& obj) {
201 pair<typename ht::iterator, bool> p = rep.insert(obj);
202 return pair<iterator, bool>(p.first, p.second); // const to non-const
203 }
204 template <class InputIterator>
205 void insert(InputIterator f, InputIterator l) { rep.insert(f, l); }
206 void insert(const_iterator f, const_iterator l) { rep.insert(f, l); }
207 // required for std::insert_iterator; the passed-in iterator is ignored
208 iterator insert(iterator, const value_type& obj) { return insert(obj).first; }
209
210
211 // Deletion and empty routines
212 // THESE ARE NON-STANDARD! I make you specify an "impossible" key
213 // value to identify deleted and empty buckets. You can change the
214 // deleted key as time goes on, or get rid of it entirely to be insert-only.
215 void set_empty_key(const key_type& key) { rep.set_empty_key(key); }
216 void set_deleted_key(const key_type& key) { rep.set_deleted_key(key); }
217 void clear_deleted_key() { rep.clear_deleted_key(); }
218
219 // These are standard
220 size_type erase(const key_type& key) { return rep.erase(key); }
221 void erase(iterator it) { rep.erase(it); }
222 void erase(iterator f, iterator l) { rep.erase(f, l); }
223
224
225 // Comparison
226 bool operator==(const dense_hash_set& hs) const { return rep == hs.rep; }
227 bool operator!=(const dense_hash_set& hs) const { return rep != hs.rep; }
228
229
230 // I/O -- this is an add-on for writing metainformation to disk
231 bool write_metadata(FILE *fp) { return rep.write_metadata(fp); }
232 bool read_metadata(FILE *fp) { return rep.read_metadata(fp); }
233 bool write_nopointer_data(FILE *fp) { return rep.write_nopointer_data(fp); }
234 bool read_nopointer_data(FILE *fp) { return rep.read_nopointer_data(fp); }
235 };
236
237 template <class Val, class HashFcn, class EqualKey, class Alloc>
238 inline void swap(dense_hash_set<Val, HashFcn, EqualKey, Alloc>& hs1,
239 dense_hash_set<Val, HashFcn, EqualKey, Alloc>& hs2) {
240 hs1.swap(hs2);
241 }
242
243 _END_GOOGLE_NAMESPACE_
244
245 #endif /* _DENSE_HASH_SET_H_ */

  ViewVC Help
Powered by ViewVC 1.1.22