blob: cf4705954ce8ef66e82e84ef1b3e00b9bf59b873 [file] [log] [blame]
Jeff Browne735f232011-11-14 18:29:15 -08001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef ANDROID_BASIC_HASHTABLE_H
18#define ANDROID_BASIC_HASHTABLE_H
19
20#include <stdint.h>
21#include <sys/types.h>
Jeff Browne735f232011-11-14 18:29:15 -080022#include <utils/TypeHelpers.h>
23
24namespace android {
25
26/* Implementation type. Nothing to see here. */
27class BasicHashtableImpl {
28protected:
29 struct Bucket {
30 // The collision flag indicates that the bucket is part of a collision chain
31 // such that at least two entries both hash to this bucket. When true, we
32 // may need to seek further along the chain to find the entry.
33 static const uint32_t COLLISION = 0x80000000UL;
34
35 // The present flag indicates that the bucket contains an initialized entry value.
36 static const uint32_t PRESENT = 0x40000000UL;
37
38 // Mask for 30 bits worth of the hash code that are stored within the bucket to
39 // speed up lookups and rehashing by eliminating the need to recalculate the
40 // hash code of the entry's key.
41 static const uint32_t HASH_MASK = 0x3fffffffUL;
42
43 // Combined value that stores the collision and present flags as well as
44 // a 30 bit hash code.
45 uint32_t cookie;
46
47 // Storage for the entry begins here.
48 char entry[0];
49 };
50
51 BasicHashtableImpl(size_t entrySize, bool hasTrivialDestructor,
52 size_t minimumInitialCapacity, float loadFactor);
53 BasicHashtableImpl(const BasicHashtableImpl& other);
Alex Ray0d8f3d62013-07-17 16:57:21 -070054 virtual ~BasicHashtableImpl();
Jeff Browne735f232011-11-14 18:29:15 -080055
56 void dispose();
Sergio Girod2529f22015-09-23 16:22:59 +010057 void edit();
Jeff Browne735f232011-11-14 18:29:15 -080058 void setTo(const BasicHashtableImpl& other);
59 void clear();
60
61 ssize_t next(ssize_t index) const;
62 ssize_t find(ssize_t index, hash_t hash, const void* __restrict__ key) const;
63 size_t add(hash_t hash, const void* __restrict__ entry);
64 void removeAt(size_t index);
65 void rehash(size_t minimumCapacity, float loadFactor);
66
67 const size_t mBucketSize; // number of bytes per bucket including the entry
68 const bool mHasTrivialDestructor; // true if the entry type does not require destruction
69 size_t mCapacity; // number of buckets that can be filled before exceeding load factor
70 float mLoadFactor; // load factor
71 size_t mSize; // number of elements actually in the table
72 size_t mFilledBuckets; // number of buckets for which collision or present is true
73 size_t mBucketCount; // number of slots in the mBuckets array
74 void* mBuckets; // array of buckets, as a SharedBuffer
75
76 inline const Bucket& bucketAt(const void* __restrict__ buckets, size_t index) const {
77 return *reinterpret_cast<const Bucket*>(
78 static_cast<const uint8_t*>(buckets) + index * mBucketSize);
79 }
80
81 inline Bucket& bucketAt(void* __restrict__ buckets, size_t index) const {
82 return *reinterpret_cast<Bucket*>(static_cast<uint8_t*>(buckets) + index * mBucketSize);
83 }
84
85 virtual bool compareBucketKey(const Bucket& bucket, const void* __restrict__ key) const = 0;
86 virtual void initializeBucketEntry(Bucket& bucket, const void* __restrict__ entry) const = 0;
87 virtual void destroyBucketEntry(Bucket& bucket) const = 0;
88
89private:
90 void clone();
91
92 // Allocates a bucket array as a SharedBuffer.
93 void* allocateBuckets(size_t count) const;
94
95 // Releases a bucket array's associated SharedBuffer.
96 void releaseBuckets(void* __restrict__ buckets, size_t count) const;
97
98 // Destroys the contents of buckets (invokes destroyBucketEntry for each
99 // populated bucket if needed).
100 void destroyBuckets(void* __restrict__ buckets, size_t count) const;
101
102 // Copies the content of buckets (copies the cookie and invokes copyBucketEntry
103 // for each populated bucket if needed).
104 void copyBuckets(const void* __restrict__ fromBuckets,
105 void* __restrict__ toBuckets, size_t count) const;
106
107 // Determines the appropriate size of a bucket array to store a certain minimum
108 // number of entries and returns its effective capacity.
109 static void determineCapacity(size_t minimumCapacity, float loadFactor,
110 size_t* __restrict__ outBucketCount, size_t* __restrict__ outCapacity);
111
112 // Trim a hash code to 30 bits to match what we store in the bucket's cookie.
113 inline static hash_t trimHash(hash_t hash) {
114 return (hash & Bucket::HASH_MASK) ^ (hash >> 30);
115 }
116
117 // Returns the index of the first bucket that is in the collision chain
118 // for the specified hash code, given the total number of buckets.
119 // (Primary hash)
120 inline static size_t chainStart(hash_t hash, size_t count) {
121 return hash % count;
122 }
123
124 // Returns the increment to add to a bucket index to seek to the next bucket
125 // in the collision chain for the specified hash code, given the total number of buckets.
126 // (Secondary hash)
127 inline static size_t chainIncrement(hash_t hash, size_t count) {
128 return ((hash >> 7) | (hash << 25)) % (count - 1) + 1;
129 }
130
131 // Returns the index of the next bucket that is in the collision chain
132 // that is defined by the specified increment, given the total number of buckets.
133 inline static size_t chainSeek(size_t index, size_t increment, size_t count) {
134 return (index + increment) % count;
135 }
136};
137
138/*
139 * A BasicHashtable stores entries that are indexed by hash code in place
140 * within an array. The basic operations are finding entries by key,
141 * adding new entries and removing existing entries.
142 *
143 * This class provides a very limited set of operations with simple semantics.
144 * It is intended to be used as a building block to construct more complex
145 * and interesting data structures such as HashMap. Think very hard before
146 * adding anything extra to BasicHashtable, it probably belongs at a
147 * higher level of abstraction.
148 *
149 * TKey: The key type.
150 * TEntry: The entry type which is what is actually stored in the array.
151 *
152 * TKey must support the following contract:
153 * bool operator==(const TKey& other) const; // return true if equal
154 * bool operator!=(const TKey& other) const; // return true if unequal
155 *
156 * TEntry must support the following contract:
157 * const TKey& getKey() const; // get the key from the entry
158 *
159 * This class supports storing entries with duplicate keys. Of course, it can't
160 * tell them apart during removal so only the first entry will be removed.
161 * We do this because it means that operations like add() can't fail.
162 */
163template <typename TKey, typename TEntry>
164class BasicHashtable : private BasicHashtableImpl {
165public:
166 /* Creates a hashtable with the specified minimum initial capacity.
167 * The underlying array will be created when the first entry is added.
168 *
169 * minimumInitialCapacity: The minimum initial capacity for the hashtable.
170 * Default is 0.
171 * loadFactor: The desired load factor for the hashtable, between 0 and 1.
172 * Default is 0.75.
173 */
174 BasicHashtable(size_t minimumInitialCapacity = 0, float loadFactor = 0.75f);
175
176 /* Copies a hashtable.
177 * The underlying storage is shared copy-on-write.
178 */
179 BasicHashtable(const BasicHashtable& other);
180
181 /* Clears and destroys the hashtable.
182 */
183 virtual ~BasicHashtable();
184
185 /* Making this hashtable a copy of the other hashtable.
186 * The underlying storage is shared copy-on-write.
187 *
188 * other: The hashtable to copy.
189 */
190 inline BasicHashtable<TKey, TEntry>& operator =(const BasicHashtable<TKey, TEntry> & other) {
191 setTo(other);
192 return *this;
193 }
194
195 /* Returns the number of entries in the hashtable.
196 */
197 inline size_t size() const {
198 return mSize;
199 }
200
201 /* Returns the capacity of the hashtable, which is the number of elements that can
202 * added to the hashtable without requiring it to be grown.
203 */
204 inline size_t capacity() const {
205 return mCapacity;
206 }
207
208 /* Returns the number of buckets that the hashtable has, which is the size of its
209 * underlying array.
210 */
211 inline size_t bucketCount() const {
212 return mBucketCount;
213 }
214
215 /* Returns the load factor of the hashtable. */
216 inline float loadFactor() const {
217 return mLoadFactor;
218 };
219
220 /* Returns a const reference to the entry at the specified index.
221 *
222 * index: The index of the entry to retrieve. Must be a valid index within
223 * the bounds of the hashtable.
224 */
225 inline const TEntry& entryAt(size_t index) const {
226 return entryFor(bucketAt(mBuckets, index));
227 }
228
229 /* Returns a non-const reference to the entry at the specified index.
230 *
231 * index: The index of the entry to edit. Must be a valid index within
232 * the bounds of the hashtable.
233 */
234 inline TEntry& editEntryAt(size_t index) {
235 edit();
236 return entryFor(bucketAt(mBuckets, index));
237 }
238
239 /* Clears the hashtable.
240 * All entries in the hashtable are destroyed immediately.
241 * If you need to do something special with the entries in the hashtable then iterate
242 * over them and do what you need before clearing the hashtable.
243 */
244 inline void clear() {
245 BasicHashtableImpl::clear();
246 }
247
248 /* Returns the index of the next entry in the hashtable given the index of a previous entry.
249 * If the given index is -1, then returns the index of the first entry in the hashtable,
250 * if there is one, or -1 otherwise.
251 * If the given index is not -1, then returns the index of the next entry in the hashtable,
252 * in strictly increasing order, or -1 if there are none left.
253 *
254 * index: The index of the previous entry that was iterated, or -1 to begin
255 * iteration at the beginning of the hashtable.
256 */
257 inline ssize_t next(ssize_t index) const {
258 return BasicHashtableImpl::next(index);
259 }
260
261 /* Finds the index of an entry with the specified key.
262 * If the given index is -1, then returns the index of the first matching entry,
263 * otherwise returns the index of the next matching entry.
264 * If the hashtable contains multiple entries with keys that match the requested
265 * key, then the sequence of entries returned is arbitrary.
266 * Returns -1 if no entry was found.
267 *
268 * index: The index of the previous entry with the specified key, or -1 to
269 * find the first matching entry.
270 * hash: The hashcode of the key.
271 * key: The key.
272 */
273 inline ssize_t find(ssize_t index, hash_t hash, const TKey& key) const {
274 return BasicHashtableImpl::find(index, hash, &key);
275 }
276
277 /* Adds the entry to the hashtable.
278 * Returns the index of the newly added entry.
279 * If an entry with the same key already exists, then a duplicate entry is added.
280 * If the entry will not fit, then the hashtable's capacity is increased and
281 * its contents are rehashed. See rehash().
282 *
283 * hash: The hashcode of the key.
284 * entry: The entry to add.
285 */
286 inline size_t add(hash_t hash, const TEntry& entry) {
287 return BasicHashtableImpl::add(hash, &entry);
288 }
289
290 /* Removes the entry with the specified index from the hashtable.
291 * The entry is destroyed immediately.
292 * The index must be valid.
293 *
294 * The hashtable is not compacted after an item is removed, so it is legal
295 * to continue iterating over the hashtable using next() or find().
296 *
297 * index: The index of the entry to remove. Must be a valid index within the
298 * bounds of the hashtable, and it must refer to an existing entry.
299 */
300 inline void removeAt(size_t index) {
301 BasicHashtableImpl::removeAt(index);
302 }
303
304 /* Rehashes the contents of the hashtable.
305 * Grows the hashtable to at least the specified minimum capacity or the
306 * current number of elements, whichever is larger.
307 *
308 * Rehashing causes all entries to be copied and the entry indices may change.
309 * Although the hash codes are cached by the hashtable, rehashing can be an
310 * expensive operation and should be avoided unless the hashtable's size
311 * needs to be changed.
312 *
313 * Rehashing is the only way to change the capacity or load factor of the
314 * hashtable once it has been created. It can be used to compact the
315 * hashtable by choosing a minimum capacity that is smaller than the current
316 * capacity (such as 0).
317 *
318 * minimumCapacity: The desired minimum capacity after rehashing.
319 * loadFactor: The desired load factor after rehashing.
320 */
321 inline void rehash(size_t minimumCapacity, float loadFactor) {
322 BasicHashtableImpl::rehash(minimumCapacity, loadFactor);
323 }
324
Raph Levienb6ea1752012-10-25 23:11:13 -0700325 /* Determines whether there is room to add another entry without rehashing.
326 * When this returns true, a subsequent add() operation is guaranteed to
327 * complete without performing a rehash.
328 */
329 inline bool hasMoreRoom() const {
330 return mCapacity > mFilledBuckets;
331 }
332
Jeff Browne735f232011-11-14 18:29:15 -0800333protected:
334 static inline const TEntry& entryFor(const Bucket& bucket) {
335 return reinterpret_cast<const TEntry&>(bucket.entry);
336 }
337
338 static inline TEntry& entryFor(Bucket& bucket) {
339 return reinterpret_cast<TEntry&>(bucket.entry);
340 }
341
342 virtual bool compareBucketKey(const Bucket& bucket, const void* __restrict__ key) const;
343 virtual void initializeBucketEntry(Bucket& bucket, const void* __restrict__ entry) const;
344 virtual void destroyBucketEntry(Bucket& bucket) const;
345
346private:
347 // For dumping the raw contents of a hashtable during testing.
348 friend class BasicHashtableTest;
349 inline uint32_t cookieAt(size_t index) const {
350 return bucketAt(mBuckets, index).cookie;
351 }
352};
353
354template <typename TKey, typename TEntry>
355BasicHashtable<TKey, TEntry>::BasicHashtable(size_t minimumInitialCapacity, float loadFactor) :
356 BasicHashtableImpl(sizeof(TEntry), traits<TEntry>::has_trivial_dtor,
357 minimumInitialCapacity, loadFactor) {
358}
359
360template <typename TKey, typename TEntry>
361BasicHashtable<TKey, TEntry>::BasicHashtable(const BasicHashtable<TKey, TEntry>& other) :
362 BasicHashtableImpl(other) {
363}
364
365template <typename TKey, typename TEntry>
366BasicHashtable<TKey, TEntry>::~BasicHashtable() {
367 dispose();
368}
369
370template <typename TKey, typename TEntry>
371bool BasicHashtable<TKey, TEntry>::compareBucketKey(const Bucket& bucket,
372 const void* __restrict__ key) const {
373 return entryFor(bucket).getKey() == *static_cast<const TKey*>(key);
374}
375
376template <typename TKey, typename TEntry>
377void BasicHashtable<TKey, TEntry>::initializeBucketEntry(Bucket& bucket,
378 const void* __restrict__ entry) const {
379 if (!traits<TEntry>::has_trivial_copy) {
380 new (&entryFor(bucket)) TEntry(*(static_cast<const TEntry*>(entry)));
381 } else {
382 memcpy(&entryFor(bucket), entry, sizeof(TEntry));
383 }
384}
385
386template <typename TKey, typename TEntry>
387void BasicHashtable<TKey, TEntry>::destroyBucketEntry(Bucket& bucket) const {
388 if (!traits<TEntry>::has_trivial_dtor) {
389 entryFor(bucket).~TEntry();
390 }
391}
392
393}; // namespace android
394
395#endif // ANDROID_BASIC_HASHTABLE_H