6 #define COLUMN def->columns
9 * This callback function returns the hashcode of a key.
11 * \param this is a pointer to the itemkeyfun record from where this
12 * callback got invoked
14 * \param key is the key to produce a hascode for
16 * \returns the hashcode which is a vector_index (i.e. unsigned long)
18 * The hashcode is used for indexing into the backing vector for
19 * finding the an item via its key. The same key must map consistently
20 * to the same hashcode while the hashtable contains an item with that
21 * key. Different keys map map to the same hashcode, in which case the
22 * vector placement is made at the first empty or hole slot following
25 static unsigned long tupleitem_hashcode(itemkeyfun *this,void *key) {
26 tupleschema *def = (tupleschema *) this;
27 tuple *kp = (tuple*) key;
29 unsigned long value = 0;
30 for ( ; i < def->arity; i++ ) {
33 value += COLUMN[i]->hashcode( COLUMN[i], (*kp)[i] );
40 * This callback function determines whether an item has a
43 static int tupleitem_haskey(itemkeyfun *this,void *item,void *key) {
44 tupleschema *def = (tupleschema *) this;
45 tuple *kp = (tuple*) key;
46 tuple *tp = (tuple*) item;
48 for ( ; i < def->arity; i++ ) {
49 if ( COLUMN[i] == 0 ) {
50 if ( (*kp)[i] && (*tp)[i] != (*kp)[i] ) {
56 COLUMN[i]->haskey( COLUMN[i], (*tp)[i], (*kp)[i] ) == 0 ) {
65 * This callback function returns the key of an item by considering
68 static void *tupleitem_itemkey(itemkeyfun *this,void *item) {
69 tupleschema *def = (tupleschema *) this;
70 tuple *tp = (tuple*) item;
73 for ( i = 0 ; i < def->arity; i++ ) {
78 void **parts = calloc( keylen, sizeof( void* ) );
79 for ( i = 0, j = 0; i < def->arity; i++ ) {
81 parts[j++] = COLUMN[i]->itemkey( COLUMN[i], (*tp)[i] );
88 * This callback function handles a key obtained from the itemkey
89 * callback function to reclaim temporary allocation.
91 static void tupleitem_releasekey(itemkeyfun *this,void *key) {
92 tupleschema *def = (tupleschema *) this;
93 tuple *kp = (tuple*) key;
95 for ( i = 0, j = 0; i < def->arity; i++ ) {
97 COLUMN[i]->releasekey( COLUMN[i], (*kp)[j++] );
104 tuple *tuple_create(int arity,...) {
107 tuple *t = (tuple *)malloc( arity * sizeof( void* ) );
108 va_start( ap, arity );
109 for ( i = 0; i < arity; i++ ) {
110 (*t)[i] = va_arg( ap, void* );
116 tupleschema *tupleschema_create(int arity,tuple *columns) {
117 tupleschema *ts = (tupleschema*) malloc( sizeof( tupleschema ) );
118 (*ts) = (tupleschema) {
120 .hashcode = tupleitem_hashcode,
121 .haskey = tupleitem_haskey,
122 .itemkey = tupleitem_itemkey,
123 .releasekey = tupleitem_releasekey
126 .columns = (itemkeyfun**) columns
131 #define COPYA(T,P,N) (T*) memcpy( malloc( N * sizeof(T) ), P, N * sizeof( T ) )
132 #define COPY(T,P) COPYA(T,P,1)
134 // Duplicate a tupleschema with optionally some columns reset.
135 tupleschema *tupleschema_mask(tupleschema *schema,...) {
136 tupleschema *masked = COPY(tupleschema,schema);
137 masked->columns = COPYA( itemkeyfun*, schema->columns, schema->arity );
140 va_start( ap, schema );
142 i = va_arg( ap, int );
143 if ( i < 0 || i >= schema->arity ) {
146 masked->columns[i] = 0;
152 unsigned long tuple_mask(int arity,tuple *t) {
153 unsigned long mask = 0;
154 while ( arity-- > 0 ) {
156 if ( (*t)[ arity ] ) {