-#include "clookup.h"
+#include "chash.h"
#define INLINE inline __attribute__((always_inline))
-chashtable_t *chashCreate(unsigned int size, float loadfactor) {
- chashtable_t *ctable;
- chashlistnode_t *nodes;
+ctable_t *cCreate(unsigned int size, float loadfactor) {
+ ctable_t *ctable;
+ cnode_t *nodes;
int i;
- if((ctable = calloc(1, sizeof(chashtable_t))) == NULL) {
+ if((ctable = calloc(1, sizeof(ctable_t))) == NULL) {
printf("Calloc error %s %d\n", __FILE__, __LINE__);
return NULL;
}
// Allocate space for the hash table
- if((nodes = calloc(size, sizeof(chashlistnode_t))) == NULL) {
+ if((nodes = calloc(size, sizeof(cnode_t))) == NULL) {
printf("Calloc error %s %d\n", __FILE__, __LINE__);
free(ctable);
return NULL;
return ctable;
}
-//Finds the right bin in the hash table
-static INLINE unsigned int chashFunction(chashtable_t *table, unsigned int key) {
- return ( key & (table->mask))>>1; //throw away low order bit
-}
-
//Store objects and their pointers into hash
-unsigned int chashInsert(chashtable_t *table, unsigned int key, void *val) {
+unsigned int cInsert(ctable_t *table, unsigned int key, void *val) {
unsigned int newsize;
int index;
- chashlistnode_t *ptr, *node;
+ cnode_t *ptr, *node;
if(table->numelements > (table->loadfactor * table->size)) {
//Resize
newsize = table->size << 1;
- chashResize(table,newsize);
+ cResize(table,newsize);
}
ptr = table->table;
table->numelements++;
- index = chashFunction(table, key);
-#ifdef DEBUG
- printf("chashInsert(): DEBUG -> index = %d, key = %d, val = %x\n", index, key, val);
-#endif
+ index =(key & table->mask)>>2;
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;
} else { // Insert in the beginning of linked list
- if ((node = calloc(1, sizeof(chashlistnode_t))) == NULL) {
+ if ((node = calloc(1, sizeof(cnode_t))) == NULL) {
printf("Calloc error %s, %d\n", __FILE__, __LINE__);
return 1;
}
}
// Search for an address for a given oid
-INLINE void * chashSearch(chashtable_t *table, unsigned int key) {
+INLINE void * cSearch(ctable_t *table, unsigned int key) {
//REMOVE HASH FUNCTION CALL TO MAKE SURE IT IS INLINED HERE
- chashlistnode_t *node = &table->table[(key & table->mask)>>1];
+ cnode_t *node = &table->table[(key & table->mask)>>2];
while(node != NULL) {
if(node->key == key) {
return NULL;
}
-unsigned int chashRemove(chashtable_t *table, unsigned int key) {
+unsigned int cRemove(ctable_t *table, unsigned int key) {
int index;
- chashlistnode_t *curr, *prev;
- chashlistnode_t *ptr, *node;
+ cnode_t *curr, *prev;
+ cnode_t *ptr, *node;
ptr = table->table;
- index = chashFunction(table,key);
+ index =(key & table->mask)>>2;
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 chashlistnode_t
+ if ((curr == &ptr[index]) && (curr->next == NULL)) { // Delete the first item inside the hashtable with no linked list of cnode_t
curr->key = 0;
curr->val = NULL;
- } else if ((curr == &ptr[index]) && (curr->next != NULL)) { //Delete the first item with a linked list of chashlistnode_t connected
+ } else if ((curr == &ptr[index]) && (curr->next != NULL)) { //Delete the first item with a linked list of cnode_t connected
curr->key = curr->next->key;
curr->val = curr->next->val;
node = curr->next;
return 1;
}
-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 cResize(ctable_t *table, unsigned int newsize) {
+ cnode_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 chashlistnode_t for each bin in hashtable
int i,index;
- chashlistnode_t *newnode;
+ cnode_t *newnode;
ptr = table->table;
oldsize = table->size;
- if((node = calloc(newsize, sizeof(chashlistnode_t))) == NULL) {
+ if((node = calloc(newsize, sizeof(cnode_t))) == NULL) {
printf("Calloc error %s %d\n", __FILE__, __LINE__);
return 1;
}
}
next = curr->next;
- index = chashFunction(table, curr->key);
+ index =(key & table->mask)>>2;
#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(chashlistnode_t))) == NULL) {
+ if((newnode = calloc(1, sizeof(cnode_t))) == NULL) {
printf("Calloc error %s, %d\n", __FILE__, __LINE__);
return 1;
}
}
//Delete the entire hash table
-void chashDelete(chashtable_t *ctable) {
+void cDelete(ctable_t *ctable) {
int i, isFirst;
- chashlistnode_t *ptr, *curr, *next;
+ cnode_t *ptr, *curr, *next;
ptr = ctable->table;
for(i=0 ; i<ctable->size ; i++) {
#include <stdlib.h>
#include <stdio.h>
-#define CLOADFACTOR 0.25
-#define CHASH_SIZE 1024
-
-typedef struct chashlistnode {
+typedef struct cnode {
unsigned int key;
void *val; //this can be cast to another type or used to point to a larger structure
- struct chashlistnode *next;
-} chashlistnode_t;
+ struct cnode *next;
+} cnode_t;
-typedef struct chashtable {
- chashlistnode_t *table; // points to beginning of hash table
+typedef struct ctable {
+ cnode_t *table; // points to beginning of hash table
unsigned int size;
unsigned int mask;
unsigned int numelements;
float loadfactor;
-} chashtable_t;
+} ctable_t;
/* Prototypes for hash*/
-chashtable_t *chashCreate(unsigned int size, float loadfactor);
-static 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);
+ctable_t *cCreate(unsigned int size, float loadfactor);
+unsigned int cInsert(ctable_t *table, unsigned int key, void *val);
+void *cSearch(chashtable_t *table, unsigned int key); //returns val, NULL if not found
+unsigned int cRemove(chashtable_t *table, unsigned int key); //returns -1 if not found
+unsigned int cResize(chashtable_t *table, unsigned int newsize);
+void cDelete(chashtable_t *table);
/* end hash */
#endif