added tostring callback
[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 = 5381;
30     for ( ; i < def->arity; i++ ) {
31         if ( COLUMN[i] ) {
32             value <<= 3;
33             if ( (*kp)[i] ) {
34                 value += COLUMN[i]->hashcode( COLUMN[i], (*kp)[i] );
35             } else {
36                 value += 17;
37             }
38         }
39     }
40     return value;
41 }
42
43 /**
44  * This callback function determines whether an item has a
45  * given key or not.
46  */
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;
51     int i = 0;
52     for ( ; i < def->arity; i++ ) {
53         if ( COLUMN[i] && (*kp)[i] ) {
54             if ( COLUMN[i]->haskey( COLUMN[i], (*tp)[i], (*kp)[i] ) == 0 ) {
55                 return 0;
56             }
57         }
58     }
59     return 1;
60 }
61
62
63 /**
64  * This callback function returns the key of an item by considering
65  * the arity and mask.
66  */
67 static void *tupleitem_itemkey(void *this,void *item) {
68     tupleschema *def = (tupleschema *) this;
69     tuple *tp = (tuple*) item;
70     int i, j;
71     int keylen = 0;
72     for ( i = 0 ; i < def->arity; i++ ) {
73         if ( COLUMN[i] ) {
74             keylen++;
75         }
76     }
77     void **parts = calloc( keylen, sizeof( void* ) );
78     for ( i = 0, j = 0; i < def->arity; i++ ) {
79         if ( COLUMN[i] ) {
80             parts[j++] = COLUMN[i]->itemkey( COLUMN[i], (*tp)[i] );
81         }
82     }
83     return (void*) parts;
84 }
85
86 /**
87  * This callback function handles a key obtained from the itemkey
88  * callback function to reclaim temporary allocation.
89  */
90 static void tupleitem_releasekey(void *this,void *key) {
91     tupleschema *def = (tupleschema *) this;
92     tuple *kp = (tuple*) key;
93     int i,j;
94     for ( i = 0, j = 0; i < def->arity; i++ ) {
95         if ( COLUMN[i] ) {
96             COLUMN[i]->releasekey( COLUMN[i], (*kp)[j++] );
97         }
98     }
99     free( key );
100 }
101
102 #define OUT(X) a = X; if ( a > limit ) return 0; buffer += a; limit -= a
103
104 /**
105  * This callback function writes a representation of an item into
106  * a character buffer.
107  */
108 static int tupleitem_tostring(void *this,void *item,char *buffer,int limit) {
109     tupleschema *def = (tupleschema *) this;
110     tuple *t = (tuple*) item;
111     char *x = "<";
112     int a, i;
113     for ( i = 0; i < def->arity; i++ ) {
114         OUT( snprintf( buffer, limit, x ) );
115         x = ",";
116         OUT( def->columns[i]->tostring(
117                  def->columns[i], (*t)[i], buffer, limit ) );
118     }
119     OUT( snprintf( buffer, limit, ">" ) );
120     return a;
121 }
122
123
124 // Allocate
125 tuple *tuple_create(int arity,...) {
126     va_list ap;
127     int i;
128     tuple *t = (tuple *)malloc( arity * sizeof( void* ) );
129     va_start( ap, arity );
130     for ( i = 0; i < arity; i++ ) {
131         (*t)[i] = va_arg( ap, void* );
132     }
133     va_end( ap );
134     return t;
135 }
136
137 itemkeyfun tupleschema_callbacks = {
138     .hashcode = tupleitem_hashcode,
139     .haskey = tupleitem_haskey,
140     .itemkey = tupleitem_itemkey,
141     .releasekey = tupleitem_releasekey,
142     .tostring = tupleitem_tostring
143 };
144
145 tupleschema *tupleschema_create(int arity,tuple *columns) {
146     tupleschema *ts = (tupleschema*) malloc( sizeof( tupleschema ) );
147     (*ts) = (tupleschema) {
148         .base = tupleschema_callbacks,
149         .arity = arity,
150         .columns = (itemkeyfun**) columns
151     };
152     return ts;
153 }
154
155 #define COPYA(T,P,N) (T*) memcpy( malloc( N * sizeof(T) ), P, N * sizeof( T ) )
156 #define COPY(T,P) COPYA(T,P,1)
157
158 // Duplicate a tupleschema with optionally some columns reset.
159 tupleschema *tupleschema_mask(tupleschema *schema,...) {
160     tupleschema *masked = COPY(tupleschema,schema);
161     masked->columns = COPYA( itemkeyfun*, schema->columns, schema->arity );
162     va_list ap;
163     int i;
164     va_start( ap, schema );
165     for ( ;; ) {
166         i = va_arg( ap, int );
167         if ( i < 0 || i >= schema->arity ) {
168             break;
169         }
170         masked->columns[i] = 0;
171     };
172     va_end( ap );
173     return masked;
174 }
175
176 unsigned long tuple_mask(int arity,tuple *t) {
177     unsigned long mask = 0;
178     while ( arity-- > 0 ) {
179         mask <<= 1;
180         if ( (*t)[ arity ] ) {
181             mask++;
182         }
183     }
184     return mask;
185 }