-
Notifications
You must be signed in to change notification settings - Fork 4
/
p3a_counting_iterator.hpp
152 lines (145 loc) · 4.1 KB
/
p3a_counting_iterator.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#pragma once
#include <iterator>
#include "p3a_macros.hpp"
namespace p3a {
template <class Integral>
class counting_iterator {
Integral m_value;
public:
using iterator_category = std::random_access_iterator_tag;
using value_type = Integral;
using difference_type = std::make_signed_t<Integral>;
using pointer = void*;
using reference = value_type;
P3A_ALWAYS_INLINE inline counting_iterator() = default;
P3A_ALWAYS_INLINE inline constexpr counting_iterator(counting_iterator&&) = default;
P3A_ALWAYS_INLINE inline constexpr counting_iterator& operator=(counting_iterator&&) = default;
P3A_ALWAYS_INLINE inline constexpr counting_iterator(counting_iterator const&) = default;
P3A_ALWAYS_INLINE inline constexpr counting_iterator& operator=(counting_iterator const&) = default;
P3A_HOST_DEVICE P3A_ALWAYS_INLINE inline constexpr
explicit counting_iterator(Integral arg)
:m_value(arg)
{
}
P3A_HOST_DEVICE P3A_ALWAYS_INLINE inline constexpr
bool operator==(counting_iterator const& other) const
{
return m_value == other.m_value;
}
P3A_HOST_DEVICE P3A_ALWAYS_INLINE inline constexpr
bool operator!=(counting_iterator const& other) const
{
return m_value != other.m_value;
}
P3A_HOST_DEVICE P3A_ALWAYS_INLINE inline constexpr
reference operator*() const
{
return m_value;
}
P3A_HOST_DEVICE P3A_ALWAYS_INLINE inline constexpr
pointer operator->() const
{
return nullptr;
}
P3A_HOST_DEVICE P3A_ALWAYS_INLINE inline constexpr
counting_iterator& operator++()
{
++m_value;
return *this;
}
P3A_HOST_DEVICE P3A_ALWAYS_INLINE inline constexpr
counting_iterator operator++(int)
{
auto result = *this;
++m_value;
return result;
}
P3A_HOST_DEVICE P3A_ALWAYS_INLINE inline constexpr
counting_iterator& operator--()
{
--m_value;
return *this;
}
P3A_HOST_DEVICE P3A_ALWAYS_INLINE inline constexpr
counting_iterator operator--(int)
{
auto result = *this;
--m_value;
return result;
}
P3A_HOST_DEVICE P3A_ALWAYS_INLINE inline constexpr
counting_iterator& operator+=(difference_type n)
{
m_value += n;
return *this;
}
P3A_HOST_DEVICE P3A_ALWAYS_INLINE inline constexpr
counting_iterator& operator-=(difference_type n)
{
m_value -= n;
return *this;
}
P3A_HOST_DEVICE P3A_ALWAYS_INLINE inline constexpr
counting_iterator operator+(difference_type n) const
{
return counting_iterator(m_value + n);
}
P3A_HOST_DEVICE P3A_ALWAYS_INLINE inline constexpr
counting_iterator operator-(difference_type n) const
{
return counting_iterator(m_value - n);
}
P3A_HOST_DEVICE P3A_ALWAYS_INLINE inline constexpr
difference_type operator-(counting_iterator const& other) const
{
return m_value - other.m_value;
}
P3A_HOST_DEVICE P3A_ALWAYS_INLINE inline constexpr
reference operator[](difference_type n) const
{
return m_value + n;
}
P3A_HOST_DEVICE P3A_ALWAYS_INLINE inline constexpr
bool operator<(counting_iterator const& other) const
{
return m_value < other.m_value;
}
P3A_HOST_DEVICE P3A_ALWAYS_INLINE inline constexpr
bool operator>(counting_iterator const& other) const
{
return m_value > other.m_value;
}
P3A_HOST_DEVICE P3A_ALWAYS_INLINE inline constexpr
bool operator<=(counting_iterator const& other) const
{
return m_value <= other.m_value;
}
P3A_HOST_DEVICE P3A_ALWAYS_INLINE inline constexpr
bool operator>=(counting_iterator const& other) const
{
return m_value >= other.m_value;
}
};
template <class Integral>
P3A_HOST_DEVICE P3A_ALWAYS_INLINE inline constexpr
counting_iterator<Integral> operator+(
typename counting_iterator<Integral>::difference_type n,
counting_iterator<Integral> const& it)
{
return it + n;
}
template <class Integral>
class counting_iterator3 {
public:
vector3<Integral> vector;
P3A_ALWAYS_INLINE inline counting_iterator3() = default;
P3A_ALWAYS_INLINE P3A_HOST_DEVICE inline constexpr
counting_iterator3(Integral a, Integral b, Integral c)
:vector(a, b, c)
{}
P3A_ALWAYS_INLINE P3A_HOST_DEVICE inline constexpr
counting_iterator3(vector3<Integral> const& v)
:vector(v)
{}
};
}