#include "clookup.h"
-cachehashtable_t *cachehashCreate(unsigned int size, float loadfactor) {
- cachehashtable_t *ctable;
- cachehashlistnode_t *nodes;
+chashtable_t *chashCreate(unsigned int size, float loadfactor) {
+ chashtable_t *ctable;
+ chashlistnode_t *nodes;
int i;
- if((ctable = calloc(1, sizeof(cachehashtable_t))) == NULL) {
+ if((ctable = calloc(1, sizeof(chashtable_t))) == NULL) {
printf("Calloc error %s %d\n", __FILE__, __LINE__);
return NULL;
}
// Allocate space for the hash table
- if((nodes = calloc(HASH_SIZE, sizeof(cachehashlistnode_t))) == NULL) {
+ if((nodes = calloc(size, sizeof(chashlistnode_t))) == NULL) {
printf("Calloc error %s %d\n", __FILE__, __LINE__);
return NULL;
}
}
//Finds the right bin in the hash table
-unsigned int cachehashFunction(cachehashtable_t *table, unsigned int key) {
+unsigned int chashFunction(chashtable_t *table, unsigned int key) {
return ( key % (table->size));
}
//Store objects and their pointers into hash
-unsigned int cachehashInsert(cachehashtable_t *table, unsigned int key, void *val) {
+unsigned int chashInsert(chashtable_t *table, unsigned int key, void *val) {
unsigned int newsize;
int index;
- cachehashlistnode_t *ptr, *node;
+ chashlistnode_t *ptr, *node;
if(table->numelements > (table->loadfactor * table->size)) {
//Resize
newsize = 2 * table->size + 1;
- cachehashResize(table,newsize);
+ chashResize(table,newsize);
}
ptr = table->table;
table->numelements++;
- index = cachehashFunction(table, key);
+ index = chashFunction(table, key);
#ifdef DEBUG
printf("DEBUG -> index = %d, key = %d, val = %x\n", index, key, val);
#endif
ptr[index].key = key;
ptr[index].val = val;
} else { // Insert in the beginning of linked list
- if ((node = calloc(1, sizeof(cachehashlistnode_t))) == NULL) {
+ if ((node = calloc(1, sizeof(chashlistnode_t))) == NULL) {
printf("Calloc error %s, %d\n", __FILE__, __LINE__);
return 1;
}
}
// Search for an address for a given oid
-void *cachehashSearch(cachehashtable_t *table, unsigned int key) {
+void *chashSearch(chashtable_t *table, unsigned int key) {
int index;
- cachehashlistnode_t *ptr, *node;
+ chashlistnode_t *ptr, *node;
ptr = table->table;
- index = cachehashFunction(table, key);
+ index = chashFunction(table, key);
node = &ptr[index];
while(node != NULL) {
if(node->key == key) {
return NULL;
}
-unsigned int cachehashRemove(cachehashtable_t *table, unsigned int key) {
+unsigned int chashRemove(chashtable_t *table, unsigned int key) {
int index;
- cachehashlistnode_t *curr, *prev;
- cachehashlistnode_t *ptr, *node;
+ chashlistnode_t *curr, *prev;
+ chashlistnode_t *ptr, *node;
ptr = table->table;
- index = cachehashFunction(table,key);
+ index = chashFunction(table,key);
curr = &ptr[index];
for (; curr != NULL; curr = curr->next) {
if (curr->key == key) { // Find a match in the hash table
table->numelements--; // Decrement the number of elements in the global hashtable
- if ((curr == &ptr[index]) && (curr->next == NULL)) { // Delete the first item inside the hashtable with no linked list of cachehashlistnode_t
+ if ((curr == &ptr[index]) && (curr->next == NULL)) { // Delete the first item inside the hashtable with no linked list of chashlistnode_t
curr->key = 0;
curr->val = NULL;
- } else if ((curr == &ptr[index]) && (curr->next != NULL)) { //Delete the first item with a linked list of cachehashlistnode_t connected
+ } else if ((curr == &ptr[index]) && (curr->next != NULL)) { //Delete the first item with a linked list of chashlistnode_t connected
curr->key = curr->next->key;
curr->val = curr->next->val;
node = curr->next;
return 1;
}
-unsigned int cachehashResize(cachehashtable_t *table, unsigned int newsize) {
- cachehashlistnode_t *node, *ptr, *curr, *next; // curr and next keep track of the current and the next cachehashlistnodes in a linked list
+unsigned int chashResize(chashtable_t *table, unsigned int newsize) {
+ chashlistnode_t *node, *ptr, *curr, *next; // curr and next keep track of the current and the next chashlistnodes in a linked list
unsigned int oldsize;
- int isfirst; // Keeps track of the first element in the cachehashlistnode_t for each bin in hashtable
+ int isfirst; // Keeps track of the first element in the chashlistnode_t for each bin in hashtable
int i,index;
- cachehashlistnode_t *newnode;
+ chashlistnode_t *newnode;
ptr = table->table;
oldsize = table->size;
- if((node = calloc(newsize, sizeof(cachehashlistnode_t))) == NULL) {
+ if((node = calloc(newsize, sizeof(chashlistnode_t))) == NULL) {
printf("Calloc error %s %d\n", __FILE__, __LINE__);
return 1;
}
}
next = curr->next;
- index = cachehashFunction(table, curr->key);
+ index = chashFunction(table, curr->key);
#ifdef DEBUG
printf("DEBUG(resize) -> index = %d, key = %d, val = %x\n", index, curr->key, curr->val);
#endif
table->table[index].val = curr->val;
table->numelements++;
}else {
- if((newnode = calloc(1, sizeof(cachehashlistnode_t))) == NULL) {
+ if((newnode = calloc(1, sizeof(chashlistnode_t))) == NULL) {
printf("Calloc error %s, %d\n", __FILE__, __LINE__);
return 1;
}
table->numelements++;
}
- //free the linked list of cachehashlistnode_t if not the first element in the hash table
+ //free the linked list of chashlistnode_t if not the first element in the hash table
if (isfirst != 1) {
free(curr);
}
}
//Delete the entire hash table
-void cachehashDelete(cachehashtable_t *table) {
+void chashDelete(chashtable_t *table) {
int i, isFirst;
- cachehashlistnode_t *ptr, *curr, *next;
+ chashlistnode_t *ptr, *curr, *next;
ptr = table->table;
for(i=0 ; i<table->size ; i++) {
free(ptr);
free(table);
+ table = NULL;
}
#define LOADFACTOR 0.75
#define HASH_SIZE 100
-typedef struct cachehashlistnode {
+typedef struct chashlistnode {
unsigned int key;
void *val; //this can be cast to another type or used to point to a larger structure
- struct cachehashlistnode *next;
-} cachehashlistnode_t;
+ struct chashlistnode *next;
+} chashlistnode_t;
typedef struct cashehashtable {
- cachehashlistnode_t *table; // points to beginning of hash table
+ chashlistnode_t *table; // points to beginning of hash table
unsigned int size;
unsigned int numelements;
float loadfactor;
-} cachehashtable_t;
+} chashtable_t;
/* Prototypes for hash*/
-cachehashtable_t *cachehashCreate(unsigned int size, float loadfactor);
-unsigned int cachehashFunction(cachehashtable_t *table, unsigned int key);
-unsigned int cachehashInsert(cachehashtable_t *table, unsigned int key, void *val);
-void *cachehashSearch(cachehashtable_t *table, unsigned int key); //returns val, NULL if not found
-unsigned int cachehashRemove(cachehashtable_t *table, unsigned int key); //returns -1 if not found
-unsigned int cachehashResize(cachehashtable_t *table, unsigned int newsize);
-void cachehashDelete(cachehashtable_t *table);
+chashtable_t *chashCreate(unsigned int size, float loadfactor);
+unsigned int chashFunction(chashtable_t *table, unsigned int key);
+unsigned int chashInsert(chashtable_t *table, unsigned int key, void *val);
+void *chashSearch(chashtable_t *table, unsigned int key); //returns val, NULL if not found
+unsigned int chashRemove(chashtable_t *table, unsigned int key); //returns -1 if not found
+unsigned int chashResize(chashtable_t *table, unsigned int newsize);
+void chashDelete(chashtable_t *table);
/* end hash */
#endif
llookup hash is an array of lhashlistnode_t
oid = mid = 0 in a given lhashlistnode_t for each bin in the hash table ONLY if the entry is empty =>
the OID's can be any unsigned int except 0
+
+ Uses pthreads. compile using -lpthread option
***************************************************************************************************/
#include "llookup.h"
lhashtable_t llookup; //Global Hash table
-// Creates a hash table with default size HASH_SIZE and an array of lhashlistnode_t
+// Creates a hash table with size and an array of lhashlistnode_t
unsigned int lhashCreate(unsigned int size, float loadfactor) {
lhashlistnode_t *nodes;
int i;
// Allocate space for the hash table
- if((nodes = calloc(HASH_SIZE, sizeof(lhashlistnode_t))) == NULL) {
+ if((nodes = calloc(size, sizeof(lhashlistnode_t))) == NULL) {
printf("Calloc error %s %d\n", __FILE__, __LINE__);
return 1;
}
llookup.size = size;
llookup.numelements = 0; // Initial number of elements in the hash
llookup.loadfactor = loadfactor;
+ //Initialize the pthread_mutex variable
+ pthread_mutex_init(&llookup.locktable, NULL);
return 0;
}
if (llookup.numelements > (llookup.loadfactor * llookup.size)) {
//Resize Table
newsize = 2 * llookup.size + 1;
+ pthread_mutex_lock(&llookup.locktable);
lhashResize(newsize);
+ pthread_mutex_unlock(&llookup.locktable);
}
+
ptr = llookup.table;
llookup.numelements++;
#ifdef DEBUG
printf("DEBUG(insert) oid = %d, mid =%d, index =%d\n",oid,mid, index);
#endif
+ pthread_mutex_lock(&llookup.locktable);
if(ptr[index].next == NULL && ptr[index].oid == 0) { // Insert at the first position in the hashtable
ptr[index].oid = oid;
ptr[index].mid = mid;
node->next = ptr[index].next;
ptr[index].next = node;
}
+
+ pthread_mutex_unlock(&llookup.locktable);
return 0;
}
ptr = llookup.table; // Address of the beginning of hash table
index = lhashFunction(oid);
node = &ptr[index];
+ pthread_mutex_lock(&llookup.locktable);
while(node != NULL) {
if(node->oid == oid) {
return node->mid;
}
node = node->next;
}
+ pthread_mutex_unlock(&llookup.locktable);
return 0;
}
ptr = llookup.table;
index = lhashFunction(oid);
curr = &ptr[index];
-
+
+ pthread_mutex_lock(&llookup.locktable);
for (; curr != NULL; curr = curr->next) {
if (curr->oid == oid) { // Find a match in the hash table
llookup.numelements--; // Decrement the number of elements in the global hashtable
}
prev = curr;
}
+ pthread_mutex_unlock(&llookup.locktable);
return 1;
}
#include <stdlib.h>
#include <stdio.h>
+#include <pthread.h>
#define LOADFACTOR 0.75
#define HASH_SIZE 100
unsigned int size;
unsigned int numelements;
float loadfactor;
+ pthread_mutex_t locktable;
} lhashtable_t;
unsigned int lhashCreate(unsigned int size, float loadfactor);// returns 0 for success and 0 for failure
mhashtable_t mlookup; //Global hash table
+// Creates a machine lookup table with size =" size"
unsigned int mhashCreate(unsigned int size, float loadfactor) {
mhashlistnode_t *nodes;
int i;
// Allocate space for the hash table
- if((nodes = calloc(HASH_SIZE, sizeof(mhashlistnode_t))) == NULL) {
+ if((nodes = calloc(size, sizeof(mhashlistnode_t))) == NULL) {
printf("Calloc error %s %d\n", __FILE__, __LINE__);
return 1;
}
mlookup.size = size;
mlookup.numelements = 0; // Initial number of elements in the hash
mlookup.loadfactor = loadfactor;
+ //Initialize the pthread_mutex variable
+ pthread_mutex_init(&mlookup.locktable, NULL);
return 0;
}
if (mlookup.numelements > (mlookup.loadfactor * mlookup.size)) {
//Resize Table
newsize = 2 * mlookup.size + 1;
+ pthread_mutex_lock(&mlookup.locktable);
mhashResize(newsize);
+ pthread_mutex_unlock(&mlookup.locktable);
}
ptr = mlookup.table;
mlookup.numelements++;
#ifdef DEBUG
printf("DEBUG -> index = %d, key = %d, val = %x\n", index, key, val);
#endif
+ pthread_mutex_lock(&mlookup.locktable);
if(ptr[index].next == NULL && ptr[index].key == 0) { // Insert at the first position in the hashtable
ptr[index].key = key;
ptr[index].val = val;
node->next = ptr[index].next;
ptr[index].next = node;
}
+ pthread_mutex_unlock(&mlookup.locktable);
return 0;
}
ptr = mlookup.table; // Address of the beginning of hash table
index = mhashFunction(key);
node = &ptr[index];
+ pthread_mutex_lock(&mlookup.locktable);
while(node != NULL) {
if(node->key == key) {
return node->val;
}
node = node->next;
}
+ pthread_mutex_unlock(&mlookup.locktable);
return NULL;
}
index = mhashFunction(key);
curr = &ptr[index];
+ pthread_mutex_lock(&mlookup.locktable);
for (; curr != NULL; curr = curr->next) {
if (curr->key == key) { // Find a match in the hash table
mlookup.numelements--; // Decrement the number of elements in the global hashtable
}
prev = curr;
}
+ pthread_mutex_unlock(&mlookup.locktable);
return 1;
}
#include <stdlib.h>
#include <stdio.h>
+#include <pthread.h>
#define LOADFACTOR 0.75
#define HASH_SIZE 100
unsigned int size;
unsigned int numelements;
float loadfactor;
+ pthread_mutex_t locktable;
} mhashtable_t;
unsigned int mhashCreate(unsigned int size, float loadfactor);
{
int i;
void *val;
- cachehashtable_t *ctable;
+ chashtable_t *ctable;
- if (( ctable = cachehashCreate(20, 0.50)) == NULL) {
- printf("cachehashCreate error\n"); //creates hashtable
+ if (( ctable = chashCreate(1000, 0.40)) == NULL) {
+ printf("chashCreate error\n"); //creates hashtable
}
- for (i = 1; i <= 10; i++) { // Checks the insert() and resize()
- if (cachehashInsert(ctable, 10*i, &i) == 1)
- printf("cachehashInsert error\n");
+ for (i = 1; i <= 2000; i++) { // Checks the insert() and resize()
+ if (chashInsert(ctable, 10*i, &i) == 1)
+ printf("chashInsert error\n");
}
- i = cachehashRemove(ctable, 10);//Delete first element in the hashtable
+ i = chashRemove(ctable, 10);//Delete first element in the hashtable
if(i == 1)
- printf("cachehashRemove error ");
+ printf("chashRemove error ");
- for (i = 1; i <= 10; i++) { // Check if it can search for all keys in hash table
- val = cachehashSearch(ctable, 10*i);
+ for (i = 1; i <= 2000; i++) { // Check if it can search for all keys in hash table
+ val = chashSearch(ctable, 10*i);
if (val != &i)
- printf("cachehashSearch error - val = %d\n", val);
+ printf("chashSearch error - val = %d\n", val);
else
- printf("cachehashSearch key = %d val = %x\n",10*i, val);
+ printf("chashSearch key = %d val = %x\n",10*i, val);
}
- i = cachehashRemove(ctable, 30);
+ i = chashRemove(ctable, 30);
if(i == 1)
- printf("cachehashRemove error\n ");
- i = cachehashRemove(ctable, 40);
+ printf("chashRemove error\n ");
+ i = chashRemove(ctable, 40);
if(i == 1)
- printf("cachehashRemove error\n ");
- i = cachehashRemove(ctable, 80);
+ printf("chashRemove error\n ");
+ i = chashRemove(ctable, 80);
if(i == 1)
- printf("cachehashRemove error\n ");
- i = cachehashRemove(ctable, 100);
+ printf("chashRemove error\n ");
+ i = chashRemove(ctable, 100);
if(i == 1)
- printf("cachehashRemove error\n ");
- i = cachehashRemove(ctable, 90);
+ printf("chashRemove error\n ");
+ i = chashRemove(ctable, 90);
if(i == 1)
- printf("cachehashRemove error\n ");
+ printf("chashRemove error\n ");
- for (i = 1; i <= 10; i++) { //Prints all left over elements inside hash after deletion and prints error if element not found in hash
- val = cachehashSearch(ctable, 10*i);
+ for (i = 1; i <= 2000; i++) { //Prints all left over elements inside hash after deletion and prints error if element not found in hash
+ val = chashSearch(ctable, 10*i);
if (val != &i)
- printf("cachehashSearch error - val = %d\n", val);
+ printf("chashSearch error - val = %d\n", val);
else
- printf("cachehashSearch key = %d val = %x\n",10*i, val);
+ printf("chashSearch key = %d val = %x\n",10*i, val);
}
printf("The total number of elements in table : %d\n", ctable->numelements);
-
+
+ chashDelete(ctable);
}
-