X-Git-Url: https://git.rrq.au/?a=blobdiff_plain;f=vector%2Fstringitem.c;h=ee8f2cba74695f39f16faf47eb8aaa4facd3d9cf;hb=e938f8c45fd191d96da48f65262e4efcfada7805;hp=12ec1aca4f2eff7190335c6ab647af82864b9104;hpb=6f54a8281e4e5d6bc05e6b4eadc3327d5e48614a;p=rrq%2Frrqmisc.git diff --git a/vector/stringitem.c b/vector/stringitem.c index 12ec1ac..ee8f2cb 100644 --- a/vector/stringitem.c +++ b/vector/stringitem.c @@ -1,16 +1,25 @@ #include #include -#include +#include -unsigned long integeritem_hashcode(itemkeyfun *this,void *key) { - return hashvector_hashcode( (unsigned char*)key, strlen( (char*)key ) ); +/** + * This callback function returns the hashcode of a key. The hashcode + * is used for indexing into the backing Vector for finding the an + * item via its key. The same key must map consistently to the same + * hashcode while the hashtable contains an item with that key. + * Different keys map map to the same hashcode, in which case the + * Vector placement is made at the first empty or hole slot following + * the hashcode index. + */ +static unsigned long stringitem_hashcode(void *this,void *key) { + return HashVector_hashcode( (unsigned char*)key, strlen( (char*)key ) ); } /** * This callback function determines whether an item has a * given key or not. */ -int integeritem_haskey(itemkeyfun *this,void *item,void *key) { +static int stringitem_haskey(void *this,void *item,void *key) { return strcmp( item, key ) == 0; } @@ -18,7 +27,7 @@ int integeritem_haskey(itemkeyfun *this,void *item,void *key) { * This callback function returns the key of an item by considering * the arity and schema. */ -void *integeritem_itemkey(itemkeyfun *this,void *item) { +static void *stringitem_itemkey(void *this,void *item) { return item; } @@ -26,14 +35,24 @@ void *integeritem_itemkey(itemkeyfun *this,void *item) { * This callback function handles a key obtained from the itemkey * callback function to reclaim temporary allocation. */ -void integeritem_releasekey(itemkeyfun *this,void *key) { +static void stringitem_releasekey(void *this,void *key) { +} + +/** + * This callback function writes a representation of an item into + * a character buffer. + */ +static int stringitem_tostring(void *this,void *item,char *buffer,int limit) { + if ( item ) { + return snprintf( buffer, limit, "\"%s\"", (char*) item ); + } + return snprintf( buffer, limit, "(null)" ); } -itemkeyfun stringitem = { +ItemKeyFun stringitem = { .hashcode = stringitem_hashcode, .haskey = stringitem_haskey, .itemkey = stringitem_itemkey, - .releasekey = stringitem_releasekey + .releasekey = stringitem_releasekey, + .tostring = stringitem_tostring }; - -