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(void *this,void *key) {
26 tupleschema *def = (tupleschema *) this;
27 tuple *kp = (tuple*) key;
29 unsigned long value = 5381;
30 for ( ; i < def->arity; i++ ) {
34 value += COLUMN[i]->hashcode( COLUMN[i], (*kp)[i] );
44 * This callback function determines whether an item has a
47 static int tupleitem_haskey(void *this,void *item,void *key) {
48 tupleschema *def = (tupleschema *) this;
49 tuple *kp = (tuple*) key;
50 tuple *tp = (tuple*) item;
52 for ( ; i < def->arity; i++ ) {
53 if ( COLUMN[i] && (*kp)[i] ) {
54 if ( COLUMN[i]->haskey( COLUMN[i], (*tp)[i], (*kp)[i] ) == 0 ) {
64 * This callback function returns the key of an item by considering
67 static void *tupleitem_itemkey(void *this,void *item) {
68 tupleschema *def = (tupleschema *) this;
69 tuple *tp = (tuple*) item;
72 for ( i = 0 ; i < def->arity; i++ ) {
77 void **parts = calloc( keylen, sizeof( void* ) );
78 for ( i = 0, j = 0; i < def->arity; i++ ) {
80 parts[j++] = COLUMN[i]->itemkey( COLUMN[i], (*tp)[i] );
87 * This callback function handles a key obtained from the itemkey
88 * callback function to reclaim temporary allocation.
90 static void tupleitem_releasekey(void *this,void *key) {
91 tupleschema *def = (tupleschema *) this;
92 tuple *kp = (tuple*) key;
94 for ( i = 0, j = 0; i < def->arity; i++ ) {
96 COLUMN[i]->releasekey( COLUMN[i], (*kp)[j++] );
103 tuple *tuple_create(int arity,...) {
106 tuple *t = (tuple *)malloc( arity * sizeof( void* ) );
107 va_start( ap, arity );
108 for ( i = 0; i < arity; i++ ) {
109 (*t)[i] = va_arg( ap, void* );
115 itemkeyfun tupleschema_callbacks = {
116 .hashcode = tupleitem_hashcode,
117 .haskey = tupleitem_haskey,
118 .itemkey = tupleitem_itemkey,
119 .releasekey = tupleitem_releasekey
122 tupleschema *tupleschema_create(int arity,tuple *columns) {
123 tupleschema *ts = (tupleschema*) malloc( sizeof( tupleschema ) );
124 (*ts) = (tupleschema) {
125 .base = tupleschema_callbacks,
127 .columns = (itemkeyfun**) columns
132 #define COPYA(T,P,N) (T*) memcpy( malloc( N * sizeof(T) ), P, N * sizeof( T ) )
133 #define COPY(T,P) COPYA(T,P,1)
135 // Duplicate a tupleschema with optionally some columns reset.
136 tupleschema *tupleschema_mask(tupleschema *schema,...) {
137 tupleschema *masked = COPY(tupleschema,schema);
138 masked->columns = COPYA( itemkeyfun*, schema->columns, schema->arity );
141 va_start( ap, schema );
143 i = va_arg( ap, int );
144 if ( i < 0 || i >= schema->arity ) {
147 masked->columns[i] = 0;
153 unsigned long tuple_mask(int arity,tuple *t) {
154 unsigned long mask = 0;
155 while ( arity-- > 0 ) {
157 if ( (*t)[ arity ] ) {