fix vararg usae; use static callbacks
[rrq/rrqmisc.git] / vector / tupleitem.c
index eff77cedb56e0146c80571aeffc504856d197c20..14c3b3ba89795aa1f928efd003353e864d7d8ce6 100644 (file)
@@ -1,19 +1,36 @@
+#include <stdarg.h>
 #include <stdlib.h>
+#include <string.h>
 #include <tupleitem.h>
 
-typedef void *tuple[];
+#define COLUMN def->columns
 
-#define COLUMN def->schema[i]
-
-unsigned long tupleitem_hashcode(itemkeyfun *this,void *key) {
+/**
+ * This callback function returns the hashcode of a key.
+ *
+ * \param this is a pointer to the itemkeyfun record from where this
+ * callback got invoked
+ *
+ * \param key is the key to produce a hascode for
+ *
+ * \returns the hashcode which is a vector_index (i.e. unsigned long)
+ *
+ * The hashcode is used for indexing into the backing vector for
+ * finding the an item via its key. The same key must map consistently
+ * to the same hashcode while the hashtable contains an item with that
+ * key. Different keys map map to the same hashcode, in which case the
+ * vector placement is made at the first empty or hole slot following
+ * the hashcode index.
+ */
+static unsigned long tupleitem_hashcode(itemkeyfun *this,void *key) {
     tupleschema *def = (tupleschema *) this;
     tuple *kp = (tuple*) key;
     int i = 0;
     unsigned long value = 0;
     for ( ; i < def->arity; i++ ) {
-       if ( COLUMN ) {
+       if ( COLUMN[i] ) {
            value <<= 3;
-           value += COLUMN->hashcode( COLUMN, (*kp)[i] );
+           value += COLUMN[i]->hashcode( COLUMN[i], (*kp)[i] );
        }
     }
     return value;
@@ -23,15 +40,15 @@ unsigned long tupleitem_hashcode(itemkeyfun *this,void *key) {
  * This callback function determines whether an item has a
  * given key or not.
  */
-int tupleitem_haskey(itemkeyfun *this,void *item,void *key) {
+static int tupleitem_haskey(itemkeyfun *this,void *item,void *key) {
     tupleschema *def = (tupleschema *) this;
     tuple *kp = (tuple*) key;
     tuple *tp = (tuple*) item;
     int i = 0;
     int haskey = 1;
     for ( ; i < def->arity; i++ ) {
-       if ( COLUMN ) {
-           haskey &= COLUMN->haskey( COLUMN, (*tp)[i], (*kp)[i] );
+       if ( COLUMN[i] ) {
+           haskey &= COLUMN[i]->haskey( COLUMN[i], (*tp)[i], (*kp)[i] );
        }
     }
     return haskey;
@@ -42,20 +59,20 @@ int tupleitem_haskey(itemkeyfun *this,void *item,void *key) {
  * This callback function returns the key of an item by considering
  * the arity and mask.
  */
-void *tupleitem_itemkey(itemkeyfun *this,void *item) {
+static void *tupleitem_itemkey(itemkeyfun *this,void *item) {
     tupleschema *def = (tupleschema *) this;
     tuple *tp = (tuple*) item;
     int i, j;
     int keylen = 0;
     for ( i = 0 ; i < def->arity; i++ ) {
-       if ( COLUMN ) {
+       if ( COLUMN[i] ) {
            keylen++;
        }
     }
     void **parts = calloc( keylen, sizeof( void* ) );
     for ( i = 0, j = 0; i < def->arity; i++ ) {
-       if ( COLUMN ) {
-           parts[j++] = COLUMN->itemkey( COLUMN, (*tp)[i] );
+       if ( COLUMN[i] ) {
+           parts[j++] = COLUMN[i]->itemkey( COLUMN[i], (*tp)[i] );
        }
     }
     return (void*) parts;
@@ -65,15 +82,74 @@ void *tupleitem_itemkey(itemkeyfun *this,void *item) {
  * This callback function handles a key obtained from the itemkey
  * callback function to reclaim temporary allocation.
  */
-void tupleitem_releasekey(itemkeyfun *this,void *key) {
+static void tupleitem_releasekey(itemkeyfun *this,void *key) {
     tupleschema *def = (tupleschema *) this;
     tuple *kp = (tuple*) key;
     int i,j;
     for ( i = 0, j = 0; i < def->arity; i++ ) {
-       if ( COLUMN ) {
-           COLUMN->releasekey( COLUMN, (*kp)[j++] );
+       if ( COLUMN[i] ) {
+           COLUMN[i]->releasekey( COLUMN[i], (*kp)[j++] );
        }
     }
     free( key );
 }
 
+// Allocate
+tuple *tuple_create(int arity,...) {
+    va_list ap;
+    int i;
+    tuple *t = (tuple *)malloc( arity * sizeof( void* ) );
+    va_start( ap, arity );
+    for ( i = 0; i < arity; i++ ) {
+       (*t)[i] = va_arg( ap, void* );
+    }
+    va_end( ap );
+    return t;
+}
+
+tupleschema *tupleschema_create(int arity,tuple *columns) {
+    tupleschema *ts = (tupleschema*) malloc( sizeof( tupleschema ) );
+    (*ts) = (tupleschema) {
+       .functions = {
+           .hashcode = tupleitem_hashcode,
+           .haskey = tupleitem_haskey,
+           .itemkey = tupleitem_itemkey,
+           .releasekey = tupleitem_releasekey
+       },
+       .arity = arity,
+       .columns = (itemkeyfun**) columns
+    };
+    return ts;
+}
+
+#define COPYA(T,P,N) (T*) memcpy( malloc( N * sizeof(T) ), P, N * sizeof( T ) )
+#define COPY(T,P) COPYA(T,P,1)
+
+// Duplicate a tupleschema with optionally some columns reset.
+tupleschema *tupleschema_mask(tupleschema *schema,...) {
+    tupleschema *masked = COPY(tupleschema,schema);
+    masked->columns = COPYA( itemkeyfun*, schema->columns, schema->arity );
+    va_list ap;
+    int i;
+    va_start( ap, schema );
+    for ( ;; ) {
+       i = va_arg( ap, int );
+       if ( i < 0 || i >= schema->arity ) {
+           break;
+       }
+       masked->columns[i] = 0;
+    };
+    va_end( ap );
+    return masked;
+}
+
+unsigned long tuple_mask(int arity,tuple *t) {
+    unsigned long mask = 0;
+    while ( arity-- > 0 ) {
+       mask <<= 1;
+       if ( (*t)[ arity ] ) {
+           mask++;
+       }
+    }
+    return mask;
+}