emlabcpp
modern opinionated embedded C++ library
numeric.h
Go to the documentation of this file.
1 
24 #pragma once
25 
26 #include "../iterator.h"
27 
28 namespace emlabcpp::iterators
29 {
30 
31 template < typename >
32 class numeric_iterator;
33 
34 }
35 
36 template < typename T >
37 struct std::iterator_traits< emlabcpp::iterators::numeric_iterator< T > >
38 {
39  using value_type = T;
40  using difference_type = std::ptrdiff_t;
41  using pointer = T*;
42  using const_pointer = T const*;
43  using reference = T&;
44  using iterator_category = std::random_access_iterator_tag;
45 };
46 
47 namespace emlabcpp::iterators
48 {
49 
54 template < typename T >
55 class numeric_iterator : public generic_iterator< numeric_iterator< T > >
56 {
57  T val_;
58 
59 public:
61  explicit constexpr numeric_iterator( T val )
62  : val_( std::move( val ) )
63  {
64  }
65 
66  constexpr T& operator*()
67  {
68  return val_;
69  }
70 
71  constexpr T const& operator*() const
72  {
73  return val_;
74  }
75 
76  constexpr numeric_iterator& operator+=( std::ptrdiff_t offset )
77  {
78  val_ += static_cast< T >( offset );
79  return *this;
80  }
81 
82  constexpr numeric_iterator& operator-=( std::ptrdiff_t offset )
83  {
84  val_ -= static_cast< T >( offset );
85  return *this;
86  }
87 
88  constexpr auto operator<=>( numeric_iterator< T > const& other ) const
89  {
90  return val_ <=> other.val_;
91  }
92 
93  constexpr bool operator==( numeric_iterator< T > const& other ) const
94  {
95  return val_ == other.val_;
96  }
97 
98  constexpr std::ptrdiff_t operator-( numeric_iterator const& other ) const
99  {
100  return static_cast< std::ptrdiff_t >( val_ ) -
101  static_cast< std::ptrdiff_t >( other.val_ );
102  }
103 };
104 
105 } // namespace emlabcpp::iterators
numeric iterator - iterator over numbers (which are calculated on the fly) Value of type T is stored ...
Definition: numeric.h:56
constexpr T & operator*()
Definition: numeric.h:66
constexpr auto operator<=>(numeric_iterator< T > const &other) const
Definition: numeric.h:88
constexpr numeric_iterator(T val)
Initializes iterator to value val.
Definition: numeric.h:61
constexpr bool operator==(numeric_iterator< T > const &other) const
Definition: numeric.h:93
constexpr numeric_iterator & operator+=(std::ptrdiff_t offset)
Definition: numeric.h:76
constexpr std::ptrdiff_t operator-(numeric_iterator const &other) const
Definition: numeric.h:98
constexpr numeric_iterator & operator-=(std::ptrdiff_t offset)
Definition: numeric.h:82
constexpr T const & operator*() const
Definition: numeric.h:71
MIT License.
Definition: convert.h:29
MIT License.
Definition: impl.h:31
generic_iterator simplifies custom iterator implementation using CRTP.
Definition: iterator.h:62
std::random_access_iterator_tag iterator_category
Definition: numeric.h:44