Home | History | Annotate | Download | only in src
      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 (e.g.,
     53   // you know that the HashMap will never be used).
     54   HashMap();
     55 
     56   // initial_capacity is the size of the initial hash map;
     57   // it must be a power of 2 (and thus must not be 0).
     58   explicit HashMap(MatchFun match,
     59                    Allocator* allocator = &DefaultAllocator,
     60                    uint32_t initial_capacity = 8);
     61 
     62   ~HashMap();
     63 
     64   // HashMap entries are (key, value, hash) triplets.
     65   // Some clients may not need to use the value slot
     66   // (e.g. implementers of sets, where the key is the value).
     67   struct Entry {
     68     void* key;
     69     void* value;
     70     uint32_t hash;  // the full hash value for key
     71   };
     72 
     73   // If an entry with matching key is found, Lookup()
     74   // returns that entry. If no matching entry is found,
     75   // but insert is set, a new entry is inserted with
     76   // corresponding key, key hash, and NULL value.
     77   // Otherwise, NULL is returned.
     78   Entry* Lookup(void* key, uint32_t hash, bool insert);
     79 
     80   // Removes the entry with matching key.
     81   void Remove(void* key, uint32_t hash);
     82 
     83   // Empties the hash map (occupancy() == 0).
     84   void Clear();
     85 
     86   // The number of (non-empty) entries in the table.
     87   uint32_t occupancy() const { return occupancy_; }
     88 
     89   // The capacity of the table. The implementation
     90   // makes sure that occupancy is at most 80% of
     91   // the table capacity.
     92   uint32_t capacity() const { return capacity_; }
     93 
     94   // Iteration
     95   //
     96   // for (Entry* p = map.Start(); p != NULL; p = map.Next(p)) {
     97   //   ...
     98   // }
     99   //
    100   // If entries are inserted during iteration, the effect of
    101   // calling Next() is undefined.
    102   Entry* Start() const;
    103   Entry* Next(Entry* p) const;
    104 
    105  private:
    106   Allocator* allocator_;
    107   MatchFun match_;
    108   Entry* map_;
    109   uint32_t capacity_;
    110   uint32_t occupancy_;
    111 
    112   Entry* map_end() const { return map_ + capacity_; }
    113   Entry* Probe(void* key, uint32_t hash);
    114   void Initialize(uint32_t capacity);
    115   void Resize();
    116 };
    117 
    118 
    119 } }  // namespace v8::internal
    120 
    121 #endif  // V8_HASHMAP_H_
    122