-
Notifications
You must be signed in to change notification settings - Fork 0
/
29_IteratorAndAlgorithm.cpp
144 lines (116 loc) · 4.06 KB
/
29_IteratorAndAlgorithm.cpp
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
#include <iostream>
#include <type_traits>
#include <cstring>
// distance
template <class InputIterator>
inline ptrdiff_t // iterator_traits<InputIterator>::difference_type
__distance(InputIterator first, InputIterator last, std::input_iterator_tag) {
ptrdiff_t n = 0; // iterator_traits<InputIterator>::difference_type
while (first!=last) {
++first; ++n;
}
return n;
}
template <class RandomAccessIterator>
inline ptrdiff_t // iterator_traits<InputIterator>::difference_type
__distance(RandomAccessIterator first, RandomAccessIterator last, std::random_access_iterator_tag) {
return last - first;
}
template <class InputIterator>
inline ptrdiff_t // iterator_traits<InputIterator>::difference_type
distance(InputIterator first, InputIterator last) {
typedef typename std::iterator_traits<InputIterator>::iterator_category category;
// why two variations of functions work out...
// think about the inheritance of iterators
// why pass object instead of typename
return __distance(first, last, category())
}
// advance
template <class InputIterator, class Distance>
inline void __advance(InputIterator& i, Distance n, std::input_iterator_tag) {
while (n--) ++i;
}
template <class BidirectionalIterator, class Distance>
inline void __advance(BidirectionalIterator& i, Distance n, std::bidirectional_iterator_tag) {
if (n>=0)
while (n--) ++i;
else
while (n++) --i;
}
template <class RandomAccessIterator, class Distance>
inline void __advance(RandomAccessIterator& i, Distance n, std::random_access_iterator_tag) {
i += n;
}
template <class Iterator>
inline typename std::iterator_traits<Iterator>::iterator_category
iterator_category(const Iterator&) {
typedef typename std::iterator_traits<Iterator>::iterator_category category;
return category();
}
template <class InputIterator, class Distance>
inline void advance(InputIterator& i, Distance n) {
__advance(i, n, iterator_category(i));
}
template <class FI>
inline void __destroy_aux(FI, FI, std::true_type) { }
template <class FI>
inline void __destroy_aux(FI first, FI last, std::false_type) {
for (; first < last; ++first)
destory(&*first); // memo! how to destruct linearly
}
template <class FI>
inline void __destory(FI first, FI last) { }
template <class FI, class T>
inline void __destory(FI first, FI last, T*) {
typedef typename std::is_trivially_destructible<T>::value trivial_destructor;
__destroy_aux(first, last, trivial_destructor());
}
template <class Iterator>
inline typename std::iterator_traits<Iterator>::value_type*
value_type(const Iterator&) {
return static_cast<typename std::iterator_traits<Iterator>::value_type*>(0);
}
inline void destory(char*, char*) { }
inline void destory(wchar_t*, wchar_t*) { }
template <class T>
inline void destroy(T* pointer) {
pointer->~T();
}
template <class FI>
inline void destory(FI first, FI last) {
__destroy(first, last, value_type(first));
}
template <class InputIterator, class OutputIterator, class T>
OutputIterator __unique_copy(InputIterator first,
InputIterator last,
OutputIterator result, T*) {
// ostream_iterator is write-only
T value = *first;
*result = value;
while (++first!=last) {
if (value != *first) {
value = *first;
*++result = value;
}
}
return ++result;
}
template <class InputIterator, class ForwardIterator>
ForwardIterator __unique_copy(InputIterator first,
InputIterator last,
ForwardIterator result,
std::forward_iterator_tag) {
// other four iterators in this function
*result = first;
while (++first!=last)
if (*result!=*first)
*++result = *first;
return ++result;
}
template <class InputIterator, class OutputIterator>
inline OutputIterator __unique_copy(InputIterator first,
InputIterator last,
OutputIterator result,
std::output_iterator_tag) {
return __unique_copy(first, last, retult, value_type(first));
}