-
Notifications
You must be signed in to change notification settings - Fork 0
/
burrows-wheeler-transform.c
70 lines (50 loc) · 1.68 KB
/
burrows-wheeler-transform.c
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
#include "utility.h"
static int length = 0;
int sortRotation(const void *x, const void *y) {
rotation *rx = (rotation *)x;
rotation *ry = (rotation *)y;
return memcmp(rx->suffix, ry->suffix, length);
}
int *suffixArray(unsigned char *input_text, long length) {
rotation *suff = malloc(sizeof(rotation) * length);
for (long i = 0; i < length; i++) {
suff[i].index = i;
suff[i].suffix = (input_text + i);
}
qsort(suff, length, sizeof(rotation), sortRotation);
int *suffix_array = malloc(length * sizeof(int));
for (long i = 0; i < length; i++)
suffix_array[i] = suff[i].index;
return suffix_array;
}
unsigned char *findLastChar(unsigned char *input_text, int *suffix_array, long n) {
unsigned char *transformed_array = malloc(n * sizeof(unsigned char));
long i;
for (i = 0; i < n; i++) {
long j = suffix_array[i] - 1;
if (j < 0)
j = j + n;
transformed_array[i] = input_text[j];
}
transformed_array[i] = '\0';
return transformed_array;
}
int main() {
long index = 0, input_size = 16;
unsigned char *input_text = malloc(sizeof(unsigned char) * input_size);
int ch;
while ((ch = getchar()) != EOF) {
input_text[index++] = ch;
if (index == input_size) {
input_size *= 2;
input_text = realloc(input_text, sizeof(unsigned char) * input_size);
}
}
length = index;
int *suffix_array = suffixArray(input_text, length);
unsigned char *transformed_array = findLastChar(input_text, suffix_array, length);
for (long i = 0; i < length; ++i) {
putchar(transformed_array[i]);
}
return 0;
}