From: Ralph Ronnquist Date: Tue, 5 Jul 2022 11:53:20 +0000 (+1000) Subject: updates towards supporting relation. X-Git-Url: https://git.rrq.au/?a=commitdiff_plain;h=84fa20543ae0464a87fde599d1db66bfdbd29198;p=rrq%2Frrqmisc.git updates towards supporting relation. --- diff --git a/vector/tupleitem.c b/vector/tupleitem.c index eff77ce..ce5c713 100644 --- a/vector/tupleitem.c +++ b/vector/tupleitem.c @@ -1,9 +1,9 @@ +#include #include +#include #include -typedef void *tuple[]; - -#define COLUMN def->schema[i] +#define COLUMN def->columns unsigned long tupleitem_hashcode(itemkeyfun *this,void *key) { tupleschema *def = (tupleschema *) this; @@ -11,9 +11,9 @@ unsigned long tupleitem_hashcode(itemkeyfun *this,void *key) { int i = 0; unsigned long value = 0; for ( ; i < def->arity; i++ ) { - if ( COLUMN ) { + if ( COLUMN[i] ) { value <<= 3; - value += COLUMN->hashcode( COLUMN, (*kp)[i] ); + value += COLUMN[i]->hashcode( COLUMN[i], (*kp)[i] ); } } return value; @@ -30,8 +30,8 @@ int tupleitem_haskey(itemkeyfun *this,void *item,void *key) { int i = 0; int haskey = 1; for ( ; i < def->arity; i++ ) { - if ( COLUMN ) { - haskey &= COLUMN->haskey( COLUMN, (*tp)[i], (*kp)[i] ); + if ( COLUMN[i] ) { + haskey &= COLUMN[i]->haskey( COLUMN[i], (*tp)[i], (*kp)[i] ); } } return haskey; @@ -48,14 +48,14 @@ void *tupleitem_itemkey(itemkeyfun *this,void *item) { int i, j; int keylen = 0; for ( i = 0 ; i < def->arity; i++ ) { - if ( COLUMN ) { + if ( COLUMN[i] ) { keylen++; } } void **parts = calloc( keylen, sizeof( void* ) ); for ( i = 0, j = 0; i < def->arity; i++ ) { - if ( COLUMN ) { - parts[j++] = COLUMN->itemkey( COLUMN, (*tp)[i] ); + if ( COLUMN[i] ) { + parts[j++] = COLUMN[i]->itemkey( COLUMN[i], (*tp)[i] ); } } return (void*) parts; @@ -70,10 +70,54 @@ void tupleitem_releasekey(itemkeyfun *this,void *key) { tuple *kp = (tuple*) key; int i,j; for ( i = 0, j = 0; i < def->arity; i++ ) { - if ( COLUMN ) { - COLUMN->releasekey( COLUMN, (*kp)[j++] ); + if ( COLUMN[i] ) { + COLUMN[i]->releasekey( COLUMN[i], (*kp)[j++] ); } } free( key ); } +// Allocate +tuple *tuple_create(int arity,...) { + va_list ap; + int i; + tuple *t = (tuple *)malloc( arity * sizeof( void* ) ); + for ( i = 0; i < arity; i++ ) { + (*t)[i] = va_arg( ap, void* ); + } + return t; +} + +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 + }, + .arity = arity, + .columns = (itemkeyfun**) columns + }; + return ts; +} + +#define COPYA(T,P,N) (T*) memcpy( malloc( N * sizeof(T) ), P, N * sizeof( T ) ) +#define COPY(T,P) COPYA(T,P,1) + +// Duplicate a tupleschema with optionally some columns reset. +tupleschema *tupleschema_mask(tupleschema *schema,...) { + tupleschema *masked = COPY(tupleschema,schema); + masked->columns = COPYA( itemkeyfun*, schema->columns, schema->arity ); + va_list ap; + int i; + for ( ;; ) { + i = va_arg( ap, int ); + if ( i < 0 || i >= schema->arity ) { + break; + } + masked->columns[i] = 0; + }; + return masked; +} diff --git a/vector/tupleitem.h b/vector/tupleitem.h index baec9e3..37d5e24 100644 --- a/vector/tupleitem.h +++ b/vector/tupleitem.h @@ -3,6 +3,11 @@ #include +/** + * A tuple is an array of void* + */ +typedef void *tuple[]; + /** * A tupleschema record declares the itemkeyfun functions for tuple * items together with applicable arity and domain combinations. The @@ -18,15 +23,17 @@ typedef struct { * provide the handling of the tuple columns. */ itemkeyfun functions; + /** * This is the number of columns in a tuple. */ int arity; + /** - * This is an array of pointers to the tuple item domains as - * represented by their associated itemkeyfun records. + * This points to an array of pointers to the tuple item domains + * as represented by their associated itemkeyfun records. */ - itemkeyfun **schema; + itemkeyfun **columns; } tupleschema; /** @@ -66,13 +73,30 @@ extern void *tupleitem_itemkey(itemkeyfun *this,void *item); */ extern void tupleitem_releasekey(itemkeyfun *this,void *key); +/** + * Create a tuples with given values. + */ +extern tuple *tuple_create(int arity,...); + +/** + * Create a tuples with given values. + */ +extern tupleschema *tupleschema_create(int arity,tuple *columns); + +/** + * Copy the given tupleschema into a new tupleschema with some columns + * masked. This represents a sub-index type using the unmasked columns + * for indexing. + */ +extern tupleschema *tupleschema_mask(tupleschema *schema,...); + /** * The TUPLEITEMINIT macro is used for initializing a tupleschema * record appropriately for a given arity and corresponding sequence * of parts itemkeyfun pointers. */ #define TUPLEITEMINIT(arity, ... ) { \ - functions = { \ + .functions = { \ .hashcode = tupleitem_hashcode, \ .haskey = tupleitem_haskey, \ .itemkey = tupleitem_itemkey, \