The qDecoder Project

qHasharr.c File Reference

Array based Hash-table Data Structure API. More...


Functions

size_t qHasharrSize (int max)
 Under-development.
bool qHasharrInit (Q_HASHARR *tbl, size_t memsize)
 Under-development.
bool qHasharrClear (Q_HASHARR *tbl)
 Under-development.
bool qHasharrPut (Q_HASHARR *tbl, char *key, char *value, int size)
 Under-development.
bool qHasharrPutStr (Q_HASHARR *tbl, char *key, char *value)
 Under-development.
bool qHasharrPutInt (Q_HASHARR *tbl, char *key, int value)
 Under-development.
char * qHasharrGet (Q_HASHARR *tbl, char *key, int *size)
 Under-development.
char * qHasharrGetStr (Q_HASHARR *tbl, char *key)
 Under-development.
int qHasharrGetInt (Q_HASHARR *tbl, char *key)
 Under-development.
char * qHasharrGetFirstKey (Q_HASHARR *tbl, int *idx)
 Under-development.
char * qHasharrGetNextKey (Q_HASHARR *tbl, int *idx)
 Under-development.
bool qHasharrRemove (Q_HASHARR *tbl, char *key)
 Under-development.
bool qHasharrPrint (Q_HASHARR *tbl, FILE *out)
 Under-development.
bool qHasharrStatus (Q_HASHARR *tbl, int *used, int *max)
 Under-development.


Detailed Description

Array based Hash-table Data Structure API.

Note:
In this array hash-table, we use some technics to effectively use memory. To verify key we use two way, if the key is smaller than (_Q_HASHARR_MAX_KEYSIZE - 1), we compare key itself. But if the key is bigger than (_Q_HASHARR_MAX_KEYSIZE - 1), we compare md5 of key and key length. If the key length and md5 of key are same we consider it's same key. So we don't need to store full key string. Actually it's not necessary to keep original key string, but we keep this because of two reasons. 1) if the length of the key is smaller than 16, it will be little bit quicker to compare key. 2) debugging reason.
Basically this hash-table based on array defines small size slot then it can links several slot for one data. This mechanism can save some wastes of memory. You can adjust default slot size to modify _Q_HASHARR_DEF_VALUESIZE.

   int maxkeys = 1000;

   // calculate how many memory do we need
   int memsize = qHasharrSize(maxkeys * 2); // generally allocate double size of max to decrease hash collision

   // allocate memory
   Q_HASHARR *hasharr = (Q_HASHARR *)malloc(memsize);

   // initialize hash-table
   if(qHasharrInit(hasharr, memsize) == false) return -1;

   // put some sample data
   if(qHasharrPut(hasharr, "sample1", "binary", 6) == false) return -1; // hash-table full
   if(qHasharrPutStr(hasharr, "sample2", "string") == false) return -1; // hash-table full
   if(qHasharrPutInt(hasharr, "sample3", 3) == false) return -1; // hash-table full

   // fetch data
   int size;
   char *sample_bin = qHasharrGet(hasharr, "sample1", &size);
   char *sample_str = qHasharrGetStr(hasharr, "sample2");
   int  sample_int  = qHasharrGetInt(hasharr, "sample3");

Another simple way to initialize hash-table.

   // define data memory as much as you needed.
   char datamem[10 * 1024];

   // just set the Q_HASHARR points to data memory.
   Q_HASHARR *hasharr = (Q_HASHARR *)datamem;

   // initialize hash-table.
   if(qHasharrInit(hasharr, sizeof(datamem)) == false) return -1;

You can create hash table on shared memory like below.

   int maxkeys = 1000;
   int memsize = qHasharrSize(maxkeys * 2);

   // create shared memory
   int shmid = qShmInit(g_conf.szEgisdavdPidfile, 's', memsize, true);
   if(shmid < 0) return -1; // creation failed
   Q_HASHARR *hasharr = (Q_HASHARR *)qShmGet(shmid);

   // initialize hash-table
   if(qHasharrInit(hasharr, memsize) == false) return -1;

   (...your codes here...)

   // destroy shared memory
   qShmFree(shmid);

Function Documentation

size_t qHasharrSize ( int  max  ) 

Under-development.

Returns:

bool qHasharrInit ( Q_HASHARR *  tbl,
size_t  memsize 
)

Under-development.

Returns:

bool qHasharrClear ( Q_HASHARR *  tbl  ) 

Under-development.

Returns:

bool qHasharrPut ( Q_HASHARR *  tbl,
char *  key,
char *  value,
int  size 
)

Under-development.

Returns:
true when successful, otherwise(table full) false

bool qHasharrPutStr ( Q_HASHARR *  tbl,
char *  key,
char *  value 
)

Under-development.

Returns:
true when successful, otherwise(table full) false

bool qHasharrPutInt ( Q_HASHARR *  tbl,
char *  key,
int  value 
)

Under-development.

Returns:
true when successful, otherwise(table full) false

char* qHasharrGet ( Q_HASHARR *  tbl,
char *  key,
int *  size 
)

Under-development.

Returns:
value data which is malloced

char* qHasharrGetStr ( Q_HASHARR *  tbl,
char *  key 
)

Under-development.

Returns:

int qHasharrGetInt ( Q_HASHARR *  tbl,
char *  key 
)

Under-development.

Returns:

char* qHasharrGetFirstKey ( Q_HASHARR *  tbl,
int *  idx 
)

Under-development.

Returns:
   char *key;
   int idx;
   for(key = qHasharrGetFirstKey(tbl, &idx); key != NULL; key = qHasharrGetNextKey(tbl, &idx) {
     char *value = qHasharrGetStr(tbl, key);
   }

char* qHasharrGetNextKey ( Q_HASHARR *  tbl,
int *  idx 
)

Under-development.

Returns:

bool qHasharrRemove ( Q_HASHARR *  tbl,
char *  key 
)

Under-development.

Returns:

bool qHasharrPrint ( Q_HASHARR *  tbl,
FILE *  out 
)

Under-development.

Returns:

bool qHasharrStatus ( Q_HASHARR *  tbl,
int *  used,
int *  max 
)

Under-development.

Returns:


[Home] [About] [Examples] [Changes] [Download] [SVN Repository] [Install] [Reference]