Functions | |
size_t | qHasharrSize (int max) |
Get how much memory is needed for N entries. | |
int | qHasharrInit (Q_HASHARR *tbl, size_t memsize) |
Initialize array-hash table. | |
bool | qHasharrClear (Q_HASHARR *tbl) |
Reset array-hash table. | |
bool | qHasharrPut (Q_HASHARR *tbl, const char *key, const void *value, int size) |
Put object into hash table. | |
bool | qHasharrPutStr (Q_HASHARR *tbl, const char *key, const char *value) |
Put string into hash table. | |
bool | qHasharrPutInt (Q_HASHARR *tbl, const char *key, int value) |
Put integer into hash table. | |
void * | qHasharrGet (Q_HASHARR *tbl, const char *key, int *size) |
Get object from hash table. | |
char * | qHasharrGetStr (Q_HASHARR *tbl, const char *key) |
Get string from hash table. | |
int | qHasharrGetInt (Q_HASHARR *tbl, const char *key) |
Get integer from hash table. | |
const char * | qHasharrGetFirstKey (Q_HASHARR *tbl, int *idx) |
Get first key name. | |
const char * | qHasharrGetNextKey (Q_HASHARR *tbl, int *idx) |
Get next key name. | |
bool | qHasharrRemove (Q_HASHARR *tbl, const char *key) |
Remove key from hash table. | |
bool | qHasharrPrint (Q_HASHARR *tbl, FILE *out) |
Print hash table for debugging purpose. | |
bool | qHasharrStatus (Q_HASHARR *tbl, int *used, int *max) |
Get hash table internal status. |
int maxkeys = 1000; // calculate how many memory do we need size_t 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) == 0) 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)) == 0) return -1; (...your codes here...) // no need to free unless you use malloc()
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) == 0) return -1; (...your codes here...) // destroy shared memory qShmFree(shmid);
size_t qHasharrSize | ( | int | max | ) |
Get how much memory is needed for N entries.
max | a number of maximum internal slots |
int qHasharrInit | ( | Q_HASHARR * | tbl, | |
size_t | memsize | |||
) |
Initialize array-hash table.
// allocate memory size_t memsize = qHasharrSize(100); Q_HASHARR *hasharr = (Q_HASHARR *)malloc(memsize); // initialize hash-table if(qHasharrInit(hasharr, memsize) == 0) return -1;
bool qHasharrClear | ( | Q_HASHARR * | tbl | ) |
Reset array-hash table.
tbl | a pointer of Q_HASHARR |
bool qHasharrPut | ( | Q_HASHARR * | tbl, | |
const char * | key, | |||
const void * | value, | |||
int | size | |||
) |
Put object into hash table.
tbl | a pointer of Q_HASHARR | |
key | key string | |
value | value object data | |
size | size of value |
bool qHasharrPutStr | ( | Q_HASHARR * | tbl, | |
const char * | key, | |||
const char * | value | |||
) |
Put string into hash table.
tbl | a pointer of Q_HASHARR | |
key | key string | |
value | value string |
bool qHasharrPutInt | ( | Q_HASHARR * | tbl, | |
const char * | key, | |||
int | value | |||
) |
Put integer into hash table.
tbl | a pointer of Q_HASHARR | |
key | key string | |
value | value integer |
void* qHasharrGet | ( | Q_HASHARR * | tbl, | |
const char * | key, | |||
int * | size | |||
) |
Get object from hash table.
tbl | a pointer of Q_HASHARR | |
key | key string | |
size | if not NULL, oject size will be stored |
char* qHasharrGetStr | ( | Q_HASHARR * | tbl, | |
const char * | key | |||
) |
Get string from hash table.
tbl | a pointer of Q_HASHARR | |
key | key string |
int qHasharrGetInt | ( | Q_HASHARR * | tbl, | |
const char * | key | |||
) |
Get integer from hash table.
tbl | a pointer of Q_HASHARR | |
key | key string |
const char* qHasharrGetFirstKey | ( | Q_HASHARR * | tbl, | |
int * | idx | |||
) |
Get first key name.
tbl | a pointer of Q_HASHARR | |
idx | index pointer |
char *key; int idx; for(key = qHasharrGetFirstKey(tbl, &idx); key != NULL; key = qHasharrGetNextKey(tbl, &idx) { char *value = qHasharrGetStr(tbl, key); if(value != NULL) free(value); }
const char* qHasharrGetNextKey | ( | Q_HASHARR * | tbl, | |
int * | idx | |||
) |
Get next key name.
tbl | a pointer of Q_HASHARR | |
idx | index pointer |
bool qHasharrRemove | ( | Q_HASHARR * | tbl, | |
const char * | key | |||
) |
Remove key from hash table.
tbl | a pointer of Q_HASHARR | |
key | key string |
bool qHasharrPrint | ( | Q_HASHARR * | tbl, | |
FILE * | out | |||
) |
Print hash table for debugging purpose.
tbl | a pointer of Q_HASHARR | |
out | output stream |
bool qHasharrStatus | ( | Q_HASHARR * | tbl, | |
int * | used, | |||
int * | max | |||
) |
Get hash table internal status.
tbl | a pointer of Q_HASHARR | |
used | if not NULL, a number of used slots(not a number of key) will be stored | |
max | if not NULL, the maximum number of slots will be stored |
[Home] [About] [Examples] [Changes] [Download] [SVN Repository] [Install] [Reference] |