C++ Library Extensions 2022.12.09
To help learn modern C++ programming
cpg_iterator.hpp
Go to the documentation of this file.
1/*
2 WARNING: If you are including Intel tbb.h, include this file after tbb.h
3*/
4#ifndef CPG_ITERATOR_HPP
5#define CPG_ITERATOR_HPP
6
7#ifndef NOMINMAX
8#define NOMINMAX
9#endif
10
11#include "cpg_types.hpp"
12
13 #ifdef __TBB_iterators_H
14
15 template <typename IntType>
17 #else
18 namespace tbb
19 {
20 // By courtesy of Intel's Threading Building Blocks team!!
21 // With best respect and gratitude for Intel Corporation.
22 template <typename IntType>
24 {
25 static_assert(std::numeric_limits<IntType>::is_integer, "Cannot instantiate counting_iterator with a non-integer type");
26 public:
27 typedef typename std::make_signed<IntType>::type difference_type;
28 typedef IntType value_type;
29 typedef const IntType* pointer;
30 typedef const IntType& reference;
31 typedef std::random_access_iterator_tag iterator_category;
32
33 counting_iterator() : my_counter() {}
34 explicit counting_iterator(IntType init) : my_counter(init) {}
35
36 reference operator*() const { return my_counter; }
37 value_type operator[](difference_type i) const { return *(*this + i); }
38
39 difference_type operator-(const counting_iterator& it) const { return my_counter - it.my_counter; }
40
41 counting_iterator& operator+=(difference_type forward) { my_counter += forward; return *this; }
42 counting_iterator& operator-=(difference_type backward) { return *this += -backward; }
43 counting_iterator& operator++() { return *this += 1; }
44 counting_iterator& operator--() { return *this -= 1; }
45
47 counting_iterator it(*this);
48 ++(*this);
49 return it;
50 }
52 counting_iterator it(*this);
53 --(*this);
54 return it;
55 }
56
57 counting_iterator operator-(difference_type backward) const { return counting_iterator(my_counter - backward); }
58 counting_iterator operator+(difference_type forward) const { return counting_iterator(my_counter + forward); }
59 friend counting_iterator operator+(difference_type forward, const counting_iterator it) { return it + forward; }
60
61 bool operator==(const counting_iterator& it) const { return *this - it == 0; }
62 bool operator!=(const counting_iterator& it) const { return !(*this == it); }
63 bool operator<(const counting_iterator& it) const {return *this - it < 0; }
64 bool operator>(const counting_iterator& it) const { return it < *this; }
65 bool operator<=(const counting_iterator& it) const { return !(*this > it); }
66 bool operator>=(const counting_iterator& it) const { return !(*this < it); }
67
68 private:
69 IntType my_counter;
70
71 }; // end of class counting_iterator
72
73 } // end of namespace tbb
74
75 #endif // end of __TBB_iterators_H
76
78{
79 template<typename IndexType = long long>
80 constexpr decltype(auto)
81 counting_iterator(IndexType idx = IndexType{})
82 {
84 }
85}
86// end of namespace cpg::parallel
87
88#endif // end of CPG_ITERATOR_HPP
friend counting_iterator operator+(difference_type forward, const counting_iterator it)
bool operator<(const counting_iterator &it) const
bool operator!=(const counting_iterator &it) const
std::random_access_iterator_tag iterator_category
counting_iterator operator+(difference_type forward) const
bool operator==(const counting_iterator &it) const
difference_type operator-(const counting_iterator &it) const
std::make_signed< IntType >::type difference_type
counting_iterator operator--(int)
counting_iterator & operator--()
reference operator*() const
bool operator>(const counting_iterator &it) const
counting_iterator operator++(int)
counting_iterator operator-(difference_type backward) const
const IntType & reference
bool operator<=(const counting_iterator &it) const
counting_iterator & operator+=(difference_type forward)
bool operator>=(const counting_iterator &it) const
counting_iterator(IntType init)
counting_iterator & operator++()
value_type operator[](difference_type i) const
counting_iterator & operator-=(difference_type backward)
const IntType * pointer
constexpr decltype(auto) counting_iterator(IndexType idx=IndexType{})