-
Notifications
You must be signed in to change notification settings - Fork 0
/
trie.h
185 lines (146 loc) · 4.11 KB
/
trie.h
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
#ifndef _TRIE_H
#define _TRIE_H 1
#include <malloc.h> // for dynamic memory allocations
#include <stdio.h> // for writing to streams
#include <stdlib.h> // for exit(), in case abnormal conditions rear their ugly head :P
#include <stdbool.h>
#include "abstract_types.h"
#define DEFAULT_CHILD_ARR_SIZE 8
#define DEFAULT_USERID_ARR_SIZE 4
// ============================ Structs/Types ============================
typedef enum {non_leaf, leaf} node_type;
struct trie_node; // Forward declaration
typedef struct trie_node* trie_t;
typedef struct char_n_addr // Holds a hash character-address pair
{
char hash_ch;
struct trie_node *addr;
} char_n_addr;
typedef struct trie_node // Holds information related to a Trie Node.
{
node_type type;
int element_count, dyn_alloc_size;
union
{
char_n_addr *children;
int *user_id;
};
} trie_node;
// ==================== Function Prototypes ====================
static trie_node* create_trie_node(node_type);
trie_t create_trie();
void add_pair(trie_t restrict, const char* restrict, int);
int* retrieve_worker(trie_t restrict, const char* restrict, int* restrict, char* restrict, int);
int* retrieve_users(trie_t restrict, const char* restrict, int* restrict);
void destroy_trie(trie_t restrict);
// ============================ Functions/Operations ============================
static trie_node* create_trie_node(node_type type)
{
trie_node* node = malloc(sizeof(trie_node));
node->type = type;
node->element_count = 0;
node->dyn_alloc_size = 0;
node->children = NULL;
return node;
}
trie_t create_trie()
{
return create_trie_node(non_leaf);
}
void add_pair(trie_t restrict root, const char* restrict hash, int user_id)
{
int i;
trie_node* next_node = NULL;
if(*hash == '\0')
{
int pos = root->element_count;
++root->element_count;
if(root->element_count > root->dyn_alloc_size)
{
if(root->dyn_alloc_size != 0)
root->dyn_alloc_size <<= 1;
else
{
root->dyn_alloc_size = DEFAULT_USERID_ARR_SIZE;
root->type = leaf;
}
root->user_id = realloc(root->user_id, root->dyn_alloc_size * sizeof(int));
if(root->user_id == NULL)
{
fprintf(stderr, "Error during re-allocation in function add_pair() in file trie.h\n");
exit(EXIT_FAILURE);
}
}
root->user_id[pos] = user_id;
return;
}
for(i = 0; i < root->element_count; ++i)
if(root->children[i].hash_ch == *hash)
{
next_node = root->children[i].addr;
break;
}
if(next_node == NULL)
{
int pos = root->element_count;
++root->element_count;
if(root->element_count > root->dyn_alloc_size)
{
if(root->dyn_alloc_size != 0)
root->dyn_alloc_size <<= 1;
else
root->dyn_alloc_size = DEFAULT_CHILD_ARR_SIZE;
root->children = realloc(root->children, root->dyn_alloc_size * sizeof(char_n_addr));
if(root->children == NULL)
{
fprintf(stderr, "Error during re-allocation in function add_pair() in file trie.h\n");
exit(EXIT_FAILURE);
}
}
next_node = create_trie_node(non_leaf);
root->children[pos].hash_ch = *hash;
root->children[pos].addr = next_node;
}
add_pair(next_node, hash + 1, user_id);
}
/* Return the base address of an array containing the list of user IDs (this array is on the heap and will be
** freed when destroy_trie() is called). The count of user IDs is stored in the argument user_count.
** NULL is returned in case the hash does not exist in the trie.
*/
int* retrieve_users(trie_t restrict root, const char* restrict hash, int* restrict user_count)
{
int pos_in_hash = 0, i;
bool found;
for(; hash[pos_in_hash] != '\0'; ++pos_in_hash)
{
for(i = 0, found = false; i < root->element_count; ++i)
if(hash[pos_in_hash] == root->children[i].hash_ch)
{
found = true;
break;
}
if(found)
root = root->children[i].addr;
else
{
*user_count = 0;
return NULL;
}
}
*user_count = root->element_count;
return root->user_id;
}
void destroy_trie(trie_t restrict root)
{
if(root->type != leaf)
{
for(int i = 0; i < root->element_count; ++i)
destroy_trie(root->children[i].addr);
free(root->children);
}
else
free(root->user_id);
free(root);
root = NULL;
}
#endif