AK: Conform SimpleIterator to the random access iterator requirements

This requires pulling in some of the STL, but the result is that our
iterator is now STL Approved ™️ and our containers can be
auto-conformed to Swift protocols.
This commit is contained in:
Andrew Kaster 2024-08-05 23:45:36 -06:00 committed by Andrew Kaster
parent 227ac9973f
commit 756ef2c722
Notes: github-actions[bot] 2024-08-17 23:45:35 +00:00
6 changed files with 109 additions and 26 deletions

View file

@ -1,5 +1,6 @@
/*
* Copyright (c) 2020, the SerenityOS developers.
* Copyright (c) 2024, Andrew Kaster <andrew@ladybird.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
@ -7,6 +8,7 @@
#pragma once
#include <AK/Forward.h>
#include <iterator>
namespace AK {
@ -15,22 +17,44 @@ class SimpleIterator {
public:
friend Container;
constexpr bool is_end() const { return m_index == SimpleIterator::end(m_container).m_index; }
using value_type = ValueType;
using iterator_category = std::random_access_iterator_tag;
constexpr bool is_end() const { return m_index == SimpleIterator::end(*m_container).m_index; }
constexpr size_t index() const { return m_index; }
constexpr bool operator==(SimpleIterator other) const { return m_index == other.m_index; }
constexpr bool operator!=(SimpleIterator other) const { return m_index != other.m_index; }
constexpr bool operator<(SimpleIterator other) const { return m_index < other.m_index; }
constexpr bool operator>(SimpleIterator other) const { return m_index > other.m_index; }
constexpr bool operator<=(SimpleIterator other) const { return m_index <= other.m_index; }
constexpr bool operator>=(SimpleIterator other) const { return m_index >= other.m_index; }
constexpr bool operator==(SimpleIterator const& other) const = default;
constexpr auto operator<=>(SimpleIterator const& other) const = default;
constexpr SimpleIterator operator+(ptrdiff_t delta) const { return SimpleIterator { m_container, m_index + delta }; }
constexpr SimpleIterator operator-(ptrdiff_t delta) const { return SimpleIterator { m_container, m_index - delta }; }
constexpr SimpleIterator operator+(ptrdiff_t delta) const { return SimpleIterator { *m_container, m_index + delta }; }
friend constexpr SimpleIterator operator+(ptrdiff_t delta, SimpleIterator const& it) { return it + delta; }
constexpr SimpleIterator& operator+=(ptrdiff_t delta)
{
m_index += delta;
return *this;
}
constexpr SimpleIterator operator-(ptrdiff_t delta) const { return SimpleIterator { *m_container, m_index - delta }; }
friend constexpr SimpleIterator operator-(ptrdiff_t delta, SimpleIterator const& it) { return it - delta; }
constexpr SimpleIterator& operator-=(ptrdiff_t delta)
{
m_index -= delta;
return *this;
}
constexpr ValueType& operator[](ptrdiff_t delta) const
requires(!IsConst<ValueType>)
{
return (*m_container)[m_index + delta];
}
constexpr ValueType const& operator[](ptrdiff_t delta) const
requires(IsConst<ValueType>)
{
return (*m_container)[m_index + delta];
}
constexpr ptrdiff_t operator-(SimpleIterator other) const { return static_cast<ptrdiff_t>(m_index) - other.m_index; }
constexpr SimpleIterator operator++()
constexpr SimpleIterator& operator++()
{
++m_index;
return *this;
@ -38,10 +62,10 @@ public:
constexpr SimpleIterator operator++(int)
{
++m_index;
return SimpleIterator { m_container, m_index - 1 };
return SimpleIterator { *m_container, m_index - 1 };
}
constexpr SimpleIterator operator--()
constexpr SimpleIterator& operator--()
{
--m_index;
return *this;
@ -49,21 +73,35 @@ public:
constexpr SimpleIterator operator--(int)
{
--m_index;
return SimpleIterator { m_container, m_index + 1 };
return SimpleIterator { *m_container, m_index + 1 };
}
ALWAYS_INLINE constexpr ValueType const& operator*() const { return m_container[m_index]; }
ALWAYS_INLINE constexpr ValueType& operator*() { return m_container[m_index]; }
ALWAYS_INLINE constexpr ValueType const* operator->() const { return &m_container[m_index]; }
ALWAYS_INLINE constexpr ValueType* operator->() { return &m_container[m_index]; }
SimpleIterator& operator=(SimpleIterator const& other)
ALWAYS_INLINE constexpr ValueType const& operator*() const
requires(IsConst<ValueType>)
{
m_index = other.m_index;
return *this;
return (*m_container)[m_index];
}
SimpleIterator(SimpleIterator const& obj) = default;
ALWAYS_INLINE constexpr ValueType& operator*() const
requires(!IsConst<ValueType>)
{
return (*m_container)[m_index];
}
ALWAYS_INLINE constexpr ValueType const* operator->() const
requires(IsConst<ValueType>)
{
return &(*m_container)[m_index];
}
ALWAYS_INLINE constexpr ValueType* operator->() const
requires(!IsConst<ValueType>)
{
return &(*m_container)[m_index];
}
constexpr SimpleIterator& operator=(SimpleIterator const& other) = default;
constexpr SimpleIterator(SimpleIterator const& obj) = default;
constexpr SimpleIterator() = default;
private:
static constexpr SimpleIterator begin(Container& container) { return { container, 0 }; }
@ -78,13 +116,13 @@ private:
}
constexpr SimpleIterator(Container& container, size_t index)
: m_container(container)
: m_container(&container)
, m_index(index)
{
}
Container& m_container;
size_t m_index;
Container* m_container = nullptr;
size_t m_index = 0;
};
}

View file

@ -23,6 +23,12 @@ TEST_CASE(compile_time_constructible)
static_assert(array.size() == 4);
}
TEST_CASE(conforms_to_iterator_protocol)
{
static_assert(std::random_access_iterator<Array<int, 8>::Iterator>);
static_assert(std::random_access_iterator<Array<int, 8>::ConstIterator>);
}
TEST_CASE(compile_time_iterable)
{
constexpr Array<int, 8> array = { 0, 1, 2, 3, 4, 5, 6, 7 };

View file

@ -27,6 +27,14 @@ TEST_CASE(ints)
EXPECT_EQ(ints[2], 2);
}
TEST_CASE(conforms_to_iterator_procotol)
{
static_assert(std::random_access_iterator<FixedArray<int>::Iterator>);
static_assert(std::random_access_iterator<FixedArray<int>::ConstIterator>);
static_assert(std::random_access_iterator<FixedArray<int const>::Iterator>);
static_assert(std::random_access_iterator<FixedArray<int const>::ConstIterator>);
}
TEST_CASE(swap)
{
FixedArray<int> first = FixedArray<int>::must_create_but_fixme_should_propagate_errors(4);

View file

@ -16,6 +16,19 @@ TEST_CASE(constexpr_default_constructor_is_empty)
static_assert(span.is_empty(), "Default constructed span should be empty.");
}
TEST_CASE(conforms_to_iterator_protocol)
{
static_assert(std::random_access_iterator<Span<int>::Iterator>);
static_assert(std::random_access_iterator<Span<int>::ConstIterator>);
static_assert(std::random_access_iterator<Span<int const>::Iterator>);
static_assert(std::random_access_iterator<Span<int const>::ConstIterator>);
static_assert(std::random_access_iterator<Bytes::Iterator>);
static_assert(std::random_access_iterator<Bytes::ConstIterator>);
static_assert(std::random_access_iterator<ReadonlyBytes::Iterator>);
static_assert(std::random_access_iterator<ReadonlyBytes::ConstIterator>);
}
TEST_CASE(implicit_conversion_to_const)
{
constexpr Bytes bytes0;

View file

@ -40,6 +40,11 @@ TEST_CASE(compare_views)
EXPECT_EQ(view1, "foo");
}
TEST_CASE(conforms_to_iterator_protocol)
{
static_assert(std::random_access_iterator<StringView::ConstIterator>);
}
TEST_CASE(string_view_literal_operator)
{
StringView literal_view = "foo"sv;

View file

@ -56,6 +56,19 @@ TEST_CASE(strings)
EXPECT_EQ(loop_counter, 2);
}
TEST_CASE(conforms_to_iterator_protocol)
{
static_assert(std::random_access_iterator<Vector<int>::Iterator>);
static_assert(std::random_access_iterator<Vector<int>::ConstIterator>);
static_assert(std::random_access_iterator<Vector<int const>::Iterator>);
static_assert(std::random_access_iterator<Vector<int const>::ConstIterator>);
static_assert(std::random_access_iterator<Vector<String>::Iterator>);
static_assert(std::random_access_iterator<Vector<String>::ConstIterator>);
static_assert(std::random_access_iterator<Vector<String const>::Iterator>);
static_assert(std::random_access_iterator<Vector<String const>::ConstIterator>);
}
TEST_CASE(strings_insert_ordered)
{
Vector<ByteString> strings;