-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathquery_builder.h
134 lines (108 loc) · 2.81 KB
/
query_builder.h
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
#pragma once
#include "array.h"
#include "common.h"
#include "random.h"
#include "rnda.h"
#include <algorithm>
#include <utility>
namespace jngen {
class QueryBuilder {
public:
QueryBuilder() {}
QueryBuilder(int n);
QueryBuilder(int l, int r);
QueryBuilder& minLen(int value);
QueryBuilder& maxLen(int value);
QueryBuilder& range(int n);
QueryBuilder& range(int l, int r);
QueryBuilder& small();
QueryBuilder& large();
QueryBuilder& ordered(bool value);
std::pair<int, int> next();
Arrayp next(int m);
private:
enum class QueryType {
Default, Large, Small
};
QueryType queryType_ = QueryType::Default;
std::pair<int, int> range_; // half-interval
std::pair<int, int> lenRange_; // segment
bool ordered_;
};
#ifndef JNGEN_DECLARE_ONLY
QueryBuilder::QueryBuilder(int n) :
QueryBuilder(0, n - 1)
{ }
QueryBuilder::QueryBuilder(int l, int r) :
range_(l, r + 1),
lenRange_(1, r - l + 1),
ordered_(true)
{
ensure(l <= r);
}
QueryBuilder& QueryBuilder::minLen(int value) {
lenRange_.first = value;
return *this;
}
QueryBuilder& QueryBuilder::maxLen(int value) {
lenRange_.second = value;
return *this;
}
QueryBuilder& QueryBuilder::range(int n) {
ensure(n > 0);
return this->range(0, n - 1);
}
QueryBuilder& QueryBuilder::range(int l, int r) {
ensure(l <= r);
range_ = {l, r+1};
lenRange_.second = std::min(lenRange_.second, r - l + 1);
lenRange_.first = std::min(lenRange_.first, lenRange_.second);
return *this;
}
QueryBuilder& QueryBuilder::small() {
queryType_ = QueryType::Small;
return *this;
}
QueryBuilder& QueryBuilder::large() {
queryType_ = QueryType::Large;
return *this;
}
QueryBuilder& QueryBuilder::ordered(bool value) {
ordered_ = value;
return *this;
}
std::pair<int, int> QueryBuilder::next() {
switch (queryType_) {
case QueryType::Default: {
// This is inaccurate to say the least. I don't know how to
// generate a random segment with length from l to r without
// calling sqrt.
int len = rnd.wnext(lenRange_.first, lenRange_.second, -1);
int l = rnd.next(range_.first, range_.second - len);
if (ordered_ || rnd.next(2)) {
return {l, l + len - 1};
} else {
return {l + len - 1, l};
}
}
case QueryType::Large: {
ENSURE(false, "not implemented");
break;
}
case QueryType::Small: {
ENSURE(false, "not implemented");
break;
}
default: ENSURE(false, "Nonexistent option");
}
}
Arrayp QueryBuilder::next(int m) {
return rnda.randomf(m, [this]() { return next(); });
}
#endif
template<typename ... Args>
QueryBuilder rndq(Args... args) {
return QueryBuilder(args...);
}
} // namespace jngen
using jngen::rndq;