mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 05:17:35 +00:00
AK: Allow HashMap to be used with non-default-constructible values.
Solve this by adding find() overloads to HashTable and SinglyLinkedList that take a templated functor for comparing the values. This allows HashMap to call HashTable::find() without having to create a temporary Entry for use as the table key. :^)
This commit is contained in:
parent
d5bb98acbc
commit
6e95b11395
3 changed files with 67 additions and 51 deletions
|
@ -147,50 +147,38 @@ public:
|
|||
ConstIterator begin() const { return ConstIterator(m_head); }
|
||||
ConstIterator end() const { return ConstIterator::universal_end(); }
|
||||
|
||||
ConstIterator find(const T& value) const
|
||||
template<typename Finder>
|
||||
ConstIterator find(Finder finder) const
|
||||
{
|
||||
Node* prev = nullptr;
|
||||
for (auto* node = m_head; node; node = node->next) {
|
||||
if (node->value == value)
|
||||
if (finder(node->value))
|
||||
return ConstIterator(node, prev);
|
||||
prev = node;
|
||||
}
|
||||
return end();
|
||||
}
|
||||
|
||||
Iterator find(const T& value)
|
||||
template<typename Finder>
|
||||
Iterator find(Finder finder)
|
||||
{
|
||||
Node* prev = nullptr;
|
||||
for (auto* node = m_head; node; node = node->next) {
|
||||
if (node->value == value)
|
||||
if (finder(node->value))
|
||||
return Iterator(node, prev);
|
||||
prev = node;
|
||||
}
|
||||
return end();
|
||||
}
|
||||
|
||||
template<typename Traits>
|
||||
ConstIterator find(const T& value) const
|
||||
{
|
||||
Node* prev = nullptr;
|
||||
for (auto* node = m_head; node; node = node->next) {
|
||||
if (Traits::equals(node->value, value))
|
||||
return ConstIterator(node, prev);
|
||||
prev = node;
|
||||
}
|
||||
return end();
|
||||
return find([&](auto& other) { return value == other; });
|
||||
}
|
||||
|
||||
template<typename Traits>
|
||||
Iterator find(const T& value)
|
||||
{
|
||||
Node* prev = nullptr;
|
||||
for (auto* node = m_head; node; node = node->next) {
|
||||
if (Traits::equals(node->value, value))
|
||||
return Iterator(node, prev);
|
||||
prev = node;
|
||||
}
|
||||
return end();
|
||||
return find([&](auto& other) { return value == other; });
|
||||
}
|
||||
|
||||
void remove(Iterator iterator)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue