-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathiterator.hpp
258 lines (219 loc) · 7.09 KB
/
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
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
/** \file
* Defines interfaces for iterators as used by the KDTree class.
*
* \author Martin F. Krafft <[email protected]>
*/
#ifndef INCLUDE_KDTREE_ITERATOR_HPP
#define INCLUDE_KDTREE_ITERATOR_HPP
#include <iterator>
#include "node.hpp"
namespace KDTree
{
template <typename _Val, typename _Ref, typename _Ptr>
class _Iterator;
template<typename _Val, typename _Ref, typename _Ptr>
inline bool
operator==(_Iterator<_Val, _Ref, _Ptr> const&,
_Iterator<_Val, _Ref, _Ptr> const&);
template<typename _Val>
inline bool
operator==(_Iterator<_Val, const _Val&, const _Val*> const&,
_Iterator<_Val, _Val&, _Val*> const&);
template<typename _Val>
inline bool
operator==(_Iterator<_Val, _Val&, _Val*> const&,
_Iterator<_Val, const _Val&, const _Val*> const&);
template<typename _Val, typename _Ref, typename _Ptr>
inline bool
operator!=(_Iterator<_Val, _Ref, _Ptr> const&,
_Iterator<_Val, _Ref, _Ptr> const&);
template<typename _Val>
inline bool
operator!=(_Iterator<_Val, const _Val&, const _Val*> const&,
_Iterator<_Val, _Val&, _Val*> const&);
template<typename _Val>
inline bool
operator!=(_Iterator<_Val, _Val&, _Val*> const&,
_Iterator<_Val, const _Val&, const _Val*> const&);
class _Base_iterator
{
protected:
typedef _Node_base::_Base_const_ptr _Base_const_ptr;
_Base_const_ptr _M_node;
inline _Base_iterator(_Base_const_ptr const __N = NULL)
: _M_node(__N) {}
inline _Base_iterator(_Base_iterator const& __THAT)
: _M_node(__THAT._M_node) {}
inline void
_M_increment()
{
if (_M_node->_M_right)
{
_M_node = _M_node->_M_right;
while (_M_node->_M_left) _M_node = _M_node->_M_left;
}
else
{
_Base_const_ptr __p = _M_node->_M_parent;
while (__p && _M_node == __p->_M_right)
{
_M_node = __p;
__p = _M_node->_M_parent;
}
if (__p) // (__p) provide undetermined behavior on end()++ rather
// than a seg fault, similar to standard iterator.
_M_node = __p;
}
}
inline void
_M_decrement()
{
if (!_M_node->_M_parent) // clearly identify the header node
{
_M_node = _M_node->_M_right;
}
else if (_M_node->_M_left)
{
_Base_const_ptr x = _M_node->_M_left;
while (x->_M_right) x = x->_M_right;
_M_node = x;
}
else
{
_Base_const_ptr __p = _M_node->_M_parent;
while (__p && _M_node == __p->_M_left) // see below
{
_M_node = __p;
__p = _M_node->_M_parent;
}
if (__p) // (__p) provide undetermined behavior on rend()++ rather
// than a seg fault, similar to standard iterator.
_M_node = __p;
}
}
template <size_t const __K, typename _Val, typename _Acc,
typename _Dist, typename _Cmp, typename _Alloc>
friend class KDTree;
};
template <typename _Val, typename _Ref, typename _Ptr>
class _Iterator : protected _Base_iterator
{
public:
typedef _Val value_type;
typedef _Ref reference;
typedef _Ptr pointer;
typedef _Iterator<_Val, _Val&, _Val*> iterator;
typedef _Iterator<_Val, _Val const&, _Val const*> const_iterator;
typedef _Iterator<_Val, _Ref, _Ptr> _Self;
typedef _Node<_Val> const* _Link_const_type;
typedef std::bidirectional_iterator_tag iterator_category;
typedef ptrdiff_t difference_type;
inline _Iterator()
: _Base_iterator() {}
inline _Iterator(_Link_const_type const __N)
: _Base_iterator(__N) {}
inline _Iterator(iterator const& __THAT)
: _Base_iterator(__THAT) {}
_Link_const_type get_raw_node() const
{
return _Link_const_type(_M_node);
}
reference
operator*() const
{
return _Link_const_type(_M_node)->_M_value;
}
pointer
operator->() const
{
return &(operator*());
}
_Self
operator++()
{
_M_increment();
return *this;
}
_Self
operator++(int)
{
_Self ret = *this;
_M_increment();
return ret;
}
_Self&
operator--()
{
_M_decrement();
return *this;
}
_Self
operator--(int)
{
_Self ret = *this;
_M_decrement();
return ret;
}
friend bool
operator== <>(_Iterator<_Val, _Ref, _Ptr> const&,
_Iterator<_Val, _Ref, _Ptr> const&);
friend bool
operator== <>(_Iterator<_Val, const _Val&, const _Val*> const&,
_Iterator<_Val, _Val&, _Val*> const&);
friend bool
operator== <>(_Iterator<_Val, _Val&, _Val*> const&,
_Iterator<_Val, const _Val&, const _Val*> const&);
friend bool
operator!= <>(_Iterator<_Val, _Ref, _Ptr> const&,
_Iterator<_Val, _Ref, _Ptr> const&);
friend bool
operator!= <>(_Iterator<_Val, const _Val&, const _Val*> const&,
_Iterator<_Val, _Val&, _Val*> const&);
friend bool
operator!= <>(_Iterator<_Val, _Val&, _Val*> const&,
_Iterator<_Val, const _Val&, const _Val*> const&);
};
template<typename _Val, typename _Ref, typename _Ptr>
bool
operator==(_Iterator<_Val, _Ref, _Ptr> const& __X,
_Iterator<_Val, _Ref, _Ptr> const& __Y)
{ return __X._M_node == __Y._M_node; }
template<typename _Val>
bool
operator==(_Iterator<_Val, const _Val&, const _Val*> const& __X,
_Iterator<_Val, _Val&, _Val*> const& __Y)
{ return __X._M_node == __Y._M_node; }
template<typename _Val>
bool
operator==(_Iterator<_Val, _Val&, _Val*> const& __X,
_Iterator<_Val, const _Val&, const _Val*> const& __Y)
{ return __X._M_node == __Y._M_node; }
template<typename _Val, typename _Ref, typename _Ptr>
bool
operator!=(_Iterator<_Val, _Ref, _Ptr> const& __X,
_Iterator<_Val, _Ref, _Ptr> const& __Y)
{ return __X._M_node != __Y._M_node; }
template<typename _Val>
bool
operator!=(_Iterator<_Val, const _Val&, const _Val*> const& __X,
_Iterator<_Val, _Val&, _Val*> const& __Y)
{ return __X._M_node != __Y._M_node; }
template<typename _Val>
bool
operator!=(_Iterator<_Val, _Val&, _Val*> const& __X,
_Iterator<_Val, const _Val&, const _Val*> const& __Y)
{ return __X._M_node != __Y._M_node; }
} // namespace KDTree
#endif // include guard
/* COPYRIGHT --
*
* This file is part of libkdtree++, a C++ template KD-Tree sorting container.
* libkdtree++ is (c) 2004-2007 Martin F. Krafft <[email protected]>
* and Sylvain Bougerel <[email protected]> distributed under the
* terms of the Artistic License 2.0. See the ./COPYING file in the source tree
* root for more information.
*
* THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES
* OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
*/