v8
V8 is Google’s open source high-performance JavaScript and WebAssembly engine, written in C++.
Loading...
Searching...
No Matches
hashmap-entry.h
Go to the documentation of this file.
1// Copyright 2016 the V8 project authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef V8_BASE_HASHMAP_ENTRY_H_
6#define V8_BASE_HASHMAP_ENTRY_H_
7
8#include <cstdint>
9#include <type_traits>
10
11#include "src/base/memory.h"
12
13namespace v8 {
14namespace base {
15
16// Marker type for hashmaps without a value (i.e. hashsets). These won't
17// allocate space for the value in the entry.
19
20// HashMap entries are (key, value, hash) triplets, with a boolean indicating if
21// they are an empty entry. Some clients may not need to use the value slot
22// (e.g. implementers of sets, where the key is the value), in which case they
23// should use NoHashMapValue.
24template <typename Key, typename Value>
26 static_assert((!std::is_same<Value, NoHashMapValue>::value));
27
28 Key key;
30 uint32_t hash; // The full hash value for key
31
32 TemplateHashMapEntry(Key key, Value value, uint32_t hash)
33 : key(key), value(value), hash(hash), exists_(true) {}
34
35 bool exists() const { return exists_; }
36
37 void clear() { exists_ = false; }
38
39 private:
40 bool exists_;
41};
42
43// Specialization for pointer-valued keys
44template <typename Key, typename Value>
46 static_assert((!std::is_same<Value, NoHashMapValue>::value));
47
48 Key* key;
50 uint32_t hash; // The full hash value for key
51
52 TemplateHashMapEntry(Key* key, Value value, uint32_t hash)
53 : key(key), value(value), hash(hash) {}
54
55 bool exists() const { return key != nullptr; }
56
57 void clear() { key = nullptr; }
58};
59
60// Specialization for Address-valued keys
61template <typename Value>
63 static_assert((!std::is_same<Value, NoHashMapValue>::value));
64
67 uint32_t hash; // The full hash value for key
68
70 : key(key), value(value), hash(hash) {}
71
72 bool exists() const { return key != -1u; }
73
74 void clear() { key = -1u; }
75};
76
77// Specialization for no value.
78template <typename Key>
80 union {
81 Key key;
82 NoHashMapValue value; // Value in union with key to not take up space.
83 };
84 uint32_t hash; // The full hash value for key
85
87 : key(key), hash(hash), exists_(true) {}
88
89 bool exists() const { return exists_; }
90
91 void clear() { exists_ = false; }
92
93 private:
94 bool exists_;
95};
96
97// Specialization for pointer-valued keys and no value.
98template <typename Key>
100 union {
101 Key* key;
102 NoHashMapValue value; // Value in union with key to not take up space.
103 };
104 uint32_t hash; // The full hash value for key
105
107 : key(key), hash(hash) {}
108
109 bool exists() const { return key != nullptr; }
110
111 void clear() { key = nullptr; }
112};
113
114} // namespace base
115} // namespace v8
116
117#endif // V8_BASE_HASHMAP_ENTRY_H_
uintptr_t Address
Definition memory.h:13
TemplateHashMapEntry(Address key, Value value, uint32_t hash)
TemplateHashMapEntry(Key key, NoHashMapValue value, uint32_t hash)
TemplateHashMapEntry(Key *key, NoHashMapValue value, uint32_t hash)
TemplateHashMapEntry(Key *key, Value value, uint32_t hash)
TemplateHashMapEntry(Key key, Value value, uint32_t hash)
std::unique_ptr< ValueMirror > key