2020-09-06 19:14:08 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2020, the SerenityOS developers.
|
2024-08-06 05:45:36 +00:00
|
|
|
* Copyright (c) 2024, Andrew Kaster <andrew@ladybird.org>
|
2020-09-06 19:14:08 +00:00
|
|
|
*
|
2021-04-22 08:24:48 +00:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-09-06 19:14:08 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <AK/Forward.h>
|
2024-08-06 05:45:36 +00:00
|
|
|
#include <iterator>
|
2020-09-06 19:14:08 +00:00
|
|
|
|
|
|
|
namespace AK {
|
|
|
|
|
|
|
|
template<typename Container, typename ValueType>
|
|
|
|
class SimpleIterator {
|
|
|
|
public:
|
|
|
|
friend Container;
|
|
|
|
|
2024-08-06 05:45:36 +00:00
|
|
|
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; }
|
2020-09-06 19:14:08 +00:00
|
|
|
constexpr size_t index() const { return m_index; }
|
|
|
|
|
2024-08-06 05:45:36 +00:00
|
|
|
constexpr bool operator==(SimpleIterator const& other) const = default;
|
|
|
|
constexpr auto operator<=>(SimpleIterator const& other) const = default;
|
2020-09-06 19:14:08 +00:00
|
|
|
|
2024-08-06 05:45:36 +00:00
|
|
|
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];
|
|
|
|
}
|
2020-09-06 19:14:08 +00:00
|
|
|
|
|
|
|
constexpr ptrdiff_t operator-(SimpleIterator other) const { return static_cast<ptrdiff_t>(m_index) - other.m_index; }
|
|
|
|
|
2024-08-06 05:45:36 +00:00
|
|
|
constexpr SimpleIterator& operator++()
|
2020-09-06 19:14:08 +00:00
|
|
|
{
|
|
|
|
++m_index;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
constexpr SimpleIterator operator++(int)
|
|
|
|
{
|
|
|
|
++m_index;
|
2024-08-06 05:45:36 +00:00
|
|
|
return SimpleIterator { *m_container, m_index - 1 };
|
2020-09-06 19:14:08 +00:00
|
|
|
}
|
|
|
|
|
2024-08-06 05:45:36 +00:00
|
|
|
constexpr SimpleIterator& operator--()
|
2020-09-06 19:14:08 +00:00
|
|
|
{
|
|
|
|
--m_index;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
constexpr SimpleIterator operator--(int)
|
|
|
|
{
|
|
|
|
--m_index;
|
2024-08-06 05:45:36 +00:00
|
|
|
return SimpleIterator { *m_container, m_index + 1 };
|
2020-09-06 19:14:08 +00:00
|
|
|
}
|
|
|
|
|
2024-08-06 05:45:36 +00:00
|
|
|
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];
|
|
|
|
}
|
2020-09-06 19:14:08 +00:00
|
|
|
|
2024-08-06 05:45:36 +00:00
|
|
|
ALWAYS_INLINE constexpr ValueType const* operator->() const
|
|
|
|
requires(IsConst<ValueType>)
|
2021-01-31 12:13:49 +00:00
|
|
|
{
|
2024-08-06 05:45:36 +00:00
|
|
|
return &(*m_container)[m_index];
|
|
|
|
}
|
|
|
|
ALWAYS_INLINE constexpr ValueType* operator->() const
|
|
|
|
requires(!IsConst<ValueType>)
|
|
|
|
{
|
|
|
|
return &(*m_container)[m_index];
|
2021-01-31 12:13:49 +00:00
|
|
|
}
|
2024-08-06 05:45:36 +00:00
|
|
|
|
|
|
|
constexpr SimpleIterator& operator=(SimpleIterator const& other) = default;
|
|
|
|
constexpr SimpleIterator(SimpleIterator const& obj) = default;
|
|
|
|
|
|
|
|
constexpr SimpleIterator() = default;
|
2021-01-31 12:13:49 +00:00
|
|
|
|
2020-09-06 19:14:08 +00:00
|
|
|
private:
|
|
|
|
static constexpr SimpleIterator begin(Container& container) { return { container, 0 }; }
|
|
|
|
static constexpr SimpleIterator end(Container& container)
|
|
|
|
{
|
2021-04-10 13:59:06 +00:00
|
|
|
using RawContainerType = RemoveCV<Container>;
|
2020-09-06 19:14:08 +00:00
|
|
|
|
2023-12-16 14:19:34 +00:00
|
|
|
if constexpr (IsSame<StringView, RawContainerType> || IsSame<ByteString, RawContainerType>)
|
2020-09-06 19:14:08 +00:00
|
|
|
return { container, container.length() };
|
|
|
|
else
|
|
|
|
return { container, container.size() };
|
|
|
|
}
|
|
|
|
|
|
|
|
constexpr SimpleIterator(Container& container, size_t index)
|
2024-08-06 05:45:36 +00:00
|
|
|
: m_container(&container)
|
2020-09-06 19:14:08 +00:00
|
|
|
, m_index(index)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2024-08-06 05:45:36 +00:00
|
|
|
Container* m_container = nullptr;
|
|
|
|
size_t m_index = 0;
|
2020-09-06 19:14:08 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|