-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsuffix_array_pattern_search.c
136 lines (118 loc) · 3.54 KB
/
suffix_array_pattern_search.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
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// implementation of very naive pattern searching using suffix arrays for learning purposes
#include "suffix_array_pattern_search.h"
int main(int argc, char* argv[])
{
if (argc == 1)
{
return 1;
}
char* strings[argc - 2];
for (int i = 1; i < argc - 1; i++)
{
const int length = strlen(argv[i]);
strings[i - 1] = (char*)malloc(sizeof(char) * length);
strncpy(strings[i - 1], argv[i], length);
}
const int patternLength = strlen(argv[argc - 1]);
char* pattern = (char*)malloc(sizeof(char) * patternLength);
strncpy(pattern, argv[argc - 1], patternLength);
for (int i = 0; i < argc - 2; i++)
{
const int length = strlen(strings[i]);
int suffixArray[length];
generate_suffixes(suffixArray, strings[i]);
suffix_search(strings[i], pattern, suffixArray, length);
}
for (int i = 0; i < argc - 2; i++)
{
free(strings[i]);
}
free(pattern);
return 0;
}
// perform pattern searching via a suffix array and binary search
int suffix_search(char* string, const char* pattern, int suffixArray[], const int n)
{
int upper = n - 1;
int lower = 0;
const int patternLength = strlen(pattern);
while (lower <= upper)
{
int mid = (upper + lower) / 2;
int suffixOffset = suffixArray[mid];
char* suffix = string + suffixOffset;
int result = strncmp(suffix, pattern, patternLength);
if (result > 0)
{
upper = mid - 1;
}
else if (result < 0)
{
lower = mid + 1;
}
else
{
printf("Found pattern at index %d\n", suffixOffset);
int i = mid + 1;
suffixOffset = suffixArray[i];
suffix = string + suffixOffset;
while (i < n && strncmp(suffix, pattern, patternLength) == 0)
{
printf("Found pattern at index %d\n", suffixOffset);
i++;
suffixOffset = suffixArray[i];
suffix = string + suffixOffset;
}
i = mid - 1;
suffixOffset = suffixArray[i];
suffix = string + suffixOffset;
while (i >= 0 && strncmp(suffix, pattern, patternLength) == 0)
{
printf("Found pattern at index %d\n", suffixOffset);
i--;
suffixOffset = suffixArray[i];
suffix = string + suffixOffset;
}
return 0;
}
}
return 0;
}
int generate_suffixes(int suffixArray[], const char* string)
{
const int length = strlen(string);
Suffix temp[length]; // temporary array for storing and sorting suffixes
for (int i = 0; i < length; i++)
{
Suffix suffix;
suffix.suffix = (char*)malloc(sizeof(char) * (length - i + 1));
if (suffix.suffix == NULL)
{
return 1;
}
suffix.startIndex = i;
suffix.suffix[0] = string[0];
for (int j = i; j < length; j++)
{
suffix.suffix[j - i] = string[j];
}
suffix.suffix[length - i] = '\0';
temp[i] = suffix;
}
qsort(temp, length, sizeof(Suffix), compare_suffixes);
for (int i = 0; i < length; i++)
{
suffixArray[i] = temp[i].startIndex;
free(temp[i].suffix);
}
return 0;
}
static int compare_suffixes(const void* a, const void* b)
{
Suffix A = *(Suffix*)a;
Suffix B = *(Suffix*)b;
return strcmp(A.suffix, B.suffix);
}