The data contained in this repository can be downloaded to your computer using one of several clients.
Please see the documentation of your version control software client for more information.

Please select the desired protocol below to get the URL.

This URL has Read-Only access.

Statistics
| Branch: | Revision:

main_repo / deps / v8 / src / hashmap.h @ 40c0f755

History | View | Annotate | Download (3.91 KB)

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 { namespace internal {
32

    
33

    
34
// Allocator defines the memory allocator interface
35
// used by HashMap and implements a default allocator.
36
class Allocator BASE_EMBEDDED {
37
 public:
38
  virtual ~Allocator()  {}
39
  virtual void* New(size_t size)  { return Malloced::New(size); }
40
  virtual void Delete(void* p)  { Malloced::Delete(p); }
41
};
42

    
43

    
44
class HashMap {
45
 public:
46
  static Allocator DefaultAllocator;
47

    
48
  typedef bool (*MatchFun) (void* key1, void* key2);
49

    
50
  // Dummy constructor.  This constructor doesn't set up the hash
51
  // map properly so don't use it unless you have good reason.
52
  HashMap();
53

    
54
  // initial_capacity is the size of the initial hash map;
55
  // it must be a power of 2 (and thus must not be 0).
56
  HashMap(MatchFun match,
57
          Allocator* allocator = &DefaultAllocator,
58
          uint32_t initial_capacity = 8);
59

    
60
  ~HashMap();
61

    
62
  // HashMap entries are (key, value, hash) triplets.
63
  // Some clients may not need to use the value slot
64
  // (e.g. implementers of sets, where the key is the value).
65
  struct Entry {
66
    void* key;
67
    void* value;
68
    uint32_t hash;  // the full hash value for key
69
  };
70

    
71
  // If an entry with matching key is found, Lookup()
72
  // returns that entry. If no matching entry is found,
73
  // but insert is set, a new entry is inserted with
74
  // corresponding key, key hash, and NULL value.
75
  // Otherwise, NULL is returned.
76
  Entry* Lookup(void* key, uint32_t hash, bool insert);
77

    
78
  // Empties the hash map (occupancy() == 0).
79
  void Clear();
80

    
81
  // The number of (non-empty) entries in the table.
82
  uint32_t occupancy() const  { return occupancy_; }
83

    
84
  // The capacity of the table. The implementation
85
  // makes sure that occupancy is at most 80% of
86
  // the table capacity.
87
  uint32_t capacity() const  { return capacity_; }
88

    
89
  // Iteration
90
  //
91
  // for (Entry* p = map.Start(); p != NULL; p = map.Next(p)) {
92
  //   ...
93
  // }
94
  //
95
  // If entries are inserted during iteration, the effect of
96
  // calling Next() is undefined.
97
  Entry* Start() const;
98
  Entry* Next(Entry* p) const;
99

    
100
 private:
101
  Allocator* allocator_;
102
  MatchFun match_;
103
  Entry* map_;
104
  uint32_t capacity_;
105
  uint32_t occupancy_;
106

    
107
  Entry* map_end() const  { return map_ + capacity_; }
108
  Entry* Probe(void* key, uint32_t hash);
109
  void Initialize(uint32_t capacity);
110
  void Resize();
111
};
112

    
113

    
114
} }  // namespace v8::internal
115

    
116
#endif  // V8_HASHMAP_H_