-
Notifications
You must be signed in to change notification settings - Fork 0
/
trie.c
789 lines (654 loc) · 19.6 KB
/
trie.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
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
/*
Include Headers
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "trie.h"
#include <pthread.h>
#define SIZE 26 //Number of child nodes
#define INDEX(c) ((int)c - (int)'a') //Macro to calculate index of an alphabet
//Initialize Trie
trie_t init_trie(void){
trie_t trie;
trie = malloc(sizeof(_trie_t)); //Malloc trie structure
if(trie == NULL) //If trie allocation failed, then return
{
printf("Cannot create trie\n");
return NULL;
}
trie->head = malloc(sizeof(_trie_node_t)); //Malloc head node of trie
if(trie->head == NULL) //If head allocation failed, then return
{
printf("Cannot create head\n");
free(trie); //Free trie
return NULL;
}
trie->head->is_end = false; //Set is_end for head to be false
for(int i = 0;i < SIZE; i++) //Set all child nodes of head to be NULL
{
trie->head->children[i] = NULL;
}
#ifndef _NO_HOH_LOCK_TRIE //Initialize HOH lock
pthread_mutex_init(&trie->head->node_lock,NULL);
// printf("HOH Lock Initialized\n");
#endif
#ifdef _NO_HOH_LOCK_TRIE //Initialize Single lock
#ifdef _S_LOCK_TRIE
pthread_mutex_init(&trie->s_lock,NULL);
// printf("Single Lock Initialized\n");
#endif
#endif
#ifndef _S_LOCK_TRIE //Initialize RW lock
#ifdef _NO_HOH_LOCK_TRIE
pthread_rwlock_init(&trie->rw_lock,NULL);
// printf("RW Lock Initialized\n");
#endif
#endif
return trie; //Return trie after allocation
}
//Create node helper function for Insert
struct node* create_node()
{
struct node* node = malloc(sizeof(_trie_node_t)); //Malloc new node
if(node == NULL) //If node cannot be created, return
{
printf("Cannot create node\n");
return node;
}
node->is_end = false; //Set is_end for node to be false
for(int i = 0;i < SIZE; i++) //Initialize child nodes to be NULL
{
node->children[i] = NULL;
}
#ifndef _NO_HOH_LOCK_TRIE //Initialize HOH node lock if defined
pthread_mutex_init(&node->node_lock,NULL);
#endif
return node; //Return new node
}
/*
Insert function puts the given key,value pair in the trie.
It overwrites the value if the key already exists.
*/
void insert(trie_t trie, char* key, int value){
#ifdef _S_LOCK_TRIE //Acquire Single Lock if defined
#ifdef _NO_HOH_LOCK_TRIE
pthread_mutex_lock(&trie->s_lock);
#endif
#endif
#ifndef _S_LOCK_TRIE //Acquire RW Lock if defined
#ifdef _NO_HOH_LOCK_TRIE
pthread_rwlock_wrlock(&trie->rw_lock);
#endif
#endif
struct node* root = trie->head;
#ifndef _NO_HOH_LOCK_TRIE //Acquire HOH node Lock if defined
pthread_mutex_lock(&root->node_lock);
struct node* prev = root; //Set a previous node to trie->head
#endif
/*
Insert each letter of the "key" given into the trie
alongwith the value specified.
The loop is run for the length of "key" times.
*/
for(int i = 0; i < strlen(key); i++)
{
/*
Find the index of the current letter in key using
the Macro.
*/
int index = INDEX(key[i]);
/*
If the node doesn't have a child for the given index
create a new node for that index.
*/
if(root->children[index] == NULL){
root->children[index] = create_node();
}
#ifndef _NO_HOH_LOCK_TRIE //Set previous node to current node for HOH lock case
prev = root;
#endif
root = root->children[index]; //Traverse down the trie to insert next key,value
#ifndef _NO_HOH_LOCK_TRIE //Acquire the next lock and release the lock of previous node
pthread_mutex_lock(&root->node_lock);
pthread_mutex_unlock(&prev->node_lock);
#endif
}
/*
Set the value of new node and mark is_end to be true.
*/
root->value = value;
root->is_end = true;
#ifndef _NO_HOH_LOCK_TRIE //Release HOH node lock if defined
pthread_mutex_unlock(&root->node_lock);
#endif
#ifdef _S_LOCK_TRIE //Release Single lock if defined
#ifdef _NO_HOH_LOCK_TRIE
pthread_mutex_unlock(&trie->s_lock);
#endif
#endif
#ifndef _S_LOCK_TRIE //Release RW lock if defined
#ifdef _NO_HOH_LOCK_TRIE
pthread_rwlock_unlock(&trie->rw_lock);
#endif
#endif
}
/*
Find function returns -1 if the key is not found.
Otherwise, it returns 0 and puts the value in the variable pointed to by val_ptr.
*/
int find(trie_t trie,char* key, int* val_ptr){
if(trie->head == NULL) //Return if the head of trie is NULL
{
return -1;
}
#ifdef _S_LOCK_TRIE //Acquire Single lock lock if defined
#ifdef _NO_HOH_LOCK_TRIE
pthread_mutex_lock(&trie->s_lock);
#endif
#endif
#ifndef _S_LOCK_TRIE //Acquire RW lock lock if defined
#ifdef _NO_HOH_LOCK_TRIE
pthread_rwlock_rdlock(&trie->rw_lock);
#endif
#endif
trie_node_t root = trie->head;
#ifndef _NO_HOH_LOCK_TRIE //Acquire HOH node lock lock if defined
pthread_mutex_lock(&root->node_lock);
trie_node_t prev = root;
#endif
int len = strlen(key);
int index;
/*
Run the loop for length of key times.
If found return 0 and its value pointed else
return -1.
*/
for(int i = 0; i < len; i++)
{
/*
Find the index of the current letter in key using
the Macro.
*/
index = INDEX(key[i]);
if(root->children[index] != NULL) //If the child is present for th index
{
#ifndef _NO_HOH_LOCK_TRIE //Set previous node to current in case of HOH lcoking
prev = root;
#endif
root = root->children[index]; //Traverse down the trie
#ifndef _NO_HOH_LOCK_TRIE //Acquire the next lock and release the lock of previous node
pthread_mutex_lock(&root->node_lock);
pthread_mutex_unlock(&prev->node_lock);
#endif
}
else //If no child is present for given index, return -1
{
#ifdef _S_LOCK_TRIE //Release Single lock if defined
#ifdef _NO_HOH_LOCK_TRIE
pthread_mutex_unlock(&trie->s_lock);
#endif
#endif
#ifndef _S_LOCK_TRIE //Release RW lock if defined
#ifdef _NO_HOH_LOCK_TRIE
pthread_rwlock_unlock(&trie->rw_lock);
#endif
#endif
#ifndef _NO_HOH_LOCK_TRIE //Release HOH node lock if defined
pthread_mutex_unlock(&root->node_lock);
#endif
return -1;
}
}
/*
If the key if found, return 0 and store
the value pointed by it in val_ptr.
*/
if(root != NULL && root->is_end)
{
*val_ptr = root->value;
#ifdef _S_LOCK_TRIE //Release Single lock if defined
#ifdef _NO_HOH_LOCK_TRIE
pthread_mutex_unlock(&trie->s_lock);
#endif
#endif
#ifndef _S_LOCK_TRIE //Release RW lock if defined
#ifdef _NO_HOH_LOCK_TRIE
pthread_rwlock_unlock(&trie->rw_lock);
#endif
#endif
#ifndef _NO_HOH_LOCK_TRIE //Release HOH node lock if defined
pthread_mutex_unlock(&root->node_lock);
#endif
return 0;
}
#ifdef _S_LOCK_TRIE //Release Single lock if defined
#ifdef _NO_HOH_LOCK_TRIE
pthread_mutex_unlock(&trie->s_lock);
#endif
#endif
#ifndef _S_LOCK_TRIE //Release RW lock if defined
#ifdef _NO_HOH_LOCK_TRIE
pthread_rwlock_unlock(&trie->rw_lock);
#endif
#endif
#ifndef _NO_HOH_LOCK_TRIE //Release HOH node lock if defined
pthread_mutex_unlock(&root->node_lock);
#endif
return -1;
}
/*
Helper function to find if a node has
no children.
Return 1 if it has any child, else 0.
*/
int isEmpty(trie_node_t root)
{
for(int i = 0;i < SIZE; i++)
{
if(root->children[i])
return 0;
}
return 1;
}
/*
Helper function to delete key, value pair
from trie.
In case of HOH locking:
The function recurses down by locking the path to be
deleted and releases them while moving bottom up after
deleting the required node.
*/
trie_node_t delete_kv_helper(trie_node_t root, char *key)
{
if(root == NULL){ //If current node is NULL, return
return NULL;
}
#ifndef _NO_HOH_LOCK_TRIE //Acquire HOH node lock if defined
pthread_mutex_lock(&root->node_lock);
#endif
/*
Recurse down the trie if "key" is still present.
Pass the pointer to next letter of key while recursing.
*/
if(*key)
{
int index = INDEX(*key);
root->children[index] = delete_kv_helper(root->children[index], key+1);
/*
If the current node is a leaf node and has is_end false,
release the HOH node lock and destroy it.
Free the node and set it to NULL.
*/
if(isEmpty(root) && root->is_end == 0){
#ifndef _NO_HOH_LOCK_TRIE
pthread_mutex_unlock(&root->node_lock);
pthread_mutex_destroy(&root->node_lock);
#endif
free(root);
root = NULL;
return root;
}
#ifndef _NO_HOH_LOCK_TRIE //Release HOH node lock if defined
pthread_mutex_unlock(&root->node_lock);
#endif
return root;
}
/*
If "key" is not present, need to delete this node.
Set is_end to false if a non-leaf node.
Else delete the node.
*/
if(*key == '\0')
{
root->is_end = false;
/*
If the node is a leaf node, release HOH node lock
and destroy it.
Free the node and set it to NULL.
*/
if(isEmpty(root))
{
#ifndef _NO_HOH_LOCK_TRIE
pthread_mutex_unlock(&root->node_lock);
pthread_mutex_destroy(&root->node_lock);
#endif
free(root);
root = NULL;
return root;
}
#ifndef _NO_HOH_LOCK_TRIE //Release HOH node lock if defined
pthread_mutex_unlock(&root->node_lock);
#endif
return root;
}
return root;
}
/*
Delete_kv function deletes the key and its value from
the trie if it is present.
*/
void delete_kv(trie_t trie, char* key){
#ifdef _S_LOCK_TRIE //Acquire Single lock if defined
#ifdef _NO_HOH_LOCK_TRIE
pthread_mutex_lock(&trie->s_lock);
#endif
#endif
#ifndef _S_LOCK_TRIE //Acquire RW lock if defined
#ifdef _NO_HOH_LOCK_TRIE
pthread_rwlock_wrlock(&trie->rw_lock);
#endif
#endif
#ifndef _NO_HOH_LOCK_TRIE //Acquire HOH node lock if defined
pthread_mutex_lock(&trie->head->node_lock);
#endif
struct node* node = trie->head;
/*
Find the index of the current letter in key using
the Macro.
Send the respective path to helper to delete the key, value.
*/
for(int i = 0; i < SIZE; i++){
if(i == INDEX(*key)){
if(node->children[i] != NULL){
node->children[i] = delete_kv_helper(node->children[i], key+1);
}
}
}
#ifdef _S_LOCK_TRIE //Release Single lock if defined
#ifdef _NO_HOH_LOCK_TRIE
pthread_mutex_unlock(&trie->s_lock);
#endif
#endif
#ifndef _S_LOCK_TRIE //Release RW lock if defined
#ifdef _NO_HOH_LOCK_TRIE
pthread_rwlock_unlock(&trie->rw_lock);
#endif
#endif
#ifndef _NO_HOH_LOCK_TRIE //Release HOH node lock if defined
pthread_mutex_unlock(&trie->head->node_lock);
#endif
return;
}
/*
Helper function to get the pointer to next character
in the passed string, to get the prefix string.
*/
char* get_prefix(char* key, int i)
{
char *str = malloc(sizeof(char)*(strlen(key) + 4));
char part[2];
part[0] = (char)(i+97);
part[1] = '\0';
strcpy(str,key);
strcat(str,part);
return str;
}
/*
Traverse Trie Helper function for keys_with_prefix
*/
void traverse(trie_node_t root, char* key, char **list, int* count)
{
/*
If node has is_end set to true, then insert this key into the prefix
list.
Increase the count by one.
*/
if(root->is_end)
{
list[*count] = malloc((strlen(key)+2)*sizeof(char));
strcpy(list[*count],key);
(*count)++;
}
/*
Return if the node is a leaf node.
*/
if(isEmpty(root))
{
return;
}
/*
Recurse in all the children of the given node, to find prefixes.
*/
for(int i = 0; i < SIZE; i++)
{
if(root->children[i] != NULL)
{
//Get the new prefix string
char * str = get_prefix(key, i);
/*
Acquire the HOH node lock of the child along the path
*/
if(root != NULL && root->children[i] != NULL)
{
#ifndef _NO_HOH_LOCK_TRIE
pthread_mutex_lock(&root->children[i]->node_lock);
#endif
}
//Traverse down the trie
traverse(root->children[i], str , list, count);
/*
Release the HOH node lock of child
*/
if(root != NULL && root->children[i] != NULL)
{
#ifndef _NO_HOH_LOCK_TRIE
pthread_mutex_unlock(&root->children[i]->node_lock);
#endif
}
//Free the malloc'd prefix string
free(str);
}
}
}
/*
Keys_with_prefix function returns an array of strings with the given prefix.
Last element of the array is NULL.
If no key matches the prefix, the array has a single NULL value.
*/
char** keys_with_prefix(trie_t trie, char* prefix){
/*
Initialize the list and the count of entries in list
*/
int *count = malloc(sizeof(int));
*count = 0;
char** list = malloc(2048*sizeof(char*));
//If list cannot be initialized, return
if(list == NULL)
{
printf("Cannot initialize list\n");
free(count);
return list;
}
//Set first index of list to NULL
list[*count] = NULL;
#ifdef _S_LOCK_TRIE //Acquire Single lock if defined
#ifdef _NO_HOH_LOCK_TRIE
pthread_mutex_lock(&trie->s_lock);
#endif
#endif
#ifndef _S_LOCK_TRIE //Acquire RW lock if defined
#ifdef _NO_HOH_LOCK_TRIE
pthread_rwlock_rdlock(&trie->rw_lock);
#endif
#endif
int index;
trie_node_t root = trie->head;
#ifndef _NO_HOH_LOCK_TRIE //Acquire HOH node lock if defined
pthread_mutex_lock(&root->node_lock);
trie_node_t prev = root;
#endif
/*
Iterate for length of the prefix string
*/
for(int i = 0; i < strlen(prefix); i++)
{
/*
Traverse down the trie if the prefix string has some
characters.
*/
index = INDEX(prefix[i]);
if(root->children[index] != NULL)
{
#ifndef _NO_HOH_LOCK_TRIE
prev = root;
#endif
root = root->children[index];
/*
Acquire the child's node lock and release the parent's
lock.
*/
#ifndef _NO_HOH_LOCK_TRIE
pthread_mutex_lock(&root->node_lock);
pthread_mutex_unlock(&prev->node_lock);
#endif
}
else
{
/*
If the prefix is not contained in the trie, return
*/
#ifdef _S_LOCK_TRIE //Release Single lock if defined
#ifdef _NO_HOH_LOCK_TRIE
pthread_mutex_unlock(&trie->s_lock);
#endif
#endif
#ifndef _S_LOCK_TRIE //Release RW lock if defined
#ifdef _NO_HOH_LOCK_TRIE
pthread_rwlock_unlock(&trie->rw_lock);
#endif
#endif
#ifndef _NO_HOH_LOCK_TRIE //Release HOH node lock if defined
pthread_mutex_unlock(&root->node_lock);
#endif
free(count);
return list;
}
}
/*
If the prefix string is exhausted and the current node
is a leaf, add the prefix to the list and return.
*/
if(root->is_end && isEmpty(root))
{
list[*count] = malloc((strlen(prefix)+2)*sizeof(char));
strcpy(list[*count], prefix);
(*count)++;
list[*count] = NULL;
#ifdef _S_LOCK_TRIE //Release Single lock if defined
#ifdef _NO_HOH_LOCK_TRIE
pthread_mutex_unlock(&trie->s_lock);
#endif
#endif
#ifndef _S_LOCK_TRIE //Release RW lock if defined
#ifdef _NO_HOH_LOCK_TRIE
pthread_rwlock_unlock(&trie->rw_lock);
#endif
#endif
#ifndef _NO_HOH_LOCK_TRIE //Release HOH node lock if defined
pthread_mutex_unlock(&root->node_lock);
#endif
free(count);
return list;
}
/*
If the prefix string is exhausted but the current
node is not a leaf node, recurse down the trie to find
prefixes.
*/
if(!isEmpty(root))
{
traverse(root, prefix, list, count);
list[*count] = NULL;
#ifdef _S_LOCK_TRIE //Release Single lock if defined
#ifdef _NO_HOH_LOCK_TRIE
pthread_mutex_unlock(&trie->s_lock);
#endif
#endif
#ifndef _S_LOCK_TRIE //Release RW lock if defined
#ifdef _NO_HOH_LOCK_TRIE
pthread_rwlock_unlock(&trie->rw_lock);
#endif
#endif
#ifndef _NO_HOH_LOCK_TRIE //Release HOH node lock if defined
pthread_mutex_unlock(&root->node_lock);
#endif
free(count);
return list;
}
#ifdef _S_LOCK_TRIE //Release Single lock if defined
#ifdef _NO_HOH_LOCK_TRIE
pthread_mutex_unlock(&trie->s_lock);
#endif
#endif
#ifndef _S_LOCK_TRIE //Release RW lock if defined
#ifdef _NO_HOH_LOCK_TRIE
pthread_rwlock_unlock(&trie->rw_lock);
#endif
#endif
#ifndef _NO_HOH_LOCK_TRIE //Release HOH node lock if defined
pthread_mutex_unlock(&root->node_lock);
#endif
free(count);
return list;
}
/*
Helper function to delete trie
*/
void delete_trie_helper(trie_node_t root)
{
//Return if the current node is NULL
if(root == NULL)
return;
/*
Recurse for all children of current node.
*/
for(int i = 0; i < SIZE; i++)
{
if(root->children[i] != NULL)
{
delete_trie_helper(root->children[i]);
}
}
/*
If the current node is a leaf node, destroy
its node lock and delete it.
*/
if(isEmpty(root) || root != NULL)
{
#ifndef _NO_HOH_LOCK_TRIE
pthread_mutex_destroy(&root->node_lock);
#endif
free(root);
root = NULL;
return;
}
}
/*
Delete Trie function clears the entire trie from memory.
*/
void delete_trie(trie_t trie){
//If the trie is NULL, return
if(trie == NULL){
return;
}
/*
If the trie head is a leaf node, destroy its
node lock and delete it.
*/
if(isEmpty(trie->head))
{
#ifndef _NO_HOH_LOCK_TRIE
pthread_mutex_destroy(&trie->head->node_lock);
#endif
free(trie->head);
trie->head = NULL;
free(trie);
trie = NULL;
return;
}
delete_trie_helper(trie->head);
free(trie);
trie = NULL;
return;
}