Skip to content

Commit

Permalink
AK: Make all DoublyLinkedList search methods use Traits<T>::equals (S…
Browse files Browse the repository at this point in the history
  • Loading branch information
tryfinally committed Sep 5, 2020
1 parent 02b3cb8 commit fad0c8e
Showing 1 changed file with 17 additions and 14 deletions.
31 changes: 17 additions & 14 deletions AK/DoublyLinkedList.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ class DoublyLinkedList {
};

public:
DoublyLinkedList() {}
DoublyLinkedList() { }
~DoublyLinkedList() { clear(); }

bool is_empty() const { return !head(); }
Expand Down Expand Up @@ -133,11 +133,7 @@ class DoublyLinkedList {

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>;
Expand All @@ -152,19 +148,17 @@ class DoublyLinkedList {

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();
}

Expand Down Expand Up @@ -220,6 +214,15 @@ class DoublyLinkedList {
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; }

Expand Down

0 comments on commit fad0c8e

Please sign in to comment.