-
Notifications
You must be signed in to change notification settings - Fork 0
/
algorithm.hpp
233 lines (208 loc) · 9.28 KB
/
algorithm.hpp
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
/*
* algorithm.hpp
* some functions to comply with the concpet of STL, like <algorithm>
*
* written by janus_wel<janus.wel.3@gmail.com>
* This source code is in public domain, and has NO WARRANTY.
* */
#ifndef ALGORITHM_HPP
#define ALGORITHM_HPP
#include <algorithm>
#include <iterator>
#include <vector>
namespace util {
namespace algorithm {
// functor to delete objects
// usage:
// std::for_each(array.begin(), array.end(), sweeper());
struct sweeper {
template<typename T>
void operator()(T* p) { delete p; }
};
// for std::generate() and std::generate_n()
template<typename T> class counter {
public:
typedef T count_type;
private:
count_type first, returned;
const count_type increment;
public:
counter(count_type first, count_type increment)
: first(first), increment(increment) {}
count_type operator()(void) {
returned = first;
first += increment;
return returned;
}
private:
counter& operator=(const counter& rhs);
};
// copy_if
template< typename InputIterator, typename OstreamIterator,
typename Predicate>
inline OstreamIterator
copy_if(InputIterator first, InputIterator last,
OstreamIterator result, Predicate pred) {
for (; first != last; ++first)
if (pred(*first)) *result++ = *first;
return result;
}
// print
template<typename InputIterator, typename OstreamIterator>
inline OstreamIterator
print( InputIterator first, InputIterator last,
OstreamIterator out) {
return std::copy(first, last, out);
}
// the version that the type of delimiter is const char_type* const
template<typename Value, typename InputIterator, typename Ostream>
inline std::ostream_iterator<Value, typename Ostream::char_type>
print( InputIterator first, InputIterator last, Ostream& ostream,
const typename Ostream::char_type* const delimiter) {
return print(
first, last,
std::ostream_iterator< Value,
typename Ostream::char_type>(
ostream, delimiter));
}
// print_op with unary oprator
template< typename InputIterator, typename OstreamIterator,
typename UnaryOperator>
inline OstreamIterator
print_op( InputIterator first, InputIterator last,
OstreamIterator out, UnaryOperator op) {
return std::transform(first, last, out, op);
}
// the version that the type of delimiter is const char_type* const
template< typename Value, typename InputIterator,
typename Ostream, typename UnaryOperator>
inline std::ostream_iterator<Value, typename Ostream::char_type>
print_op( InputIterator first, InputIterator last,
Ostream& ostream,
const typename Ostream::char_type* const delimiter,
UnaryOperator op) {
return print_op(
first, last,
std::ostream_iterator< Value,
typename Ostream::char_type>(
ostream, delimiter), op);
}
// print_op with binary oprator
template< typename InputIterator1, typename InputIterator2,
typename OstreamIterator, typename BinaryOperator>
inline OstreamIterator
print_op( InputIterator1 first1, InputIterator1 last1,
InputIterator2 first2, OstreamIterator out,
BinaryOperator op) {
return std::transform(first1, last1, first2, out, op);
}
// the version that the type of delimiter is const char_type>* const
template< typename Value, typename InputIterator1,
typename InputIterator2, typename Ostream,
typename BinaryOperator>
inline std::ostream_iterator<Value, typename Ostream::char_type>
print_op( InputIterator1 first1, InputIterator1 last1,
InputIterator2 first2, Ostream& ostream,
const typename Ostream::char_type* const delimiter,
BinaryOperator op) {
return print_op(
first1, last1, first2,
std::ostream_iterator< Value,
typename Ostream::char_type>(
ostream, delimiter), op);
}
// print_if
template< typename InputIterator, typename OstreamIterator,
typename Predicate>
inline OstreamIterator
print_if( InputIterator first, InputIterator last,
OstreamIterator out, Predicate pred) {
return util::algorithm::copy_if(first, last, out, pred);
}
// the version that the type of delimiter is const char_type* const
template< typename Value, typename InputIterator,
typename Ostream, typename Predicate>
inline std::ostream_iterator<Value, typename Ostream::char_type>
print_if( InputIterator first, InputIterator last,
Ostream& ostream,
const typename Ostream::char_type* const delimiter,
Predicate pred) {
return print_if(
first, last,
std::ostream_iterator< Value,
typename Ostream::char_type>(
ostream, delimiter), pred);
}
// grep in Perl
template< typename InputIterator, typename SequenceContainer,
typename Predicate>
inline SequenceContainer&
grep( InputIterator first, InputIterator last,
SequenceContainer& result, Predicate pred) {
result.resize(0);
for (; first != last; ++first)
if (pred(*first)) result.push_back(*first);
return result;
}
template< typename InputIterator, typename Value,
typename Predicate>
inline std::vector<Value>&
grep( InputIterator first, InputIterator last,
std::vector<Value>& result, Predicate pred) {
unsigned int n = std::count_if(first, last, pred);
result.resize(n);
result.reserve(n);
util::algorithm::copy_if(first, last, result.begin(), pred);
return result;
}
/*
* A class to show progress
* Usage:
*
* std::transform(
* longlongstring.begin(), longlongstring.end(),
* std::ostream_iterator<char>(std::cout),
* basic_progress<char, unsigned int>(
* std::cerr, 4096, numof_data));
* */
template<typename Result, typename Base>
class basic_progress {
public:
typedef Result result_type;
typedef Base base_type;
protected:
std::ostream* out;
base_type frequency;
base_type denominator;
base_type numerator;
public:
basic_progress(
std::ostream& out,
const base_type& frequency,
const base_type& denominator,
const base_type& numerator = 0)
: out(&out), frequency(frequency),
denominator(denominator), numerator(numerator) {}
result_type operator()(const result_type& result) {
++numerator;
if ( numerator % frequency == 0
|| numerator == denominator) {
output(*out, numerator, denominator);
}
return result;
}
protected:
virtual void output( std::ostream& out,
const base_type& numerator,
const base_type& denominator) {
double percentage =
static_cast<double>(numerator) / denominator * 100;
out
<< "\r"
<< numerator << "/" << denominator << " samples"
<< " (" << percentage << "%)";
}
};
}
}
#endif // ALGORITHM_HPP