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;
49 for ( ; i < def->arity; i++ ) {
51 haskey &= COLUMN[i]->haskey( COLUMN[i], (*tp)[i], (*kp)[i] );
59 * This callback function returns the key of an item by considering
62 static void *tupleitem_itemkey(itemkeyfun *this,void *item) {
63 tupleschema *def = (tupleschema *) this;
64 tuple *tp = (tuple*) item;
67 for ( i = 0 ; i < def->arity; i++ ) {
72 void **parts = calloc( keylen, sizeof( void* ) );
73 for ( i = 0, j = 0; i < def->arity; i++ ) {
75 parts[j++] = COLUMN[i]->itemkey( COLUMN[i], (*tp)[i] );
82 * This callback function handles a key obtained from the itemkey
83 * callback function to reclaim temporary allocation.
85 static void tupleitem_releasekey(itemkeyfun *this,void *key) {
86 tupleschema *def = (tupleschema *) this;
87 tuple *kp = (tuple*) key;
89 for ( i = 0, j = 0; i < def->arity; i++ ) {
91 COLUMN[i]->releasekey( COLUMN[i], (*kp)[j++] );
98 tuple *tuple_create(int arity,...) {
101 tuple *t = (tuple *)malloc( arity * sizeof( void* ) );
102 va_start( ap, arity );
103 for ( i = 0; i < arity; i++ ) {
104 (*t)[i] = va_arg( ap, void* );
110 tupleschema *tupleschema_create(int arity,tuple *columns) {
111 tupleschema *ts = (tupleschema*) malloc( sizeof( tupleschema ) );
112 (*ts) = (tupleschema) {
114 .hashcode = tupleitem_hashcode,
115 .haskey = tupleitem_haskey,
116 .itemkey = tupleitem_itemkey,
117 .releasekey = tupleitem_releasekey
120 .columns = (itemkeyfun**) columns
125 #define COPYA(T,P,N) (T*) memcpy( malloc( N * sizeof(T) ), P, N * sizeof( T ) )
126 #define COPY(T,P) COPYA(T,P,1)
128 // Duplicate a tupleschema with optionally some columns reset.
129 tupleschema *tupleschema_mask(tupleschema *schema,...) {
130 tupleschema *masked = COPY(tupleschema,schema);
131 masked->columns = COPYA( itemkeyfun*, schema->columns, schema->arity );
134 va_start( ap, schema );
136 i = va_arg( ap, int );
137 if ( i < 0 || i >= schema->arity ) {
140 masked->columns[i] = 0;
146 unsigned long tuple_mask(int arity,tuple *t) {
147 unsigned long mask = 0;
148 while ( arity-- > 0 ) {
150 if ( (*t)[ arity ] ) {