-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathPrefixTableOperation.c
42 lines (35 loc) · 1.07 KB
/
PrefixTableOperation.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
#include <stdlib.h>
#include <string.h>
#include "PrefixTableOperation.h"
void push(PrefixTable prefixTable, char subString[2], char* pattern) {
int key = subString[0] * 256 + subString[1];
PatternNode node = malloc(sizeof(PatternNodeStruct));
node->pattern = pattern;
node->length = strlen(pattern);
node->next = NULL;
// new prefix
if(!prefixTable[key]) {
prefixTable[key] = node;
}
// have the same prefix
else {
PatternNode current = prefixTable[key];
while(current->next) {
current = current->next;
}
current->next = node;
}
}
PatternNode pop(PrefixTable prefixTable, char subString[2]) {
int key = subString[0] * 256 + subString[1];
return prefixTable[key];
}
PrefixTableOperation PrefixTableOperationFactory() {
static PrefixTableOperation prefixTableOperation = NULL;
if(!prefixTableOperation) {
prefixTableOperation = malloc(sizeof(PrefixTableOperationStruct));
prefixTableOperation->push = push;
prefixTableOperation->pop = pop;
}
return prefixTableOperation;
}