Refactor: Change the AK::binary_search signature to use AK::Span.

This commit is contained in:
asynts 2020-07-26 11:22:05 +02:00 committed by Andreas Kling
parent ac9c2bc492
commit 707d92db61
Notes: sideshowbarker 2024-07-19 04:36:28 +09:00
5 changed files with 20 additions and 19 deletions

View file

@ -26,6 +26,7 @@
#pragma once
#include <AK/Span.h>
#include <AK/StdLibExtras.h>
#include <AK/Types.h>
@ -38,10 +39,10 @@ int integral_compare(const T& a, const T& b)
}
template<typename T, typename Compare>
T* binary_search(T* haystack, size_t haystack_size, const T& needle, Compare compare = integral_compare, int* nearby_index = nullptr)
T* binary_search(Span<T> haystack, const T& needle, Compare compare = integral_compare, int* nearby_index = nullptr)
{
int low = 0;
int high = haystack_size - 1;
int high = haystack.size() - 1;
while (low <= high) {
int middle = (low + high) / 2;
int comparison = compare(needle, haystack[middle]);

View file

@ -36,9 +36,9 @@ TEST_CASE(vector_ints)
ints.append(2);
ints.append(3);
auto test1 = *binary_search(ints.data(), ints.size(), 1, AK::integral_compare<int>);
auto test2 = *binary_search(ints.data(), ints.size(), 2, AK::integral_compare<int>);
auto test3 = *binary_search(ints.data(), ints.size(), 3, AK::integral_compare<int>);
auto test1 = *binary_search(ints.span(), 1, AK::integral_compare<int>);
auto test2 = *binary_search(ints.span(), 2, AK::integral_compare<int>);
auto test3 = *binary_search(ints.span(), 3, AK::integral_compare<int>);
EXPECT_EQ(test1, 1);
EXPECT_EQ(test2, 2);
EXPECT_EQ(test3, 3);
@ -48,9 +48,9 @@ TEST_CASE(array_doubles)
{
double doubles[] = { 1.1, 9.9, 33.33 };
auto test1 = *binary_search(doubles, 3, 1.1, AK::integral_compare<double>);
auto test2 = *binary_search(doubles, 3, 9.9, AK::integral_compare<double>);
auto test3 = *binary_search(doubles, 3, 33.33, AK::integral_compare<double>);
auto test1 = *binary_search({ doubles, 3 }, 1.1, AK::integral_compare<double>);
auto test2 = *binary_search({ doubles, 3 }, 9.9, AK::integral_compare<double>);
auto test3 = *binary_search({ doubles, 3 }, 33.33, AK::integral_compare<double>);
EXPECT_EQ(test1, 1.1);
EXPECT_EQ(test2, 9.9);
EXPECT_EQ(test3, 33.33);
@ -66,9 +66,9 @@ TEST_CASE(vector_strings)
auto string_compare = [](const String& a, const String& b) -> int {
return strcmp(a.characters(), b.characters());
};
auto test1 = *binary_search(strings.data(), strings.size(), String("bat"), string_compare);
auto test2 = *binary_search(strings.data(), strings.size(), String("cat"), string_compare);
auto test3 = *binary_search(strings.data(), strings.size(), String("dog"), string_compare);
auto test1 = *binary_search(strings.span(), String("bat"), string_compare);
auto test2 = *binary_search(strings.span(), String("cat"), string_compare);
auto test3 = *binary_search(strings.span(), String("dog"), string_compare);
EXPECT_EQ(test1, String("bat"));
EXPECT_EQ(test2, String("cat"));
EXPECT_EQ(test3, String("dog"));
@ -79,7 +79,7 @@ TEST_CASE(single_element)
Vector<int> ints;
ints.append(1);
auto test1 = *binary_search(ints.data(), ints.size(), 1, AK::integral_compare<int>);
auto test1 = *binary_search(ints.span(), 1, AK::integral_compare<int>);
EXPECT_EQ(test1, 1);
}
@ -90,9 +90,9 @@ TEST_CASE(not_found)
ints.append(2);
ints.append(3);
auto test1 = binary_search(ints.data(), ints.size(), -1, AK::integral_compare<int>);
auto test2 = binary_search(ints.data(), ints.size(), 0, AK::integral_compare<int>);
auto test3 = binary_search(ints.data(), ints.size(), 4, AK::integral_compare<int>);
auto test1 = binary_search(ints.span(), -1, AK::integral_compare<int>);
auto test2 = binary_search(ints.span(), 0, AK::integral_compare<int>);
auto test3 = binary_search(ints.span(), 4, AK::integral_compare<int>);
EXPECT_EQ(test1, nullptr);
EXPECT_EQ(test2, nullptr);
EXPECT_EQ(test3, nullptr);
@ -102,7 +102,7 @@ TEST_CASE(no_elements)
{
Vector<int> ints;
auto test1 = binary_search(ints.data(), ints.size(), 1, AK::integral_compare<int>);
auto test1 = binary_search(ints.span(), 1, AK::integral_compare<int>);
EXPECT_EQ(test1, nullptr);
}

View file

@ -174,7 +174,7 @@ void RangeAllocator::deallocate(Range range)
int nearby_index = 0;
auto* existing_range = binary_search(
m_available_ranges.data(), m_available_ranges.size(), range, [](auto& a, auto& b) {
m_available_ranges.span(), range, [](auto& a, auto& b) {
return a.base().get() - b.end().get();
},
&nearby_index);

View file

@ -187,7 +187,7 @@ size_t XtermSuggestionDisplay::fit_to_page_boundary(size_t selection_index)
int index = 0;
auto* match = binary_search(
m_pages.data(), m_pages.size(), { selection_index, selection_index }, [](auto& a, auto& b) -> int {
m_pages.span(), { selection_index, selection_index }, [](auto& a, auto& b) -> int {
if (a.start >= b.start && a.start < b.end)
return 0;
return a.start - b.start;

View file

@ -878,7 +878,7 @@ Vector<Line::CompletionSuggestion> Shell::complete_path(const String& base, cons
Vector<Line::CompletionSuggestion> Shell::complete_program_name(const String& name, size_t offset)
{
auto match = binary_search(cached_path.data(), cached_path.size(), name, [](const String& name, const String& program) -> int {
auto match = binary_search(cached_path.span(), name, [](const String& name, const String& program) -> int {
return strncmp(name.characters(), program.characters(), name.length());
});