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] );
43 * This callback function determines whether an item has a
46 static int tupleitem_haskey(void *this,void *item,void *key) {
47 tupleschema *def = (tupleschema *) this;
48 tuple *kp = (tuple*) key;
49 tuple *tp = (tuple*) item;
51 for ( ; i < def->arity; i++ ) {
52 if ( COLUMN[i] && (*kp)[i] ) {
53 if ( COLUMN[i]->haskey( COLUMN[i], (*tp)[i], (*kp)[i] ) == 0 ) {
63 * This callback function returns the key of an item by considering
66 static void *tupleitem_itemkey(void *this,void *item) {
67 tupleschema *def = (tupleschema *) this;
68 tuple *tp = (tuple*) item;
70 int keylen = def->arity;
71 void **parts = calloc( keylen, sizeof( void* ) );
72 for ( i = 0; i < def->arity; i++ ) {
74 parts[i] = COLUMN[i]->itemkey( COLUMN[i], (*tp)[i] );
81 * This callback function handles a key obtained from the itemkey
82 * callback function to reclaim temporary allocation.
84 static void tupleitem_releasekey(void *this,void *key) {
85 tupleschema *def = (tupleschema *) this;
86 tuple *kp = (tuple*) key;
88 for ( i = 0; i < def->arity; i++ ) {
90 COLUMN[i]->releasekey( COLUMN[i], (*kp)[i] );
96 #define OUT(X) a = X; if ( a > limit ) return 0; buffer += a; limit -= a
99 * This callback function writes a representation of an item into
100 * a character buffer.
102 static int tupleitem_tostring(void *this,void *item,char *buffer,int limit) {
103 tupleschema *def = (tupleschema *) this;
104 tuple *t = (tuple*) item;
107 for ( i = 0; i < def->arity; i++ ) {
108 OUT( snprintf( buffer, limit, x ) );
110 OUT( def->columns[i]->tostring(
111 def->columns[i], (*t)[i], buffer, limit ) );
113 OUT( snprintf( buffer, limit, ">" ) );
119 tuple *tuple_create(int arity,...) {
122 tuple *t = (tuple *)malloc( arity * sizeof( void* ) );
123 va_start( ap, arity );
124 for ( i = 0; i < arity; i++ ) {
125 (*t)[i] = va_arg( ap, void* );
131 itemkeyfun tupleschema_callbacks = {
132 .hashcode = tupleitem_hashcode,
133 .haskey = tupleitem_haskey,
134 .itemkey = tupleitem_itemkey,
135 .releasekey = tupleitem_releasekey,
136 .tostring = tupleitem_tostring
139 tupleschema *tupleschema_create(int arity,tuple *columns) {
140 tupleschema *ts = (tupleschema*) malloc( sizeof( tupleschema ) );
141 (*ts) = (tupleschema) {
142 .base = tupleschema_callbacks,
144 .columns = (itemkeyfun**) columns
149 #define COPYA(T,P,N) (T*) memcpy( malloc( N * sizeof(T) ), P, N * sizeof( T ) )
150 #define COPY(T,P) COPYA(T,P,1)
152 // Duplicate a tupleschema with optionally some columns reset.
153 tupleschema *tupleschema_mask(tupleschema *schema,...) {
154 tupleschema *masked = COPY(tupleschema,schema);
155 masked->columns = COPYA( itemkeyfun*, schema->columns, schema->arity );
158 va_start( ap, schema );
160 i = va_arg( ap, int );
161 if ( i < 0 || i >= schema->arity ) {
164 masked->columns[i] = 0;
170 unsigned long tuple_mask(int arity,tuple *t) {
171 unsigned long mask = 0;
172 while ( arity-- > 0 ) {
174 if ( (*t)[ arity ] ) {