-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSarrayTypes.cpp
More file actions
184 lines (176 loc) · 4.04 KB
/
Copy pathSarrayTypes.cpp
File metadata and controls
184 lines (176 loc) · 4.04 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
# include "SarrayTypes.h"
// A constructor
Rsarray :: Rsarray()
{
N = rn = 0;
generator.seed(system_clock :: now().time_since_epoch().count());
}
// A function that generates a random sorted array of given distribution
void Rsarray :: generate(int n, int d, Timer &t1, int l, int r)
{
g.clear();
if(d == 0)
{
uniform_int_distribution <int> distribution(l, r);
rn = distribution(generator);
for(int i = 0; i < n; i++)
{
g.push_back(rn);
t1.time(1);
}
}
else if(d == 1)
{
rn = l;
for(int i = 0; i < n; i++)
{
g.push_back(rn);
t1.time(1);
}
}
else if(d == 2)
{
rn = r;
for(int i = 0; i < n; i++)
{
g.push_back(rn);
t1.time(1);
}
}
else if(d == 3)
{
uniform_int_distribution <int> distribution(l, r);
for(int i = 0; i < n; i++)
{
rn = distribution(generator);
g.push_back(rn);
t1.time(1);
}
}
else if(d == 7)
{
int p = rand() % 101;
binomial_distribution <int> distribution((l + r) / 2, p / 100.0);
for(int i = 0; i < n; i++)
{
rn = distribution(generator);
g.push_back(l + rn % (r - l + 1));
t1.time(1);
}
}
else if(d == 8)
{
uniform_real_distribution <float> d1(l, r);
poisson_distribution <int> distribution(d);
for(int i = 0; i < n; i++)
{
rn = distribution(generator);
g.push_back(l + rn % (r - l + 1));
t1.time(1);
}
}
else if(d == 9)
{
uniform_real_distribution <float> d1(l, r);
float mid = d1(generator);
array <float, 3> intervals {l * 1.0, mid, r * 1.0};
array <float, 3> weights {10.0, 1.0, 10.0};
piecewise_linear_distribution <float> distribution(intervals.begin(), intervals.end(), weights.begin());
for(int i = 0; i < n; i++)
{
rn = l + ((int)(distribution(generator))) % (r - l + 1);
g.push_back(rn);
t1.time(1);
}
}
}
// A function that generates sorted array test case files
void Rsarray :: setCase(string &s, int T, int t, int n, int l, int r, int d, bool neg, int sz, string &folder_name)
{
int pt = FileOp :: printT(t);
cout << "Generating sarray test files: " << '\n';
for(int i = 0; i < T; i++)
{
FileOp :: setFile(folder_name, s, i, fout);
vector <int> times = numOp :: giveRints(t, n, sz);
N = times.size();
if(pt)
fout << N << '\n';
int tcnt = 0;
for(int j = 0; j < N; j++)
tcnt += times[j];
Timer t1(2 * tcnt);
vector <int> dist = setCaseDist(N, d, neg);
for(int j = 0; j < N; j++)
{
fout << times[j] << '\n';
generate(times[j], dist[j], t1, l, r);
sort(g.begin(), g.end());
for(int k = 0; k < times[j]; k++)
{
fout << g[k] << " \n"[i == times[j] - 1];
t1.time(1);
}
}
fout.close();
}
cout << "sarray generation completed." << '\n';
}
// A function that sets distribution for all test cases
vector <int> Rsarray :: setCaseDist(int n, int d, bool neg)
{
Distribution d1;
vector <int> dist;
d1.setCaseDis(n, d, dist, (int)1, neg);
return dist;
}
// A constructor
Rsdarray :: Rsdarray()
{
N = 0;
generator.seed(system_clock :: now().time_since_epoch().count());
}
// A function that generates a random sorted array of distinct elements
void Rsdarray :: generate(int n, Timer &t1, int l, int r)
{
g.clear();
uniform_int_distribution <int> distribution(l, r);
while((int)g.size() < n)
{
int t = g.size();
int rn = distribution(generator);
g.insert(rn);
if((int)g.size() > t)
t1.time(1);
}
}
// A function that generates sorted distinct elements test case files
void Rsdarray :: setCase(string &s, int T, int t, int n, int l, int r, int sz, string &folder_name)
{
int pt = FileOp :: printT(t);
cout << "Generating sdarray test files: " << '\n';
for(int i = 0; i < T; i++)
{
FileOp :: setFile(folder_name, s, i, fout);
vector <int> times = numOp :: giveRints(t, n, sz);
N = times.size();
if(pt)
fout << N << '\n';
int tcnt = 0;
for(int j = 0; j < N; j++)
tcnt += times[j];
Timer t1(2 * tcnt);
for(int j = 0; j < N; j++)
{
fout << times[j] << '\n';
generate(times[j], t1, l, r);
for(auto k : g)
{
fout << k << " \n"[i == times[j] - 1];
t1.time(1);
}
}
fout.close();
}
cout << "sdarray generation completed." << '\n';
}