5 #ifndef V8_OBJECTS_HASH_TABLE_INL_H_ 6 #define V8_OBJECTS_HASH_TABLE_INL_H_ 8 #include "src/objects/hash-table.h" 10 #include "src/heap/heap.h" 11 #include "src/objects/fixed-array-inl.h" 12 #include "src/roots-inl.h" 15 #include "src/objects/object-macros.h" 20 int HashTableBase::NumberOfElements()
const {
21 return Smi::ToInt(
get(kNumberOfElementsIndex));
24 int HashTableBase::NumberOfDeletedElements()
const {
25 return Smi::ToInt(
get(kNumberOfDeletedElementsIndex));
28 int HashTableBase::Capacity()
const {
return Smi::ToInt(
get(kCapacityIndex)); }
30 void HashTableBase::ElementAdded() {
31 SetNumberOfElements(NumberOfElements() + 1);
34 void HashTableBase::ElementRemoved() {
35 SetNumberOfElements(NumberOfElements() - 1);
36 SetNumberOfDeletedElements(NumberOfDeletedElements() + 1);
39 void HashTableBase::ElementsRemoved(
int n) {
40 SetNumberOfElements(NumberOfElements() - n);
41 SetNumberOfDeletedElements(NumberOfDeletedElements() + n);
45 int HashTableBase::ComputeCapacity(
int at_least_space_for) {
49 int raw_cap = at_least_space_for + (at_least_space_for >> 1);
50 int capacity = base::bits::RoundUpToPowerOfTwo32(raw_cap);
51 return Max(capacity, kMinCapacity);
54 void HashTableBase::SetNumberOfElements(
int nof) {
55 set(kNumberOfElementsIndex, Smi::FromInt(nof));
58 void HashTableBase::SetNumberOfDeletedElements(
int nod) {
59 set(kNumberOfDeletedElementsIndex, Smi::FromInt(nod));
62 template <
typename Key>
63 RootIndex BaseShape<Key>::GetMapRootIndex() {
64 return RootIndex::kHashTableMap;
67 RootIndex EphemeronHashTableShape::GetMapRootIndex() {
68 return RootIndex::kEphemeronHashTableMap;
71 template <
typename Derived,
typename Shape>
72 int HashTable<Derived, Shape>::FindEntry(Isolate* isolate, Key key) {
73 return FindEntry(ReadOnlyRoots(isolate), key, Shape::Hash(isolate, key));
77 template <
typename Derived,
typename Shape>
78 int HashTable<Derived, Shape>::FindEntry(ReadOnlyRoots roots, Key key,
81 uint32_t entry = FirstProbe(hash, capacity);
84 Object* undefined = roots.undefined_value();
85 Object* the_hole = roots.the_hole_value();
88 Object* element = KeyAt(entry);
91 if (element == undefined)
break;
92 if (!(Shape::kNeedsHoleCheck && the_hole == element)) {
93 if (Shape::IsMatch(key, element))
return entry;
95 entry = NextProbe(entry, count++, capacity);
100 template <
typename Derived,
typename Shape>
101 bool HashTable<Derived, Shape>::IsKey(ReadOnlyRoots roots, Object* k) {
102 return Shape::IsKey(roots, k);
105 template <
typename Derived,
typename Shape>
106 bool HashTable<Derived, Shape>::ToKey(ReadOnlyRoots roots,
int entry,
108 Object* k = KeyAt(entry);
109 if (!IsKey(roots, k))
return false;
110 *out_k = Shape::Unwrap(k);
114 template <
typename KeyT>
115 bool BaseShape<KeyT>::IsKey(ReadOnlyRoots roots, Object* key) {
116 return IsLive(roots, key);
119 template <
typename KeyT>
120 bool BaseShape<KeyT>::IsLive(ReadOnlyRoots roots, Object* k) {
121 return k != roots.the_hole_value() && k != roots.undefined_value();
124 bool ObjectHashSet::Has(Isolate* isolate, Handle<Object> key, int32_t hash) {
125 return FindEntry(ReadOnlyRoots(isolate), key, hash) != kNotFound;
128 bool ObjectHashSet::Has(Isolate* isolate, Handle<Object> key) {
129 Object* hash = key->GetHash();
130 if (!hash->IsSmi())
return false;
131 return FindEntry(ReadOnlyRoots(isolate), key, Smi::ToInt(hash)) != kNotFound;
137 #include "src/objects/object-macros-undef.h" 139 #endif // V8_OBJECTS_HASH_TABLE_INL_H_