SinglyLinkedList: Implement find in terms of AK::find

Problem:
- The implementation of `find` is coupled to the implementation of
  `SinglyLinkedList`.

Solution:
- Decouple the implementation of `find` from the class by using a
  generic `find` algorithm.
This commit is contained in:
Lenny Maiorani 2021-01-10 11:27:41 -07:00 committed by Andreas Kling
parent 853cb8af5c
commit 1b2364846f
Notes: sideshowbarker 2024-07-18 23:56:11 +09:00
7 changed files with 106 additions and 30 deletions

View file

@ -65,7 +65,8 @@ private:
explicit Node(U&& v) explicit Node(U&& v)
: value(forward<U>(v)) : value(forward<U>(v))
{ {
static_assert(IsSame<T, U>::value); static_assert(
requires { T(v); }, "Conversion operator is missing.");
} }
T value; T value;
Node* next { nullptr }; Node* next { nullptr };
@ -113,7 +114,8 @@ public:
template<typename U> template<typename U>
void append(U&& value) void append(U&& value)
{ {
static_assert(IsSame<T, U>::value); static_assert(
requires { T(value); }, "Conversion operator is missing.");
auto* node = new Node(forward<U>(value)); auto* node = new Node(forward<U>(value));
if (!m_head) { if (!m_head) {
ASSERT(!m_tail); ASSERT(!m_tail);

View file

@ -27,6 +27,7 @@
#pragma once #pragma once
#include <AK/Assertions.h> #include <AK/Assertions.h>
#include <AK/Find.h>
#include <AK/StdLibExtras.h> #include <AK/StdLibExtras.h>
#include <AK/Traits.h> #include <AK/Traits.h>
#include <AK/Types.h> #include <AK/Types.h>
@ -166,38 +167,26 @@ public:
ConstIterator begin() const { return ConstIterator(m_head); } ConstIterator begin() const { return ConstIterator(m_head); }
ConstIterator end() const { return {}; } ConstIterator end() const { return {}; }
template<typename Finder> template<typename TUnaryPredicate>
ConstIterator find(Finder finder) const ConstIterator find_if(TUnaryPredicate&& pred) const
{ {
Node* prev = nullptr; return AK::find_if(begin(), end(), forward<TUnaryPredicate>(pred));
for (auto* node = m_head; node; node = node->next) {
if (finder(node->value))
return ConstIterator(node, prev);
prev = node;
}
return end();
} }
template<typename Finder> template<typename TUnaryPredicate>
Iterator find(Finder finder) Iterator find_if(TUnaryPredicate&& pred)
{ {
Node* prev = nullptr; return AK::find_if(begin(), end(), forward<TUnaryPredicate>(pred));
for (auto* node = m_head; node; node = node->next) {
if (finder(node->value))
return Iterator(node, prev);
prev = node;
}
return end();
} }
ConstIterator find(const T& value) const ConstIterator find(const T& value) const
{ {
return find([&](auto& other) { return Traits<T>::equals(value, other); }); return find_if([&](auto& other) { return Traits<T>::equals(value, other); });
} }
Iterator find(const T& value) Iterator find(const T& value)
{ {
return find([&](auto& other) { return Traits<T>::equals(value, other); }); return find_if([&](auto& other) { return Traits<T>::equals(value, other); });
} }
void remove(Iterator iterator) void remove(Iterator iterator)

View file

@ -27,6 +27,7 @@
#pragma once #pragma once
#include <AK/SinglyLinkedList.h> #include <AK/SinglyLinkedList.h>
#include <AK/StdLibExtras.h>
namespace AK { namespace AK {
@ -106,16 +107,16 @@ public:
ConstIterator begin() const { return List::begin(); } ConstIterator begin() const { return List::begin(); }
ConstIterator end() const { return List::end(); } ConstIterator end() const { return List::end(); }
template<typename Finder> template<typename TUnaryPredicate>
ConstIterator find(Finder finder) const ConstIterator find(TUnaryPredicate&& pred) const
{ {
return List::find(finder); return List::find_if(forward<TUnaryPredicate>(pred));
} }
template<typename Finder> template<typename TUnaryPredicate>
Iterator find(Finder finder) Iterator find(TUnaryPredicate&& pred)
{ {
return List::find(finder); return List::find_if(forward<TUnaryPredicate>(pred));
} }
ConstIterator find(const T& value) const ConstIterator find(const T& value) const

View file

@ -31,6 +31,7 @@ set(AK_TEST_SOURCES
TestQueue.cpp TestQueue.cpp
TestQuickSort.cpp TestQuickSort.cpp
TestRefPtr.cpp TestRefPtr.cpp
TestSinglyLinkedList.cpp
TestSourceGenerator.cpp TestSourceGenerator.cpp
TestSpan.cpp TestSpan.cpp
TestString.cpp TestString.cpp

View file

@ -0,0 +1,83 @@
/*
* Copyright (c) 2021, the SerenityOS developers.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <AK/TestSuite.h>
#include <AK/SinglyLinkedList.h>
static SinglyLinkedList<int> make_list()
{
SinglyLinkedList<int> list {};
list.append(0);
list.append(1);
list.append(2);
list.append(3);
list.append(4);
list.append(5);
list.append(6);
list.append(7);
list.append(8);
list.append(9);
return list;
}
TEST_CASE(should_find_mutable)
{
auto sut = make_list();
EXPECT_EQ(4, *sut.find(4));
EXPECT_EQ(sut.end(), sut.find(42));
}
TEST_CASE(should_find_mutable_with_predicate)
{
auto sut = make_list();
EXPECT_EQ(4, *sut.find_if([](const auto v) { return v == 4; }));
EXPECT_EQ(sut.end(), sut.find_if([](const auto v) { return v == 42; }));
}
TEST_CASE(should_find_const)
{
const auto sut = make_list();
EXPECT_EQ(4, *sut.find(4));
EXPECT_EQ(sut.end(), sut.find(42));
}
TEST_CASE(should_find_const_with_predicate)
{
const auto sut = make_list();
EXPECT_EQ(4, *sut.find_if([](const auto v) { return v == 4; }));
EXPECT_EQ(sut.end(), sut.find_if([](const auto v) { return v == 42; }));
}
TEST_MAIN(SinglyLinkedList)

View file

@ -266,7 +266,7 @@ void Track::set_note(int note, Switch switch_note)
void Track::sync_roll(int note) void Track::sync_roll(int note)
{ {
auto it = m_roll_notes[note].find([&](auto& roll_note) { return roll_note.off_sample > m_time; }); auto it = m_roll_notes[note].find_if([&](auto& roll_note) { return roll_note.off_sample > m_time; });
if (it.is_end()) if (it.is_end())
m_roll_iters[note] = m_roll_notes[note].begin(); m_roll_iters[note] = m_roll_notes[note].begin();
else else

View file

@ -75,7 +75,7 @@ public:
{ {
ScopedSpinLock lock(m_requests_lock); ScopedSpinLock lock(m_requests_lock);
was_empty = m_requests.is_empty(); was_empty = m_requests.is_empty();
m_requests.append(RefPtr<AsyncDeviceRequest>(request)); m_requests.append(request);
} }
if (was_empty) if (was_empty)
request->do_start({}); request->do_start({});