-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathalgorithm.h
164 lines (143 loc) · 3.56 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
#ifndef ALGORITHM_H
#define ALGORITHM_H
#include <numeric>
#include <algorithm>
#include <iostream>
#define InputIterator typename
#define ForwardIterator typename
#define RandomAccessIterator typename
#define BidirectionalIterator typename
#define Integral typename
template <ForwardIterator I, Integral N>
N iota(I first, I last, N start = N(0), N step = N(1)) {
typedef typename std::iterator_traits<I>::value_type T;
while (first != last) {
*first = T(start);
start += step;
++first;
}
return start;
}
template <RandomAccessIterator I>
void random_iota(I first, I last) {
::iota(first, last, 0);
std::random_shuffle(first, last);
}
template <BidirectionalIterator I>
void reverse_iota(I first, I last) {
::iota(first, last, 0);
std::reverse(first, last);
}
template <typename I>
void print_range(I first, I last) {
while (first != last) {
std::cout << *first << " ";
++first;
}
std::cout << std::endl;
}
template <typename I, typename N>
// I is InputIterator
// N is Integral
inline
void advance(I& first, N n) {
// [first, n) is a valid range
advance(first, n, std::iterator_traits<I>::iterator_category());
}
template <typename I, typename N>
// I is InputIterator
// N is Integral
inline
void advance(I& first, N n, std::input_iterator_tag) {
// [first, n) is a valid range
while (n--) ++first;
}
template <typename I, typename N>
// I is RandomAccessIterator
// N is Integral
inline
void advance(I& first, N n, std::random_access_iterator_tag) {
// [first, n) is a valid range
first += n;
}
template <typename I>
// I is InputIterator
inline
typename std::iterator_traits<I>::difference_type
distance(I first, I last) {
// [first, last) is a valid range
return distance(first, last,
std::iterator_traits<I>::iterator_category());
}
template <typename I>
// I is InputIterator
inline
typename std::iterator_traits<I>::difference_type
distance(I first, I last, std::input_iterator_tag) {
// [first, last) is a valid range
typename std::iterator_traits<I>::difference_type n(0);
while (first != last) {
++n;
++first;
}
return n;
}
template <typename I>
// I is RandomAccessIterator
inline
typename std::iterator_traits<I>::difference_type
distance(I first, I last, std::random_access_iterator_tag) {
// [first, last) is a valid range
return last - first;
}
template <typename I>
// requires I is InputIterator
inline
I successor(I x) { return ++x; }
template <typename I>
// requires I is BidirectionalIterator
inline
I predecessor(I x) { return --x; }
template <typename I, typename N>
// requires I is RandomAccessIterator
// requires N is Integral
inline
I successor(I x, N n, std::random_access_iterator_tag) {
return x + n;
}
template <typename I, typename N>
// requires I is InputIterator
// requires N is Integral
inline
I successor(I x, N n, std::input_iterator_tag) {
while (n != N(0)) {
++x;
--n;
}
return x;
}
template <typename I, typename N>
// requires I is InputIterator
// requires N is Integral
inline
I successor(I x, N n) {
typedef typename std::iterator_traits<I>::iterator_category C;
return successor(x, n, C());
}
template <typename I>
// requires I is BidirectionalIterator
void hill(I first, I last)
{
I middle = successor(first, std::distance(first, last)/2);
iota(first, middle);
reverse_iota(middle, last);
}
template <typename I>
// requires I is BidirectionalIterator
void valley(I first, I last)
{
I middle = successor(first, std::distance(first, last)/2);
reverse_iota(first, middle);
iota(middle, last);
}
#endif