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__);
71 node->next = ptr[index].next;
72 ptr[index].next = node;
75 pthread_mutex_unlock(&llookup.locktable);
79 // Return mid for a given oid in the hash table
80 unsigned int lhashSearch(unsigned int oid) {
82 lhashlistnode_t *ptr, *node;
84 ptr = llookup.table; // Address of the beginning of hash table
85 index = lhashFunction(oid);
87 pthread_mutex_lock(&llookup.locktable);
89 if(node->oid == oid) {
94 pthread_mutex_unlock(&llookup.locktable);
98 // Remove an entry from the hash table
99 unsigned int lhashRemove(unsigned int oid) {
101 lhashlistnode_t *curr, *prev;
102 lhashlistnode_t *ptr, *node;
105 index = lhashFunction(oid);
108 pthread_mutex_lock(&llookup.locktable);
109 for (; curr != NULL; curr = curr->next) {
110 if (curr->oid == oid) { // Find a match in the hash table
111 llookup.numelements--; // Decrement the number of elements in the global hashtable
112 if ((curr == &ptr[index]) && (curr->next == NULL)) { // Delete the first item inside the hashtable with no linked list of lhashlistnode_t
115 } else if ((curr == &ptr[index]) && (curr->next != NULL)) { //Delete the first item with a linked list of lhashlistnode_t connected
116 curr->oid = curr->next->oid;
117 curr->mid = curr->next->mid;
119 curr->next = curr->next->next;
121 } else { // Regular delete from linked listed
122 prev->next = curr->next;
129 pthread_mutex_unlock(&llookup.locktable);
134 unsigned int lhashResize(unsigned int newsize) {
135 lhashlistnode_t *node, *ptr, *curr, *next; // curr and next keep track of the current and the next lhashlistnodes in a linked list
136 unsigned int oldsize;
137 int isfirst; // Keeps track of the first element in the lhashlistnode_t for each bin in hashtable
139 lhashlistnode_t *newnode;
142 oldsize = llookup.size;
144 if((node = calloc(newsize, sizeof(lhashlistnode_t))) == NULL) {
145 printf("Calloc error %s %d\n", __FILE__, __LINE__);
149 llookup.table = node; //Update the global hashtable upon resize()
150 llookup.size = newsize;
151 llookup.numelements = 0;
153 for(i = 0; i < oldsize; i++) { //Outer loop for each bin in hash table
156 while (curr != NULL) { //Inner loop to go through linked lists
157 if (curr->oid == 0) { //Exit inner loop if there the first element for a given bin/index is NULL
158 break; //oid = mid =0 for element if not present within the hash table
161 index = lhashFunction(curr->oid);
162 // Insert into the new table
163 if(llookup.table[index].next == NULL && llookup.table[index].oid == 0) {
164 llookup.table[index].oid = curr->oid;
165 llookup.table[index].mid = curr->mid;
166 llookup.numelements++;
168 if((newnode = calloc(1, sizeof(lhashlistnode_t))) == NULL) {
169 printf("Calloc error %s, %d\n", __FILE__, __LINE__);
172 newnode->oid = curr->oid;
173 newnode->mid = curr->mid;
174 newnode->next = llookup.table[index].next;
175 llookup.table[index].next = newnode;
176 llookup.numelements++;
179 //free the linked list of lhashlistnode_t if not the first element in the hash table
190 free(ptr); //Free the memory of the old hash table