-
Notifications
You must be signed in to change notification settings - Fork 0
/
algorithm.h
168 lines (149 loc) · 5.7 KB
/
algorithm.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
#ifndef _GAYALGORITHM_H_
#define _GAYALGORITHM_H_
#include <cstring> // for operation of memory
#include "gay_iterator.h"
#include "gay_type_traits.h"
namespace uvwxyz
{
namespace gay_stl
{
template <class T>
const T& max(const T& lhs, const T& rhs)
{
return(lhs < rhs) ? rhs : lhs;
}
template <class T>
const T& min(const T& lhs, const T& rhs)
{
return(rhs < lhs) ? rhs : lhs;
}
template <class InputIterator>
typename __gay_iterator_traits<InputIterator>::difference_type
__distance(InputIterator first, InputIterator last, gay_input_iterator_tag)
{
typename __gay_iterator_traits<InputIterator>::difference_type n = 0;
while(first != last)
++first, ++n;
return n;
}
template <class InputIterator>
typename __gay_iterator_traits<InputIterator>::difference_type
__distance(InputIterator first, InputIterator last, gay_random_access_iterator_tag)
{
return last - first;
}
template <class InputIterator>
typename __gay_iterator_traits<InputIterator>::difference_type
distance(InputIterator first, InputIterator last)
{
typedef typename __gay_iterator_traits<InputIterator>::iterator_category category;
return __distance(first, last, category());
}
template <class RandomAccessIterator, class Distance>
void
__advance(RandomAccessIterator& input, Distance distance, gay_random_access_iterator_tag)
{
input += distance;
}
template <class BidirectionalIterator, class Distance>
void
__advance(BidirectionalIterator& input, Distance distance, gay_bidirectional_iterator_tag)
{
if(distance > 0)
while(distance--)
++input;
else
while(distance++)
--input;
}
template <class InputIterator, class Distance>
void
__advance(InputIterator& input, Distance distance, gay_input_iterator_tag)
{
while(distance--)
++input;
}
template <class InputIterator, class Distance>
void
advance(InputIterator& input, Distance distance)
{
__advance(input, distance, gay_iterator_category(input));
}
// copy for pod type
template <class RandomAccessIterator, class OutputIterator>
OutputIterator
__copy_aux_(RandomAccessIterator first, RandomAccessIterator last, OutputIterator result, __gay_true_type)
{
auto dis = distance(first, last);
memcpy(result, first, dis * sizeof(*first));
advance(result, dis);
return result;
}
template <class RandomAccessIterator, class OutputIterator>
OutputIterator
__copy_aux_(RandomAccessIterator first, RandomAccessIterator last, OutputIterator result, __gay_false_type)
{
for(; first != last; ++result, ++first)
*result = *first;
return result;
}
template <class RandomAccessIterator, class OutputIterator, class ValueType>
OutputIterator
__copy_aux(RandomAccessIterator first, RandomAccessIterator last, OutputIterator result, ValueType*)
{
typedef typename __gay__type_traits<ValueType>::is_pod pod_type;
return __copy_aux_(first, last, result, pod_type());
}
template <class RandomAccessIterator, class OutputIterator>
OutputIterator
__copy(RandomAccessIterator first, RandomAccessIterator last, OutputIterator result, gay_random_access_iterator_tag)
{
return __copy_aux(first, last, result, gay_iterator_value_type(first));
}
template <class ForwardIterator, class OutputIterator>
OutputIterator
__copy(ForwardIterator first, ForwardIterator last, OutputIterator result, gay_forward_iterator_tag)
{
for(; first != last; ++result, ++first)
*result = *first;
return result;
}
// return result + (last - first)
template <class InputIterator, class OutputIterator>
OutputIterator
copy(InputIterator first, InputIterator last, OutputIterator result)
{
typedef typename __gay_iterator_traits<InputIterator>::iterator_category category;
// call __copy to handle
return __copy(first, last, result, category());
}
template <class ForwardIterator, class Size, class T>
ForwardIterator
fill_n(ForwardIterator first, Size n, const T& v)
{
ForwardIterator cur = first;
for(; n > 0; --n, ++cur)
*cur = v;
return cur;
}
// Assigns x to all the elements in the range [first,last).
template <class ForwardIterator, class T>
void fill(ForwardIterator first, ForwardIterator last, const T& x)
{
ForwardIterator cur = first;
for(; cur != last; ++cur)
*cur = x;
}
// Copies the elements in the range [first,last) starting from the end into the range terminating at result.
template <class BidirectionalIterator1, class BidirectionalIterataor2>
BidirectionalIterataor2
copy_backward(BidirectionalIterator1 first, BidirectionalIterator1 last,
BidirectionalIterataor2 result)
{
while(first != last)
*(--result) = *(--last);
return result;
}
}
}
#endif // _GAYALGORITHM_H_