blob: 5c13212eba138589c2b213b66fa65973b3a5a09e [file] [log] [blame]
Ben Murdoch257744e2011-11-30 15:57:28 +00001// Copyright 2011 the V8 project authors. All rights reserved.
Steve Blocka7e24c12009-10-30 11:49:00 +00002// 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
Ben Murdoch257744e2011-11-30 15:57:28 +000031#include "allocation.h"
32
Steve Blocka7e24c12009-10-30 11:49:00 +000033namespace v8 {
34namespace internal {
35
36
37// Allocator defines the memory allocator interface
38// used by HashMap and implements a default allocator.
39class 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
47class 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
Steve Block1e0659c2011-05-24 12:43:12 +010054 // map properly so don't use it unless you have good reason (e.g.,
55 // you know that the HashMap will never be used).
Steve Blocka7e24c12009-10-30 11:49:00 +000056 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).
Steve Block44f0eee2011-05-26 01:26:41 +010060 explicit HashMap(MatchFun match,
61 Allocator* allocator = &DefaultAllocator,
62 uint32_t initial_capacity = 8);
Steve Blocka7e24c12009-10-30 11:49:00 +000063
64 ~HashMap();
65
66 // HashMap entries are (key, value, hash) triplets.
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
82 // Removes the entry with matching key.
83 void Remove(void* key, uint32_t hash);
84
85 // Empties the hash map (occupancy() == 0).
86 void Clear();
87
88 // The number of (non-empty) entries in the table.
Kristian Monsen0d5e1162010-09-30 15:31:59 +010089 uint32_t occupancy() const { return occupancy_; }
Steve Blocka7e24c12009-10-30 11:49:00 +000090
91 // The capacity of the table. The implementation
92 // makes sure that occupancy is at most 80% of
93 // the table capacity.
Kristian Monsen0d5e1162010-09-30 15:31:59 +010094 uint32_t capacity() const { return capacity_; }
Steve Blocka7e24c12009-10-30 11:49:00 +000095
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
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100114 Entry* map_end() const { return map_ + capacity_; }
Steve Blocka7e24c12009-10-30 11:49:00 +0000115 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_