c63e3ec1072bb3544af68f74fa39e46a7840b412
[rrq/rrqmisc.git] / vector / tupleitem.c
1 #include <stdarg.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <tupleitem.h>
5
6 #define COLUMN def->columns
7
8 /**
9  * This callback function returns the hashcode of a key.
10  *
11  * \param this is a pointer to the itemkeyfun record from where this
12  * callback got invoked
13  *
14  * \param key is the key to produce a hascode for
15  *
16  * \returns the hashcode which is a vector_index (i.e. unsigned long)
17  *
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
23  * the hashcode index.
24  */
25 static unsigned long tupleitem_hashcode(void *this,void *key) {
26     tupleschema *def = (tupleschema *) this;
27     tuple *kp = (tuple*) key;
28     int i = 0;
29     unsigned long value = 0;
30     for ( ; i < def->arity; i++ ) {
31         if ( COLUMN[i] ) {
32             value <<= 3;
33             value += COLUMN[i]->hashcode( COLUMN[i], (*kp)[i] );
34         }
35     }
36     return value;
37 }
38
39 /**
40  * This callback function determines whether an item has a
41  * given key or not.
42  */
43 static int tupleitem_haskey(void *this,void *item,void *key) {
44     tupleschema *def = (tupleschema *) this;
45     tuple *kp = (tuple*) key;
46     tuple *tp = (tuple*) item;
47     int i = 0;
48     for ( ; i < def->arity; i++ ) {
49         if ( COLUMN[i] == 0 ) {
50             if ( (*kp)[i] &&  (*tp)[i] != (*kp)[i] ) {
51                 return 0;
52             }
53             continue;
54         }
55         if ( (*kp)[i] &&
56              COLUMN[i]->haskey( COLUMN[i], (*tp)[i], (*kp)[i] ) == 0 ) {
57             return 0;
58         }
59     }
60     return 1;
61 }
62
63
64 /**
65  * This callback function returns the key of an item by considering
66  * the arity and mask.
67  */
68 static void *tupleitem_itemkey(void *this,void *item) {
69     tupleschema *def = (tupleschema *) this;
70     tuple *tp = (tuple*) item;
71     int i, j;
72     int keylen = 0;
73     for ( i = 0 ; i < def->arity; i++ ) {
74         if ( COLUMN[i] ) {
75             keylen++;
76         }
77     }
78     void **parts = calloc( keylen, sizeof( void* ) );
79     for ( i = 0, j = 0; i < def->arity; i++ ) {
80         if ( COLUMN[i] ) {
81             parts[j++] = COLUMN[i]->itemkey( COLUMN[i], (*tp)[i] );
82         }
83     }
84     return (void*) parts;
85 }
86
87 /**
88  * This callback function handles a key obtained from the itemkey
89  * callback function to reclaim temporary allocation.
90  */
91 static void tupleitem_releasekey(void *this,void *key) {
92     tupleschema *def = (tupleschema *) this;
93     tuple *kp = (tuple*) key;
94     int i,j;
95     for ( i = 0, j = 0; i < def->arity; i++ ) {
96         if ( COLUMN[i] ) {
97             COLUMN[i]->releasekey( COLUMN[i], (*kp)[j++] );
98         }
99     }
100     free( key );
101 }
102
103 // Allocate
104 tuple *tuple_create(int arity,...) {
105     va_list ap;
106     int i;
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* );
111     }
112     va_end( ap );
113     return t;
114 }
115
116 tupleschema *tupleschema_create(int arity,tuple *columns) {
117     tupleschema *ts = (tupleschema*) malloc( sizeof( tupleschema ) );
118     (*ts) = (tupleschema) {
119         .base = {
120             .hashcode = tupleitem_hashcode,
121             .haskey = tupleitem_haskey,
122             .itemkey = tupleitem_itemkey,
123             .releasekey = tupleitem_releasekey
124         },
125         .arity = arity,
126         .columns = (itemkeyfun**) columns
127     };
128     return ts;
129 }
130
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)
133
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 );
138     va_list ap;
139     int i;
140     va_start( ap, schema );
141     for ( ;; ) {
142         i = va_arg( ap, int );
143         if ( i < 0 || i >= schema->arity ) {
144             break;
145         }
146         masked->columns[i] = 0;
147     };
148     va_end( ap );
149     return masked;
150 }
151
152 unsigned long tuple_mask(int arity,tuple *t) {
153     unsigned long mask = 0;
154     while ( arity-- > 0 ) {
155         mask <<= 1;
156         if ( (*t)[ arity ] ) {
157             mask++;
158         }
159     }
160     return mask;
161 }
162