fschneider@chromium.org | fb144a0 | 2011-05-04 12:43:48 +0000 | [diff] [blame^] | 1 | // Copyright 2011 the V8 project authors. All rights reserved. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2 | // Redistribution and use in source and binary forms, with or without |
| 3 | // modification, are permitted provided that the following conditions are |
| 4 | // met: |
| 5 | // |
| 6 | // * Redistributions of source code must retain the above copyright |
| 7 | // notice, this list of conditions and the following disclaimer. |
| 8 | // * Redistributions in binary form must reproduce the above |
| 9 | // copyright notice, this list of conditions and the following |
| 10 | // disclaimer in the documentation and/or other materials provided |
| 11 | // with the distribution. |
| 12 | // * Neither the name of Google Inc. nor the names of its |
| 13 | // contributors may be used to endorse or promote products derived |
| 14 | // from this software without specific prior written permission. |
| 15 | // |
| 16 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 17 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 18 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 19 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 20 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 21 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 22 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 23 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 24 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 25 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 26 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 | |
| 28 | #ifndef V8_HASHMAP_H_ |
| 29 | #define V8_HASHMAP_H_ |
| 30 | |
fschneider@chromium.org | fb144a0 | 2011-05-04 12:43:48 +0000 | [diff] [blame^] | 31 | #include "allocation.h" |
| 32 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 33 | namespace v8 { |
| 34 | namespace internal { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 35 | |
| 36 | |
| 37 | // Allocator defines the memory allocator interface |
| 38 | // used by HashMap and implements a default allocator. |
| 39 | class Allocator BASE_EMBEDDED { |
| 40 | public: |
| 41 | virtual ~Allocator() {} |
| 42 | virtual void* New(size_t size) { return Malloced::New(size); } |
| 43 | virtual void Delete(void* p) { Malloced::Delete(p); } |
| 44 | }; |
| 45 | |
| 46 | |
| 47 | class HashMap { |
| 48 | public: |
| 49 | static Allocator DefaultAllocator; |
| 50 | |
| 51 | typedef bool (*MatchFun) (void* key1, void* key2); |
| 52 | |
| 53 | // Dummy constructor. This constructor doesn't set up the hash |
kmillikin@chromium.org | 31b1277 | 2011-02-02 16:08:26 +0000 | [diff] [blame] | 54 | // map properly so don't use it unless you have good reason (e.g., |
| 55 | // you know that the HashMap will never be used). |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 56 | HashMap(); |
| 57 | |
| 58 | // initial_capacity is the size of the initial hash map; |
| 59 | // it must be a power of 2 (and thus must not be 0). |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 60 | explicit HashMap(MatchFun match, |
| 61 | Allocator* allocator = &DefaultAllocator, |
| 62 | uint32_t initial_capacity = 8); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 63 | |
| 64 | ~HashMap(); |
| 65 | |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 66 | // HashMap entries are (key, value, hash) triplets. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 67 | // Some clients may not need to use the value slot |
| 68 | // (e.g. implementers of sets, where the key is the value). |
| 69 | struct Entry { |
| 70 | void* key; |
| 71 | void* value; |
| 72 | uint32_t hash; // the full hash value for key |
| 73 | }; |
| 74 | |
| 75 | // If an entry with matching key is found, Lookup() |
| 76 | // returns that entry. If no matching entry is found, |
| 77 | // but insert is set, a new entry is inserted with |
| 78 | // corresponding key, key hash, and NULL value. |
| 79 | // Otherwise, NULL is returned. |
| 80 | Entry* Lookup(void* key, uint32_t hash, bool insert); |
| 81 | |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 82 | // Removes the entry with matching key. |
| 83 | void Remove(void* key, uint32_t hash); |
| 84 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 85 | // Empties the hash map (occupancy() == 0). |
| 86 | void Clear(); |
| 87 | |
| 88 | // The number of (non-empty) entries in the table. |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 89 | uint32_t occupancy() const { return occupancy_; } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 90 | |
| 91 | // The capacity of the table. The implementation |
| 92 | // makes sure that occupancy is at most 80% of |
| 93 | // the table capacity. |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 94 | uint32_t capacity() const { return capacity_; } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 95 | |
| 96 | // Iteration |
| 97 | // |
| 98 | // for (Entry* p = map.Start(); p != NULL; p = map.Next(p)) { |
| 99 | // ... |
| 100 | // } |
| 101 | // |
| 102 | // If entries are inserted during iteration, the effect of |
| 103 | // calling Next() is undefined. |
| 104 | Entry* Start() const; |
| 105 | Entry* Next(Entry* p) const; |
| 106 | |
| 107 | private: |
| 108 | Allocator* allocator_; |
| 109 | MatchFun match_; |
| 110 | Entry* map_; |
| 111 | uint32_t capacity_; |
| 112 | uint32_t occupancy_; |
| 113 | |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 114 | Entry* map_end() const { return map_ + capacity_; } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 115 | Entry* Probe(void* key, uint32_t hash); |
| 116 | void Initialize(uint32_t capacity); |
| 117 | void Resize(); |
| 118 | }; |
| 119 | |
| 120 | |
| 121 | } } // namespace v8::internal |
| 122 | |
| 123 | #endif // V8_HASHMAP_H_ |