Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame^] | 1 | // Copyright 2008 the V8 project authors. All rights reserved. |
| 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 | |
| 31 | namespace v8 { |
| 32 | namespace internal { |
| 33 | |
| 34 | |
| 35 | // Allocator defines the memory allocator interface |
| 36 | // used by HashMap and implements a default allocator. |
| 37 | class Allocator BASE_EMBEDDED { |
| 38 | public: |
| 39 | virtual ~Allocator() {} |
| 40 | virtual void* New(size_t size) { return Malloced::New(size); } |
| 41 | virtual void Delete(void* p) { Malloced::Delete(p); } |
| 42 | }; |
| 43 | |
| 44 | |
| 45 | class HashMap { |
| 46 | public: |
| 47 | static Allocator DefaultAllocator; |
| 48 | |
| 49 | typedef bool (*MatchFun) (void* key1, void* key2); |
| 50 | |
| 51 | // Dummy constructor. This constructor doesn't set up the hash |
| 52 | // map properly so don't use it unless you have good reason. |
| 53 | HashMap(); |
| 54 | |
| 55 | // initial_capacity is the size of the initial hash map; |
| 56 | // it must be a power of 2 (and thus must not be 0). |
| 57 | HashMap(MatchFun match, |
| 58 | Allocator* allocator = &DefaultAllocator, |
| 59 | uint32_t initial_capacity = 8); |
| 60 | |
| 61 | ~HashMap(); |
| 62 | |
| 63 | // HashMap entries are (key, value, hash) triplets. |
| 64 | // Some clients may not need to use the value slot |
| 65 | // (e.g. implementers of sets, where the key is the value). |
| 66 | struct Entry { |
| 67 | void* key; |
| 68 | void* value; |
| 69 | uint32_t hash; // the full hash value for key |
| 70 | }; |
| 71 | |
| 72 | // If an entry with matching key is found, Lookup() |
| 73 | // returns that entry. If no matching entry is found, |
| 74 | // but insert is set, a new entry is inserted with |
| 75 | // corresponding key, key hash, and NULL value. |
| 76 | // Otherwise, NULL is returned. |
| 77 | Entry* Lookup(void* key, uint32_t hash, bool insert); |
| 78 | |
| 79 | // Removes the entry with matching key. |
| 80 | void Remove(void* key, uint32_t hash); |
| 81 | |
| 82 | // Empties the hash map (occupancy() == 0). |
| 83 | void Clear(); |
| 84 | |
| 85 | // The number of (non-empty) entries in the table. |
| 86 | uint32_t occupancy() const { return occupancy_; } |
| 87 | |
| 88 | // The capacity of the table. The implementation |
| 89 | // makes sure that occupancy is at most 80% of |
| 90 | // the table capacity. |
| 91 | uint32_t capacity() const { return capacity_; } |
| 92 | |
| 93 | // Iteration |
| 94 | // |
| 95 | // for (Entry* p = map.Start(); p != NULL; p = map.Next(p)) { |
| 96 | // ... |
| 97 | // } |
| 98 | // |
| 99 | // If entries are inserted during iteration, the effect of |
| 100 | // calling Next() is undefined. |
| 101 | Entry* Start() const; |
| 102 | Entry* Next(Entry* p) const; |
| 103 | |
| 104 | private: |
| 105 | Allocator* allocator_; |
| 106 | MatchFun match_; |
| 107 | Entry* map_; |
| 108 | uint32_t capacity_; |
| 109 | uint32_t occupancy_; |
| 110 | |
| 111 | Entry* map_end() const { return map_ + capacity_; } |
| 112 | Entry* Probe(void* key, uint32_t hash); |
| 113 | void Initialize(uint32_t capacity); |
| 114 | void Resize(); |
| 115 | }; |
| 116 | |
| 117 | |
| 118 | } } // namespace v8::internal |
| 119 | |
| 120 | #endif // V8_HASHMAP_H_ |