forked from EnjoyHacking/klcs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
merge_common_prefix.c
155 lines (107 loc) · 2.5 KB
/
merge_common_prefix.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#include <stdio.h>
#include <string.h>
#include <assert.h>
#include "trie.h"
#include "merge_common_prefix.h"
#define NUM_TEST_VALUES 100
#define MAX_SUBSTRING_SIZE 20
int test_array[NUM_TEST_VALUES];
char test_strings[NUM_TEST_VALUES][10];
Trie *generate_trie(void)
{
Trie *trie;
int i;
unsigned int entries;
/* Create a trie and fill it with a large number of values */
trie = trie_new();
entries = 0;
for(i = 0; i < NUM_TEST_VALUES; ++i) {
test_array[i] = i;
sprintf(test_strings[i], "%i", i);
trie_insert(trie, test_strings[i], &test_array[i]);
++entries;
assert(trie_num_entries(trie) == entries);
}
return trie;
}
void lookup_trie(Trie * trie){
int i;
int *val;
char buf[10];
if (!trie) {
printf("Error: trie is null!\n");
return;
}
for(i = 0; i < NUM_TEST_VALUES; ++i) {
sprintf(buf, "%i", i);
val = (int *) trie_lookup(trie, buf);
assert(*val == i);
printf("%i \t", *val);
}
printf("\n");
return ;
}
/*
void trie_node_merge_callback(TrieNode *node, void *extension){
merge_common_prefix_t * mcp = (merge_common_prefix_t *)extension;
if (node->use_count < mcp->alpha_merge && node->use_count > 1) {
char * new_data = (char *)malloc(sizeof(char) * MAX_SUBSTRING_SIZE);
strcmp(new_data, (char *)node->data);
strcat(new_data, "\(");
for(i = 0; i < 256; ++i) {
if (node->next[i] != NULL) {
char *ch = (char *) malloc(sizeof(char) * 2);
sprintf(ch, "%c", i);
strcat(new_data, ch);
TrieNode *child = node->next[i];
node->next[i] = child->next[i];
}
}
node->data = new_data;
}
}
*/
static void int_callback(TrieNode *node, void *extension) {
if(!node){
return;
}
if(!node->data){
return;
}
int * value = (int *) node->data;
printf("%i \t", *value);
return;
}
static void str_callback(TrieNode *node, void *extension) {
if(!node){
return;
}
if(!node->data){
return;
}
char * value = (char *) node->data;
printf("%s\n", value);
return ;
}
int main(int argc, char** argv) {
/*
Trie *trie = generate_trie();
lookup_trie(trie);
*/
Trie * trie = trie_new();
/*
trie_insert(trie, "aa", "aa");
trie_insert(trie, "ab", "ab");
trie_insert(trie, "ac", "ac");
trie_insert(trie, "ba", "ba");
trie_insert(trie, "bb", "bb");
trie_insert(trie, "bc", "bc");
*/
trie_insert(trie, "welcom", "welcom");
trie_insert(trie, "welcome", "welcome");
trie_insert(trie, "elcome", "elcome");
printf("-------------------------\n");
void *extension = NULL;
trie_dfs(trie, str_callback, extension);
return 0;
}