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
8 Uses pthreads. compile using -lpthread option
9 ***************************************************************************************************/
12 lhashtable_t llookup; //Global Hash table
14 // Creates a hash table with size and an array of lhashlistnode_t
15 unsigned int lhashCreate(unsigned int size, float loadfactor) {
16 lhashlistnode_t *nodes;
19 // Allocate space for the hash table
20 if((nodes = calloc(size, sizeof(lhashlistnode_t))) == NULL) {
21 printf("Calloc error %s %d\n", __FILE__, __LINE__);
25 llookup.table = nodes;
27 llookup.numelements = 0; // Initial number of elements in the hash
28 llookup.loadfactor = loadfactor;
29 //Initialize the pthread_mutex variable
30 pthread_mutex_init(&llookup.locktable, NULL);
34 // Assign to oids to bins inside hash table
35 unsigned int lhashFunction(unsigned int oid) {
36 return( oid % (llookup.size));
39 // Insert oid and mid mapping into the hash table
40 unsigned int lhashInsert(unsigned int oid, unsigned int mid) {
43 lhashlistnode_t *ptr, *node;
45 if (llookup.numelements > (llookup.loadfactor * llookup.size)) {
47 newsize = 2 * llookup.size + 1;
48 pthread_mutex_lock(&llookup.locktable);
50 pthread_mutex_unlock(&llookup.locktable);
54 llookup.numelements++;
56 index = lhashFunction(oid);
58 printf("DEBUG(insert) oid = %d, mid =%d, index =%d\n",oid,mid, index);
60 pthread_mutex_lock(&llookup.locktable);
61 if(ptr[index].next == NULL && ptr[index].oid == 0) { // Insert at the first position in the hashtable
64 } else { // Insert in the linked list
65 if ((node = calloc(1, sizeof(lhashlistnode_t))) == NULL) {
66 printf("Calloc error %s, %d\n", __FILE__, __LINE__);
67 pthread_mutex_unlock(&llookup.locktable);
72 node->next = ptr[index].next;
73 ptr[index].next = node;
76 pthread_mutex_unlock(&llookup.locktable);
80 // Return mid for a given oid in the hash table
81 unsigned int lhashSearch(unsigned int oid) {
83 lhashlistnode_t *ptr, *node;
85 ptr = llookup.table; // Address of the beginning of hash table
86 index = lhashFunction(oid);
88 pthread_mutex_lock(&llookup.locktable);
90 if(node->oid == oid) {
91 pthread_mutex_unlock(&llookup.locktable);
96 pthread_mutex_unlock(&llookup.locktable);
100 // Remove an entry from the hash table
101 unsigned int lhashRemove(unsigned int oid) {
103 lhashlistnode_t *curr, *prev;
104 lhashlistnode_t *ptr, *node;
107 index = lhashFunction(oid);
110 pthread_mutex_lock(&llookup.locktable);
111 for (; curr != NULL; curr = curr->next) {
112 if (curr->oid == oid) { // Find a match in the hash table
113 llookup.numelements--; // Decrement the number of elements in the global hashtable
114 if ((curr == &ptr[index]) && (curr->next == NULL)) { // Delete the first item inside the hashtable with no linked list of lhashlistnode_t
117 } else if ((curr == &ptr[index]) && (curr->next != NULL)) { //Delete the first item with a linked list of lhashlistnode_t connected
118 curr->oid = curr->next->oid;
119 curr->mid = curr->next->mid;
121 curr->next = curr->next->next;
123 } else { // Regular delete from linked listed
124 prev->next = curr->next;
127 pthread_mutex_unlock(&llookup.locktable);
132 pthread_mutex_unlock(&llookup.locktable);
137 unsigned int lhashResize(unsigned int newsize) {
138 lhashlistnode_t *node, *ptr, *curr, *next; // curr and next keep track of the current and the next lhashlistnodes in a linked list
139 unsigned int oldsize;
140 int isfirst; // Keeps track of the first element in the lhashlistnode_t for each bin in hashtable
142 lhashlistnode_t *newnode;
145 oldsize = llookup.size;
147 if((node = calloc(newsize, sizeof(lhashlistnode_t))) == NULL) {
148 printf("Calloc error %s %d\n", __FILE__, __LINE__);
152 llookup.table = node; //Update the global hashtable upon resize()
153 llookup.size = newsize;
154 llookup.numelements = 0;
156 for(i = 0; i < oldsize; i++) { //Outer loop for each bin in hash table
159 while (curr != NULL) { //Inner loop to go through linked lists
160 if (curr->oid == 0) { //Exit inner loop if there the first element for a given bin/index is NULL
161 break; //oid = mid =0 for element if not present within the hash table
164 index = lhashFunction(curr->oid);
165 // Insert into the new table
166 if(llookup.table[index].next == NULL && llookup.table[index].oid == 0) {
167 llookup.table[index].oid = curr->oid;
168 llookup.table[index].mid = curr->mid;
169 llookup.numelements++;
171 if((newnode = calloc(1, sizeof(lhashlistnode_t))) == NULL) {
172 printf("Calloc error %s, %d\n", __FILE__, __LINE__);
175 newnode->oid = curr->oid;
176 newnode->mid = curr->mid;
177 newnode->next = llookup.table[index].next;
178 llookup.table[index].next = newnode;
179 llookup.numelements++;
182 //free the linked list of lhashlistnode_t if not the first element in the hash table
193 free(ptr); //Free the memory of the old hash table