-
Notifications
You must be signed in to change notification settings - Fork 0
/
arr_optimization.cc
executable file
·453 lines (358 loc) · 14.2 KB
/
arr_optimization.cc
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
445
446
447
448
449
450
451
452
453
#include "arr_optimization.hpp"
#include "config.hpp"
// 初始化一下,看看有没有压缩的可能性
// 这个数组必然是增序的
linear_compress_t *init_linear_compressor(void *source_arr, data_type type, unsigned long arr_size, bool need_checked)
{
// cout << "arr_size:" << arr_size << ", source_arr:" << source_arr << endl;
if (arr_size <= 1)
{
return NULL;
}
assert(arr_size > 1 && source_arr != NULL);
// 首先查看前两位
unsigned long first_content = read_from_array_with_data_type(source_arr, type, 0);
unsigned long second_content = read_from_array_with_data_type(source_arr, type, 1);
if (second_content < first_content)
{
return NULL;
}
// 两位之间的两位之间的相减就是斜率
unsigned long tmp_coef = second_content - first_content;
// 看看是不是需要检查
if (need_checked == true)
{
// 遍历剩下的内容,看看是不是一样的步长递进
for (unsigned long i = 0; i < arr_size - 1; i++)
{
first_content = read_from_array_with_data_type(source_arr, type, i);
second_content = read_from_array_with_data_type(source_arr, type, i + 1);
if (second_content < first_content)
{
return NULL;
}
if (second_content - first_content != tmp_coef)
{
cout << "linear_compress_t:can not pass the check in index:" << i << endl;
return NULL;
}
}
}
linear_compress_t *return_compressor = new linear_compress_t();
// 这里是通过了检查
return_compressor->coefficient = tmp_coef;
return_compressor->intercept = read_from_array_with_data_type(source_arr, type, 0);
return return_compressor;
}
// 对变量进行赋值,传入两个变量,被赋值的变量,用来索引数组的变量
string code_of_arr_read(linear_compress_t *compressor, string output_var_name, string index_var_name)
{
assert(compressor != NULL);
string return_str = output_var_name + " = " + index_var_name;
if (compressor->coefficient != 1)
{
return_str = return_str + " * " + to_string(compressor->coefficient);
}
// 根据截距是不是0来决定要不要包含截距
if (compressor->intercept != 0)
{
return_str = return_str + " + " + to_string(compressor->intercept);
}
return return_str;
}
constant_compress_t *init_constant_compressor(void *source_arr, data_type type, unsigned long arr_size, bool need_checked)
{
assert(source_arr != NULL && arr_size > 0);
unsigned long first_content = read_from_array_with_data_type(source_arr, type, 0);
if (need_checked == true)
{
for (unsigned long i = 0; i < arr_size; i++)
{
// 检查每一位是不是都一样
if (read_from_array_with_data_type(source_arr, type, i) != first_content)
{
cout << "constant_compress_t:can not pass the check in index:" << i << endl;
return NULL;
}
}
}
constant_compress_t *return_compressor = new constant_compress_t();
// 通过了检查
return_compressor->constant = first_content;
return return_compressor;
}
// 赋值的代码
string code_of_arr_read(constant_compress_t *compressor, string output_var_name, string index_var_name)
{
assert(compressor != NULL);
string return_str = output_var_name + " = " + to_string(compressor->constant);
return return_str;
}
branch_compress_t *init_branch_compressor(void *source_arr, data_type type, unsigned long arr_size, bool need_checked)
{
assert(source_arr != NULL && arr_size > 2);
vector<unsigned long> new_low_bound_vec;
vector<unsigned long> new_up_bound_vec;
vector<unsigned long> new_val_vec;
// 用一开始那一个初始化
new_low_bound_vec.push_back(0);
new_val_vec.push_back(read_from_array_with_data_type(source_arr, type, 0));
// 最多只能有三个分支
for (unsigned long i = 0; i < arr_size; i++)
{
// 如果发现不一样的,就补上下界,并且定义新的值和上界
unsigned long cur_val = read_from_array_with_data_type(source_arr, type, i);
if (cur_val != new_val_vec[new_val_vec.size() - 1])
{
new_up_bound_vec.push_back(i - 1);
// 设定新的下界和值
new_low_bound_vec.push_back(i);
new_val_vec.push_back(cur_val);
if (new_up_bound_vec.size() >= get_config()["BRANCH_COMPRESS_MAX_SIZE"].as_integer())
{
cout << "branch_compress_t:too many branches at index:" << i << endl;
cout << "[";
for (unsigned j = 0; j < new_low_bound_vec.size(); j++)
{
cout << new_low_bound_vec[j] << ",";
}
cout << "]" << endl;
// cout << new_up_bound_vec << endl;
return NULL;
}
}
}
assert(new_up_bound_vec.size() < get_config()["BRANCH_COMPRESS_MAX_SIZE"].as_integer() && new_low_bound_vec.size() <= get_config()["BRANCH_COMPRESS_MAX_SIZE"].as_integer() && new_val_vec.size() <= get_config()["BRANCH_COMPRESS_MAX_SIZE"].as_integer());
// 用arr-size - 1 初始化上界
new_up_bound_vec.push_back(arr_size - 1);
// 初始化压缩元数据
branch_compress_t *compressor = new branch_compress_t();
compressor->index_low_bound = new_low_bound_vec;
compressor->index_up_bound = new_up_bound_vec;
compressor->constant = new_val_vec;
return compressor;
}
// 分支压缩对应的代码
string code_of_arr_read(branch_compress_t *compressor, string output_var_name, string index_var_name)
{
assert(compressor != NULL);
assert(compressor->index_low_bound.size() <= get_config()["BRANCH_COMPRESS_MAX_SIZE"].as_integer() && compressor->index_up_bound.size() <= get_config()["BRANCH_COMPRESS_MAX_SIZE"].as_integer() && compressor->constant.size() <= get_config()["BRANCH_COMPRESS_MAX_SIZE"].as_integer());
string return_str = "\n";
// 遍历所有分支生成代码
for (unsigned long branch_index = 0; branch_index < compressor->index_low_bound.size(); branch_index++)
{
unsigned long low_bound = compressor->index_low_bound[branch_index];
unsigned long up_bound = compressor->index_up_bound[branch_index];
unsigned long constant = compressor->constant[branch_index];
assert(low_bound <= up_bound);
// 如果上界和下界相等,就用等号,反之用大于小于号
if (low_bound == up_bound)
{
return_str = return_str + "if(" + index_var_name + "==" + to_string(low_bound) + ")\n{\n";
}
else
{
return_str = return_str + "if(" + index_var_name + " <= " + to_string(up_bound) + " && " + index_var_name + " >= " + to_string(low_bound) + ")\n{\n";
}
// 这里填充一个赋值语句
return_str = return_str + output_var_name + " = " + to_string(constant) + ";\n";
// 根据是不是最后一个分支处理
if (branch_index == compressor->index_low_bound.size() - 1)
{
return_str = return_str + "}\n";
}
else
{
return_str = return_str + "}else ";
}
}
return return_str;
}
cycle_linear_compress_t *init_cycle_linear_compressor(void *source_arr, data_type type, unsigned long arr_size, unsigned long cycle_num, bool need_checked)
{
assert(source_arr != NULL);
// 循环的周期不满足要求就压缩失败
if (cycle_num < arr_size)
{
}
else
{
return NULL;
}
if (arr_size % cycle_num == 0)
{
}
else
{
return NULL;
}
assert(cycle_num < arr_size);
assert(arr_size % cycle_num == 0);
if (cycle_num <= 1)
{
cout << "cycle_linear_compress_t:cycle_num <= 1" << endl;
return NULL;
}
// 用第一个值来初始化截距
unsigned long new_intercept = read_from_array_with_data_type(source_arr, type, 0);
unsigned long new_coefficient = read_from_array_with_data_type(source_arr, type, 1) - read_from_array_with_data_type(source_arr, type, 0);
// 查看是不是要检查
if (need_checked == true)
{
// 如果需要检查,就需要查看是不是满足周期性,是不是每个周期的起始位置和斜率都一样
// 遍历所有的非零元
for (unsigned long i = 0; i < arr_size; i++)
{
// 获取当前数组的值
unsigned long item_val = read_from_array_with_data_type(source_arr, type, i);
// 查看当前索引和在一个周期中的位置
unsigned long index_inner_cycle = i % cycle_num;
if (index_inner_cycle != 0)
{
// 看看斜率是不是正确,这里要求斜率要是一个整数
assert(item_val >= new_intercept && (item_val - new_intercept) % index_inner_cycle == 0);
}
if (index_inner_cycle != 0 && (item_val - new_intercept) / index_inner_cycle != new_coefficient)
{
// 这里代表不符合周期性斜率
cout << "cycle_linear_compress_t:can not pass the check in index:" << i << endl;
return NULL;
}
}
}
// 这里代表符合周期律,初始化并输出
cycle_linear_compress_t *compressor = new cycle_linear_compress_t();
compressor->cycle = cycle_num;
compressor->coefficient = new_coefficient;
compressor->intercept = new_intercept;
return compressor;
}
// 打印对应优化后的代码
string code_of_arr_read(cycle_linear_compress_t *compressor, string output_var_name, string index_var_name)
{
assert(compressor != NULL);
// 先用索引取余,然后乘斜率,最后加上截距
string return_str = output_var_name + " = (" + index_var_name + " % " + to_string(compressor->cycle) + ") * " + to_string(compressor->coefficient);
if (compressor->intercept != 0)
{
return_str = return_str + " + " + to_string(compressor->intercept);
}
return return_str;
}
// 周期自增压缩器的初始化
cycle_increase_compress_t *init_cycle_increase_compressor(void *source_arr, data_type type, unsigned long arr_size, bool need_checked)
{
assert(source_arr != NULL);
// 如果数组的大小不够长,那就放弃
if (arr_size <= 2)
{
cout << "init_cycle_increase_compressor: arr_size is not larger than 2, arr_size: " << arr_size << endl;
return NULL;
}
// for (int i = arr_size - 10; i < arr_size; i++)
// {
// cout << read_from_array_with_data_type(source_arr, type, i) << ",";
// }
// cout << endl;
// 查看第一个元素出现了几次来确定周期
unsigned long first_element = read_from_array_with_data_type(source_arr, type, 0);
unsigned long new_cycle_num = 0;
// 遍历整个数组,直到和first_element元素不一样
for (unsigned long i = 0; i < arr_size; i++)
{
unsigned long cur_element = read_from_array_with_data_type(source_arr, type, i);
// 如果不一样
if (cur_element != first_element)
{
// 如果不是自增的,那就直接退出
if (cur_element < first_element)
{
cout << "init_cycle_increase_compressor:arr is not self-increasing" << endl;
return NULL;
}
// i就是周期大小
// cout << new_cycle_num << endl;
new_cycle_num = i;
// exit(-1);
break;
}
}
assert(new_cycle_num > 0);
// 这些压缩在空行的时候会失效
// 完整的周期检查
if (need_checked == true)
{
// 检查周期是否可以被完整的数组数量整除
if (arr_size % new_cycle_num != 0)
{
cout << "init_cycle_increase_compressor: unvalid num" << endl;
return NULL;
}
for (unsigned long i = 0; i < arr_size; i++)
{
unsigned long cur_element = read_from_array_with_data_type(source_arr, type, i);
// 查看所属的周期
unsigned long cycle_id = (unsigned long)(i / new_cycle_num);
if (cur_element != first_element + cycle_id)
{
cout << "init_cycle_increase_compressor: cycle is not satisfied in index:" << i << endl;
return NULL;
}
}
}
// 通过了检查
cycle_increase_compress_t *compressor = new cycle_increase_compress_t();
// 初始化截距和周期
compressor->cycle = new_cycle_num;
compressor->intercept = first_element;
return compressor;
}
// 打印对应的压缩之后的代码
string code_of_arr_read(cycle_increase_compress_t *compressor, string output_var_name, string index_var_name)
{
assert(compressor != NULL);
string return_str = output_var_name + " = " + index_var_name + " / " + to_string(compressor->cycle);
// 如果截距是0,就不加了
if (compressor->intercept != 0)
{
// 加在后面
return_str = return_str + " + " + to_string(compressor->intercept);
}
return return_str;
}
void delete_compressor_with_type(void* del_compressor, arr_compress_type type)
{
assert(del_compressor != NULL);
if (type == LINEAR_COMPRESS)
{
linear_compress_t* compressor = (linear_compress_t *)del_compressor;
delete compressor;
return;
}
if (type == CONSTANT_COMPRESS)
{
constant_compress_t* compressor = (constant_compress_t *)del_compressor;
delete compressor;
return;
}
if (type == BRANCH_COMPRESS)
{
branch_compress_t* compressor = (branch_compress_t *)del_compressor;
delete compressor;
return;
}
if (type == CYCLE_LINEAR_COMPRESS)
{
cycle_linear_compress_t* compressor = (cycle_linear_compress_t *)del_compressor;
delete compressor;
return;
}
if (type == CYCLE_INCREASE_COMPRESS)
{
cycle_increase_compress_t* compressor = (cycle_increase_compress_t *)del_compressor;
delete compressor;
return;
}
cout << "delete_compressor_with_type: compressor type is not supported" << endl;
assert(false);
}