-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathiterator.h
352 lines (297 loc) · 8.97 KB
/
iterator.h
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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
/* THOR - THOR Template Library
* Joshua M. Kriegshauser
*
* iterator.h
*
* This file defines functionality required by iterators
*/
#ifndef THOR_ITERATOR_H
#define THOR_ITERATOR_H
#pragma once
#ifndef THOR_BASETYPES_H
#include "basetypes.h"
#endif
namespace thor
{
template <class T> struct nonconst_traits
{
typedef T value_type;
typedef T* pointer;
typedef T& reference;
typedef nonconst_traits<T> non_const_traits;
};
template <class T> struct const_traits
{
typedef T value_type;
typedef const T* pointer;
typedef const T& reference;
typedef nonconst_traits<T> non_const_traits;
};
struct false_type {};
struct true_type {};
#ifdef __GNUC__
// GCC 3.2.3 really doesn't like the crazy template discrimination that happens below
#define THOR_GET_CATEGORY(f,t) typename iterator_traits<t>::iterator_category()
#define THOR_GET_VALUE_TYPE(f,t) ((typename iterator_traits<t>::value_type*)0)
template <class T> struct is_pod_type
{
typedef false_type Type;
};
#else
// Is Pointer Type discrimination
template <int Is> struct bool2type
{
typedef false_type Ret;
};
template <> struct bool2type<1>
{
typedef true_type Ret;
};
template <> struct bool2type<0>
{
typedef false_type Ret;
};
// Note that these functions are only used at compile time for type/size discrimination and therefore need no implementation
struct PointerShim { PointerShim(const volatile void*); };
char IsP(bool, PointerShim);
char* IsP(bool, ...);
template <class T> struct IsPtr
{
static T& nullrep();
enum { Ret = (sizeof(IsP(false,nullrep())) == sizeof(char)) };
};
template <class T>
struct IsPtrType
{
enum { Is = IsPtr<T>::Ret };
typedef bool2type<Is> BoolType;
typedef typename BoolType::Ret Type;
static Type Ret() { return Type(); }
};
template <class T> struct is_pod_type
{
typedef typename IsPtrType<T>::Type Type;
};
#define THOR_GET_CATEGORY(f,t) get_category(f, IsPtrType<t>().Ret())
#define THOR_GET_VALUE_TYPE(f,t) get_value_type(f, IsPtrType<t>().Ret())
#endif
// Specializations for plain-old-data types
#define DEFINE_POD_TYPE(T) template <> struct is_pod_type<T> { typedef true_type Type; }
DEFINE_POD_TYPE(bool);
DEFINE_POD_TYPE(char);
DEFINE_POD_TYPE(unsigned char);
DEFINE_POD_TYPE(short);
DEFINE_POD_TYPE(unsigned short);
DEFINE_POD_TYPE(int);
DEFINE_POD_TYPE(unsigned int);
DEFINE_POD_TYPE(long);
DEFINE_POD_TYPE(unsigned long);
DEFINE_POD_TYPE(long long);
DEFINE_POD_TYPE(unsigned long long);
DEFINE_POD_TYPE(float);
DEFINE_POD_TYPE(double);
DEFINE_POD_TYPE(long double);
#ifdef _NATIVE_WCHAR_T_DEFINED
DEFINE_POD_TYPE(wchar_t);
#endif
#undef DEFINE_POD_TYPE
// Iterator types
struct input_iterator_tag {};
struct output_iterator_tag {};
struct forward_iterator_tag : public input_iterator_tag {};
struct bidirectional_iterator_tag : public forward_iterator_tag {};
struct random_access_iterator_tag : public bidirectional_iterator_tag {};
template <class Category, class T, class Pointer = T*, class Reference = T&>
struct iterator_type
{
typedef Category iterator_category;
typedef T value_type;
typedef Pointer pointer;
typedef Reference reference;
typedef thor_size_type size_type;
typedef thor_diff_type difference_type;
};
template <class Iterator>
struct iterator_traits
{
typedef typename Iterator::iterator_category iterator_category;
typedef typename Iterator::value_type value_type;
typedef typename Iterator::pointer pointer;
typedef typename Iterator::reference reference;
};
template <class T>
struct iterator_traits<const T*>
{
typedef random_access_iterator_tag iterator_category;
typedef T value_type;
typedef const T* pointer;
typedef const T& reference;
};
template <class T>
struct iterator_traits<T*>
{
typedef random_access_iterator_tag iterator_category;
typedef T value_type;
typedef const T* pointer;
typedef const T& reference;
};
template <> struct iterator_type<output_iterator_tag, void, void, void>
{
typedef output_iterator_tag iterator_category;
typedef void value_type;
typedef void pointer;
typedef void reference;
};
#ifndef __GNUC__
template <class InputIterator> random_access_iterator_tag get_category(const InputIterator&, const true_type&)
{
return random_access_iterator_tag();
}
template <class InputIterator> typename InputIterator::iterator_category get_category(const InputIterator&, const false_type&)
{
return InputIterator::iterator_category();
}
template <class T> T* get_value_type(const T*, const true_type&)
{
return static_cast<T*>(0);
}
template <class InputIterator> typename InputIterator::value_type* get_value_type(const InputIterator&, const false_type&)
{
return static_cast<typename InputIterator::value_type*>(0);
}
#endif
// Distance implementation for pointers and random access iterators: Constant time
template <class InputIterator> difference_type __distance(const InputIterator& first, const InputIterator& last, const random_access_iterator_tag&)
{
return (last - first);
}
// Distance implementation for all other input iterators: Linear time
template <class InputIterator, class T> difference_type __distance(const InputIterator& first, const InputIterator& last, const T&)
{
difference_type d = 0;
InputIterator iter(first);
while (iter != last)
{
++iter, ++d;
}
return d;
}
// Discriminating distance() implementation: Will be constant time for random access iterators and pointers and linear time for all other iterators
template <class InputIterator> difference_type distance(const InputIterator& first, const InputIterator& last)
{
return __distance(first, last, THOR_GET_CATEGORY(first,InputIterator));
}
template <class RandomAccessIterator, class Distance> void __advance(RandomAccessIterator& iter, Distance n, const random_access_iterator_tag&)
{
iter += n;
}
template <class InputIterator, class Distance, class T> void __advance(InputIterator& iter, Distance n, const T&)
{
if (n > 0)
{
while (n-- > 0)
{
++iter;
}
}
else
{
while (n++ < 0)
{
--iter;
}
}
}
template <class InputIterator, class Distance> void advance(InputIterator& iter, Distance n)
{
__advance(iter, n, THOR_GET_CATEGORY(iter,InputIterator));
}
template <class InputIterator, class T> InputIterator __find(InputIterator first, InputIterator last, const T& val, const random_access_iterator_tag&)
{
difference_type check_count = (last - first) / 4;
for (; check_count > 0; --check_count)
{
if (*first == val) return first;
++first;
if (*first == val) return first;
++first;
if (*first == val) return first;
++first;
if (*first == val) return first;
++first;
}
switch (last - first)
{
case 3:
if (*first == val) return first;
++first;
case 2:
if (*first == val) return first;
++first;
case 1:
if (*first == val) return first;
++first;
case 0:
default:
return first;
}
}
template <class InputIterator, class T> InputIterator __find(InputIterator first, InputIterator last, const T& val, const input_iterator_tag&)
{
while (first != last && !(*first == val))
{
++first;
}
return first;
}
template <class InputIterator, class T> InputIterator find(InputIterator first, InputIterator last, const T& val)
{
return __find(first, last, val, THOR_GET_CATEGORY(first,InputIterator));
}
template <class InputIterator, class Predicate> InputIterator __find_if(InputIterator first, InputIterator last, Predicate pred, const random_access_iterator_tag&)
{
difference_type check_count = (last - first) / 4;
for (; check_count > 0; --check_count)
{
if (pred(*first)) return first;
++first;
if (pred(*first)) return first;
++first;
if (pred(*first)) return first;
++first;
if (pred(*first)) return first;
++first;
}
switch (last - first)
{
case 3:
if (pred(*first)) return first;
++first;
case 2:
if (pred(*first)) return first;
++first;
case 1:
if (pred(*first)) return first;
++first;
case 0:
default:
return first;
}
}
template <class InputIterator, class Predicate> InputIterator __find_if(InputIterator first, InputIterator last, Predicate pred, const input_iterator_tag&)
{
while (first != last && !pred(*first))
{
++first;
}
return first;
}
template <class InputIterator, class Predicate> InputIterator find_if(InputIterator first, InputIterator last, Predicate pred)
{
return __find_if(first, last, pred, THOR_GET_CATEGORY(first, InputIterator));
}
}; // namespace thor
#ifndef THOR_TYPETRAITS_H
#include "typetraits.h"
#endif
#endif