-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtransform.h
More file actions
289 lines (235 loc) · 7.62 KB
/
transform.h
File metadata and controls
289 lines (235 loc) · 7.62 KB
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
/*
* =====================================================================================
*
* filename: transform.h
*
* description: STL container transform functions
*
* version: 1.0
* created: 2013-04-08
* revision: none
* compiler: g++ -std=c++11
*
* author: amadeu zou
* company: blog.youtueye.com
*
* copyright: 2013 amadeuzou#gmail.com Inc. All Rights Reserved.
*
* =====================================================================================
*/
#ifndef TRANSFORM_H
#define TRANSFORM_H
#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <algorithm>
#include <iterator>
/* function like object for container sort */
template<typename T1, typename T2>
struct less_second
{
typedef std::pair<T1, T2> type;
bool operator ()(type const &x, type const &y) const
{
/* sort descending , not ascending */
return x.second > y.second;
}
};
/*array to vector*/
template <typename T, size_t N>
static inline std::vector<T> array_to_vector(T const(& src)[N])
{
return std::vector<T>(src, src + sizeof(src) / sizeof(T));
}
/*vector to array*/
template <typename T, size_t N>
static inline size_t vector_to_array(const std::vector<T> &src, T (dst)[N])
{
size_t count = std::min(N, src.size());
std::copy(src.begin(), src.begin()+count, dst);
return count;
}
/*array to vector pair*/
template <typename T, size_t N>
static inline std::vector< std::pair<T, T> > array_to_vector(T const(& src)[N][2])
{
std::vector< std::pair<T, T> > result;
result.reserve(N);
std::transform(
src, src+N, std::inserter(result, result.begin()),
[] (T const(&p)[2]) { return std::make_pair(p[0], p[1]); }
);
return result;
}
/*array to vector*/
template <typename T1, typename T2, size_t N>
static inline std::vector< std::pair<T1, T2> > array_to_vector(T1 const(& first)[N], T2 const(& second)[N] )
{
std::vector< std::pair<T1, T2> > result;
result.reserve(N);
std::transform(
first, first+N, second, std::inserter(result, result.begin()),
[] (T1 const &x, T2 const &y) { return std::make_pair(x, y); }
);
return result;
}
/*array to vector*/
template <typename T1, typename T2, size_t N>
static inline std::vector< std::pair<T1, T2> > array_to_vector(const std::vector<T1> &first, const std::vector<T2> &second)
{
size_t count = std::min(N, std::min(first.size(), second.size()));
std::vector< std::pair<T1, T2> > result;
result.reserve(count);
std::transform(
first, first+count, second, std::inserter(result, result.begin()),
[] (T1 const &x, T2 const &y) { return std::make_pair(x, y); }
);
return result;
}
/*vector pair to array*/
template <typename T, size_t N>
static inline size_t vector_to_array(const std::vector< std::pair<T, T> > &src, T (dst)[N][2])
{
size_t count = std::min(N, src.size());
std::transform(
src.begin(), src.begin() + count, dst,
[] (const std::pair<T, T> &v) {T p[2]; p[0] = v.first; p[1] = v.second; return p; }
);
return count;
}
/*vector pair to array*/
template <typename T1, typename T2, size_t N>
static inline size_t vector_to_array(const std::vector< std::pair<T1, T2> > &src, T1 (first)[N], T2 (second)[N])
{
size_t count = std::min(N, src.size());
std::transform(
src.begin(), src.begin() + count, first,
[] (const std::pair<T1, T2> &v) { return v.first; }
);
std::transform(
src.begin(), src.begin() + count, second,
[] (const std::pair<T1, T2> &v) { return v.second; }
);
return count;
}
/*vector pair to array*/
template <typename T1, typename T2, size_t N>
static inline size_t vector_to_array(const std::vector< std::pair<T1, T2> > &src, std::vector<T1> &first, std::vector<T2> &second)
{
size_t count = std::min(N, src.size());
first.resize(count);
second.resize(count);
std::transform(
src.begin(), src.begin() + count, std::inserter(first, first.begin()),
[] (const std::pair<T1, T2> &v) { return v.first; }
);
std::transform(
src.begin(), src.begin() + count, std::inserter(second, second.begin()),
[] (const std::pair<T1, T2> &v) { return v.second; }
);
return count;
}
/*array to map*/
template <typename T, size_t N>
static inline std::map<T, T> array_to_map(T const(& src)[N][2])
{
std::map<T, T> result;
std::transform(
src, src+N, std::inserter(result, result.begin()),
[] (T const(&p)[2]) { return std::make_pair(p[0], p[1]); }
);
return result;
}
/*array to map*/
template <typename T1, typename T2, size_t N>
static inline std::map<T1, T2> array_to_map(T1 const(& first)[N], T2 const(& second)[N] )
{
std::map<T1, T2> result;
std::transform(
first, first+N, second, std::inserter(result, result.begin()),
[] (T1 const &x, T2 const &y) { return std::make_pair(x, y); }
);
return result;
}
/*array to map*/
template <typename T1, typename T2, size_t N>
static inline std::map<T1, T2> array_to_map(const std::vector<T1> &first, const std::vector<T2> &second)
{
size_t count = std::min(N, std::min(first.size(), second.size()));
std::map<T1, T2> result;
std::transform(
first, first+count, second, std::inserter(result, result.begin()),
[] (T1 const &x, T2 const &y) { return std::make_pair(x, y); }
);
return result;
}
/*map to array*/
template <typename T, size_t N>
static inline size_t map_to_array(const std::map<T, T>src, T (dst)[N][2])
{
size_t count = std::min(N, src.size());
std::transform(
src.begin(), src.begin() + count, dst,
[] (const std::pair<T, T> &v) {T p[2]; p[0] = v.first; p[1] = v.second; return p; }
);
return count;
}
/*map to array*/
template <typename T1, typename T2, size_t N>
static inline size_t map_to_array(const std::map<T1, T2> &src, T1 (first)[N], T2 (second)[N])
{
size_t count = std::min(N, src.size());
std::transform(
src.begin(), src.begin() + count, first,
[] (const std::pair<T1, T2> &v) { return v.first; }
);
std::transform(
src.begin(), src.begin() + count, second,
[] (const std::pair<T1, T2> &v) { return v.second; }
);
return count;
}
/*map to array*/
template <typename T1, typename T2, size_t N>
static inline size_t map_to_array(const std::map<T1, T2> &src, std::vector<T1> &first, std::vector<T2> &second)
{
size_t count = std::min(N, src.size());
first.resize(count);
second.resize(count);
std::transform(
src.begin(), src.begin() + count, std::inserter(first, first.begin()),
[] (const std::pair<T1, T2> &v) { return v.first; }
);
std::transform(
src.begin(), src.begin() + count, std::inserter(second, second.begin()),
[] (const std::pair<T1, T2> &v) { return v.second; }
);
return count;
}
/*map to array*/
template <typename T1, typename T2, size_t N>
static inline std::vector<std::pair<T1, T2> > map_to_vector(const std::map<T1, T2> &src)
{
size_t count = std::min(N, src.size());
std::vector< std::pair<T1, T2> > dst;
dst.reserve(count);
std::transform(
src.begin(), src.begin() + count, std::inserter(dst, dst.begin()),
[] (const std::pair<T1, T2> &v) { return v; }
);
return count;
}
/*vector to map*/
template <typename T1, typename T2, size_t N>
static inline std::map<T1, T2> vector_to_map(const std::vector<std::pair<T1, T2> > &src)
{
size_t count = std::min(N, src.size());
std::map<T1, T2> dst;
std::transform(
src.begin(), src.begin() + count, std::inserter(dst, dst.begin()),
[] (const std::pair<T1, T2> &v) { return v; }
);
return count;
}
#endif