mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-12-04 13:30:31 +00:00
Add HashTable::remove() and fix a bug where ConstIterator would skip the first.
This commit is contained in:
parent
f794190de0
commit
c2ef54c044
Notes:
sideshowbarker
2024-07-19 18:49:17 +09:00
Author: https://github.com/awesomekling Commit: https://github.com/SerenityOS/serenity/commit/c2ef54c0449
2 changed files with 48 additions and 3 deletions
|
@ -186,12 +186,12 @@ public:
|
|||
{
|
||||
if (!isEnd && !m_table.isEmpty() && !(m_bucketIterator != DoublyLinkedList<T>::ConstIterator::universalEnd())) {
|
||||
#ifdef HASHTABLE_DEBUG
|
||||
printf("bucket iterator init!\n");
|
||||
printf("const bucket iterator init!\n");
|
||||
#endif
|
||||
const DoublyLinkedList<T>& chain = m_table.m_buckets[0].chain;
|
||||
m_bucketIterator = chain.begin();
|
||||
|
||||
skipToNext();
|
||||
if (m_bucketIterator.isEnd())
|
||||
skipToNext();
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -207,6 +207,15 @@ public:
|
|||
Iterator find(const T&);
|
||||
ConstIterator find(const T&) const;
|
||||
|
||||
void remove(const T& value)
|
||||
{
|
||||
auto it = find(value);
|
||||
if (it != end())
|
||||
remove(it);
|
||||
}
|
||||
|
||||
void remove(Iterator&);
|
||||
|
||||
private:
|
||||
Bucket& lookup(const T&);
|
||||
const Bucket& lookup(const T&) const;
|
||||
|
@ -315,6 +324,14 @@ auto HashTable<T, TraitsForT>::find(const T& value) const -> ConstIterator
|
|||
return end();
|
||||
}
|
||||
|
||||
template<typename T, typename TraitsForT>
|
||||
void HashTable<T, TraitsForT>::remove(Iterator& it)
|
||||
{
|
||||
ASSERT(!isEmpty());
|
||||
m_buckets[it.m_bucketIndex].chain.remove(it.m_bucketIterator);
|
||||
--m_size;
|
||||
}
|
||||
|
||||
template<typename T, typename TraitsForT>
|
||||
typename HashTable<T, TraitsForT>::Bucket& HashTable<T, TraitsForT>::lookup(const T& value)
|
||||
{
|
||||
|
|
28
AK/test.cpp
28
AK/test.cpp
|
@ -155,5 +155,33 @@ int main(int, char**)
|
|||
printInts(v);
|
||||
}
|
||||
|
||||
{
|
||||
auto printInts = [] (const HashTable<int>& h) {
|
||||
printf("HashTable {\n size: %u\n capacity: %u\n elements: ", h.size(), h.capacity());
|
||||
for (auto i : h)
|
||||
printf("%d ", i);
|
||||
printf("\n}\n");
|
||||
};
|
||||
|
||||
HashTable<int> h;
|
||||
|
||||
h.set(10);
|
||||
h.set(20);
|
||||
h.set(30);
|
||||
h.set(40);
|
||||
h.set(50);
|
||||
|
||||
h.dump();
|
||||
|
||||
printInts(h);
|
||||
|
||||
h.remove(30);
|
||||
printInts(h);
|
||||
|
||||
h.set(30);
|
||||
h.remove(30);
|
||||
printInts(h);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue