-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsubsets_gen.cpp
278 lines (251 loc) · 9.56 KB
/
subsets_gen.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
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
#include <iostream> // for std::cout
#include <vector> // for std::vector
#include <concepts> // std::convertible_to
#include <functional> // for std::function type erasure
#include <utility> // for std::declval
#include <cstdint> // for std::uint64_t
#include <chrono> // for std::chrono::time_point<std::chrono::steady_clock> std::chrono::duration, std::chrono::duration_cast etc.
template<typename T>
concept ReadOnlyContainer = requires(const T& container)
{
container.cbegin();
container.cend();
{ container.size() } -> std::integral;
};
template<ReadOnlyContainer T>
std::ostream& operator<<(std::ostream& stream, const T& container) noexcept
{
stream << "{ ";
for(decltype(container.size()) i = 0; auto& value : container)
{
stream << value;
if((i + 1) != container.size())
stream << ", ";
++i;
}
stream << " }";
return stream;
}
template<typename T>
concept ReadOnlyVector = requires(const T& vector)
{
typename T::value_type;
typename T::size_type;
{ vector.size() } -> std::convertible_to<typename T::size_type>;
vector.operator[](std::declval<typename T::size_type>());
};
// Time complexity: T(n) = 2 * T(n - 1) + c
template<ReadOnlyVector T>
void traverse_subsets(const T& set, typename T::size_type index, std::vector<typename T::value_type>& buf, std::function<void(void)>& callback) noexcept
{
if(index == set.size())
{
callback();
return;
}
buf.push_back(set[index]);
traverse_subsets(set, index + 1, buf, callback);
buf.pop_back();
traverse_subsets(set, index + 1, buf, callback);
}
struct subset_traverser_1
{
template<ReadOnlyVector T>
void operator()(const T& set, std::vector<typename T::value_type>& buf, std::function<void(void)>& callback) noexcept
{
std::chrono::time_point<std::chrono::steady_clock> start = std::chrono::steady_clock::now();
traverse_subsets(set, 0, buf, callback);
auto end = std::chrono::steady_clock::now();
auto timeElapsed = std::chrono::duration_cast<std::chrono::duration<double, std::milli>>(end - start).count();
std::cout << "ST1, Time Elapsed: " << timeElapsed << " ms" << std::endl;
}
};
// Time Complexity: T(n) = n * 2^n
template<ReadOnlyVector T>
void traverse_subsets2(const T& set, std::vector<typename T::value_type>& buf, std::function<void(void)>& callback) noexcept
{
auto n = set.size();
auto bitCount = static_cast<std::uint64_t>(1) << n;
for(decltype(bitCount) i = 0; i < bitCount; ++i)
{
buf.clear();
for(typename T::size_type j = 0; j < n; ++j)
if(i & (static_cast<std::uint64_t>(1) << j))
buf.push_back(set[j]);
callback();
}
}
struct subset_traverser_2
{
template<ReadOnlyVector T>
void operator ()(const T& set, std::vector<typename T::value_type>& buf, std::function<void(void)>& callback) noexcept
{
std::chrono::time_point<std::chrono::steady_clock> start = std::chrono::steady_clock::now();
traverse_subsets2(set, buf, callback);
auto end = std::chrono::steady_clock::now();
auto timeElapsed = std::chrono::duration_cast<std::chrono::duration<double, std::milli>>(end - start).count();
std::cout << "ST2, Time Elapsed: " << timeElapsed << " ms" << std::endl;
}
};
// NOTE: it just puts indices of the elements, not their values
// Time Complexity: T(n) = n * 2^n
template<ReadOnlyVector T> requires (std::integral<typename T::value_type>)
void traverse_subsets3(const T& set, std::vector<typename T::value_type>& buf, std::function<void(void)>& callback) noexcept
{
auto n = set.size();
auto bitCount = static_cast<std::uint64_t>(1) << n;
for(decltype(bitCount) i = 0; i < bitCount; ++i)
{
buf.clear();
for(typename T::value_type j = 0; j < n; ++j)
if(i & (static_cast<std::uint64_t>(1) << j))
buf.push_back(j);
callback();
}
}
struct subset_traverser_3
{
template<ReadOnlyVector T>
void operator ()(const T& set, std::vector<typename T::value_type>& buf, std::function<void(void)>& callback) noexcept
{
std::chrono::time_point<std::chrono::steady_clock> start = std::chrono::steady_clock::now();
traverse_subsets3(set, buf, callback);
auto end = std::chrono::steady_clock::now();
auto timeElapsed = std::chrono::duration_cast<std::chrono::duration<double, std::milli>>(end - start).count();
std::cout << "ST3, Time Elapsed: " << timeElapsed << " ms" << std::endl;
}
};
template<ReadOnlyVector T> requires (std::integral<typename T::value_type>)
void traverse_subsets4(const T& set, std::vector<typename T::value_type>& buf, std::function<void(void)>& callback) noexcept
{
auto n = set.size();
auto bitCount = static_cast<std::uint64_t>(1) << n;
for(decltype(bitCount) i = 0; i < bitCount; ++i)
{
buf.clear();
for(int j = 0; j < n; ++j)
if(i & (static_cast<std::uint64_t>(1) << j))
buf.push_back(j);
callback();
}
}
struct subset_traverser_4
{
template<ReadOnlyVector T>
void operator ()(const T& set, std::vector<typename T::value_type>& buf, std::function<void(void)>& callback) noexcept
{
std::chrono::time_point<std::chrono::steady_clock> start = std::chrono::steady_clock::now();
traverse_subsets4(set, buf, callback);
auto end = std::chrono::steady_clock::now();
auto timeElapsed = std::chrono::duration_cast<std::chrono::duration<double, std::milli>>(end - start).count();
std::cout << "ST4, Time Elapsed: " << timeElapsed << " ms" << std::endl;
}
};
template<ReadOnlyVector T> requires (std::integral<typename T::value_type>)
void traverse_subsets5(const T& set, std::vector<typename T::value_type>& buf, std::function<void(void)>& callback) noexcept
{
auto n = set.size();
auto bitCount = static_cast<std::uint64_t>(1) << n;
for(decltype(bitCount) i = 0; i < bitCount; ++i)
{
buf.clear();
for(unsigned int j = 0; j < n; ++j)
if(i & (static_cast<std::uint64_t>(1) << j))
buf.push_back(j);
callback();
}
}
struct subset_traverser_5
{
template<ReadOnlyVector T>
void operator ()(const T& set, std::vector<typename T::value_type>& buf, std::function<void(void)>& callback) noexcept
{
std::chrono::time_point<std::chrono::steady_clock> start = std::chrono::steady_clock::now();
traverse_subsets5(set, buf, callback);
auto end = std::chrono::steady_clock::now();
auto timeElapsed = std::chrono::duration_cast<std::chrono::duration<double, std::milli>>(end - start).count();
std::cout << "ST5, Time Elapsed: " << timeElapsed << " ms" << std::endl;
}
};
template<typename T>
typename std::add_lvalue_reference<T>::type decllval()
{
return *reinterpret_cast<T*>(nullptr);
}
template<typename S, typename T>
concept SubsetTraverser = ReadOnlyVector<T> && requires(S s, T t)
{
std::is_default_constructible_v<S>;
s(t, decllval<std::vector<typename T::value_type>>(), decllval<std::function<void(void)>>());
};
template<ReadOnlyVector T, SubsetTraverser<T> S = subset_traverser_1>
std::vector<std::vector<typename T::value_type>> get_subsets(const T& set) noexcept
{
std::vector<std::vector<typename T::value_type>> subsets;
subsets.reserve(1 << set.size());
std::vector<typename T::value_type> buf;
buf.reserve(set.size());
// NOTE: here std::function<void> results in in-complete type definition, so we are using std::function<void(void)>
std::function<void(void)> fn = [&buf, &subsets]()
{
std::vector<typename T::value_type> cloned = buf;
subsets.push_back(std::move(cloned));
};
S { } (set, buf, fn);
return subsets;
}
std::vector<std::vector<int>> get_subsets_int(const std::vector<int>& set) noexcept
{
std::vector<std::vector<int>> subsets;
std::size_t n = set.size();
subsets.reserve(1 << n);
std::vector<int> buf;
buf.reserve(n);
for(std::size_t i = 0; i < (1 << n); ++i)
{
buf.clear();
for(std::size_t j = 0; j < n; ++j)
if(i & (1 << j))
buf.push_back(set[j]);
subsets.push_back(std::vector<int> { buf });
}
return subsets;
}
// Output on Core i5 8th gen
// With intType = int;
// Given set: { 1, 2, 3, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19 }
// ST1, Time Elapsed: 3571.2 ms
// ST2, Time Elapsed: 6276.73 ms
// ST3, Time Elapsed: 9437.55 ms <--- slower than ST4 as it contains unsigned to signed conversion
// ST4, Time Elapsed: 5915.98 ms <--- faster than ST3!
// ST5, Time Elapsed: 9484.11 ms <--- same as ST3 as it also contains unsigned to signed conversion
//
// With intType = unsigned int;
// Given set: { 1, 2, 3, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19 }
// ST1, Time Elapsed: 3553.49 ms
// ST2, Time Elapsed: 6367.11 ms
// ST3, Time Elapsed: 9557.46 ms <--- slower than ST5, though it doesn't contain any sign conversion but it does bit-width conversion
// ST4, Time Elapsed: 9564.46 ms <--- slower than ST5 as there is signed to unsigned conversion
// ST5, Time Elapsed: 6111.33 ms <--- faster than ST3 as there is no unsigned to signed conversion
//
// If we use 'unsigned int' in the inner loop in ST3, with intType = unsigned int, ST3 becomes as fast as ST5, because there would no bit-width conversion then I guess.
// Conclusion:
// I think convertion from unsigned to signed (or opposite), and bit-width conversion might be additional overheads
// And they can add significant difference in performance.
int main()
{
using intType = unsigned int;
std::vector<intType> set = { 1, 2, 3 };
for(int i = 0; i < 20; i++)
set.push_back(i);
std::cout << "Given set: " << set << std::endl;
auto result1 = get_subsets<std::vector<intType>, subset_traverser_1>(set);
auto result2 = get_subsets<std::vector<intType>, subset_traverser_2>(set);
auto result3 = get_subsets<std::vector<intType>, subset_traverser_3>(set);
auto result4 = get_subsets<std::vector<intType>, subset_traverser_4>(set);
auto result5 = get_subsets<std::vector<intType>, subset_traverser_5>(set);
// std::cout << "Subsets: " << std::endl;
// for(auto& subset : subsets)
// std::cout << subset << std::endl;
return 0;
}