-
Notifications
You must be signed in to change notification settings - Fork 1
/
Vars.cpp
208 lines (187 loc) · 4.17 KB
/
Vars.cpp
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
#include <string>
#include <map>
#include <vector>
#include <iostream>
#include <sstream>
#include <gmpxx.h>
#include "utils.h"
#include "Vars.h"
using namespace std;
#define COST_SCALAR_MUL 5
#define COST_SCALAR_NEG 2
#define COST_SCALAR_COPY 1
// deletes variables with value 0
void Vars::reduce() {
for(map<int, mpz_class>::iterator it = var_map.begin(); it != var_map.end();) {
if((it->second % mod) == 0) {
it = var_map.erase(it);
} else {
it++;
}
}
}
void Vars::add_var(char type, int idx, int val) {
int s = var_offset(type);
var_map[4*idx + s] = val % mod;
}
bool Vars::has_var(char type, int idx) {
int s = var_offset(type);
int key = 4*idx + s;
return var_map.count(key) != 0;
}
mpz_class Vars::get_var(char type, int idx) {
int s = var_offset(type);
int key = 4*idx + s;
return var_map[key];
}
void Vars::index_temp_vars(map<int, vector<int>>& index, int i, Linear& eq, bool check_dups /* = false */) {
reduce();
for (auto const&x : var_map) {
if (var_type(x.first) == 'T') {
int idx = var_idx(x.first);
// cout << "considering T" << idx << " for indexing" << endl;
if (check_dups && eq.has_var('T', idx))
continue;
/* if (idx == 7) {
cout << "indexing: T" << idx << " at: " << i << endl;
eq.to_str();
cout << endl;
}*/
if (index.count(idx) == 0)
index[idx] = {i};
else
index[idx].push_back(i);
}
}
}
int Vars::num_vars() {
return var_map.size();
}
void Vars::add(Vars& other) {
for (auto const&x : other.var_map) {
if (var_map.find(x.first) == var_map.end()) {
var_map[x.first] = x.second % mod;
} else {
var_map[x.first] = (var_map[x.first] + x.second) % mod;
}
}
reduce();
}
void Vars::sub(Vars& other) {
for (auto const&x : other.var_map) {
if (var_map.find(x.first) == var_map.end()) {
var_map[x.first] = (mod - x.second) % mod;
} else {
var_map[x.first] = (var_map[x.first] - x.second + mod) % mod;
}
}
reduce();
}
void Vars::mul(mpz_class v) {
for (auto const&x : var_map) {
var_map[x.first] = (var_map[x.first] * v) % mod;
}
}
void Vars::div(mpz_class v) {
for (auto const&x : var_map) {
var_map[x.first] = (var_map[x.first] / v) % mod;
}
}
bool Vars::is_empty() {
return var_map.empty();
}
bool Vars::is_zero() {
for (auto const&x : var_map) {
if (x.second != 0) {
return false;
}
}
return true;
}
bool Vars::has_temp_var() {
for (auto const&x : var_map) {
if (var_type(x.first) == 'T') {
return true;
}
}
return false;
}
bool Vars::has_several_temps() {
size_t count = 0;
for (auto const&x : var_map) {
if (var_type(x.first) == 'T') {
count += 1;
if (count == 2)
return true;
}
}
return false;
}
void Vars::str(mpz_class constant) {
vector<string> terms;
for (auto const&e : var_map) {
string name = var_str(e.first);
mpz_class val = e.second;
stringstream s;
if (val == 1)
terms.push_back(name);
else if ((val + 1) % mod == 0)
terms.push_back("-" + name);
else if (mod - val < 10000) {
s << "-" << mod-val << "*" << name;
terms.push_back(s.str());
} else {
s << (val) << "*" << name;
terms.push_back(s.str());
}
}
stringstream s;
if (terms.empty() || constant != 0) {
if (mod - constant < 10000) {
s << "-" << mod-constant;
terms.push_back(s.str());
}
else {
s << constant;
terms.push_back(s.str());
}
}
for (auto const&x : terms) {
cout << x << " + ";
}
}
int Vars::cost() {
int total = 0;
map<mpz_class, int> negs;
map<mpz_class, int> counts;
mpz_class high = 0;
for (auto& e : var_map) {
mpz_class val = e.second;
total += 1;
int negate = 0;
mpz_class diff = mod - val;
if (diff < val) {
val = diff;
negate = 1;
}
if (counts.count(val) != 0) {
counts[val] += 1;
negs[val] += negate;
} else {
counts[val] = 1;
negs[val] = negate;
}
if (high == 0 || counts[val] > counts[high]) {
high = val;
}
}
int plus_one = max(negs[high], counts[high] - negs[high]);
int neg_one = counts[high] - plus_one;
return COST_SCALAR_MUL * (total - plus_one - neg_one) + COST_SCALAR_NEG * neg_one + COST_SCALAR_COPY;
}
map<int, mpz_class>::iterator Vars::vars_begin() {
return var_map.begin();
}
map<int, mpz_class>::iterator Vars::vars_end() {
return var_map.end();
}