1
+ #include <stdlib.h>
2
+
3
+ #include "article.h"
4
+ #include "hashtable.h"
5
+
6
+ int main (void )
7
+ {
8
+ HashTable_t * ht = ht_new ();
9
+
10
+ // Show initial state and capacity
11
+ ht_display_states (ht , stdout );
12
+
13
+ const char * const key_one = "DOI_one" ;
14
+ Article_t * article_one = make_article (key_one , "Some_Title" , "Some Author" , 2000 );
15
+
16
+ const char * const key_two = "DOI_two" ;
17
+ Article_t * article_two = make_article (key_two , "Another_Title" , "Some other author" , 2013 );
18
+ Article_t * article_two_v2 = make_article (key_two , "Another_Title" , "Some other author et al." , 2015 );
19
+
20
+ const char * const key_three = "Yet Another DOI" ;
21
+ Article_t * article_three = make_article (key_three , "Last Title" , "Last Author" , 2011 );
22
+
23
+ ht_insert (ht , article_one );
24
+ ht_insert (ht , article_two );
25
+ ht_insert (ht , article_three );
26
+ ht_insert (ht , article_two_v2 ); // Inserting same key again only updates value already in table
27
+
28
+ delete_article (article_one );
29
+ delete_article (article_two );
30
+ delete_article (article_two_v2 );
31
+ delete_article (article_three );
32
+
33
+ // Should have 3 articles
34
+ ht_display_states (ht , stdout );
35
+
36
+ // Searches
37
+ printf ("Articles in ht:\n" );
38
+ display_article (ht_fetch (ht , key_one ), stdout );
39
+ display_article (ht_fetch (ht , key_two ), stdout );
40
+ display_article (ht_fetch (ht , key_three ), stdout );
41
+
42
+ // Non-existent keys can be queried through ht_contains
43
+ const char * const non_existent_key = "HELLO I AM A DOI THAT DOES NOT EXIST" ;
44
+ printf ("%s in ht: %s\n" , key_two , ht_contains (ht , key_two ) ? "True" : "False" );
45
+ printf ("%s in ht: %s\n" , non_existent_key , ht_contains (ht , non_existent_key ) ? "True" : "False" );
46
+
47
+ // ht_fetch returns NULL for non-existent keys
48
+ printf ("Make sure this is NULL: %p\n" , ht_fetch (ht , non_existent_key ));
49
+
50
+ // Removing
51
+ ht_remove (ht , key_one );
52
+ ht_remove (ht , key_three );
53
+
54
+ // Removed keys are not in table anymore
55
+ ht_display_states (ht , stdout );
56
+ printf ("%s in ht: %s\n" , key_one , ht_contains (ht , key_one ) ? "True" : "False" );
57
+ printf ("%s in ht: %s\n" , key_three , ht_contains (ht , key_three ) ? "True" : "False" );
58
+
59
+ // Removing non-valid key does nothing to the table
60
+ ht_remove (ht , non_existent_key );
61
+ ht_display_states (ht , stdout );
62
+
63
+ // Table can be expanded
64
+ ht_expand (ht );
65
+ ht_display_states (ht , stdout );
66
+
67
+ // ... and shrunk
68
+ ht_shrink (ht );
69
+ ht_display_states (ht , stdout );
70
+
71
+ // These last two operations don't work for file-loaded tables, sorry
72
+
73
+ // The table resizes manually to fit more keys (expand) or save memory (shrink) when if seems fit
74
+ // But you can specify the size manually if you're feeling fancy!
75
+ ht_resize (ht , 17 ); // After doing this, expand and shrink don't work anymore
76
+ ht_display_states (ht , stdout );
77
+
78
+ // Table can be dumped to a file
79
+ FILE * fp = fopen ("demonstration_ht.txt" , "w" );
80
+ ht_dump (ht , fp );
81
+
82
+ // You can still manipulate ht here...
83
+
84
+ ht_delete (ht );
85
+
86
+ // And read back from it
87
+ freopen ("demonstration_ht.txt" , "r" , fp );
88
+ ht = ht_from_file (fp );
89
+
90
+ // Dumping / reading and/or resizing 'cleans' the 'REMOVED' cells of the table
91
+ ht_display_states (ht , stdout );
92
+
93
+ ht_delete (ht );
94
+
95
+ return EXIT_SUCCESS ;
96
+ }
0 commit comments