-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.cpp
More file actions
117 lines (104 loc) · 3.54 KB
/
utils.cpp
File metadata and controls
117 lines (104 loc) · 3.54 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
#include <algorithm>
#include <chrono>
#include <random>
#include <set>
#include "utils.h"
Utils::Utils() : generator(std::chrono::system_clock::now().time_since_epoch().count()) {
}
bool Utils::getRandomBool() {
return getRandomInt(0, 1) == 0;
}
int Utils::getRandomInt(int minValue, int maxValue) {
std::uniform_int_distribution<int> distribution(minValue, maxValue);
return distribution(generator);
}
int64_t Utils::getRandomInt64(int64_t minValue, int64_t maxValue) {
std::uniform_int_distribution<int64_t> distribution(minValue, maxValue);
return distribution(generator);
}
char Utils::getRandomChar(char minValue, char maxValue) {
std::uniform_int_distribution<char> distribution(minValue, maxValue);
return distribution(generator);
}
char Utils::getRandomChar(uint_fast8_t charTypeMask) {
int c;
do {
int val = getRandomInt(0, 61);
if (val < 26) {
c = 'a' + val;
} else if (val < 52) {
c = 'A' + val - 26;
} else {
c = '0' + val - 52;
}
} while (!((std::islower(c) && (charTypeMask & CharType::LOWER)) ||
(std::isupper(c) && (charTypeMask & CharType::UPPER)) ||
(std::isdigit(c) && (charTypeMask & CharType::DIGIT))));
return static_cast<char>(c);
}
std::string Utils::getRandomString(int length, char minValue, char maxValue) {
std::string s;
for (int i = 0; i < length; ++i) {
s += getRandomChar(minValue, maxValue);
}
return s;
}
std::string Utils::getRandomString(int length, uint_fast8_t charTypeMask) {
std::string s;
for (int i = 0; i < length; ++i) {
s += getRandomChar(charTypeMask);
}
return s;
}
std::vector<int> Utils::getShuffledSequence(int size, int startFrom) {
std::vector<int> seq(size);
for (int i = 0; i < size; ++i) {
seq[i] = i + startFrom;
}
std::shuffle(seq.begin(), seq.end(), generator);
return seq;
}
std::vector<std::pair<int, int>> Utils::getTree(int nodeCount, int startIndexFrom) {
std::vector<int> indices = getShuffledSequence(nodeCount, startIndexFrom);
std::vector<std::pair<int, int>> edges;
for (int i = 1; i < nodeCount; ++i) {
int parent = getRandomInt(0, i - 1);
edges.emplace_back(indices[parent], indices[i]);
}
std::shuffle(edges.begin(), edges.end(), generator);
return edges;
}
std::vector<std::pair<int, int>> Utils::getGraph(int nodeCount, int edgeCount, int startIndexFrom) {
return addEdgesToGraph(std::vector<std::pair<int, int>>(), nodeCount, edgeCount, startIndexFrom);
}
std::vector<std::pair<int, int>> Utils::getConnectedGraph(int nodeCount, int edgeCount, int startIndexFrom) {
auto edges = getTree(nodeCount, startIndexFrom);
return addEdgesToGraph(edges, nodeCount, edgeCount, startIndexFrom);
}
std::vector<std::pair<int, int>>
Utils::addEdgesToGraph(const std::vector<std::pair<int, int>>& edges, int nodeCount, int totalEdgeCount,
int startIndexFrom) {
std::set<std::pair<int, int>> edgeSet;
for (auto edge : edges) {
if (edge.first > edge.second) {
std::swap(edge.first, edge.second);
}
edgeSet.insert(edge);
}
while (static_cast<int>(edgeSet.size()) < totalEdgeCount) {
int x = getRandomInt(0, nodeCount - 1) + startIndexFrom;
int y = getRandomInt(0, nodeCount - 1) + startIndexFrom;
if (x > y) {
std::swap(x, y);
}
edgeSet.emplace(x, y);
}
std::vector<std::pair<int, int>> edgeList(edgeSet.begin(), edgeSet.end());
std::shuffle(edgeList.begin(), edgeList.end(), generator);
for (auto& edge : edgeList) {
if (getRandomBool()) {
std::swap(edge.first, edge.second);
}
}
return edgeList;
}