-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenrandomdata.cpp
More file actions
304 lines (256 loc) · 7.49 KB
/
genrandomdata.cpp
File metadata and controls
304 lines (256 loc) · 7.49 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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
// Copyright (c) 2024-2026 Stefan Teleman.
//
// Licensed under the MIT License.
// See https://opensource.org/license/mit
// SPDX-License-Identifier: MIT
//
#include <iostream>
#include <vector>
#include <set>
#include <fstream>
#include <cstdlib>
#include <cstring>
#include <cstdint>
#include <cmath>
#include <ctime>
#include <getopt.h>
static std::vector<std::set<uint32_t>> SV;
static std::set<uint32_t> FUS;
static inline int32_t GenSetElement(uint32_t ub) {
uint32_t e = 0;
do {
e = (int32_t) lrand48();
} while (e > ub || e < 1U);
return (int32_t) e;
}
static inline uint32_t GenLowerBound(uint32_t ub) {
uint32_t lb = 0;
do {
lb = (int32_t) lrand48();
} while (lb > ub);
return lb;
}
static inline uint32_t GenUpperBound(uint32_t lim) {
uint32_t ub = 0;
do {
ub = (int32_t) lrand48();
} while (ub > lim);
return ub;
}
static inline uint32_t GenSetSize(uint32_t usize, uint32_t mSize, uint32_t MSize) {
uint32_t ssize = 0;
do {
ssize = (uint32_t) lrand48();
} while (ssize > MSize || ssize > usize || ssize < mSize);
return ssize;
}
bool WriteFile(const std::string& Filename) {
std::ofstream OF(Filename);
if (!OF.good()) {
std::cerr << "Could not open file " << Filename << " for writing."
<< std::endl;
return false;
}
std::vector<std::set<uint32_t>>::const_iterator VI = SV.begin();
std::vector<std::set<uint32_t>>::const_iterator VE = SV.end();
while (VI != VE) {
const std::set<uint32_t>& S = *VI;
std::set<uint32_t>::const_iterator SI = S.begin();
std::set<uint32_t>::const_iterator SE = S.end();
OF << *SI;
while (++SI != SE) {
OF << ", " << *SI;
}
OF << '\n';
++VI;
}
OF.close();
return true;
}
void PrintSet(const std::set<uint32_t>& S) {
std::cerr << "{ ";
if (!S.empty()) {
std::set<uint32_t>::const_iterator I = S.begin();
std::set<uint32_t>::const_iterator E = S.end();
std::cerr << *I;
while (++I != E) {
std::cerr << ", " << *I;
}
}
std::cerr << " }" << std::endl;
}
void PrintSetVector(const std::vector<std::set<uint32_t>> SV) {
if (!SV.empty()) {
std::cerr << "======================================" << std::endl;
std::vector<std::set<uint32_t>>::const_iterator I = SV.begin();
std::vector<std::set<uint32_t>>::const_iterator E = SV.end();
while (I != E) {
std::cerr << "--------------------------------------" << std::endl;
PrintSet(*I);
std::cerr << "--------------------------------------" << std::endl;
++I;
}
std::cerr << "======================================" << std::endl;
}
}
static void PrintHelp()
{
std::cerr << "Usage: genrandomdata [--help | -h]" << std::endl;
std::cerr << " (print this message)" << std::endl;
std::cerr << " [--quiet | -q]" << std::endl;
std::cerr << " (nothing printed to stdout)" << std::endl;
std::cerr << " [--auto | -a]" << std::endl;
std::cerr << " (auto-fill the generated sets to cover the full universe)" << std::endl;
std::cerr << " [--fixed | -f]" << std::endl;
std::cerr << " (constant (fixed) set size. maxsize == minsize)." << std::endl;
std::cerr << " [--output <filename> | -o <filename>]"
<< std::endl;
std::cerr << " (output filename)" << std::endl;
std::cerr << " [--usize <universe-size> | -u <universe-size>]"
<< std::endl;
std::cerr << " (number of elements in the universe)" << std::endl;
std::cerr << " [--minsize <minimum-subset-size> | -m <minimum-subset-size>]" << std::endl;
std::cerr << " (minimum size of a generated subset)" << std::endl;
std::cerr << " [--maxsize <maximum-subset-size> | -M <maximum-subset-size>]" << std::endl;
std::cerr << " (maximum size of a generated subset)" << std::endl;
std::cerr << " [--nsets <number-of-sets> | -n <number-of-sets>]"
<< std::endl;
}
static bool CheckArguments(uint32_t U, uint32_t S, bool A) {
return A ? U != 0U : U != 0U && S != 0U;
}
static bool ValidateUniverseSets(uint32_t USize) {
return FUS.size() == USize;
}
static struct option long_options[] = {
{ "help", no_argument, 0, 'h' },
{ "quiet", no_argument, 0, 'q' },
{ "auto", no_argument, 0, 'a' },
{ "fixed", no_argument, 0, 'f' },
{ "output", required_argument, 0, 'o' },
{ "usize", required_argument, 0, 'u' },
{ "nsets", required_argument, 0, 'n' },
{ "minsize", required_argument, 0, 'm' },
{ "maxsize", required_argument, 0, 'M' },
{ 0, 0, 0, 0 }
};
int main(int argc, char* const argv[])
{
int c;
int OIx = 0;
uint32_t USize = 0U;
uint32_t NSets = 0U;
uint32_t mSize = 4U;
uint32_t MSize = 8U;
uint32_t GSets = 0;
std::string Outfile;
bool Quiet = false;
bool Autofill = false;
bool Fixed = false;
while (1) {
c = getopt_long(argc, argv, "afhqo:u:m:M:n:",
long_options, &OIx);
if (c == -1)
break;
switch (c) {
case 'o':
Outfile = optarg;
break;
case 'u':
USize = (uint32_t) std::stoul(optarg);
break;
case 'n':
NSets = (uint32_t) std::stoul(optarg);
break;
case 'm':
mSize = (uint32_t) std::stoul(optarg);
break;
case 'M':
MSize = (uint32_t) std::stoul(optarg);
break;
case 'q':
Quiet = true;
break;
case 'a':
Autofill = true;
break;
case 'f':
Fixed = true;
break;
default:
PrintHelp();
return 1;
break;
}
}
if (!CheckArguments(USize, NSets, Autofill)) {
PrintHelp();
return 1;
}
if (Fixed)
MSize = mSize;
srand48(time(0));
if (Autofill) {
while (FUS.size() < USize) {
uint32_t SSize = GenSetSize(USize, mSize, MSize);
std::set<uint32_t> TS;
while (TS.size() < SSize) {
uint32_t E = GenSetElement(USize);
if (TS.insert(E).second) {
FUS.insert(E);
if (!Quiet)
std::cerr << E << ' ';
}
}
SV.push_back(TS);
++GSets;
if (!Quiet)
std::cerr << std::endl;
}
if (GSets < NSets) {
for (uint32_t j = 0; j < NSets - GSets; ++j) {
uint32_t SSize = GenSetSize(USize, mSize, MSize);
std::set<uint32_t> TS;
while (TS.size() < SSize) {
uint32_t E = GenSetElement(USize);
if (TS.insert(E).second) {
FUS.insert(E);
if (!Quiet)
std::cerr << E << ' ';
}
}
SV.push_back(TS);
if (!Quiet)
std::cerr << std::endl;
}
}
} else {
for (uint32_t j = 0; j < NSets; ++j) {
uint32_t SSize = GenSetSize(USize, mSize, MSize);
std::set<uint32_t> TS;
while (TS.size() < SSize) {
uint32_t E = GenSetElement(USize);
if (TS.insert(E).second) {
FUS.insert(E);
if (!Quiet)
std::cerr << E << ' ';
}
}
SV.push_back(TS);
if (!Quiet)
std::cerr << std::endl;
}
}
if (!Quiet)
PrintSetVector(SV);
if (!ValidateUniverseSets(USize)) {
std::cerr << "Generated Universe Sets do not cover the entire Universe."
<< std::endl;
return 1;
}
if (!Outfile.empty()) {
if (!WriteFile(Outfile))
return 1;
}
return 0;
}