Repository navigation
Expand file tree
/
Copy pathbase_iterator.h
More file actions
189 lines (153 loc) · 5.09 KB
/
Copy pathbase_iterator.h
File metadata and controls
189 lines (153 loc) · 5.09 KB
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
WINRT_EXPORT namespace winrt::impl
{
template <typename T>
struct fast_iterator
{
using iterator_concept = std::random_access_iterator_tag;
using iterator_category = std::input_iterator_tag;
using value_type = decltype(std::declval<T>().GetAt(0));
using difference_type = std::ptrdiff_t;
using pointer = void;
using reference = value_type;
fast_iterator() noexcept = default;
fast_iterator(T const& collection, std::uint32_t const index) noexcept :
m_collection(&collection),
m_index(index)
{}
fast_iterator& operator++() noexcept
{
++m_index;
return *this;
}
fast_iterator operator++(int) noexcept
{
auto previous = *this;
++m_index;
return previous;
}
fast_iterator& operator--() noexcept
{
--m_index;
return *this;
}
fast_iterator operator--(int) noexcept
{
auto previous = *this;
--m_index;
return previous;
}
fast_iterator& operator+=(difference_type n) noexcept
{
m_index += static_cast<std::uint32_t>(n);
return *this;
}
fast_iterator operator+(difference_type n) const noexcept
{
return fast_iterator(*this) += n;
}
fast_iterator& operator-=(difference_type n) noexcept
{
return *this += -n;
}
fast_iterator operator-(difference_type n) const noexcept
{
return *this + -n;
}
difference_type operator-(fast_iterator const& other) const noexcept
{
WINRT_ASSERT(m_collection == other.m_collection);
return static_cast<difference_type>(m_index) - static_cast<difference_type>(other.m_index);
}
reference operator*() const
{
return m_collection->GetAt(m_index);
}
reference operator[](difference_type n) const
{
return m_collection->GetAt(m_index + static_cast<std::uint32_t>(n));
}
bool operator==(fast_iterator const& other) const noexcept
{
WINRT_ASSERT(m_collection == other.m_collection);
return m_index == other.m_index;
}
bool operator<(fast_iterator const& other) const noexcept
{
WINRT_ASSERT(m_collection == other.m_collection);
return m_index < other.m_index;
}
bool operator>(fast_iterator const& other) const noexcept
{
WINRT_ASSERT(m_collection == other.m_collection);
return m_index > other.m_index;
}
bool operator!=(fast_iterator const& other) const noexcept
{
return !(*this == other);
}
bool operator<=(fast_iterator const& other) const noexcept
{
return !(*this > other);
}
bool operator>=(fast_iterator const& other) const noexcept
{
return !(*this < other);
}
friend fast_iterator operator+(difference_type n, fast_iterator it) noexcept
{
return it + n;
}
friend fast_iterator operator-(difference_type n, fast_iterator it) noexcept
{
return it - n;
}
private:
T const* m_collection = nullptr;
std::uint32_t m_index = 0;
};
template <typename T>
class has_GetAt
{
template <typename U, typename = decltype(std::declval<U>().GetAt(0))> static constexpr bool get_value(int) { return true; }
template <typename> static constexpr bool get_value(...) { return false; }
public:
static constexpr bool value = get_value<T>(0);
};
template <typename T, std::enable_if_t<!has_GetAt<T>::value, int> = 0>
auto get_begin_iterator(T const& collection) -> decltype(collection.First())
{
auto result = collection.First();
if (!result.HasCurrent())
{
return {};
}
return result;
}
template <typename T, std::enable_if_t<!has_GetAt<T>::value, int> = 0>
auto get_end_iterator([[maybe_unused]] T const& collection) noexcept -> decltype(collection.First())
{
return {};
}
template <typename T, std::enable_if_t<has_GetAt<T>::value, int> = 0>
fast_iterator<T> get_begin_iterator(T const& collection) noexcept
{
return { collection, 0 };
}
template <typename T, std::enable_if_t<has_GetAt<T>::value, int> = 0>
fast_iterator<T> get_end_iterator(T const& collection)
{
return { collection, collection.Size() };
}
template <typename T, std::enable_if_t<has_GetAt<T>::value, int> = 0>
auto rbegin(T const& collection)
{
return std::make_reverse_iterator(get_end_iterator(collection));
}
template <typename T, std::enable_if_t<has_GetAt<T>::value, int> = 0>
auto rend(T const& collection)
{
return std::make_reverse_iterator(get_begin_iterator(collection));
}
using std::begin;
using std::end;
}