00001 // Copyright 2008 the V8 project authors. All rights reserved. 00002 // Redistribution and use in source and binary forms, with or without 00003 // modification, are permitted provided that the following conditions are 00004 // met: 00005 // 00006 // * Redistributions of source code must retain the above copyright 00007 // notice, this list of conditions and the following disclaimer. 00008 // * Redistributions in binary form must reproduce the above 00009 // copyright notice, this list of conditions and the following 00010 // disclaimer in the documentation and/or other materials provided 00011 // with the distribution. 00012 // * Neither the name of Google Inc. nor the names of its 00013 // contributors may be used to endorse or promote products derived 00014 // from this software without specific prior written permission. 00015 // 00016 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 00017 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 00018 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 00019 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 00020 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 00021 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 00022 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 00023 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 00024 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 00025 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 00026 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00027 00028 #ifndef V8_HASHMAP_H_ 00029 #define V8_HASHMAP_H_ 00030 00031 namespace v8 { namespace internal { 00032 00033 00034 // Allocator defines the memory allocator interface 00035 // used by HashMap and implements a default allocator. 00036 class Allocator BASE_EMBEDDED { 00037 public: 00038 virtual ~Allocator() {} 00039 virtual void* New(size_t size) { return Malloced::New(size); } 00040 virtual void Delete(void* p) { Malloced::Delete(p); } 00041 }; 00042 00043 00044 class HashMap { 00045 public: 00046 static Allocator DefaultAllocator; 00047 00048 typedef bool (*MatchFun) (void* key1, void* key2); 00049 00050 // Dummy constructor. This constructor doesn't set up the hash 00051 // map properly so don't use it unless you have good reason. 00052 HashMap(); 00053 00054 // initial_capacity is the size of the initial hash map; 00055 // it must be a power of 2 (and thus must not be 0). 00056 HashMap(MatchFun match, 00057 Allocator* allocator = &DefaultAllocator, 00058 uint32_t initial_capacity = 8); 00059 00060 ~HashMap(); 00061 00062 // HashMap entries are (key, value, hash) tripplets. 00063 // Some clients may not need to use the value slot 00064 // (e.g. implementers of sets, where the key is the value). 00065 struct Entry { 00066 void* key; 00067 void* value; 00068 uint32_t hash; // the full hash value for key 00069 }; 00070 00071 // If an entry with matching key is found, Lookup() 00072 // returns that entry. If no matching entry is found, 00073 // but insert is set, a new entry is inserted with 00074 // corresponding key, key hash, and NULL value. 00075 // Otherwise, NULL is returned. 00076 Entry* Lookup(void* key, uint32_t hash, bool insert); 00077 00078 // Empties the hash map (occupancy() == 0). 00079 void Clear(); 00080 00081 // The number of (non-empty) entries in the table. 00082 uint32_t occupancy() const { return occupancy_; } 00083 00084 // The capacity of the table. The implementation 00085 // makes sure that occupancy is at most 80% of 00086 // the table capacity. 00087 uint32_t capacity() const { return capacity_; } 00088 00089 // Iteration 00090 // 00091 // for (Entry* p = map.Start(); p != NULL; p = map.Next(p)) { 00092 // ... 00093 // } 00094 // 00095 // If entries are inserted during iteration, the effect of 00096 // calling Next() is undefined. 00097 Entry* Start() const; 00098 Entry* Next(Entry* p) const; 00099 00100 private: 00101 Allocator* allocator_; 00102 MatchFun match_; 00103 Entry* map_; 00104 uint32_t capacity_; 00105 uint32_t occupancy_; 00106 00107 Entry* map_end() const { return map_ + capacity_; } 00108 Entry* Probe(void* key, uint32_t hash); 00109 void Initialize(uint32_t capacity); 00110 void Resize(); 00111 }; 00112 00113 00114 } } // namespace v8::internal 00115 00116 #endif // V8_HASHMAP_H_