Skip to content

Commit

Permalink
Rename detail::iterator, simplify implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
patrickroberts committed Feb 8, 2025
1 parent 76932a0 commit 8d20ddf
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 64 deletions.
45 changes: 18 additions & 27 deletions include/beman/any_view/any_view.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#include <beman/any_view/concepts.hpp>
#include <beman/any_view/config.hpp>
#include <beman/any_view/detail/intrusive_small_ptr.hpp>
#include <beman/any_view/detail/iterator.hpp>
#include <beman/any_view/detail/any_iterator.hpp>
#include <beman/any_view/detail/view_adaptor.hpp>
#include <beman/any_view/detail/view_interface.hpp>

Expand Down Expand Up @@ -46,7 +46,7 @@ template <class ElementT,
class DiffT = std::ptrdiff_t>
class any_view : public std::ranges::view_interface<any_view<ElementT, OptionsV, RefT, RValueRefT, DiffT>> {
using iterator_concept = decltype(detail::get_iterator_concept<OptionsV>());
using iterator = detail::iterator<iterator_concept, ElementT, RefT, RValueRefT, DiffT>;
using iterator = detail::any_iterator<iterator_concept, ElementT, RefT, RValueRefT, DiffT>;
using size_type = std::make_unsigned_t<DiffT>;

static constexpr bool sized = (OptionsV & any_view_options::sized) == any_view_options::sized;
Expand All @@ -61,13 +61,10 @@ class any_view : public std::ranges::view_interface<any_view<ElementT, OptionsV,
// inplace storage sufficient for a vtable pointer and a std::vector<T>
detail::intrusive_small_ptr<interface_type, 4 * sizeof(void*)> view_ptr;

template <class RangeT>
using adaptor_type =
detail::view_adaptor<iterator_concept, ElementT, RefT, RValueRefT, DiffT, std::views::all_t<RangeT>>;

template <class RangeT>
static consteval auto get_in_place_adaptor_type() {
return std::in_place_type<adaptor_type<RangeT>>;
return std::in_place_type<
detail::view_adaptor<iterator_concept, ElementT, RefT, RValueRefT, DiffT, std::views::all_t<RangeT>>>;
}

public:
Expand Down Expand Up @@ -118,7 +115,7 @@ class any_view : public std::ranges::view_interface<any_view<ElementT, RangeTrai
detail::rvalue_reference_type_or_t<detail::as_rvalue_t<reference_type>, RangeTraitsT>;
using difference_type = detail::difference_type_or_t<std::ptrdiff_t, RangeTraitsT>;
using iterator =
detail::iterator<iterator_concept, ElementT, reference_type, rvalue_reference_type, difference_type>;
detail::any_iterator<iterator_concept, ElementT, reference_type, rvalue_reference_type, difference_type>;
using size_type = std::make_unsigned_t<difference_type>;

static constexpr bool sized = detail::sized_or_v<false, RangeTraitsT>;
Expand All @@ -134,17 +131,14 @@ class any_view : public std::ranges::view_interface<any_view<ElementT, RangeTrai
// inplace storage sufficient for a vtable pointer and a std::vector<T>
detail::intrusive_small_ptr<interface_type, 4 * sizeof(void*)> view_ptr;

template <class RangeT>
using adaptor_type = detail::view_adaptor<iterator_concept,
ElementT,
reference_type,
rvalue_reference_type,
difference_type,
std::views::all_t<RangeT>>;

template <class RangeT>
static consteval auto get_in_place_adaptor_type() {
return std::in_place_type<adaptor_type<RangeT>>;
return std::in_place_type<detail::view_adaptor<iterator_concept,
ElementT,
reference_type,
rvalue_reference_type,
difference_type,
std::views::all_t<RangeT>>>;
}

public:
Expand Down Expand Up @@ -194,7 +188,7 @@ class any_view : public std::ranges::view_interface<any_view<ElementT, OptionsV>
using rvalue_reference_type = decltype(OptionsV.rvalue_reference_type)::type;
using difference_type = decltype(OptionsV.difference_type)::type;
using iterator =
detail::iterator<iterator_concept, ElementT, reference_type, rvalue_reference_type, difference_type>;
detail::any_iterator<iterator_concept, ElementT, reference_type, rvalue_reference_type, difference_type>;
using size_type = std::make_unsigned_t<difference_type>;

static constexpr bool sized = OptionsV.sized;
Expand All @@ -210,17 +204,14 @@ class any_view : public std::ranges::view_interface<any_view<ElementT, OptionsV>
// inplace storage sufficient for a vtable pointer and a std::vector<T>
detail::intrusive_small_ptr<interface_type, 4 * sizeof(void*)> view_ptr;

template <class RangeT>
using adaptor_type = detail::view_adaptor<iterator_concept,
ElementT,
reference_type,
rvalue_reference_type,
difference_type,
std::views::all_t<RangeT>>;

template <class RangeT>
static consteval auto get_in_place_adaptor_type() {
return std::in_place_type<adaptor_type<RangeT>>;
return std::in_place_type<detail::view_adaptor<iterator_concept,
ElementT,
reference_type,
rvalue_reference_type,
difference_type,
std::views::all_t<RangeT>>>;
}

public:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception

#ifndef BEMAN_ANY_VIEW_DETAIL_ITERATOR_HPP
#define BEMAN_ANY_VIEW_DETAIL_ITERATOR_HPP
#ifndef BEMAN_ANY_VIEW_DETAIL_ANY_ITERATOR_HPP
#define BEMAN_ANY_VIEW_DETAIL_ANY_ITERATOR_HPP

#include <beman/any_view/concepts.hpp>
#include <beman/any_view/detail/intrusive_small_ptr.hpp>
Expand All @@ -16,7 +16,7 @@
namespace beman::any_view::detail {

template <class IterConceptT, class ElementT, class RefT, class RValueRefT, class DiffT>
class iterator {
class any_iterator {
using reference = RefT;
using rvalue_reference = RValueRefT;
using pointer = std::add_pointer_t<RefT>;
Expand All @@ -38,16 +38,9 @@ class iterator {
intrusive_small_ptr<interface_type, 3 * sizeof(void*)> iterator_ptr;

template <class IteratorT, class SentinelT>
using adaptor_type = detail::iterator_adaptor<ElementT, reference, rvalue_reference, DiffT, IteratorT, SentinelT>;

template <class IteratorT, class SentinelT>
static constexpr auto get_noexcept() {
return std::is_nothrow_constructible_v<adaptor_type<IteratorT, SentinelT>, IteratorT, SentinelT>;
}

template <class IteratorT, class SentinelT>
static constexpr auto get_in_place_adaptor_type() {
return std::in_place_type<adaptor_type<IteratorT, SentinelT>>;
static consteval auto get_in_place_adaptor_type() {
return std::in_place_type<
detail::iterator_adaptor<ElementT, reference, rvalue_reference, DiffT, IteratorT, SentinelT>>;
}

public:
Expand All @@ -56,28 +49,28 @@ class iterator {
using difference_type = DiffT;

template <detail::iterator_compatible_with<range_traits> IteratorT, std::sentinel_for<IteratorT> SentinelT>
constexpr iterator(IteratorT iterator, SentinelT sentinel) noexcept(get_noexcept<IteratorT, SentinelT>())
constexpr any_iterator(IteratorT iterator, SentinelT sentinel)
: iterator_ptr(get_in_place_adaptor_type<IteratorT, SentinelT>(), std::move(iterator), std::move(sentinel)) {}

constexpr iterator() noexcept
constexpr any_iterator() noexcept
requires forward
: iterator(pointer(nullptr), pointer(nullptr)) {}
: any_iterator(pointer(nullptr), pointer(nullptr)) {}

constexpr iterator(const iterator&)
constexpr any_iterator(const any_iterator&)
requires forward
= default;

constexpr iterator(iterator&&) noexcept = default;
constexpr any_iterator(any_iterator&&) noexcept = default;

constexpr auto operator=(const iterator&) -> iterator&
constexpr auto operator=(const any_iterator&) -> any_iterator&
requires forward
= default;

constexpr auto operator=(iterator&&) noexcept -> iterator& = default;
constexpr auto operator=(any_iterator&&) noexcept -> any_iterator& = default;

[[nodiscard]] constexpr auto operator*() const -> reference { return **iterator_ptr; }

friend constexpr auto iter_move(const iterator& other) -> rvalue_reference {
friend constexpr auto iter_move(const any_iterator& other) -> rvalue_reference {
return other.iterator_ptr->iter_move();
}

Expand All @@ -87,83 +80,83 @@ class iterator {
return std::to_address(*iterator_ptr);
}

constexpr auto operator++() -> iterator& {
constexpr auto operator++() -> any_iterator& {
++*iterator_ptr;
return *this;
}

constexpr auto operator++(int) -> void { ++*this; }

[[nodiscard]] constexpr auto operator++(int) -> iterator
[[nodiscard]] constexpr auto operator++(int) -> any_iterator
requires forward
{
const auto other = *this;
++*this;
return other;
}

[[nodiscard]] constexpr auto operator==(const iterator& other) const -> bool
[[nodiscard]] constexpr auto operator==(const any_iterator& other) const -> bool
requires forward
{
return *iterator_ptr == *other.iterator_ptr;
}

constexpr auto operator--() -> iterator&
constexpr auto operator--() -> any_iterator&
requires bidirectional
{
--*iterator_ptr;
return *this;
}

[[nodiscard]] constexpr auto operator--(int) -> iterator
[[nodiscard]] constexpr auto operator--(int) -> any_iterator
requires bidirectional
{
const auto other = *this;
--*this;
return other;
}

[[nodiscard]] constexpr auto operator<=>(const iterator& other) const -> std::partial_ordering
[[nodiscard]] constexpr auto operator<=>(const any_iterator& other) const -> std::partial_ordering
requires random_access
{
return *iterator_ptr <=> *other.iterator_ptr;
}

[[nodiscard]] constexpr auto operator-(const iterator& other) const -> difference_type
[[nodiscard]] constexpr auto operator-(const any_iterator& other) const -> difference_type
requires random_access
{
return *iterator_ptr - *other.iterator_ptr;
}

constexpr auto operator+=(difference_type offset) -> iterator&
constexpr auto operator+=(difference_type offset) -> any_iterator&
requires random_access
{
*iterator_ptr += offset;
return *this;
}

[[nodiscard]] constexpr auto operator+(difference_type offset) const -> iterator
[[nodiscard]] constexpr auto operator+(difference_type offset) const -> any_iterator
requires random_access
{
auto other = *this;
other += offset;
return other;
}

[[nodiscard]] friend constexpr auto operator+(difference_type offset, const iterator& other) -> iterator
[[nodiscard]] friend constexpr auto operator+(difference_type offset, const any_iterator& other) -> any_iterator
requires random_access
{
return other + offset;
}

constexpr auto operator-=(difference_type offset) -> iterator&
constexpr auto operator-=(difference_type offset) -> any_iterator&
requires random_access
{
*iterator_ptr -= offset;
return *this;
}

[[nodiscard]] constexpr auto operator-(difference_type offset) const -> iterator
[[nodiscard]] constexpr auto operator-(difference_type offset) const -> any_iterator
requires random_access
{
auto other = *this;
Expand All @@ -184,4 +177,4 @@ class iterator {

} // namespace beman::any_view::detail

#endif // BEMAN_ANY_VIEW_DETAIL_ITERATOR_HPP
#endif // BEMAN_ANY_VIEW_DETAIL_ANY_ITERATOR_HPP
2 changes: 1 addition & 1 deletion include/beman/any_view/detail/view_adaptor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ namespace beman::any_view::detail {

template <class IterConceptT, class ElementT, class RefT, class RValueRefT, class DiffT, std::ranges::view ViewT>
class view_adaptor : public view_interface<IterConceptT, ElementT, RefT, RValueRefT, DiffT> {
using iterator = detail::iterator<IterConceptT, ElementT, RefT, RValueRefT, DiffT>;
using iterator = detail::any_iterator<IterConceptT, ElementT, RefT, RValueRefT, DiffT>;
using size_type = std::make_unsigned_t<DiffT>;

[[no_unique_address]] ViewT view;
Expand Down
4 changes: 2 additions & 2 deletions include/beman/any_view/detail/view_interface.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
#ifndef BEMAN_ANY_VIEW_DETAIL_VIEW_INTERFACE_HPP
#define BEMAN_ANY_VIEW_DETAIL_VIEW_INTERFACE_HPP

#include <beman/any_view/detail/iterator.hpp>
#include <beman/any_view/detail/any_iterator.hpp>

namespace beman::any_view::detail {

template <class IterConceptT, class ElementT, class RefT, class RValueRefT, class DiffT>
class view_interface {
using iterator = detail::iterator<IterConceptT, ElementT, RefT, RValueRefT, DiffT>;
using iterator = detail::any_iterator<IterConceptT, ElementT, RefT, RValueRefT, DiffT>;
using size_type = std::make_unsigned_t<DiffT>;

public:
Expand Down

0 comments on commit 8d20ddf

Please sign in to comment.