Skip to content

Commit

Permalink
AK: SinglyLinkedList use Traits<T>::equals in find
Browse files Browse the repository at this point in the history
Use Traits<T>::equals for equality checking in search
functions instead of  operator==
  • Loading branch information
tryfinally authored and awesomekling committed Sep 6, 2020
1 parent f1a6884 commit ad3e6ef
Showing 1 changed file with 6 additions and 9 deletions.
15 changes: 6 additions & 9 deletions AK/SinglyLinkedList.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,15 @@

#include <AK/Assertions.h>
#include <AK/StdLibExtras.h>
#include <AK/Traits.h>
#include <AK/Types.h>

namespace AK {

template<typename ListType, typename ElementType>
class SinglyLinkedListIterator {
public:
SinglyLinkedListIterator() {}
SinglyLinkedListIterator() { }
bool operator!=(const SinglyLinkedListIterator& other) const { return m_node != other.m_node; }
SinglyLinkedListIterator& operator++()
{
Expand Down Expand Up @@ -76,7 +77,7 @@ class SinglyLinkedList {
};

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

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

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(value) != end();
}

using Iterator = SinglyLinkedListIterator<SinglyLinkedList, T>;
Expand Down Expand Up @@ -195,12 +192,12 @@ class SinglyLinkedList {

ConstIterator find(const T& value) const
{
return find([&](auto& other) { return value == other; });
return find([&](auto& other) { return Traits<T>::equals(value, other); });
}

Iterator find(const T& value)
{
return find([&](auto& other) { return value == other; });
return find([&](auto& other) { return Traits<T>::equals(value, other); });
}

void remove(Iterator iterator)
Expand Down

0 comments on commit ad3e6ef

Please sign in to comment.