Skip to content

Commit

Permalink
Allow removing current iter from ListRange without invalidating
Browse files Browse the repository at this point in the history
  • Loading branch information
chriselrod committed Jul 20, 2023
1 parent 6064800 commit 38a5da2
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions include/Utilities/ListRanges.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,10 @@ class End {
}
};

/// Safe for removing current iter from `list` while iterating.
template <typename T, class Op, class Proj> class ListIterator {
T *state_;
T *next_{nullptr};
[[no_unique_address]] Op op_{};
[[no_unique_address]] Proj p_{};

Expand All @@ -54,12 +56,14 @@ template <typename T, class Op, class Proj> class ListIterator {
// constexpr auto operator->() const noexcept -> T * { return state_; }
constexpr auto getState() const noexcept -> T * { return state_; }
constexpr auto operator++() noexcept -> ListIterator & {
state_ = op_(state_);
state_ = next_;
if (next_) next_ = op_(next_);
return *this;
}
constexpr auto operator++(int) noexcept -> ListIterator {
ListIterator tmp{*this};
state_ = op_(state_);
state_ = next_;
if (next_) next_ = op_(next_);
return tmp;
}
constexpr auto operator-(ListIterator const &other) const noexcept
Expand All @@ -74,9 +78,10 @@ template <typename T, class Op, class Proj> class ListIterator {
constexpr auto operator==(End) const noexcept -> bool {
return state_ == nullptr;
}
constexpr ListIterator(T *state) noexcept : state_{state} {}
constexpr ListIterator(T *state) noexcept
: state_{state}, next_{state ? Op{}(state) : nullptr} {}
constexpr ListIterator(T *state, Op op, Proj p) noexcept
: state_{state}, op_{op}, p_{p} {}
: state_{state}, next_{state ? op(state) : nullptr}, op_{op}, p_{p} {}
constexpr ListIterator() noexcept = default;
constexpr ListIterator(const ListIterator &) noexcept = default;
constexpr ListIterator(ListIterator &&) noexcept = default;
Expand Down

0 comments on commit 38a5da2

Please sign in to comment.