1 /************************************************************************************************
3 All llookup hash function prototypes returns 0 on sucess and 1 otherwise
4 llookup hash is an array of lhashlistnode_t
5 oid = mid = 0 in a given lhashlistnode_t for each bin in the hash table ONLY if the entry is empty =>
6 the OID's can be any unsigned int except 0
7 ***************************************************************************************************/
10 lhashtable_t llookup; //Global Hash table
12 // Creates a hash table with default size HASH_SIZE and an array of lhashlistnode_t
13 unsigned int lhashCreate(unsigned int size, float loadfactor) {
14 lhashlistnode_t *nodes;
17 // Allocate space for the hash table
18 if((nodes = calloc(HASH_SIZE, sizeof(lhashlistnode_t))) == NULL) {
19 printf("Calloc error %s %d\n", __FILE__, __LINE__);
23 llookup.table = nodes;
25 llookup.numelements = 0; // Initial number of elements in the hash
26 llookup.loadfactor = loadfactor;
30 // Assign to oids to bins inside hash table
31 unsigned int lhashFunction(unsigned int oid) {
32 return( oid % (llookup.size));
35 // Insert oid and mid mapping into the hash table
36 unsigned int lhashInsert(unsigned int oid, unsigned int mid) {
39 lhashlistnode_t *ptr, *node;
41 if (llookup.numelements > (llookup.loadfactor * llookup.size)) {
43 newsize = 2 * llookup.size + 1;
47 llookup.numelements++;
49 index = lhashFunction(oid);
51 printf("DEBUG(insert) oid = %d, mid =%d, index =%d\n",oid,mid, index);
53 if(ptr[index].next == NULL && ptr[index].oid == 0) { // Insert at the first position in the hashtable
56 } else { // Insert in the linked list
57 if ((node = calloc(1, sizeof(lhashlistnode_t))) == NULL) {
58 printf("Calloc error %s, %d\n", __FILE__, __LINE__);
63 node->next = ptr[index].next;
64 ptr[index].next = node;
69 // Return mid for a given oid in the hash table
70 unsigned int lhashSearch(unsigned int oid) {
72 lhashlistnode_t *ptr, *node;
74 ptr = llookup.table; // Address of the beginning of hash table
75 index = lhashFunction(oid);
78 if(node->oid == oid) {
86 // Remove an entry from the hash table
87 unsigned int lhashRemove(unsigned int oid) {
89 lhashlistnode_t *curr, *prev;
90 lhashlistnode_t *ptr, *node;
93 index = lhashFunction(oid);
96 for (; curr != NULL; curr = curr->next) {
97 if (curr->oid == oid) { // Find a match in the hash table
98 llookup.numelements--; // Decrement the number of elements in the global hashtable
99 if ((curr == &ptr[index]) && (curr->next == NULL)) { // Delete the first item inside the hashtable with no linked list of lhashlistnode_t
102 } else if ((curr == &ptr[index]) && (curr->next != NULL)) { //Delete the first item with a linked list of lhashlistnode_t connected
103 curr->oid = curr->next->oid;
104 curr->mid = curr->next->mid;
106 curr->next = curr->next->next;
108 } else { // Regular delete from linked listed
109 prev->next = curr->next;
120 unsigned int lhashResize(unsigned int newsize) {
121 lhashlistnode_t *node, *ptr, *curr, *next; // curr and next keep track of the current and the next lhashlistnodes in a linked list
122 unsigned int oldsize;
123 int isfirst; // Keeps track of the first element in the lhashlistnode_t for each bin in hashtable
125 lhashlistnode_t *newnode;
128 oldsize = llookup.size;
130 if((node = calloc(newsize, sizeof(lhashlistnode_t))) == NULL) {
131 printf("Calloc error %s %d\n", __FILE__, __LINE__);
135 llookup.table = node; //Update the global hashtable upon resize()
136 llookup.size = newsize;
137 llookup.numelements = 0;
139 for(i = 0; i < oldsize; i++) { //Outer loop for each bin in hash table
142 while (curr != NULL) { //Inner loop to go through linked lists
143 if (curr->oid == 0) { //Exit inner loop if there the first element for a given bin/index is NULL
144 break; //oid = mid =0 for element if not present within the hash table
147 index = lhashFunction(curr->oid);
148 // Insert into the new table
149 if(llookup.table[index].next == NULL && llookup.table[index].oid == 0) {
150 llookup.table[index].oid = curr->oid;
151 llookup.table[index].mid = curr->mid;
152 llookup.numelements++;
154 if((newnode = calloc(1, sizeof(lhashlistnode_t))) == NULL) {
155 printf("Calloc error %s, %d\n", __FILE__, __LINE__);
158 newnode->oid = curr->oid;
159 newnode->mid = curr->mid;
160 newnode->next = llookup.table[index].next;
161 llookup.table[index].next = newnode;
162 llookup.numelements++;
165 //free the linked list of lhashlistnode_t if not the first element in the hash table
176 free(ptr); //Free the memory of the old hash table