eff77cedb56e0146c80571aeffc504856d197c20
[rrq/rrqmisc.git] / vector / tupleitem.c
1 #include <stdlib.h>
2 #include <tupleitem.h>
3
4 typedef void *tuple[];
5
6 #define COLUMN def->schema[i]
7
8 unsigned long tupleitem_hashcode(itemkeyfun *this,void *key) {
9     tupleschema *def = (tupleschema *) this;
10     tuple *kp = (tuple*) key;
11     int i = 0;
12     unsigned long value = 0;
13     for ( ; i < def->arity; i++ ) {
14         if ( COLUMN ) {
15             value <<= 3;
16             value += COLUMN->hashcode( COLUMN, (*kp)[i] );
17         }
18     }
19     return value;
20 }
21
22 /**
23  * This callback function determines whether an item has a
24  * given key or not.
25  */
26 int tupleitem_haskey(itemkeyfun *this,void *item,void *key) {
27     tupleschema *def = (tupleschema *) this;
28     tuple *kp = (tuple*) key;
29     tuple *tp = (tuple*) item;
30     int i = 0;
31     int haskey = 1;
32     for ( ; i < def->arity; i++ ) {
33         if ( COLUMN ) {
34             haskey &= COLUMN->haskey( COLUMN, (*tp)[i], (*kp)[i] );
35         }
36     }
37     return haskey;
38 }
39
40
41 /**
42  * This callback function returns the key of an item by considering
43  * the arity and mask.
44  */
45 void *tupleitem_itemkey(itemkeyfun *this,void *item) {
46     tupleschema *def = (tupleschema *) this;
47     tuple *tp = (tuple*) item;
48     int i, j;
49     int keylen = 0;
50     for ( i = 0 ; i < def->arity; i++ ) {
51         if ( COLUMN ) {
52             keylen++;
53         }
54     }
55     void **parts = calloc( keylen, sizeof( void* ) );
56     for ( i = 0, j = 0; i < def->arity; i++ ) {
57         if ( COLUMN ) {
58             parts[j++] = COLUMN->itemkey( COLUMN, (*tp)[i] );
59         }
60     }
61     return (void*) parts;
62 }
63
64 /**
65  * This callback function handles a key obtained from the itemkey
66  * callback function to reclaim temporary allocation.
67  */
68 void tupleitem_releasekey(itemkeyfun *this,void *key) {
69     tupleschema *def = (tupleschema *) this;
70     tuple *kp = (tuple*) key;
71     int i,j;
72     for ( i = 0, j = 0; i < def->arity; i++ ) {
73         if ( COLUMN ) {
74             COLUMN->releasekey( COLUMN, (*kp)[j++] );
75         }
76     }
77     free( key );
78 }
79