AK: Make all DoublyLinkedList search methods use Traits<T>::equals (#3404)

This commit is contained in:
Muhammad Zahalqa 2020-09-05 15:17:14 +03:00 committed by GitHub
parent 02b3cb8123
commit fad0c8e712
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -74,7 +74,7 @@ private:
};
public:
DoublyLinkedList() {}
DoublyLinkedList() { }
~DoublyLinkedList() { clear(); }
bool is_empty() const { return !head(); }
@ -133,11 +133,7 @@ public:
bool contains_slow(const T& value) const
{
for (auto* node = m_head; node; node = node->next) {
if (node->value == value)
return true;
}
return false;
return find_node(value) != nullptr;
}
using Iterator = DoublyLinkedListIterator<DoublyLinkedList, T>;
@ -152,19 +148,17 @@ public:
ConstIterator find(const T& value) const
{
for (auto* node = m_head; node; node = node->next) {
if (Traits<T>::equals(node->value, value))
return ConstIterator(node);
}
Node* node = find_node(value);
if (node)
return ConstIterator(node);
return end();
}
Iterator find(const T& value)
{
for (auto* node = m_head; node; node = node->next) {
if (Traits<T>::equals(node->value, value))
return Iterator(node);
}
Node* node = find_node(value);
if (node)
return Iterator(node);
return end();
}
@ -220,6 +214,15 @@ private:
m_head = node;
}
Node* find_node(const T& value) const
{
for (auto* node = m_head; node; node = node->next) {
if (Traits<T>::equals(node->value, value))
return node;
}
return nullptr;
}
Node* head() { return m_head; }
const Node* head() const { return m_head; }