-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbwTransform.cpp
More file actions
59 lines (43 loc) · 1.09 KB
/
bwTransform.cpp
File metadata and controls
59 lines (43 loc) · 1.09 KB
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
#include "radixsort.h"
#include "bwTransform.h"
using namespace std;
void computeSuffixArray(char* str, int n, int* res) {
int* curr = new int[3 * n];
int positions[n];
for(int i = 0; i < n; i++) {
positions[i] = str[i];
}
CUcontext cuContext;
cudaInit(cuContext);
for(int len = 1; ; len *= 2) {
for(int i = 0; i < n; i++) {
curr[3 * i] = positions[i];
curr[3 * i + 1] = i + len < n ? positions[i + len] : -1;
curr[3 * i + 2] = i;
}
curr = radixsort(curr, n);
for(int i = 0, prevPos = -1; i < n; i++) {
if(i > 0 && curr[3 * i] == curr[3 * (i-1)] && curr[3 * i + 1] == curr[3 * (i-1) + 1]) {
positions[curr[3 * i + 2]] = prevPos;
} else {
positions[curr[3 * i + 2]] = ++prevPos;
}
}
if (positions[n-1] == n-1) {
break;
}
}
cudaDestroy(cuContext);
for(int i = 0; i < n; i++) {
res[positions[i]] = i;
}
delete[] curr;
}
char* bwEncode(char* str, int n) {
int* suffArray = new int[n];
computeSuffixArray(str, n, suffArray);
char* encoded = new char[n];
takeLastColumn(str, suffArray, n, encoded);
delete[] suffArray;
return encoded;
}