32 #ifndef MCKL_CORE_ITERATOR_HPP 33 #define MCKL_CORE_ITERATOR_HPP 43 template <
typename Base>
48 typename std::iterator_traits<Base>::difference_type>;
49 using value_type =
typename std::iterator_traits<Base>::value_type;
50 using pointer =
typename std::iterator_traits<Base>::pointer;
51 using reference =
typename std::iterator_traits<Base>::reference;
53 typename std::iterator_traits<Base>::iterator_category;
57 : base_(Base()), step_(1)
70 std::is_nothrow_copy_constructible<Base>::value)
71 : base_(base), step_(
step)
80 std::is_nothrow_copy_constructible<Base>::value)
81 : base_(other.
base()), step_(other.
step())
86 Base
base()
const {
return base_; }
101 template <
typename IntType>
104 return *((*this) + n);
115 "Compare two StepIterator objects with different steps");
117 return iter1.base_ == iter2.base_;
128 "Compare two StepIterator objects with different steps");
130 return iter1.base_ != iter2.base_;
140 "Compare two StepIterator objects with different steps");
142 return iter1.base_ < iter2.base_;
153 "Compare two StepIterator objects with different steps");
155 return iter1.base_ <= iter2.base_;
165 "Compare two StepIterator objects with different steps");
167 return iter1.base_ > iter2.base_;
178 "Compare two StepIterator objects with different steps");
180 return iter1.base_ >= iter2.base_;
192 advance(iter.base_, iter.step_);
218 advance(iter.base_, -iter.step_);
240 template <
typename IntType>
246 advance(ret.base_, static_cast<difference_type>(n) * ret.step_);
255 template <
typename IntType>
266 template <
typename IntType>
276 template <
typename IntType>
279 return iter = iter + n;
286 template <
typename IntType>
289 return iter = iter - n;
301 "Subtract two StepIterator objects with different steps");
305 return distance(iter2.base_, iter1.base_) / iter1.step_;
313 template <typename T, bool = std::is_integral<T>::value>
324 template <
typename IntType>
332 Range() noexcept : begin_(0), end_(0), grainsize_(1) {}
342 : begin_(begin), end_(end), grainsize_(grainsize)
345 "**Range::Range** used with invalid pair of integers");
358 template <
typename Split>
360 : begin_(other.begin_ + (other.end_ - other.begin_) / 2)
362 , grainsize_(other.grainsize_)
376 bool empty()
const {
return begin_ >= end_; }
401 template <
typename Iterator>
407 using value_type =
typename std::iterator_traits<iterator>::value_type;
408 using pointer =
typename std::iterator_traits<iterator>::pointer;
409 using reference =
typename std::iterator_traits<iterator>::reference;
423 : begin_(begin), end_(end), grainsize_(grainsize)
428 "**Range::Range** used with invalid pair of iterators");
444 template <
typename Split>
446 : begin_(other.begin_), end_(other.end_), grainsize_(other.grainsize_)
451 advance(begin_, distance(begin_, end_) / 2);
462 return empty() ? 0 :
static_cast<size_type>(distance(begin_, end_));
473 return distance(begin_, end_) <= 0;
491 runtime_assert<std::out_of_range>(
492 n < size(),
"**Range::at** index out of range");
527 #endif // MCKL_CORE_ITERATOR_HPP typename std::iterator_traits< Base >::reference reference
Range(iterator begin, iterator end, size_type grainsize=1)
Construct a new range with a pair of iterators.
friend StepIterator & operator--(StepIterator &iter)
Prefix decrement: advance the base iterator by a distance of -iter.step()
reference at(size_type n) const
Access an element within the range with bound checking.
iterator end() const
The upper bound of the range.
typename std::iterator_traits< Base >::iterator_category iterator_category
size_type grainsize() const
Return the grain size.
Range(Range &other, Split)
Copy construct a new range which split the original.
StepIterator(const StepIterator< T > &other) noexcept(std::is_nothrow_copy_constructible< Base >::value)
Convert from compatible StepIterator.
Range(Range &other, Split)
Copy construct a new range which split the original.
difference_type step() const
Return the increment step size.
Base operator->() const
Member selection through the base iterator.
typename std::iterator_traits< iterator >::pointer pointer
friend bool operator>=(const StepIterator &iter1, const StepIterator &iter2)
Compare greater than relation or equality.
size_type size() const
Return the size of the range.
friend bool operator==(const StepIterator &iter1, const StepIterator &iter2)
Compare equality.
Range()
Default constructor.
reference front() const
Access the first element in the range.
size_type size() const
Return the size of the range.
reference operator[](IntType n) const
Equivalent to *((*this) + n)
Range() noexcept
Default constructor.
typename std::iterator_traits< iterator >::value_type value_type
friend StepIterator operator+(const StepIterator &iter, IntType n)
Addition with integer: advance the base iterator by a distance of n * iter.step() ...
Base base() const
Return the underlying base iterator.
bool empty() const
If the range is empty, i.e., begin() >= end()
friend StepIterator operator++(StepIterator &iter, int)
Postfix increment.
bool empty() const
If the range is empty, i.e., distance(begin(), end()) <= 0
friend StepIterator & operator++(StepIterator &iter)
Prefix increment: advance the base iterator by a distance of iter.step()
friend StepIterator & operator-=(StepIterator &iter, IntType n)
Compound subtraction with integer.
friend StepIterator operator-(const StepIterator &iter, IntType n)
Subtraction with integer: advance the base iterator by a distance -n * iter.step() ...
reference operator[](size_type n) const
Access an element within the range.
iterator begin() const
The lower bound of the range.
iterator begin() const
The lower bound of the range.
friend StepIterator operator+(IntType n, const StepIterator &iter)
Addition with integer.
reference back() const
Access the last element in the range.
iterator end() const
The upper bound of the range.
bool is_divisible() const
If the range is divisible, i.e., grainsize() <= size()
StepIterator() noexcept(std::is_nothrow_default_constructible< Base >::value)
Default constructor.
friend bool operator>(const StepIterator &iter1, const StepIterator &iter2)
Compare greater than relation.
friend bool operator<(const StepIterator &iter1, const StepIterator &iter2)
Compare less than relation.
size_type grainsize() const
Return the grain size.
friend bool operator!=(const StepIterator &iter1, const StepIterator &iter2)
Compare inequality.
std::make_signed_t< typename std::iterator_traits< Base >::difference_type > difference_type
Iterator adapter which increment the base iterator in multiple steps.
StepIterator(Base base, difference_type step=1) noexcept(std::is_nothrow_copy_constructible< Base >::value)
Convert from a base iterator.
friend difference_type operator-(const StepIterator &iter1, const StepIterator &iter2)
Difference between two iterators.
typename std::iterator_traits< iterator >::reference reference
friend StepIterator operator--(StepIterator &iter, int)
Postfix decrement.
typename std::iterator_traits< Base >::value_type value_type
Range(iterator begin, iterator end, size_type grainsize=1)
Construct a new range with a pair of integers.
friend bool operator<=(const StepIterator &iter1, const StepIterator &iter2)
Compare less than relation or equality.
reference operator*() const
Dereference the base iterator.
bool is_divisible() const
If the range is divisible, i.e., grainsize() <= size()
typename std::iterator_traits< Base >::pointer pointer
friend StepIterator & operator+=(StepIterator &iter, IntType n)
Compound addition with integer.
void runtime_assert(bool cond, const char *msg, bool soft=false)