-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmapscplinear.cpp
More file actions
445 lines (363 loc) · 11 KB
/
Copy pathmapscplinear.cpp
File metadata and controls
445 lines (363 loc) · 11 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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
// Copyright (c) 2024-2026 Stefan Teleman.
//
// Licensed under the MIT License.
// See https://opensource.org/license/mit
// SPDX-License-Identifier: MIT
//
#include <iostream>
#include <iomanip>
#include <vector>
#include <set>
#include <map>
#include <sstream>
#include <fstream>
#include <limits>
#include <algorithm>
#include <cstring>
#include <cstdint>
#include <cassert>
#include <ctime>
#include <cerrno>
#include <getopt.h>
static bool PS;
static int Timestamp(struct timespec* ts) {
if (clock_gettime(CLOCK_MONOTONIC, ts) != 0) {
std::cerr << "clock_gettime(2) failed: " << strerror(errno)
<< std::endl;
return -1;
}
return 0;
}
static void PrintTimediff(const struct timespec* S,
const struct timespec* E) {
struct timespec R;
if (clock_getres(CLOCK_REALTIME, &R) != 0) {
std::cerr << "clock_getres(2) failed: " << strerror(errno)
<< std::endl;
return;
}
int64_t Sec = ((int64_t) E->tv_sec - (int64_t) S->tv_sec) * 1000000000;
int64_t Nns = ((int64_t) E->tv_nsec - (int64_t) S->tv_nsec) / 1000000;
std::cerr << "Clock Resolution: " << (int64_t) R.tv_sec << '.'
<< std::setfill('0') << std::setw(9) << (int64_t) R.tv_nsec
<< '.' << std::endl;
std::cerr << "CPU time: " << (int64_t) Sec / 1000000000L << '.'
<< std::setfill('0') << std::setw(12) << (int64_t) Nns
<< " second(s)." << std::endl;
}
struct SCSet {
uint32_t IX;
std::set<int32_t>* EL;
SCSet()
: IX(static_cast<uint32_t>(~0x0)), EL(new std::set<int32_t>()) { }
~SCSet() = default;
size_t Size() const {
return EL->size();
}
inline bool operator<(const SCSet& R) const {
return *EL < *(R.EL);
}
inline bool operator>(const SCSet& R) const {
return *EL > *(R.EL);
}
};
struct SCSetElement {
uint32_t SetIndex;
uint32_t SetSize;
mutable bool Covered;
SCSetElement() : SetIndex(static_cast<uint32_t>(~0x0)),
SetSize(static_cast<uint32_t>(~0x0)), Covered(false) { }
SCSetElement(uint32_t SI, uint32_t SS)
: SetIndex(SI), SetSize(SS), Covered(false) { }
void SetCovered(bool V) {
Covered = V;
}
void SetCovered(bool V) const {
Covered = V;
}
inline bool operator<(const SCSetElement& R) const {
return SetSize > R.SetSize ? true :
SetSize == R.SetSize ? SetIndex > R.SetIndex : false;
}
inline bool operator==(const SCSetElement& R) const {
return SetIndex == R.SetIndex &&
SetSize == R.SetSize;
}
inline bool operator>(const SCSetElement& R) const {
return !(*this < R) && !(*this == R);
}
void Print() const {
std::cerr << '[' << SetIndex << ':' << SetSize << ':'
<< std::boolalpha << Covered << ']';
}
};
class SCSetMatrix {
private:
std::vector<SCSet> MX;
public:
SCSetMatrix() = default;
void ReadFromFile(const std::string& IN) {
MX.clear();
std::ifstream IFS(IN);
if (IFS.good()) {
std::string Line;
const char* Sep = ", \t\n";
char* End;
uint32_t IX = 0U;
while (std::getline(IFS, Line)) {
SCSet MR;
if (!Line.empty()) {
if (char* Tok = strtok_r(const_cast<char*>(Line.data()), Sep, &End))
MR.EL->insert(std::stoi(Tok));
while (char* Tok = strtok_r(NULL, Sep, &End))
MR.EL->insert(std::stoi(Tok));
MR.IX = IX++;
MX.push_back(MR);
}
}
}
}
void SortLowToHigh() {
auto SCSetSort = [=] (const SCSet& L, const SCSet& R) {
return L.Size() < R.Size() ? true : L.Size() == R.Size() ? L < R : false;
};
std::sort(MX.begin(), MX.end(), SCSetSort);
}
void SortHighToLow() {
auto SCSetSort = [=] (const SCSet& L, const SCSet& R) {
return L.Size() > R.Size() ? true : L.Size() == R.Size() ? L > R : false;
};
std::sort(MX.begin(), MX.end(), SCSetSort);
}
void Clear() {
MX.clear();
}
const std::vector<SCSet>& Matrix() const {
return MX;
}
size_t Size() const {
return MX.size();
}
};
class SCPLinear {
private:
struct SCSetComp {
inline bool operator()(const SCSetElement& L, const SCSetElement& R) const {
return L.SetSize > R.SetSize ? true :
L.SetSize == R.SetSize ? L > R : false;
}
};
private:
struct SCSetSizeComp {
inline bool operator()(size_t L, size_t R) const {
return L > R;
}
};
private:
std::vector<SCSet> Sets;
std::vector<const std::set<int32_t>*> UNV;
std::map<int32_t, std::set<SCSetElement, SCSetComp>> SCSetElementMap;
std::map<uint32_t, const std::set<int32_t>* const> SCSetIndexMap;
std::multimap<size_t, const std::set<int32_t>* const, SCSetSizeComp> SCSetSizeMap;
std::vector<uint32_t> CSetIndexVec;
std::set<int32_t> UniverseSet;
uint64_t IT;
uint32_t MC;
void PrintSet(const std::set<int32_t>& S) const {
std::cerr << "{ ";
if (!S.empty()) {
std::set<int32_t>::const_iterator I = S.begin();
std::set<int32_t>::const_iterator E = S.end();
std::cerr << *I;
while (++I != E) {
std::cerr << ", " << *I;
}
}
std::cerr << " }";
}
public:
SCPLinear()
: Sets(), UNV(), SCSetElementMap(), SCSetIndexMap(), SCSetSizeMap(),
CSetIndexVec(), UniverseSet(), IT(0UL), MC(static_cast<uint32_t>(~0x0)) { }
uint64_t Iterations() const {
return IT;
}
uint32_t MinCoverage() const {
return MC == static_cast<uint32_t>(~0x0) ? 0 : MC;
}
const std::vector<const std::set<int32_t>*>& Universe() const {
return UNV;
}
void SetUniverse(const std::vector<const std::set<int32_t>*>& UV) {
UNV = UV;
}
void CreateUniverse(const std::vector<SCSet>& M) {
UNV.clear();
std::vector<SCSet>::const_iterator I = M.begin();
std::vector<SCSet>::const_iterator E = M.end();
std::map<int32_t, std::set<SCSetElement, SCSetComp>>::iterator MI;
while (I != E) {
const std::set<int32_t>* const S = (*I).EL;
UniverseSet.insert((*I).EL->begin(), (*I).EL->end());
UNV.push_back((*I).EL);
SCSetSizeMap.insert(std::make_pair((*I).EL->size(), (*I).EL));
for (std::set<int32_t>::const_iterator SI = S->begin();
SI != S->end(); ++SI) {
MI = SCSetElementMap.find(*SI);
if (MI == SCSetElementMap.end()) {
SCSetElement SEL((*I).IX, (*I).EL->size());
std::set<SCSetElement, SCSetComp> SCM;
SCM.insert(SEL);
SCSetElementMap.insert(std::make_pair(*SI, SCM));
SCSetIndexMap.insert(std::make_pair((*I).IX, (*I).EL));
} else {
std::set<SCSetElement, SCSetComp>& SCM = (*MI).second;
SCSetElement SEL((*I).IX, (*I).EL->size());
SCM.insert(SEL);
SCSetIndexMap.insert(std::make_pair((*I).IX, (*I).EL));
}
}
++I;
}
}
void PrintSetUniverse() const {
if (PS) {
std::multimap<size_t, const std::set<int32_t>* const,
SCSetSizeComp>::const_iterator I = SCSetSizeMap.begin();
std::multimap<size_t, const std::set<int32_t>* const,
SCSetSizeComp>::const_iterator E = SCSetSizeMap.end();
while (I != E) {
std::cerr << '[' << (*I).first << "]: ";
PrintSet(*(*I).second);
std::cerr << std::endl;
++I;
}
}
}
void LinearSetCoverage() {
std::set<int32_t> CoveredSet;
std::map<const std::set<int32_t>* const, uint32_t> SCSetMapIX;
uint32_t IX = 0U;
CSetIndexVec.clear();
MC = 0U;
for (std::vector<const std::set<int32_t>*>::iterator UI = UNV.begin();
UI != UNV.end(); ++UI) {
SCSetMapIX.insert(std::make_pair((*UI), IX++));
}
std::multimap<size_t, const std::set<int32_t>* const,
SCSetSizeComp>::const_iterator MBI = SCSetSizeMap.begin();
std::multimap<size_t, const std::set<int32_t>* const,
SCSetSizeComp>::const_iterator MEI = SCSetSizeMap.end();
std::multimap<size_t, const std::set<int32_t>* const,
SCSetSizeComp>::const_iterator NEI = MBI;
std::map<const std::set<int32_t>* const, uint32_t>::const_iterator IXMI;
while (CoveredSet.size() < UniverseSet.size()) {
if (MBI == MEI)
break;
const std::set<int32_t>* MSet = (*MBI).second;
++NEI;
if (!std::includes(CoveredSet.begin(), CoveredSet.end(),
MSet->begin(), MSet->end())) {
CoveredSet.insert(MSet->begin(), MSet->end());
IXMI = SCSetMapIX.find(MSet);
assert(IXMI != SCSetMapIX.end() && "Set not found in the SetIndexMap!");
CSetIndexVec.push_back((*IXMI).second);
++IT;
++MC;
}
if (NEI != MEI) {
const std::set<int32_t>* NSet = (*NEI).second;
MBI = NEI;
if (!std::includes(CoveredSet.begin(), CoveredSet.end(),
NSet->begin(), NSet->end())) {
CoveredSet.insert(NSet->begin(), NSet->end());
IXMI = SCSetMapIX.find(NSet);
assert(IXMI != SCSetMapIX.end() && "Set not found in the SetIndexMap!");
CSetIndexVec.push_back((*IXMI).second);
++IT;
++MC;
}
}
++MBI;
}
}
void PrintCoverageSets() const {
for (std::vector<uint32_t>::const_iterator I = CSetIndexVec.begin();
I != CSetIndexVec.end(); ++I) {
std::map<uint32_t, const std::set<int32_t>* const>::const_iterator MI =
SCSetIndexMap.find(*I);
if (MI != SCSetIndexMap.end()) {
std::cerr << '[' << *I << "]: ";
PrintSet(*((*MI).second));
std::cerr << std::endl;
}
}
}
};
static bool ValidateArguments(const std::string& In) {
return !In.empty();
}
static void PrintHelp() {
std::cerr << "Usage: mapscplinear [-h | --help]" << std::endl;
std::cerr << " [ -i <input-file> | --input <input-file>]"
<< std::endl;
}
static struct option long_options[] = {
{ "help", no_argument, 0, 'h' },
{ "input", required_argument, 0, 'i' },
{ "print", required_argument, 0, 'p' },
{ 0, 0, 0, 0 }
};
int main(int argc, char* const argv[])
{
int C;
int OIx = 0;
std::string InFile;
while (1) {
C = getopt_long(argc, argv, "hpi:", long_options, &OIx);
if (C == -1)
break;
switch (C) {
case 'i':
InFile = optarg;
break;
case 'p':
PS = true;
break;
case 'h':
PrintHelp();
return 0;
break;
default:
PrintHelp();
return 1;
break;
}
}
if (!ValidateArguments(InFile)) {
PrintHelp();
return 1;
}
SCSetMatrix SCSM;
SCSM.ReadFromFile(InFile);
SCPLinear SCPL;
SCPL.CreateUniverse(SCSM.Matrix());
SCPL.LinearSetCoverage();
struct timespec TSStart;
struct timespec TSEnd;
Timestamp(&TSStart);
uint32_t MC = SCPL.MinCoverage();
Timestamp(&TSEnd);
if (MC > 0) {
SCPL.PrintCoverageSets();
std::cout << "MinCover: " << SCPL.MinCoverage() << " Iterations: "
<< SCPL.Iterations() << std::endl;
PrintTimediff(&TSStart, &TSEnd);
} else {
std::cerr << "No covering possible. Iterations: "
<< SCPL.Iterations() << '.' << std::endl;
PrintTimediff(&TSStart, &TSEnd);
}
SCPL.PrintSetUniverse();
return 0;
}