-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathpointer_iterator.hpp
More file actions
203 lines (175 loc) · 6.59 KB
/
Copy pathpointer_iterator.hpp
File metadata and controls
203 lines (175 loc) · 6.59 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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
// Copyright (C) 2018 Jonathan Müller <jonathanmueller.dev@gmail.com>
// This file is subject to the license terms in the LICENSE file
// found in the top-level directory of this distribution.
#ifndef FOONATHAN_ARRAY_POINTER_ITERATOR_HPP_INCLUDED
#define FOONATHAN_ARRAY_POINTER_ITERATOR_HPP_INCLUDED
#include <iterator>
#include <foonathan/array/config.hpp>
#include <foonathan/array/contiguous_iterator.hpp>
namespace foonathan
{
namespace array
{
template <typename Tag, typename T>
class pointer_iterator;
namespace detail
{
struct pointer_iterator_ctor
{};
template <typename Tag, typename T>
struct pointer_iterator_base
{
T* ptr_;
explicit constexpr pointer_iterator_base(pointer_iterator_ctor, T* ptr) : ptr_(ptr) {}
explicit constexpr pointer_iterator_base(Tag, T* ptr) : ptr_(ptr) {}
template <class ContIter>
explicit constexpr pointer_iterator_base(Tag, ContIter iter)
: ptr_(iterator_to_pointer(iter))
{}
constexpr pointer_iterator_base() noexcept : ptr_(nullptr) {}
};
template <typename Tag, typename T>
struct pointer_iterator_base<Tag, const T>
{
const T* ptr_;
explicit constexpr pointer_iterator_base(pointer_iterator_ctor, const T* ptr)
: ptr_(ptr)
{}
explicit constexpr pointer_iterator_base(Tag, const T* ptr) : ptr_(ptr) {}
template <class ContIter>
explicit constexpr pointer_iterator_base(Tag, ContIter iter)
: ptr_(iterator_to_pointer(iter))
{}
constexpr pointer_iterator_base() noexcept : ptr_(nullptr) {}
constexpr pointer_iterator_base(const pointer_iterator<Tag, T>& non_const) noexcept
: ptr_(non_const.operator->())
{}
};
} // namespace detail
/// A `RandomAccessIterator` that is simply a type-safe wrapper over a pointer.
template <typename Tag, typename T>
class pointer_iterator : detail::pointer_iterator_base<Tag, T>
{
public:
using iterator_category = std::random_access_iterator_tag;
using value_type = typename std::remove_cv<T>::type;
using difference_type = std::ptrdiff_t;
using pointer = T*;
using reference = T&;
using detail::pointer_iterator_base<Tag, T>::pointer_iterator_base;
//=== access ===//
constexpr reference operator*() const noexcept
{
return *this->ptr_;
}
constexpr pointer operator->() const noexcept
{
return this->ptr_;
}
constexpr reference operator[](difference_type dist) const noexcept
{
return this->ptr_[dist];
}
//=== increment/decrement ===//
FOONATHAN_ARRAY_CONSTEXPR14 pointer_iterator& operator++() noexcept
{
++this->ptr_;
return *this;
}
FOONATHAN_ARRAY_CONSTEXPR14 pointer_iterator operator++(int) noexcept
{
auto save = *this;
++this->ptr_;
return save;
}
FOONATHAN_ARRAY_CONSTEXPR14 pointer_iterator& operator--() noexcept
{
--this->ptr_;
return *this;
}
FOONATHAN_ARRAY_CONSTEXPR14 pointer_iterator operator--(int) noexcept
{
auto save = *this;
--this->ptr_;
return save;
}
FOONATHAN_ARRAY_CONSTEXPR14 pointer_iterator& operator+=(difference_type dist) noexcept
{
this->ptr_ += dist;
return *this;
}
FOONATHAN_ARRAY_CONSTEXPR14 pointer_iterator& operator-=(difference_type dist) noexcept
{
this->ptr_ -= dist;
return *this;
}
//=== addition/subtraction ===//
friend constexpr pointer_iterator operator+(pointer_iterator iter,
difference_type dist) noexcept
{
return pointer_iterator(detail::pointer_iterator_ctor{}, iter.ptr_ + dist);
}
friend constexpr pointer_iterator operator+(difference_type dist,
pointer_iterator iter) noexcept
{
return iter + dist;
}
friend constexpr pointer_iterator operator-(pointer_iterator iter,
difference_type dist) noexcept
{
return pointer_iterator(detail::pointer_iterator_ctor{}, iter.ptr_ - dist);
}
friend constexpr pointer_iterator operator-(difference_type dist,
pointer_iterator iter) noexcept
{
return iter - dist;
}
friend constexpr difference_type operator-(pointer_iterator lhs,
pointer_iterator rhs) noexcept
{
return lhs.ptr_ - rhs.ptr_;
}
//=== comparision ===//
friend constexpr bool operator==(pointer_iterator lhs, pointer_iterator rhs) noexcept
{
return lhs.ptr_ == rhs.ptr_;
}
friend constexpr bool operator!=(pointer_iterator lhs, pointer_iterator rhs) noexcept
{
return lhs.ptr_ != rhs.ptr_;
}
friend constexpr bool operator<(pointer_iterator lhs, pointer_iterator rhs) noexcept
{
return lhs.ptr_ < rhs.ptr_;
}
friend constexpr bool operator>(pointer_iterator lhs, pointer_iterator rhs) noexcept
{
return lhs.ptr_ > rhs.ptr_;
}
friend constexpr bool operator<=(pointer_iterator lhs, pointer_iterator rhs) noexcept
{
return lhs.ptr_ <= rhs.ptr_;
}
friend constexpr bool operator>=(pointer_iterator lhs, pointer_iterator rhs) noexcept
{
return lhs.ptr_ >= rhs.ptr_;
}
private:
template <typename U>
friend struct is_contiguous_iterator;
};
template <typename Tag, typename T>
struct is_contiguous_iterator<pointer_iterator<Tag, T>> : std::true_type
{
static constexpr T* to_pointer(pointer_iterator<Tag, T> iterator) noexcept
{
return iterator.operator->();
}
static constexpr pointer_iterator<Tag, T> to_iterator(T* pointer) noexcept
{
return pointer_iterator<Tag, T>(detail::pointer_iterator_ctor{}, pointer);
}
};
} // namespace array
} // namespace foonathan
#endif // FOONATHAN_ARRAY_POINTER_ITERATOR_HPP_INCLUDED