-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFeatureStore.cpp
173 lines (133 loc) · 3.23 KB
/
FeatureStore.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
//
// Created by Matthieu Rudelle on 27/05/16.
//
#include <iostream>
#include "FeatureStore.h"
#include "FeatureIndex.h"
FeatureStore::FeatureStore(int initSize) {
maxSize = initSize;
size = 0;
db = new db_feature[initSize];
HS_end = 0;
isSorted = false;
}
void FeatureStore::add(int feature, int file, float reporank, int line) {
if (size < maxSize) {
db[size] = {feature, file, line, reporank};
size++;
isSorted = false;
}
}
void FeatureStore::sort() {
HS_sort();
isSorted = true;
}
void FeatureStore::print() {
for (int i = 0; i < size; i++) {
printf("%d\t%d\t%d\t%f\n", db[i].feature_id, db[i].file_id, db[i].line_nb, db[i].reporank);
}
}
void FeatureStore::buildFeatureIndex(FeatureIndex *index) {
// if data is not sorted yet, will have to do that first
if (!isSorted) {
sort();
}
int currentFID = -1;
for (int i = 0; i < size; i++) {
if (db[i].feature_id != currentFID) {
currentFID = db[i].feature_id;
index->build_addStart(currentFID, i);
}
}
}
db_feature *FeatureStore::at(int pos) {
return &db[pos];
}
/*
* Parts relative to heap sorting
*/
int FeatureStore::HS_parent(int i) {
return (i-1)/2;
}
int FeatureStore::HS_firstChild(int i) {
return 2*i +1;
}
int FeatureStore::HS_secondChild(int i) {
return 2*i +2;
}
void FeatureStore::HS_siftUp(int i) {
if (i <= 0) {
return;
}
int p = HS_parent(i);
if (HS_isBefore(p, i)) {
HS_swap(p, i);
HS_siftUp(p);
}
}
void FeatureStore::HS_siftDown(int i) {
int fc = HS_firstChild(i);
int sc = HS_secondChild(i);
if (fc >= HS_end) {
return;
}
// Only the first child needs to be compared
if (sc >= HS_end) {
if (HS_isBefore(i, fc)) {
HS_swap(i, fc);
HS_siftDown(fc);
}
return;
}
// First and second children have to be compared now
// children are in order
if (HS_isBefore(fc, i) && HS_isBefore(sc, i)) {
return;
}
// one child needs to step up
if (HS_isBefore(sc, fc)) {
HS_swap(fc, i);
HS_siftDown(fc);
} else {
HS_swap(sc, i);
HS_siftDown(sc);
}
}
void FeatureStore::HS_heapify() {
for (int i = HS_end/2; i >= 0; i--) {
HS_siftDown(i);
}
}
void FeatureStore::HS_swap(int i, int j) {
db_feature temp = db[i];
db[i] = db[j];
db[j] = temp;
}
// sort order is feature_id, file_id, line nb
bool FeatureStore::HS_isBefore(int i, int j) {
if(db[i].feature_id < db[j].feature_id) {
return true;
} else if (db[i].feature_id == db[j].feature_id) {
if(db[i].file_id < db[j].file_id) {
return true;
} else if (db[i].file_id == db[j].file_id) {
return db[i].line_nb < db[j].line_nb;
} else {
return false;
}
} else {
return false;
}
}
void FeatureStore::HS_sort() {
HS_end = size;
HS_heapify();
while (HS_end > 0) {
HS_end--;
if (db[0].feature_id < db[1].feature_id || db[0].feature_id < db[2].feature_id) {
printf("big problem\n");
}
HS_swap(0, HS_end);
HS_siftDown(0);
}
}