libcaramel
random_iterator.hpp
1 #pragma once
2 
3 #include <libcaramel/iterators/iterator_facade.hpp>
4 
5 #include <iterator>
6 
7 namespace caramel
8 {
9  template <typename Any>
10  class random_access_iterator : public iterator_facade<random_access_iterator<Any>>
11  {
12  public:
13  random_access_iterator() = default;
14  random_access_iterator(Any* p_value) : mp_value(p_value) {}
15 
16  [[nodiscard]] auto dereference() const noexcept -> Any& { return *mp_value; }
17 
18  void advance(std::ptrdiff_t off) noexcept { mp_value += off; }
19  [[nodiscard]] auto distance_to(random_access_iterator other) const noexcept -> std::ptrdiff_t
20  {
21  return other.mp_value - mp_value;
22  }
23  auto operator==(random_access_iterator other) const noexcept -> bool
24  {
25  return other.mp_value == mp_value;
26  }
27 
28  private:
29  Any* mp_value{nullptr};
30  };
31 } // namespace caramel
Definition: iterator_facade.hpp:144
Definition: random_iterator.hpp:11