-
Notifications
You must be signed in to change notification settings - Fork 1
/
function3_L.cpp
1934 lines (1739 loc) · 54.1 KB
/
function3_L.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
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
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//
// function3_L.cpp
// 6Tree
//
// Created by Zhizhu Liu on 2019/12/10.
//
#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
#include <string>
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <ctime>
#include <sys/stat.h>
#include <sys/types.h>
#include "definition.hpp"
#include "function1_T.hpp"
#include "function2_G.hpp"
#include "function3_L.hpp"
using namespace std;
void f3_trim(string &s)
{
// Delete space characters in the string.
int index = 0;
if (!s.empty())
{
while ((index = (int )(s.find(' ', index))) != string::npos)
{
s.erase(index, 1);
}
}
}
void f3_initialize_DS_expr(struct PreparedSpaceTreeNode *node, struct DimenStack *parent_DS, int dimensionality, string *arr)
{
// Initialize the dimensional stack and the region expression.
// Its index starts from 0.
// Inherit the stack from the parent node.
node->DS.stack = new int [dimensionality];
int *times = new int [dimensionality];
for (int i = 0; i < dimensionality; i++)
{
times[i] = 0;
}
int parent_num = parent_DS->num;
int num = 0;
for (int i = 0; i < parent_num; i++)
{
node->DS.stack[i] = parent_DS->stack[i];
times[node->DS.stack[i]]++;
}
num = parent_num;
node->DS.num = num;
// Initialize the region expression, and it needs update.
node->subspace = arr[node->inf];
// Push new steady dimensions into the DS.
int inf = node->inf;
int sup = node->sup;
for (int i = 0; i < dimensionality; i++)
{
if (times[i] == 0)
{
bool steady = true;
for (int j = inf; j < sup; j++)
{
if (arr[j][i] != arr[j + 1][i])
{
steady = false;
break;
}
}
if (steady == true)
{
node->DS.stack[num++] = i;
times[i]++;
}
}
}
node->DS.num = num;
// Update the region expression, based on the current DS.
int *dimen_time = new int [dimensionality];
for (int i = 0; i < dimensionality; i++)
{
dimen_time[i] = 0;
}
for (int i = 0; i < num; i++)
{
dimen_time[node->DS.stack[i]] = 1;
}
for (int i = 0; i < dimensionality; i++)
{
if (dimen_time[i] == 0)
{
node->subspace[i] = '*';
}
}
int children_num = node->children_num;
for (int i = 0; i < children_num; i++)
{
f3_initialize_DS_expr(node->children[i], &(node->DS), dimensionality, arr);
}
// For leaf nodes, push remaining dimensions.
if (children_num == 0)
{
for (int i = 0; i < dimensionality; i++)
{
if (times[i] == 0)
{
node->DS.stack[num++] = i;
times[i]++;
}
}
}
node->DS.num = num;
delete [] times;
}
int f3_DS_pop(struct PreparedSpaceTreeNode *node)
{
// Pop a dimension from the DS.
int num = node->DS.num;
num--;
int res = node->DS.stack[num];
node->DS.num = num;
return res;
}
void f3_TS_expand(struct PreparedSpaceTreeNode *node, int dimen)
{
// Expand the TS based on a dimension.
int num = node->TS.num;
string *exps = node->TS.expressions;
for (int i = 0; i < num; i++)
{
exps[i][dimen] = '*';
}
sort(exps, exps + num, f1_str_cmp);
int new_num;
new_num = (int)(unique(exps, exps + num) - exps);
node->TS.num = new_num;
}
void f3_initialize_TS_SS_NDA(struct PreparedSpaceTreeNode **node_arr, int node_num, string *arr)
{
for (int i = 1; i <= node_num; i++)
{
struct PreparedSpaceTreeNode *node = node_arr[i];
node->NDA = 0;
node->SS.num = 0;
node->SS.expressions = NULL;
if (node->children_num == 0)
{
int inf = node->inf;
int sup = node->sup;
node->TS.num = sup - inf + 1;
node->TS.expressions = new string [sup - inf + 1];
int step = 0;
for (int j = inf; j <= sup; j++)
{
node->TS.expressions[step++] = arr[j];
}
int dimen = f3_DS_pop(node);
f3_TS_expand(node, dimen);
}
else
{
node->TS.num = 0;
node->TS.expressions = NULL;
}
}
}
struct PreparedSpaceTreeNode *f3_prepare_space_tree(string treedir_name)
{
// Read the space tree information, and configure necessary data structures.
ifstream treefile;
string line;
treefile.open("./" + treedir_name + "/" + _TREE_FILE);
// Read the base number.
getline(treefile, line);
vector<string> split_res = f1_str_split(line, ':');
string num_str = split_res[1];
split_res.clear();
f3_trim(num_str);
base_num = atoi(num_str.c_str());
// Read the number of space tree nodes.
getline(treefile, line);
split_res = f1_str_split(line, ':');
num_str = split_res[1];
split_res.clear();
f3_trim(num_str);
int node_num = atoi(num_str.c_str());
// Generate a node array, then read node information.
// Its index starts from 1.
struct PreparedSpaceTreeNode **node_arr = new struct PreparedSpaceTreeNode *[node_num + 10];
getline(treefile, line);
while (getline(treefile, line))
{
split_res = f1_str_split(line, ',');
string str = split_res[0];
f3_trim(str);
int num = atoi(str.c_str());
str = split_res[1];
f3_trim(str);
int inf = atoi(str.c_str());
str = split_res[2];
f3_trim(str);
int sup = atoi(str.c_str());
str = split_res[3];
f3_trim(str);
int parent_num = atoi(str.c_str());
str = split_res[4];
f3_trim(str);
split_res.clear();
int children_num = atoi(str.c_str());
node_arr[num] = new struct PreparedSpaceTreeNode;
node_arr[num]->number = num;
node_arr[num]->inf = inf;
node_arr[num]->sup = sup;
node_arr[num]->is_aliased = false;
if (parent_num == 0)
{
node_arr[num]->parent = NULL;
}
else
{
node_arr[num]->parent = node_arr[parent_num];
int original_children_num = node_arr[parent_num]->children_num;
node_arr[parent_num]->children[original_children_num] = node_arr[num];
node_arr[parent_num]->children_num++;
}
node_arr[num]->children_num = 0;
if (children_num != 0)
{
node_arr[num]->children = new struct PreparedSpaceTreeNode *[children_num + 2];
}
else
{
node_arr[num]->children = NULL;
}
}
treefile.close();
// Read the vector sequence information.
ifstream vecfile;
vecfile.open("./" + treedir_name + "/" + _ARR_FILE);
int arr_scale = 0;
while (getline(vecfile, line))
{
arr_scale++;
}
vecfile.close();
string *arr = new string[arr_scale + 10];
arr_scale = 0;
vecfile.open("./" + treedir_name + "/" + _ARR_FILE);
while (getline(vecfile, line))
{
arr[arr_scale++] = line;
}
vecfile.close();
// Configure necessary data structures.
struct PreparedSpaceTreeNode *root = node_arr[1];
struct DimenStack emptyStack;
emptyStack.num = 0;
emptyStack.stack = NULL;
int dimensionality = f2_get_dimensionality(base_num);
f3_initialize_DS_expr(root, &emptyStack, dimensionality, arr);
f3_initialize_TS_SS_NDA(node_arr, node_num, arr);
delete [] node_arr;
delete [] arr;
return root;
}
struct SequenceNode *f3_gather_leaves(struct PreparedSpaceTreeNode *node, struct SequenceNode *link_node)
{
// Generate the leaf node sequence for the pre-scanning.
if (node->children_num == 0)
{
struct SequenceNode *new_link_node = new struct SequenceNode;
new_link_node->node = node;
new_link_node->next = link_node;
link_node = new_link_node;
}
else
{
int children_num = node->children_num;
for (int i = 0; i < children_num; i++)
{
struct SequenceNode *new_link_node = f3_gather_leaves(node->children[i], link_node);
link_node = new_link_node;
}
}
return link_node;
}
int f3_get_type(int base_num)
{
if (base_num == 2)
{
return _INS_OUTB1;
}
else if (base_num == 4)
{
return _INS_OUTB2;
}
else if (base_num == 8)
{
return _INS_OUTB3;
}
else if (base_num == 16)
{
return _INS_OUTB4;
}
else // base_num == 32
{
return _INS_OUTB5;
}
}
struct SearchTreeNode *f3_generate_search_tree(string *arr, int arr_scale)
{
int dimensionality = f2_get_dimensionality(base_num);
struct SearchTreeNode *root = new struct SearchTreeNode;
root->NAA = 0;
root->level = 0;
root->ch = '-';
root->is_leaf = false;
root->parent = NULL;
root->children_num = base_num;
root->children = new struct SearchTreeNode *[base_num];
for (int i = 0; i < base_num; i++)
{
root->children[i] = NULL;
}
string str = "";
for (int i = 0; i < dimensionality; i++)
{
str += '*';
}
root->expression = str;
for (int i = 0; i < arr_scale; i++)
{
string line = arr[i];
struct SearchTreeNode *sit = root;
sit->NAA++;
for (int j = 0; j < dimensionality; j++)
{
char ch = line[j];
int index;
if (ch >= '0' && ch <= '9')
{
index = ch - '0';
}
else if (ch >= 'A' && ch <= 'V')
{
index = ch - 'A' + 10;
}
else // ch >= 'a' && ch <= 'v'
{
index = ch - 'a' + 10;
}
if (sit->children[index] == NULL)
{
struct SearchTreeNode *new_node = new struct SearchTreeNode;
new_node->NAA = 0;
new_node->level = j + 1;
new_node->ch = ch;
new_node->parent = sit;
if (new_node->level == dimensionality)
{
new_node->is_leaf = true;
new_node->children_num = 0;
new_node->children = NULL;
}
else
{
new_node->is_leaf = false;
new_node->children_num = base_num;
new_node->children = new struct SearchTreeNode *[base_num];
for (int k = 0; k < base_num; k++)
{
new_node->children[k] = NULL;
}
}
new_node->expression = sit->expression;
new_node->expression[j] = ch;
sit->children[index] = new_node;
}
sit = sit->children[index];
sit->NAA++;
}
}
return root;
}
int f3_testtype_tran_intype(int test_type)
{
if (test_type == _INS_TESTSTD)
{
return _INS_INSTD;
}
else if (test_type == _INS_TESTB1)
{
return _INS_INB1;
}
else if (test_type == _INS_TESTB2)
{
return _INS_INB2;
}
else if (test_type == _INS_TESTB3)
{
return _INS_INB3;
}
else if (test_type == _INS_TESTB4)
{
return _INS_INB4;
}
else // test_type == _INS_TESTB1
{
return _INS_INB5;
}
}
struct SearchTreeNode *f3_prepare_test_data(string testfile_name, int test_type, string treedir_name)
{
// Read test address information, and configure a search tree for the local simulation scan.
// 1. Count number of addresses, and generate an array.
ifstream testfile;
string line;
int arr_scale = 0;
testfile.open(testfile_name);
while (getline(testfile, line))
{
arr_scale++;
}
testfile.close();
string *arr = new string[arr_scale + 10];
// 2. Read address vectors.
arr_scale = 0;
testfile.open(testfile_name);
while (getline(testfile, line))
{
int in_type = f3_testtype_tran_intype(test_type);
if (f1_check_intype(in_type, line))
{
arr[arr_scale++] = f1_tran_in_b4(in_type, line);
}
}
testfile.close();
/* // Sort and unique vectors.
sort(arr, arr + arr_scale, f1_str_cmp);
int new_arr_scale;
new_arr_scale = (int)(unique(arr, arr + arr_scale) - arr);
arr_scale = new_arr_scale;
*/
// 3. Adjust the base mode.
ifstream treefile;
treefile.open("./" + treedir_name + "/" + _TREE_FILE);
getline(treefile, line);
vector<string> split_res = f1_str_split(line, ':');
string num_str = split_res[1];
f3_trim(num_str);
base_num = atoi(num_str.c_str());
int tree_type = f3_get_type(base_num);
treefile.close();
for (int i = 0; i < arr_scale; i++)
{
arr[i] = f1_tran_b4_out(tree_type, arr[i]);
}
split_res.clear();
// 4. Generate the search tree and return.
struct SearchTreeNode *search_root = f3_generate_search_tree(arr, arr_scale);
delete [] arr;
return search_root;
}
void f3_read_search_parameters(int &budget, int &itn_budget, string treedir_name)
{
ifstream treefile;
treefile.open("./" + treedir_name + "/" + _SEARCH_FILE);
// The budget.
string line;
getline(treefile, line);
vector<string> split_res = f1_str_split(line, ':');
string num_str = split_res[1];
f3_trim(num_str);
budget = atoi(num_str.c_str());
split_res.clear();
// The step budget.
getline(treefile, line);
split_res = f1_str_split(line, ':');
num_str = split_res[1];
f3_trim(num_str);
itn_budget = atoi(num_str.c_str());
split_res.clear();
treefile.close();
}
void f3_release_region_tree(struct RegionTreeNode *regn_ptr)
{
if (regn_ptr->is_leaf == true)
{
delete regn_ptr;
return ;
}
int children_num = regn_ptr->children_num;
for (int i = 0; i < children_num; i++)
{
if (regn_ptr->children[i] != NULL)
{
f3_release_region_tree(regn_ptr->children[i]);
}
}
delete [] regn_ptr->children;
delete regn_ptr;
}
void f3_region_tree_subtract(struct RegionTreeNode *regn_root, string expr)
{
// The deleted region tree node (which corresponds to the SS) pointer will become NULL.
// 1. From regn_root, search the leaf node whose expression is equal to or include expr.
struct RegionTreeNode *regn_ptr = regn_root;
while (regn_ptr->is_leaf == false)
{
for (int i = 0; i < base_num; i++) // children_num == base_num
{
struct RegionTreeNode *chd_ptr = regn_ptr->children[i];
if (chd_ptr != NULL && f3_expression_belong(expr, chd_ptr->expression))
{
regn_ptr = chd_ptr;
break;
}
}
}
// 2. Perform the subtract operation.
// 2.1 Generate/Find the region tree node which corresponds to the SS (expr).
int dimensionality = f2_get_dimensionality(base_num);
while (regn_ptr->expression != expr)
{
int dft_dimension;
for (dft_dimension = 0; dft_dimension < dimensionality; dft_dimension++)
{
if (regn_ptr->expression[dft_dimension] == '*' && expr[dft_dimension] != '*')
{
break;
}
}
regn_ptr->is_leaf = false;
regn_ptr->children_num = base_num;
regn_ptr->children = new struct RegionTreeNode *[base_num];
for (int i = 0; i < base_num; i++)
{
regn_ptr->children[i] = new struct RegionTreeNode;
struct RegionTreeNode *chd_ptr = regn_ptr->children[i];
chd_ptr->expression = regn_ptr->expression;
if (i < 10)
{
chd_ptr->expression[dft_dimension] = i + '0';
}
else
{
chd_ptr->expression[dft_dimension] = i - 10 + 'a';
}
chd_ptr->is_leaf = true;
chd_ptr->spe_node_ptr = regn_ptr->spe_node_ptr;
chd_ptr->parent = regn_ptr;
chd_ptr->children = NULL;
chd_ptr->children_num = 0;
}
for (int i = 0; i < base_num; i++)
{
if (f3_expression_belong(expr, regn_ptr->children[i]->expression))
{
regn_ptr = regn_ptr->children[i];
break;
}
}
}
// 2.2 Delete the region tree node which corresponds to the SS (expr).
for (int i = 0; i < base_num; i++)
{
if (regn_ptr->parent->children[i] == regn_ptr)
{
regn_ptr->parent->children[i] = NULL;
break;
}
}
f3_release_region_tree(regn_ptr);
}
bool f3_expression_belong(string str1, string str2)
{
// If str1 belongs to or is equal to str2.
// For instance, 567*7*8 belongs to 56**7*8, return true.
// Their lengths must be identical.
int len = (int )str1.length();
for (int i = 0; i < len; i++)
{
if (str1[i] != str2[i] && str2[i] != '*')
{
return false;
}
}
return true;
}
void f3_init_region_trees(struct RegionTreeNode **region_forest, int &forest_idx, struct SequenceNode *xi_ptr)
{
// Initialize the region trees.
// A root of a region tree corresponds to an expression of a TS of a space tree node.
struct PreparedSpaceTreeNode *spe_node = xi_ptr->node;
int TS_expression_num = spe_node->TS.num;
for (int i = 0; i < TS_expression_num; i++)
{
region_forest[forest_idx] = new struct RegionTreeNode;
struct RegionTreeNode *regn_root = region_forest[forest_idx];
// Input the information of TS.
regn_root->expression = spe_node->TS.expressions[i];
regn_root->is_leaf = true;
regn_root->spe_node_ptr = spe_node;
regn_root->parent = NULL;
regn_root->children = NULL;
regn_root->children_num = 0;
// Renew the tree based on the information of SSs.
int SS_expression_num = spe_node->SS.num;
for (int j = 0; j < SS_expression_num; j++)
{
// If the scanned region belongs to the target region of regn_root, perform a subtract operation.
string str = spe_node->SS.expressions[j];
if (f3_expression_belong(str, regn_root->expression))
{
f3_region_tree_subtract(regn_root, str);
}
}
forest_idx++;
}
}
string f3_addr_tran_std(string expr)
{
if (base_num == 2)
{
return f1_b4_tran_std(f1_b1_tran_b4(expr));
}
else if (base_num == 4)
{
return f1_b4_tran_std(f1_b2_tran_b4(expr));
}
else if (base_num == 8)
{
return f1_b4_tran_std(f1_b3_tran_b4(expr));
}
else if (base_num == 16)
{
return f1_b4_tran_std(expr);
}
else // base_num == 32
{
return f1_b4_tran_std(f1_b5_tran_b4(expr));
}
}
void f3_local_rec_output_addrs(struct SearchTreeNode *sch_node, ofstream &addr_total_res, ofstream &active_addrs)
{
// Output active addresses which corresponds to leaf nodes from sch_node.
if (sch_node->is_leaf == true)
{
addr_total_res << f3_addr_tran_std(sch_node->expression) << endl;
active_addrs << f3_addr_tran_std(sch_node->expression) << endl;
}
else
{
for (int i = 0; i < base_num; i++)
{
if (sch_node->children[i] != NULL)
{
f3_local_rec_output_addrs(sch_node->children[i], addr_total_res, active_addrs);
}
}
}
}
void f3_local_rec_search(string expr, struct SearchTreeNode *sch_node, ofstream &addr_total_res, ofstream &active_addrs)
{
// Search active addresses through a recursive design.
// Found active addresses will be stored in addr_total_res and active_addrs based on colon-hexadecimal notation.
// Return total scanned address number.
int expr_len = (int )expr.length();
if (expr[0] == '*')
{
bool all_star = true;
for (int i = 1; i < expr_len; i++)
{
if (expr[i] != '*')
{
all_star = false;
break;
}
}
if (all_star == true)
{
f3_local_rec_output_addrs(sch_node, addr_total_res, active_addrs);
}
else
{
for (int i = 0; i < base_num; i++)
{
if (sch_node->children[i] != NULL)
{
f3_local_rec_search(expr.substr(1), sch_node->children[i], addr_total_res, active_addrs);
}
}
}
}
else
{
int index;
if (expr[0] >= 'a' && expr[0] <= 'v')
{
index = expr[0] + 10 - 'a';
}
else if (expr[0] >= 'A' && expr[0] <= 'V')
{
index = expr[0] + 10 - 'A';
}
else // expr[0] >= '0' && expr[0] <= '9'
{
index = expr[0] - '0';
}
if (sch_node->children[index] != NULL)
{
if (expr_len == 1)
{
f3_local_rec_output_addrs(sch_node->children[index], addr_total_res, active_addrs);
}
else
{
f3_local_rec_search(expr.substr(1), sch_node->children[index], addr_total_res, active_addrs);
}
}
}
}
int f3_scan_on_leaves(struct RegionTreeNode *regn_node, struct SearchTreeNode *A, ofstream &addr_total_res, ofstream &active_addrs)
{
// Found active addresses will be stored in addr_total_res and active_addrs based on colon-hexadecimal notation.
// Return total scanned address number.
if (regn_node->is_leaf == true)
{
string expr = regn_node->expression;
f3_local_rec_search(expr, A, addr_total_res, active_addrs);
int expr_len = (int )expr.length();
int star_num = 0;
for (int i = 0; i < expr_len; i++)
{
if (expr[i] == '*')
{
star_num++;
}
}
int used_budget = pow(base_num, star_num);
return used_budget;
}
else
{
int children_num = regn_node->children_num;
int used_budget = 0;
for (int i = 0; i < children_num; i++)
{
if (regn_node->children[i] != NULL)
{
used_budget += f3_scan_on_leaves(regn_node->children[i], A, addr_total_res, active_addrs);
}
}
return used_budget;
}
}
void f3_local_scan(struct RegionTreeNode **regn_forest, int tree_num, int &budget, ofstream &addr_total_res, struct SearchTreeNode *A)
{
// Perform a local scan, the found active addresses will be stored both in addr_total_res and active_addrs.
ofstream active_addrs;
active_addrs.open(_STEP_RES_FILE);
for (int i = 0; i < tree_num; i++)
{
struct RegionTreeNode *regn_tree = regn_forest[i];
budget -= f3_scan_on_leaves(regn_tree, A, addr_total_res, active_addrs);
}
active_addrs.close();
}
void f3_copy_TS2SS(struct PreparedSpaceTreeNode *spe_node)
{
if (spe_node->SS.expressions != NULL)
{
delete [] spe_node->SS.expressions;
}
int SS_num = spe_node->TS.num;
spe_node->SS.num = SS_num;
spe_node->SS.expressions = new string [SS_num + 2];
for (int i = 0; i < SS_num; i++)
{
spe_node->SS.expressions[i] = spe_node->TS.expressions[i];
}
}
string f3_std_tran_addr(string expr)
{
if (base_num == 2)
{
return f1_b4_tran_b1(f1_std_tran_b4(expr));
}
else if (base_num == 4)
{
return f1_b4_tran_b2(f1_std_tran_b4(expr));
}
else if (base_num == 8)
{
return f1_b4_tran_b3(f1_std_tran_b4(expr));
}
else if (base_num == 16)
{
return f1_std_tran_b4(expr);
}
else // base_num == 32
{
return f1_b4_tran_b5(f1_std_tran_b4(expr));
}
}
void f3_renew_NDA(struct RegionTreeNode **regn_forest, int regn_tree_num, string *arr, int arr_scale)
{
int rtree_idx = 0;
for (int i = 0; i < arr_scale; i++)
{
string expr = arr[i];
while (!f3_expression_belong(expr, regn_forest[rtree_idx]->expression))
{
rtree_idx++;
}
regn_forest[rtree_idx]->spe_node_ptr->NDA++;
}
}
int f3_density_cmp(struct SequenceNode *se_node1, struct SequenceNode *se_node2)
{
// Compare the density: log(NDA_1/|SS_1|) > log(NDA_2/|SS_2|)
int dimensionality = f2_get_dimensionality(base_num);
struct PreparedSpaceTreeNode *spe_node = se_node1->node;
int NDA = spe_node->NDA;
int expr_num = spe_node->SS.num;
int SS_scale = 0;
for (int i = 0; i < expr_num; i++)
{
string expr = spe_node->SS.expressions[i];
int star_num = 0;
for (int j = 0; j < dimensionality; j++)
{
if (expr[j] == '*')
{
star_num++;
}
}
SS_scale += pow(base_num, star_num);
}
double log_den1 = log((double )NDA) - log((double )SS_scale);
spe_node = se_node2->node;
NDA = spe_node->NDA;
expr_num = spe_node->SS.num;
SS_scale = 0;
for (int i = 0; i < expr_num; i++)
{
string expr = spe_node->SS.expressions[i];
int star_num = 0;
for (int j = 0; j < dimensionality; j++)
{
if (expr[j] == '*')
{
star_num++;
}
}
SS_scale += pow(base_num, star_num);
}
double log_den2 = log((double )NDA) - log((double )SS_scale);
return log_den1 > log_den2;
}
int f3_regn_cmp(struct RegionTreeNode *node1, struct RegionTreeNode *node2)
{
string s1 = node1->expression;
string s2 = node2->expression;
return s1.compare(s2) < 0;
}
struct SequenceNode *f3_local_feedback(struct RegionTreeNode **regn_forest, int regn_tree_num, struct SequenceNode *xi_h, int &active_addr_num)
{
// 1. Renew TSs and SSs of space tree nodes in xi_h.
struct SequenceNode *xi_ptr = xi_h;
while (xi_ptr != NULL)
{
struct PreparedSpaceTreeNode *spe_node = xi_ptr->node;
f3_copy_TS2SS(spe_node);
int dimension = f3_DS_pop(spe_node);
f3_TS_expand(spe_node, dimension);
xi_ptr = xi_ptr->next;
}
// 2. Read active addresses from active_addrs_read, renew NDAs of space tree nodes in xi_h.
// 2.1 Get and sort detected active addresses.
ifstream active_addrs_read;
active_addrs_read.open(_STEP_RES_FILE);
string line;
active_addr_num = 0;
while (getline(active_addrs_read, line))
{
active_addr_num++;
}
active_addrs_read.close();
string *arr = new string [active_addr_num + 2];
active_addrs_read.open(_STEP_RES_FILE);
active_addr_num = 0;
while (getline(active_addrs_read, line))
{
arr[active_addr_num++] = f3_std_tran_addr(line);
}
active_addrs_read.close();
sort(arr, arr + active_addr_num, f1_str_cmp);
// 2.2 Sort region trees based on their region expressions.
sort(regn_forest, regn_forest + regn_tree_num, f3_regn_cmp);
// 2.3 Perform the renew operation.
f3_renew_NDA(regn_forest, regn_tree_num, arr, active_addr_num);
delete [] arr;
// 3. Resort space tree nodes of xi_h, based on NDA/|SS|.
int xi_len = 0;
xi_ptr = xi_h;
while (xi_ptr != NULL)