Commit d5040c43 authored by Sathya Gunasekaran's avatar Sathya Gunasekaran Committed by Commit Bot

[collections] Add OrderedHashTable::Delete

Bug: v8:5717
Change-Id: Icc601c409ac79195991facf1cb2027aab6145ff8
Reviewed-on: https://chromium-review.googlesource.com/540659
Commit-Queue: Sathya Gunasekaran <gsathya@chromium.org>
Reviewed-by: 's avatarCamillo Bruni <cbruni@chromium.org>
Cr-Commit-Position: refs/heads/master@{#46066}
parent da607264
......@@ -18158,14 +18158,9 @@ Object* OrderedHashTable<Derived, entrysize>::HasKey(Isolate* isolate,
Object* key) {
DCHECK(table->IsOrderedHashTable());
DisallowHeapAllocation no_gc;
int entry = table->KeyToFirstEntry(isolate, key);
// Walk the chain in the bucket to find the key.
while (entry != kNotFound) {
Object* candidate_key = table->KeyAt(entry);
if (candidate_key->SameValueZero(key)) return isolate->heap()->true_value();
entry = table->NextChainEntry(entry);
}
return isolate->heap()->false_value();
int entry = table->FindEntry(isolate, key);
return entry == kNotFound ? isolate->heap()->false_value()
: isolate->heap()->true_value();
}
Handle<OrderedHashSet> OrderedHashSet::Add(Handle<OrderedHashSet> table,
......@@ -18267,20 +18262,35 @@ Handle<Derived> OrderedHashTable<Derived, entrysize>::Rehash(
return new_table;
}
template <class Derived, int entrysize>
bool OrderedHashTable<Derived, entrysize>::Delete(Isolate* isolate,
Derived* table, Object* key) {
DisallowHeapAllocation no_gc;
int entry = table->FindEntry(isolate, key);
if (entry == kNotFound) return false;
int nof = table->NumberOfElements();
int nod = table->NumberOfDeletedElements();
int index = table->EntryToIndex(entry);
Object* hole = isolate->heap()->the_hole_value();
for (int i = 0; i < entrysize; ++i) {
table->set(index + i, hole);
}
table->SetNumberOfElements(nof - 1);
table->SetNumberOfDeletedElements(nod + 1);
return true;
}
Object* OrderedHashMap::Get(Isolate* isolate, OrderedHashMap* table,
Object* key) {
DCHECK(table->IsOrderedHashMap());
DisallowHeapAllocation no_gc;
int entry = table->KeyToFirstEntry(isolate, key);
// Walk the chain in the bucket to find the key.
while (entry != kNotFound) {
Object* candidate_key = table->KeyAt(entry);
if (candidate_key->SameValueZero(key)) {
return table->ValueAt(entry);
}
entry = table->NextChainEntry(entry);
}
return isolate->heap()->undefined_value();
int entry = table->FindEntry(isolate, key);
if (entry == kNotFound) return isolate->heap()->undefined_value();
return table->ValueAt(entry);
}
Handle<OrderedHashMap> OrderedHashMap::Add(Handle<OrderedHashMap> table,
......@@ -18332,6 +18342,10 @@ template Handle<OrderedHashSet> OrderedHashTable<OrderedHashSet, 1>::Clear(
template Object* OrderedHashTable<OrderedHashSet, 1>::HasKey(
Isolate* isolate, OrderedHashSet* table, Object* key);
template bool OrderedHashTable<OrderedHashSet, 1>::Delete(Isolate* isolate,
OrderedHashSet* table,
Object* key);
template Handle<OrderedHashMap> OrderedHashTable<OrderedHashMap, 2>::Allocate(
Isolate* isolate, int capacity, PretenureFlag pretenure);
......@@ -18347,6 +18361,10 @@ template Handle<OrderedHashMap> OrderedHashTable<OrderedHashMap, 2>::Clear(
template Object* OrderedHashTable<OrderedHashMap, 2>::HasKey(
Isolate* isolate, OrderedHashMap* table, Object* key);
template bool OrderedHashTable<OrderedHashMap, 2>::Delete(Isolate* isolate,
OrderedHashMap* table,
Object* key);
template <>
Handle<SmallOrderedHashSet>
SmallOrderedHashTable<SmallOrderedHashSet>::Allocate(Isolate* isolate,
......
......@@ -410,6 +410,10 @@ class OrderedHashTable : public FixedArray {
// existing iterators can be updated.
static Handle<Derived> Clear(Handle<Derived> table);
// Returns a true value if the OrderedHashTable contains the key and
// the key has been deleted. This does not shrink the table.
static bool Delete(Isolate* isolate, Derived* table, Object* key);
// Returns a true value if the OrderedHashTable contains the key
static Object* HasKey(Isolate* isolate, Derived* table, Object* key);
......@@ -457,6 +461,18 @@ class OrderedHashTable : public FixedArray {
return HashToEntry(Smi::cast(hash)->value());
}
int FindEntry(Isolate* isolate, Object* key) {
int entry = KeyToFirstEntry(isolate, key);
// Walk the chain in the bucket to find the key.
while (entry != kNotFound) {
Object* candidate_key = KeyAt(entry);
if (candidate_key->SameValueZero(key)) break;
entry = NextChainEntry(entry);
}
return entry;
}
int NextChainEntry(int entry) {
Object* next_entry = get(EntryToIndex(entry) + kChainOffset);
return Smi::cast(next_entry)->value();
......
This diff is collapsed.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment