X-Git-Url: https://git.rrq.au/?a=blobdiff_plain;f=vector%2Ftupleitem.c;h=f3dfe2c70b0aff012c7b39397f3881817148bd81;hb=62e848c28a7083828fd47cbd8a29fbc875cf3ecb;hp=14c3b3ba89795aa1f928efd003353e864d7d8ce6;hpb=e47e7f1e366224e9e514e28f172afa65c732db21;p=rrq%2Frrqmisc.git diff --git a/vector/tupleitem.c b/vector/tupleitem.c index 14c3b3b..f3dfe2c 100644 --- a/vector/tupleitem.c +++ b/vector/tupleitem.c @@ -22,15 +22,19 @@ * vector placement is made at the first empty or hole slot following * the hashcode index. */ -static unsigned long tupleitem_hashcode(itemkeyfun *this,void *key) { +static unsigned long tupleitem_hashcode(void *this,void *key) { tupleschema *def = (tupleschema *) this; tuple *kp = (tuple*) key; int i = 0; - unsigned long value = 0; + unsigned long value = 5381; for ( ; i < def->arity; i++ ) { if ( COLUMN[i] ) { value <<= 3; - value += COLUMN[i]->hashcode( COLUMN[i], (*kp)[i] ); + if ( (*kp)[i] ) { + value += COLUMN[i]->hashcode( COLUMN[i], (*kp)[i] ); + } else { + value += 17; + } } } return value; @@ -40,18 +44,19 @@ static unsigned long tupleitem_hashcode(itemkeyfun *this,void *key) { * This callback function determines whether an item has a * given key or not. */ -static int tupleitem_haskey(itemkeyfun *this,void *item,void *key) { +static int tupleitem_haskey(void *this,void *item,void *key) { tupleschema *def = (tupleschema *) this; tuple *kp = (tuple*) key; tuple *tp = (tuple*) item; int i = 0; - int haskey = 1; for ( ; i < def->arity; i++ ) { - if ( COLUMN[i] ) { - haskey &= COLUMN[i]->haskey( COLUMN[i], (*tp)[i], (*kp)[i] ); + if ( COLUMN[i] && (*kp)[i] ) { + if ( COLUMN[i]->haskey( COLUMN[i], (*tp)[i], (*kp)[i] ) == 0 ) { + return 0; + } } } - return haskey; + return 1; } @@ -59,7 +64,7 @@ static int tupleitem_haskey(itemkeyfun *this,void *item,void *key) { * This callback function returns the key of an item by considering * the arity and mask. */ -static void *tupleitem_itemkey(itemkeyfun *this,void *item) { +static void *tupleitem_itemkey(void *this,void *item) { tupleschema *def = (tupleschema *) this; tuple *tp = (tuple*) item; int i, j; @@ -82,7 +87,7 @@ static void *tupleitem_itemkey(itemkeyfun *this,void *item) { * This callback function handles a key obtained from the itemkey * callback function to reclaim temporary allocation. */ -static void tupleitem_releasekey(itemkeyfun *this,void *key) { +static void tupleitem_releasekey(void *this,void *key) { tupleschema *def = (tupleschema *) this; tuple *kp = (tuple*) key; int i,j; @@ -94,6 +99,28 @@ static void tupleitem_releasekey(itemkeyfun *this,void *key) { free( key ); } +#define OUT(X) a = X; if ( a > limit ) return 0; buffer += a; limit -= a + +/** + * This callback function writes a representation of an item into + * a character buffer. + */ +static int tupleitem_tostring(void *this,void *item,char *buffer,int limit) { + tupleschema *def = (tupleschema *) this; + tuple *t = (tuple*) item; + char *x = "<"; + int a, i; + for ( i = 0; i < def->arity; i++ ) { + OUT( snprintf( buffer, limit, x ) ); + x = ","; + OUT( def->columns[i]->tostring( + def->columns[i], (*t)[i], buffer, limit ) ); + } + OUT( snprintf( buffer, limit, ">" ) ); + return a; +} + + // Allocate tuple *tuple_create(int arity,...) { va_list ap; @@ -107,15 +134,18 @@ tuple *tuple_create(int arity,...) { return t; } +itemkeyfun tupleschema_callbacks = { + .hashcode = tupleitem_hashcode, + .haskey = tupleitem_haskey, + .itemkey = tupleitem_itemkey, + .releasekey = tupleitem_releasekey, + .tostring = tupleitem_tostring +}; + tupleschema *tupleschema_create(int arity,tuple *columns) { tupleschema *ts = (tupleschema*) malloc( sizeof( tupleschema ) ); (*ts) = (tupleschema) { - .functions = { - .hashcode = tupleitem_hashcode, - .haskey = tupleitem_haskey, - .itemkey = tupleitem_itemkey, - .releasekey = tupleitem_releasekey - }, + .base = tupleschema_callbacks, .arity = arity, .columns = (itemkeyfun**) columns };