forked from msinilo/rdestl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
iterator.h
78 lines (70 loc) · 1.93 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
#ifndef RDESTL_ITERATOR_H
#define RDESTL_ITERATOR_H
#include "rdestl_common.h"
namespace rde
{
//-----------------------------------------------------------------------------
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<typename TIter>
struct iterator_traits
{
typedef typename TIter::iterator_category iterator_category;
};
template<typename T>
struct iterator_traits<T*>
{
typedef random_access_iterator_tag iterator_category;
};
//-----------------------------------------------------------------------------
namespace internal
{
template<typename TIter, typename TDist> RDE_FORCEINLINE
void distance(TIter first, TIter last, TDist& dist, rde::random_access_iterator_tag)
{
dist = TDist(last - first);
}
template<typename TIter, typename TDist> RDE_FORCEINLINE
void distance(TIter first, TIter last, TDist& dist, rde::input_iterator_tag)
{
dist = 0;
while (first != last)
{
++dist;
++first;
}
}
template<typename TIter, typename TDist> RDE_FORCEINLINE
void advance(TIter& iter, TDist d, rde::random_access_iterator_tag)
{
iter += d;
}
template<typename TIter, typename TDist> RDE_FORCEINLINE
void advance(TIter& iter, TDist d, rde::bidirectional_iterator_tag)
{
if (d >= 0)
{
while (d--)
++iter;
}
else
{
while (d++)
--iter;
}
}
template<typename TIter, typename TDist> RDE_FORCEINLINE
void advance(TIter& iter, TDist d, rde::input_iterator_tag)
{
RDE_ASSERT(d >= 0);
while (d--)
++iter;
}
} // namespace internal
} // namespace rde
//-----------------------------------------------------------------------------
#endif // #ifndef RDESTL_ITERATOR_H