From: Ralph Ronnquist Date: Thu, 30 Jun 2022 11:51:34 +0000 (+1000) Subject: initial tuple support X-Git-Tag: 0.1~2 X-Git-Url: https://git.rrq.au/?a=commitdiff_plain;h=6f54a8281e4e5d6bc05e6b4eadc3327d5e48614a;p=rrq%2Frrqmisc.git initial tuple support --- diff --git a/vector/integeritem.c b/vector/integeritem.c new file mode 100644 index 0000000..57d309f --- /dev/null +++ b/vector/integeritem.c @@ -0,0 +1,35 @@ +#include + +unsigned long integeritem_hashcode(itemkeyfun *this,void *key) { + return (unsigned long) key; +} + +/** + * This callback function determines whether an item has a + * given key or not. + */ +int integeritem_haskey(itemkeyfun *this,void *item,void *key) { + return item == key; +} + +/** + * This callback function returns the key of an item by considering + * the arity and schema. + */ +void *integeritem_itemkey(itemkeyfun *this,void *item) { + return item; +} + +/** + * This callback function handles a key obtained from the itemkey + * callback function to reclaim temporary allocation. + */ +void integeritem_releasekey(itemkeyfun *this,void *key) { +} + +itemkeyfun integeritem = { + .hashcode = integeritem_hashcode, + .haskey = integeritem_haskey, + .itemkey = integeritem_itemkey, + .releasekey = integeritem_releasekey +}; diff --git a/vector/integeritem.h b/vector/integeritem.h new file mode 100644 index 0000000..3dc8545 --- /dev/null +++ b/vector/integeritem.h @@ -0,0 +1,41 @@ +#ifndef integeritem_H +#define integeritem_H + +#include + +/** + * The stringitem record declares the itemkeyfun functions for integer + * items. + */ +extern itemkeyfun integeritem; + +/** + * 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. + */ +extern unsigned long integeritem_hashcode(itemkeyfun *this,void *key); + +/** + * This callback function determines whether an item has a + * given key or not. + */ +extern int integeritem_haskey(itemkeyfun *this,void *item,void *key); + +/** + * This callback function returns the key of an item by considering + * the arity and schema. + */ +extern void *integeritem_itemkey(itemkeyfun *this,void *item); + +/** + * This callback function handles a key obtained from the itemkey + * callback function to reclaim temporary allocation. + */ +extern void integeritem_releasekey(itemkeyfun *this,void *key); + +#endif diff --git a/vector/stringitem.c b/vector/stringitem.c new file mode 100644 index 0000000..12ec1ac --- /dev/null +++ b/vector/stringitem.c @@ -0,0 +1,39 @@ +#include +#include +#include + +unsigned long integeritem_hashcode(itemkeyfun *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) { + return strcmp( item, key ) == 0; +} + +/** + * This callback function returns the key of an item by considering + * the arity and schema. + */ +void *integeritem_itemkey(itemkeyfun *this,void *item) { + return item; +} + +/** + * This callback function handles a key obtained from the itemkey + * callback function to reclaim temporary allocation. + */ +void integeritem_releasekey(itemkeyfun *this,void *key) { +} + +itemkeyfun stringitem = { + .hashcode = stringitem_hashcode, + .haskey = stringitem_haskey, + .itemkey = stringitem_itemkey, + .releasekey = stringitem_releasekey +}; + + diff --git a/vector/stringitem.h b/vector/stringitem.h new file mode 100644 index 0000000..0923bcd --- /dev/null +++ b/vector/stringitem.h @@ -0,0 +1,42 @@ +#ifndef stringitem_H +#define stringitem_H + +#include + +/** + * The stringitem record declares the itemkeyfun functions for string + * items. + */ +extern itemkeyfun stringitem; + +/** + * 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. + */ +extern unsigned long stringitem_hashcode(itemkeyfun *this,void *key); + +/** + * This callback function determines whether an item has a + * given key or not. + */ +extern int stringitem_haskey(itemkeyfun *this,void *item,void *key); + +/** + * This callback function returns the key of an item by considering + * the arity and schema. + */ +extern void *stringitem_itemkey(itemkeyfun *this,void *item); + +/** + * This callback function handles a key obtained from the itemkey + * callback function to reclaim temporary allocation. + */ +extern void stringitem_releasekey(itemkeyfun *this,void *key); + + +#endif diff --git a/vector/tupleitem.c b/vector/tupleitem.c new file mode 100644 index 0000000..eff77ce --- /dev/null +++ b/vector/tupleitem.c @@ -0,0 +1,79 @@ +#include +#include + +typedef void *tuple[]; + +#define COLUMN def->schema[i] + +unsigned long tupleitem_hashcode(itemkeyfun *this,void *key) { + tupleschema *def = (tupleschema *) this; + tuple *kp = (tuple*) key; + int i = 0; + unsigned long value = 0; + for ( ; i < def->arity; i++ ) { + if ( COLUMN ) { + value <<= 3; + value += COLUMN->hashcode( COLUMN, (*kp)[i] ); + } + } + return value; +} + +/** + * This callback function determines whether an item has a + * given key or not. + */ +int tupleitem_haskey(itemkeyfun *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 ) { + haskey &= COLUMN->haskey( COLUMN, (*tp)[i], (*kp)[i] ); + } + } + return haskey; +} + + +/** + * This callback function returns the key of an item by considering + * the arity and mask. + */ +void *tupleitem_itemkey(itemkeyfun *this,void *item) { + tupleschema *def = (tupleschema *) this; + tuple *tp = (tuple*) item; + int i, j; + int keylen = 0; + for ( i = 0 ; i < def->arity; i++ ) { + if ( COLUMN ) { + 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] ); + } + } + return (void*) parts; +} + +/** + * This callback function handles a key obtained from the itemkey + * callback function to reclaim temporary allocation. + */ +void tupleitem_releasekey(itemkeyfun *this,void *key) { + tupleschema *def = (tupleschema *) this; + tuple *kp = (tuple*) key; + int i,j; + for ( i = 0, j = 0; i < def->arity; i++ ) { + if ( COLUMN ) { + COLUMN->releasekey( COLUMN, (*kp)[j++] ); + } + } + free( key ); +} + diff --git a/vector/tupleitem.h b/vector/tupleitem.h new file mode 100644 index 0000000..baec9e3 --- /dev/null +++ b/vector/tupleitem.h @@ -0,0 +1,85 @@ +#ifndef tupleitem_H +#define tupleitem_H + +#include + +/** + * A tupleschema record declares the itemkeyfun functions for tuple + * items together with applicable arity and domain combinations. The + * macro \ref TUPLEITEMINIT may be used to initialize particular + * tupleschema records, which each become tupleitem declaration for + * its particular arity and parts declarations + */ +typedef struct { + /** + * These are the itemkeyfun callback functions to support + * hashvector use for tuple items. The functions expects their + * itemkeyfun pointer to be within a tupleschema record so as to + * 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. + */ + itemkeyfun **schema; +} tupleschema; + +/** + * This callback function returns the hashcode of a key. + * + * \param this is a pointer to the itemkeyfun record from where this + * callback got invoked + * + * \param key is the key to produce a hascode for + * + * \returns the hashcode which is a vector_index (i.e. unsigned long) + * + * 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. + */ +extern unsigned long tupleitem_hashcode(itemkeyfun *this,void *key); + +/** + * This callback function determines whether an item has a + * given key or not. + */ +extern int tupleitem_haskey(itemkeyfun *this,void *item,void *key); + +/** + * This callback function returns the key of an item by considering + * the arity and schema. + */ +extern void *tupleitem_itemkey(itemkeyfun *this,void *item); + +/** + * This callback function handles a key obtained from the itemkey + * callback function to reclaim temporary allocation. + */ +extern void tupleitem_releasekey(itemkeyfun *this,void *key); + +/** + * 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 = { \ + .hashcode = tupleitem_hashcode, \ + .haskey = tupleitem_haskey, \ + .itemkey = tupleitem_itemkey, \ + .releasekey = tupleitem_releasekey \ + }, \ + .arity = arity, \ + .schema = { __VA_ARGS__ } \ + } + +#endif