-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.hpp
326 lines (288 loc) · 6.89 KB
/
utils.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
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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
/**
* @file utils.hpp: various utilities for
* Cube Product Checker program.
*
* Contains power function,
* Timewatch class,
* Random class,
* mm_bitset type alias,
* mm_vector_with_properties class,
* mm_vector_with_properties_options class,
* Vectors_Presolve_Data class.
*
* @author Eugene Petkevich
* @version pre-alpha
*/
#ifndef UTILS_HPP_INCLUDED
#define UTILS_HPP_INCLUDED
#include <ctime>
#include <random>
#include <set>
#include <vector>
#include <bitset>
#include <chrono>
using namespace std;
//=============================================================================
/**
* Type alias for bitset.
*/
template <size_t N>
using mm_bitset = bitset<N>;
//=============================================================================
int power(int a, int b); /// Rise a to power of b.
int popcount(int x); /// Return number of set bits in a number.
//=============================================================================
/**
* Timewatch class, to measure time intervals.
*/
class Timewatch {
public:
double timestamp; /// Timestamp of the last call.
Timewatch();
double watch(); /// Get interval duration since last call.
};
//=============================================================================
/**
* Random class, for integer uniform distribution.
*/
class Random {
public:
mt19937 generator;
uniform_int_distribution<int>* distribution;
Random();
~Random();
void init(int i, int s); /// Init with interval [i,s].
int next(); /// Return random integer form the interval.
};
//=============================================================================
/**
* Options for adding randomized bits to vectors.
*
* Contains sets of bit indices to add up
* for making additional bits in vectors.
*/
class mm_vector_with_properties_options
{
public:
vector<set<int>> rnd_sets; /// Sets of the bit indices.
};
/**
* Bitset vector with additional properties.
*
* Additional bits are added
* which are linear combinations of original bits.
*
* @param N: size of the bitsets.
*/
template <size_t N>
class mm_vector_with_properties {
public:
mm_bitset<N> v; /// Main bitset.
int v_count; /// number of set bits in v
static const int rnd_size = 0; /// Number of terms for linear combination.
static const int rnd_count = 0; /// Number of additional bits.
mm_bitset<rnd_size> r; /// Additional bits.
int r_count; /// number of set bits in r
void calculate_properties(mm_vector_with_properties_options& o); /// Calculate additional bits.
static void make_options(mm_vector_with_properties_options& o); /// Make options randomly.
};
//=============================================================================
/**
* Bitset set properties.
*
* Stores some properties of a set of bitsets,
* to do fast check for linear independence.
*
* @param N: size of the bitsets.
*/
template <size_t N>
class Vectors_Presolve_Data
{
public:
mm_bitset<N> mor; /// Arithmetical OR of the all bitsets.
Vectors_Presolve_Data();
void add_vector(const mm_bitset<N>& a); /// Add a bitset.
void presolve(); /// presolve
bool check(const mm_bitset<N>& a) const; /// Check if a vector could be in the span.
};
//=============================================================================
//=============================================================================
/**
* Constructor.
*/
Timewatch::Timewatch()
{
timestamp = omp_get_wtime();
}
/**
* Get interval duration since last call.
*
* @return interval duration in seconds.
*/
double
Timewatch::watch()
{
double elapsed_secs = double(omp_get_wtime() - timestamp);
timestamp = omp_get_wtime();
return elapsed_secs;
}
//=============================================================================
Random::Random() :
generator(chrono::system_clock::now().time_since_epoch().count())
{
distribution = new uniform_int_distribution<int>(0, 1);
}
Random::~Random()
{
delete distribution;
}
/**
* Init with interval [i,s].
*
* @param lower bound of the interval (inclusive);
* @param upper bound of the interval (inclusive).
*/
void
Random::
init(int i, int s)
{
delete distribution;
distribution = new uniform_int_distribution<int>(i, s);
}
/**
* Return random integer form the interval.
*
* @return resulting random number.
*/
int
Random::
next()
{
return (*distribution)(generator);
}
//=============================================================================
/**
* Calculate properties of the vector.
*
* @param o: options.
*/
template <size_t N>
void
mm_vector_with_properties<N>::
calculate_properties(mm_vector_with_properties_options& o)
{
v_count = v.count();
for (int i = 0; i < rnd_size; ++i) {
r.set(i,false);
for (set<int>::iterator j = o.rnd_sets[i].begin(); j != o.rnd_sets[i].end(); ++j) {
r[i] = r[i] != v[*j];
}
}
r_count = r.count();
}
/**
* Generate options randomly.
*
* @param o: options that will be changed.
*/
template <size_t N>
void
mm_vector_with_properties<N>::
make_options(mm_vector_with_properties_options& o)
{
Random g;
g.init(0, N-1);
for (int i = 0; i < rnd_size; ++i) {
set<int> c;
for (int j = 0; j < rnd_count; ++j) {
int n = g.next();
while (c.find(n) != c.end()) {
n = g.next();
}
c.insert(n);
}
o.rnd_sets.push_back(c);
}
}
//=============================================================================
/**
* Constructor.
*/
template <size_t N>
Vectors_Presolve_Data<N>::
Vectors_Presolve_Data()
{
mor.reset();
}
/**
* Add a bitvector to the current set.
*
* @param a: bitvector to add.
*/
template <size_t N>
void
Vectors_Presolve_Data<N>::
add_vector(const mm_bitset<N>& a)
{
mor |= a;
}
/**
* Presolve for the current set.
*/
template <size_t N>
void
Vectors_Presolve_Data<N>::
presolve()
{
mor = ~mor;
}
/**
* Check if a vector could be in the span of current set.
*
* @param a: a vector to check.
*
* @return false if the vector is definitely not in the span.
*/
template <size_t N>
bool Vectors_Presolve_Data<N>::check(const mm_bitset<N>& a) const
{
if ((mor & a).any())
return false;
return true;
}
//=============================================================================
/**
* Rise a to power of b.
*
* @param a: base;
* @param b: exponent.
*
* @return a^b.
*/
int
power(int a, int b)
{
if (b == 0) {
return 1;
}
int result = a;
while (--b) {
result *= a;
}
return result;
}
//=============================================================================
/**
* Return number of set bits in a number (max 16 bit).
*
* @param x: the number;
*
* @return the number of set bits.
*/
int popcount(int x)
{
bitset<16> b(x);
return b.count();
}
//=============================================================================
#endif // UTILS_HPP_INCLUDED