forked from chrisosaurus/linear_hash
-
Notifications
You must be signed in to change notification settings - Fork 1
/
linear_hash.c
1008 lines (834 loc) · 24.1 KB
/
linear_hash.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
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* The MIT License (MIT)
*
* Author: Chris Hall <followingthepath at gmail dot c0m>
*
* Copyright (c) 2015 Chris Hall (cjh)
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#include <stdio.h> /* puts, printf */
#include <limits.h> /* ULONG_MAX */
#include <stdlib.h> /* calloc, free */
#include <string.h> /* strcmp, strlen */
#include <stddef.h> /* size_t */
#include "linear_hash.h"
/* default number of slots */
#define LH_DEFAULT_SIZE 32
/* factor we grow the number of slots by each resize */
#define LH_SCALING_FACTOR 2
/* default loading factor we resize after in base 10
* 0 through to 10
*
* default is 6 so 60 %
*/
#define LH_DEFAULT_THRESHOLD 6
/* leaving this in place as we have some internal only helper functions
* that we only exposed to allow for easy testing and extension
*/
#pragma GCC diagnostic ignored "-Wmissing-prototypes"
/**********************************************
**********************************************
**********************************************
******** simple helper functions *************
**********************************************
**********************************************
***********************************************/
/* NOTE these helper functions are not exposed in our header
* but are not static so as to allow easy unit testing
* or extension
*/
/* logic for testing if the current entry is eq to the
* provided hash, key_len and key
* this is to centralise the once scattered logic
*/
unsigned int lh_entry_eq(struct lh_entry *cur, unsigned long int hash, unsigned long int key_len, const char *key){
if( ! cur ){
puts("lh_entry_eq: cur was null");
return 0;
}
if( ! key ){
puts("lh_entry_eq: key was null");
return 0;
}
if( cur->hash != hash ){
return 0;
}
if( cur->key_len != key_len ){
return 0;
}
if( strncmp(key, cur->key, key_len) ){
return 0;
}
return 1;
}
/* internal strdup equivalent
*
* returns char* to new memory containing a strcpy on success
* returns 0 on failure
*/
char * lh_strdupn(const char *str, size_t len){
/* our new string */
char *new_str = 0;
if( ! str ){
puts("lh_strdupn: str undef");
return 0;
}
/* if len is 0 issue a warning and recalculate
* note that if strlen is still 0 then all is well
*/
if( len == 0 ){
puts("lh_strdupn: provided len was 0, recalculating");
len = strlen(str);
}
/* allocate our new string
* len + 1 to fit null terminator
*/
new_str = calloc(len + 1, sizeof(char));
if( ! new_str ){
puts("lh_strdupn: call to calloc failed");
return 0;
}
/* perform copy */
strncpy(new_str, str, len);
/* ensure null terminator
* do not rely on calloc as we may switch
* to alt. alloc. later
*/
new_str[len] = '\0';
return new_str;
}
/* initialise an existing lh_entry
*
* returns 1 on success
* returns 0 on failure
*/
unsigned int lh_entry_init(struct lh_entry *entry,
unsigned long int hash,
const char *key,
size_t key_len,
void *data ){
if( ! entry ){
puts("lh_entry_init: entry was null");
return 0;
}
if( ! key ){
puts("lh_entry_init: key was null");
return 0;
}
/* we allow data to be null */
/* we allow next to be null */
/* if key_len is 0 we issue a warning and recalcualte */
if( key_len == 0 ){
puts("warning lh_entry_init: provided key_len was 0, recalcuating");
key_len = strlen(key);
}
/* if hash is 0 we issue a warning and recalculate */
if( hash == 0 ){
puts("warning lh_entry_init: provided hash was 0, recalculating");
hash = lh_hash(key, key_len);
}
/* setup our simple fields */
entry->hash = hash;
entry->key_len = key_len;
entry->data = data;
entry->state = LH_ENTRY_OCCUPIED;
/* we duplicate the string */
entry->key = lh_strdupn(key, key_len);
if( ! entry->key ){
puts("lh_entry_init: call to lh_strdupn failed");
return 0;
}
/* return success */
return 1;
}
/* destroy entry
*
* will only free *data if `free_data` is 1
* will NOT free *next
* will free all other values
*
* returns 1 on success
* returns 0 on failure
*/
unsigned int lh_entry_destroy(struct lh_entry *entry, unsigned int free_data){
if( ! entry ){
puts("lh_entry_destroy: entry undef");
return 0;
}
if( free_data && entry->data ){
free(entry->data);
}
/* free key as strdup */
free(entry->key);
return 1;
}
/* find the lh_entry that should be holding this key
*
* returns a pointer to it on success
* return 0 on failure
*/
struct lh_entry * lh_find_entry(const struct lh_table *table, const char *key){
/* our cur entry */
struct lh_entry *cur = 0;
/* hash */
unsigned long int hash = 0;
/* position in hash table */
size_t pos = 0;
/* iterator through entries */
size_t i = 0;
/* cached strlen */
size_t key_len = 0;
if( ! table ){
puts("lh_find_entry: table undef");
return 0;
}
if( ! key ){
puts("lh_find_entry: key undef");
return 0;
}
/* cache strlen */
key_len = strlen(key);
/* calculate hash */
hash = lh_hash(key, key_len);
/* calculate pos
* we know table is defined here
* so lh_pos cannot fail
*/
pos = lh_pos(hash, table->size);
/* search pos..size */
for( i=pos; i < table->size; ++i ){
cur = &(table->entries[i]);
/* if this is an empty then we stop */
if( cur->state == LH_ENTRY_EMPTY ){
/* failed to find element */
#ifdef DEBUG
puts("lh_find_entry: failed to find key, encountered empty");
#endif
return 0;
}
/* if this is a dummy then we skip but continue */
if( cur->state == LH_ENTRY_DUMMY ){
continue;
}
if( ! lh_entry_eq(cur, hash, key_len, key) ){
continue;
}
return cur;
}
/* search 0..pos */
for( i=0; i < pos; ++i ){
cur = &(table->entries[i]);
/* if this is an empty then we stop */
if( cur->state == LH_ENTRY_EMPTY ){
/* failed to find element */
#ifdef DEBUG
puts("lh_find_entry: failed to find key, encountered empty");
#endif
return 0;
}
/* if this is a dummy then we skip but continue */
if( cur->state == LH_ENTRY_DUMMY ){
continue;
}
if( ! lh_entry_eq(cur, hash, key_len, key) ){
continue;
}
return cur;
}
/* failed to find element */
#ifdef DEBUG
puts("lh_find_entry: failed to find key");
#endif
return 0;
}
/**********************************************
**********************************************
**********************************************
******** linear_hash.h implementation ********
**********************************************
**********************************************
***********************************************/
/* function to return number of elements
*
* returns number on success
* returns 0 on failure
*/
unsigned int lh_nelems(const struct lh_table *table){
if( ! table ){
puts("lh_nelems: table was null");
return 0;
}
return table->n_elems;
}
/* function to calculate load
* (table->n_elems * 10) / table->size
*
* returns loading factor 0 -> 10 on success
* returns 0 on failure
*/
unsigned int lh_load(const struct lh_table *table){
if( ! table ){
puts("lh_load: table was null");
return 0;
}
/* here we multiply by 10 to avoid floating point
* as we only care about the most significant figure
*/
return (table->n_elems * 10) / table->size;
}
/* set the load that we resize at
* load is (table->n_elems * 10) / table->size
*
* this sets lh_table->threshold
* this defaults to LH_DEFAULT_THRESHOLD in linear_hash.c
* this is set to 6 (meaning 60% full) by default
*
* this will accept any value between 1 (10%) to 10 (100%)
*
* returns 1 on success
* returns 0 on failure
*/
unsigned int lh_tune_threshold(struct lh_table *table, unsigned int threshold){
if( ! table ){
puts("lh_tune_threshold: table was null");
return 0;
}
if( threshold < 1 || threshold > 10 ){
puts("lh_tune_threshold: threshold must be between 1 and 9 (inclusive)");
return 0;
}
table->threshold = threshold;
return 1;
}
/* takes a char* representing a string
*
* will recalculate key_len if 0
*
* returns an unsigned long integer hash value on success
* returns 0 on failure
*/
unsigned long int lh_hash(const char *key, size_t key_len){
/* our hash value */
unsigned long int hash = 0;
/* our iterator through the key */
size_t i = 0;
if( ! key ){
puts("lh_hash: key undef");
return 0;
}
/* we allow key_len to be 0
* we issue a warning and then recalculate
*/
if( ! key_len ){
puts("lh_hash: key_len was 0, recalculating");
key_len = strlen(key);
}
#ifdef DEBUG
printf("lh_hash: hashing string '%s'\n", key);
#endif
/* hashing time */
for( i=0; i < key_len; ++i ){
#ifdef DEBUG
printf("lh_hash: looking at i '%zd', char '%c'\n", i, key[i]);
#endif
/* we do not have to worry about overflow doing silly things:
*
* C99 section 6.2.5.9 page 34:
* A computation involving unsigned operands can never overflow,
* because a result that cannot be represented by the resulting
* unsigned integer type is reduced modulo the number that is one
* greater than the largest value that can be represented by the
* resulting type.
*/
/* hash this character
* http://www.cse.yorku.ca/~oz/hash.html
* djb2
*/
hash = ((hash << 5) + hash) + key[i];
}
#ifdef DEBUG
printf("lh_hash: success for key '%s', hash value '%zd'\n", key, hash);
#endif
return hash;
}
/* takes a table and a hash value
*
* returns the index into the table for this hash
* returns 0 on failure (if table is null)
*
* note the error value is indistinguishable from the 0th bucket
* this function can only error if table is null
* so the caller can distinguish these 2 cases
*/
size_t lh_pos(unsigned long int hash, size_t table_size){
/* force hash value into a bucket */
return hash % table_size;
}
/* allocate and initialise a new lh_table
*
* will automatically assume a size of 32
*
* lh_table will automatically resize when a call to
* lh_insert detects the load factor is over table->threshold
*
* returns pointer on success
* returns 0 on failure
*/
struct lh_table * lh_new(void){
struct lh_table *sht = 0;
/* alloc */
sht = calloc(1, sizeof(struct lh_table));
if( ! sht ){
puts("lh_new: calloc failed");
return 0;
}
/* init */
if( ! lh_init(sht, LH_DEFAULT_SIZE) ){
puts("lh_new: call to lh_init failed");
/* make sure to free our allocate lh_table */
free(sht);
return 0;
}
return sht;
}
/* free an existing lh_table
* this will free all the sh entries stored
* this will free all the keys (as they are strdup-ed)
*
* this will only free the *table pointer if `free_table` is set to 1
* this will only free the *data pointers if `free_data` is set to 1
*
* returns 1 on success
* returns 0 on failure
*/
unsigned int lh_destroy(struct lh_table *table, unsigned int free_table, unsigned int free_data){
/* iterator through table */
size_t i = 0;
if( ! table ){
puts("lh_destroy: table undef");
return 0;
}
/* iterate through `entries` list
* calling lh_entry_destroy on each
*/
for( i=0; i < table->size; ++i ){
if( ! lh_entry_destroy( &(table->entries[i]), free_data ) ){
puts("lh_destroy: call to lh_entry_destroy failed, continuing...");
}
}
/* free entires table */
free(table->entries);
/* finally free table if asked to */
if( free_table ){
free(table);
}
return 1;
}
/* initialise an already allocated lh_table to size size
*
* returns 1 on success
* returns 0 on failure
*/
unsigned int lh_init(struct lh_table *table, size_t size){
if( ! table ){
puts("lh_init: table undef");
return 0;
}
if( size == 0 ){
puts("lh_init: specified size of 0, impossible");
return 0;
}
table->size = size;
table->n_elems = 0;
table->threshold = LH_DEFAULT_THRESHOLD;
/* calloc our buckets (pointer to lh_entry) */
table->entries = calloc(size, sizeof(struct lh_entry));
if( ! table->entries ){
puts("lh_init: calloc failed");
return 0;
}
return 1;
}
/* resize an existing table to new_size
* this will reshuffle all the buckets around
*
* you can use this to make a hash larger or smaller
*
* returns 1 on success
* returns 0 on failure
*/
unsigned int lh_resize(struct lh_table *table, size_t new_size){
/* our new data area */
struct lh_entry *new_entries = 0;
/* the current entry we are copying across */
struct lh_entry *cur = 0;
/* our iterator through the old hash */
size_t i = 0;
/* our iterator through the new data */
size_t j = 0;
/* our new position for each element */
size_t new_pos = 0;
if( ! table ){
puts("lh_resize: table was null");
return 0;
}
if( new_size == 0 ){
puts("lh_resize: asked for new_size of 0, impossible");
return 0;
}
if( new_size <= table->n_elems ){
puts("lh_resize: asked for new_size smaller than number of existing elements, impossible");
return 0;
}
/* allocate an array of lh_entry */
new_entries = calloc(new_size, sizeof(struct lh_entry));
if( ! new_entries ){
puts("lh_resize: call to calloc failed");
return 0;
}
/* iterate through old data */
for( i=0; i < table->size; ++i ){
cur = &(table->entries[i]);
/* if we are not occupied then skip */
if( cur->state != LH_ENTRY_OCCUPIED ){
continue;
}
/* our position within new entries */
new_pos = lh_pos(cur->hash, new_size);
for( j = new_pos; j < new_size; ++ j){
/* skip if not empty */
if( new_entries[j].state != LH_ENTRY_EMPTY ){
continue;
}
goto LH_RESIZE_FOUND;
}
for( j = 0; j < new_pos; ++ j){
/* skip if not empty */
if( new_entries[j].state != LH_ENTRY_EMPTY ){
continue;
}
goto LH_RESIZE_FOUND;
}
puts("lh_resize: failed to find spot for new element!");
/* make sure to free our new_entries since we don't store them
* no need to free items in as they are still held in our old elems
*/
free(new_entries);
return 0;
LH_RESIZE_FOUND:
new_entries[j].hash = cur->hash;
new_entries[j].key = cur->key;
new_entries[j].key_len = cur->key_len;
new_entries[j].data = cur->data;
new_entries[j].state = cur->state;
}
/* free old data */
free(table->entries);
/* swap */
table->size = new_size;
table->entries = new_entries;
return 1;
}
/* check if the supplied key already exists in this hash
*
* returns 1 on success (key exists)
* returns 0 if key doesn't exist or on failure
*/
unsigned int lh_exists(const struct lh_table *table, const char *key){
struct lh_entry *she = 0;
if( ! table ){
puts("lh_exists: table undef");
return 0;
}
if( ! key ){
puts("lh_exists: key undef");
return 0;
}
#ifdef DEBUG
printf("lh_exist: called with key '%s', dispatching to lh_find_entry\n", key);
#endif
/* find entry */
she = lh_find_entry(table, key);
if( ! she ){
/* not found */
return 0;
}
/* found */
return 1;
}
/* insert `data` under `key`
* this will only success if !lh_exists(table, key)
*
* returns 1 on success
* returns 0 on failure
*/
unsigned int lh_insert(struct lh_table *table, const char *key, void *data){
/* our new entry */
struct lh_entry *she = 0;
/* hash */
unsigned long int hash = 0;
/* position in hash table */
size_t pos = 0;
/* iterator through table */
size_t i = 0;
/* cached strlen */
size_t key_len = 0;
if( ! table ){
puts("lh_insert: table undef");
return 0;
}
if( ! key ){
puts("lh_insert: key undef");
return 0;
}
#ifdef DEBUG
printf("lh_insert: asked to insert for key '%s'\n", key);
#endif
/* we allow data to be 0 */
#ifdef DEBUG
puts("lh_insert: calling lh_exists");
#endif
/* check for already existing key
* insert only works if the key is not already present
*/
if( lh_exists(table, key) ){
puts("lh_insert: key already exists in table");
return 0;
}
/* determine if we have to resize
* note we are checking the load before the insert
*/
if( lh_load(table) >= table->threshold ){
if( ! lh_resize(table, table->size * LH_SCALING_FACTOR) ){
puts("lh_insert: call to lh_resize failed");
return 0;
}
}
/* cache strlen */
key_len = strlen(key);
/* calculate hash */
hash = lh_hash(key, key_len);
/* calculate pos
* we know table is defined here
* so lh_pos cannot fail
*/
pos = lh_pos(hash, table->size);
#ifdef DEBUG
printf("lh_insert: trying to insert key '%s', hash value '%zd', starting at pos '%zd'\n", key, hash, pos);
#endif
/* iterate from pos to size */
for( i=pos; i < table->size; ++i ){
she = &(table->entries[i]);
/* if taken keep searching */
if( she->state == LH_ENTRY_OCCUPIED ){
continue;
}
/* otherwise (empty or dummy) jump to found */
goto LH_INSERT_FOUND;
}
/* iterate from 0 to pos */
for( i=0; i < pos; ++i ){
she = &(table->entries[i]);
/* if taken keep searching */
if( she->state == LH_ENTRY_OCCUPIED ){
continue;
}
/* otherwise (empty or dummy) jump to found */
goto LH_INSERT_FOUND;
}
/* no slot found */
puts("lh_insert: unable to find insertion slot");
return 0;
LH_INSERT_FOUND:
/* she is already set! */
#ifdef DEBUG
printf("lh_insert: inserting insert key '%s', hash value '%zd', starting at pos '%zd', into '%zd'\n", key, hash, pos, i);
#endif
/* construct our new lh_entry
* lh_entry_new(unsigned long int hash,
* char *key,
* size_t key_len,
* void *data,
* struct lh_entry *next){
*
* only key needs to be defined
*
*/
/* (entry, hash, key, key_len, data) */
if( ! lh_entry_init(she, hash, key, key_len, data) ){
puts("lh_insert: call to lh_entry_init failed");
return 0;
}
/* increment number of elements */
++table->n_elems;
/* return success */
return 1;
}
/* set `data` under `key`
* this will only succeed if lh_exists(table, key)
*
* returns old data on success
* returns 0 on failure
*/
void * lh_set(struct lh_table *table, const char *key, void *data){
struct lh_entry *she = 0;
void * old_data = 0;
if( ! table ){
puts("lh_set: table undef");
return 0;
}
if( ! key ){
puts("lh_set: key undef");
return 0;
}
/* allow data to be null */
/* find entry */
she = lh_find_entry(table, key);
if( ! she ){
/* not found */
return 0;
}
/* save old data */
old_data = she->data;
/* overwrite */
she->data = data;
/* return old data */
return old_data;
}
/* get `data` stored under `key`
*
* returns data on success
* returns 0 on failure
*/
void * lh_get(const struct lh_table *table, const char *key){
struct lh_entry *she = 0;
if( ! table ){
puts("lh_get: table undef");
return 0;
}
if( ! key ){
puts("lh_get: key undef");
return 0;
}
/* find entry */
she = lh_find_entry(table, key);
if( ! she ){
/* not found */
return 0;
}
/* found */
return she->data;
}
/* delete entry stored under `key`
*
* returns data on success
* returns 0 on failure
*/
void * lh_delete(struct lh_table *table, const char *key){
/* our cur entry */
struct lh_entry *cur = 0;
/* hash */
unsigned long int hash = 0;
/* position in hash table */
size_t pos = 0;
/* iterator through has table */
size_t i =0;
/* cached strlen */
size_t key_len = 0;
/* our old data */
void *old_data = 0;
if( ! table ){
puts("lh_delete: table undef");
return 0;
}
if( ! key ){
puts("lh_delete: key undef");
return 0;
}
/* cache strlen */
key_len = strlen(key);
/* calculate hash */
hash = lh_hash(key, key_len);
/* calculate pos
* we know table is defined here
* so lh_pos cannot fail
*/
pos = lh_pos(hash, table->size);
/* starting at pos search for element
* searches pos .. size
*/
for( i = pos; i < table->size ; ++i ){
cur = &(table->entries[i]);
/* if this is an empty then we stop */
if( cur->state == LH_ENTRY_EMPTY ){
/* failed to find element */
#ifdef DEBUG
puts("lh_delete: failed to find key, encountered empty");
#endif
return 0;
}
/* if this is a dummy then we skip but continue */
if( cur->state == LH_ENTRY_DUMMY ){
continue;
}
if( ! lh_entry_eq(cur, hash, key_len, key) ){
continue;
}
goto LH_DELETE_FOUND;
}
/* if we are here then we hit the end,
* searches 0 .. pos
*/
for( i = 0; i < pos; ++i ){
cur = &(table->entries[i]);
/* if this is an empty then we stop */
if( cur->state == LH_ENTRY_EMPTY ){
/* failed to find element */
#ifdef DEBUG
puts("lh_delete: failed to find key, encountered empty");
#endif
return 0;
}
/* if this is a dummy then we skip but continue */
if( cur->state == LH_ENTRY_DUMMY ){
continue;
}
if( ! lh_entry_eq(cur, hash, key_len, key) ){
continue;
}
goto LH_DELETE_FOUND;
}
/* failed to find element */
puts("lh_delete: failed to find key, both loops terminated");
return 0;
LH_DELETE_FOUND:
/* cur is already set! */
/* save old data pointer */
old_data = cur->data;
/* clear out */
cur->data = 0;
cur->key = 0;
cur->key_len = 0;
cur->hash = 0;
cur->state = LH_ENTRY_DUMMY;