refactoring
[rrq/rrqmisc.git] / vector / TupleSchema.c
index 378ac9aa762d9f28a092c432407c0e5a8b2c3739..5a92b64db32d52239fcb83304c0bfcd40e2a2c9f 100644 (file)
@@ -3,8 +3,6 @@
 #include <string.h>
 #include <TupleSchema.h>
 
-#define COLUMN def->columns
-
 /**
  * This callback function returns the hashcode of a key.
  *
  */
 static unsigned long TupleSchema_hashcode(void *this,void *key) {
     TupleSchema *def = (TupleSchema *) this;
-    tuple *kp = (tuple*) key;
+    ItemKeyFun **columns = (ItemKeyFun**) def->columns->elements;
+    Tuple *kp = (Tuple*) key;
     int i = 0;
     unsigned long value = 5381;
-    for ( ; i < def->arity; i++ ) {
-       if ( COLUMN[i] ) {
+    for ( ; i < def->columns->size; i++ ) {
+       if ( columns[i] ) {
            value <<= 3;
-           if ( (*kp)[i] ) {
-               value += COLUMN[i]->hashcode( COLUMN[i], (*kp)[i] );
+           if ( kp->elements[i] ) {
+               value += columns[i]->hashcode( columns[i], kp->elements[i] );
            }
        }
        value += 17;
@@ -45,12 +44,14 @@ static unsigned long TupleSchema_hashcode(void *this,void *key) {
  */
 static int TupleSchema_haskey(void *this,void *item,void *key) {
     TupleSchema *def = (TupleSchema *) this;
-    tuple *kp = (tuple*) key;
-    tuple *tp = (tuple*) item;
+    ItemKeyFun **columns = (ItemKeyFun**) def->columns->elements;
+    Tuple *kp = (Tuple*) key;
+    Tuple *tp = (Tuple*) item;
     int i = 0;
-    for ( ; i < def->arity; i++ ) {
-       if ( COLUMN[i] && (*kp)[i] ) {
-           if ( COLUMN[i]->haskey( COLUMN[i], (*tp)[i], (*kp)[i] ) == 0 ) {
+    for ( ; i < def->columns->size; i++ ) {
+       if ( columns[i] && kp->elements[i] ) {
+           if ( columns[i]->haskey(
+                    columns[i], tp->elements[i], kp->elements[i] ) == 0 ) {
                return 0;
            }
        }
@@ -63,31 +64,35 @@ static int TupleSchema_haskey(void *this,void *item,void *key) {
  * This callback function returns the key of an item by considering
  * the arity and mask.
  */
-static void *tupleitem_itemkey(void *this,void *item) {
+static void *TupleSchema_itemkey(void *this,void *item) {
     TupleSchema *def = (TupleSchema *) this;
-    tuple *tp = (tuple*) item;
+    ItemKeyFun **columns = (ItemKeyFun**) def->columns->elements;
+    Tuple *tp = (Tuple*) item;
+    Tuple *key = Tuple_clone( tp );
     int i;
-    int keylen = def->arity;
-    void **parts = calloc( keylen, sizeof( void* ) );
-    for ( i = 0; i < def->arity; i++ ) {
-       if ( COLUMN[i] ) {
-           parts[i] = COLUMN[i]->itemkey( COLUMN[i], (*tp)[i] );
+    for ( i = 0; i < def->columns->size; i++ ) {
+       if ( columns[i] ) {
+           key->elements[i] = columns[i]->itemkey(
+               columns[i], tp->elements[i] );
+       } else {
+           key->elements[i] = 0;
        }
     }
-    return (void*) parts;
+    return (void*) key;
 }
 
 /**
  * This callback function handles a key obtained from the itemkey
  * callback function to reclaim temporary allocation.
  */
-static void tupleitem_releasekey(void *this,void *key) {
+static void TupleSchema_releasekey(void *this,void *key) {
     TupleSchema *def = (TupleSchema *) this;
-    tuple *kp = (tuple*) key;
+    ItemKeyFun **columns = (ItemKeyFun**) def->columns->elements;
+    Tuple *kp = (Tuple*) key;
     int i;
-    for ( i = 0; i < def->arity; i++ ) {
-       if ( COLUMN[i] ) {
-           COLUMN[i]->releasekey( COLUMN[i], (*kp)[i] );
+    for ( i = 0; i < def->columns->size; i++ ) {
+       if ( columns[i] ) {
+           columns[i]->releasekey( columns[i], kp->elements[i] );
        }
     }
     free( key );
@@ -99,55 +104,35 @@ static void tupleitem_releasekey(void *this,void *key) {
  * This callback function writes a representation of an item into
  * a character buffer.
  */
-static int tupleitem_tostring(void *this,void *item,char *buffer,int limit) {
+static int TupleSchema_tostring(void *this,void *item,char *buffer,int limit) {
     TupleSchema *def = (TupleSchema *) this;
-    tuple *t = (tuple*) item;
+    ItemKeyFun **columns = (ItemKeyFun**) def->columns->elements;
+    Tuple *t = (Tuple*) item;
     char *x = "<";
     int a, i;
-    for ( i = 0; i < def->arity; i++ ) {
+    for ( i = 0; i < def->columns->size; i++ ) {
        OUT( snprintf( buffer, limit, x ) );
        x = ",";
-       OUT( def->columns[i]->tostring(
-                def->columns[i], (*t)[i], buffer, limit ) );
+       OUT( columns[i]->tostring(
+                columns[i], t->elements[i], buffer, limit ) );
     }
     OUT( snprintf( buffer, limit, ">" ) );
     return a;
 }
 
-
-// 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;
-}
-
-tuple *tuple_clone(int arity,tuple *t) {
-    tuple *ct = (tuple *)malloc( arity * sizeof( void* ) );
-    memcpy( ct, t, arity * sizeof( void* ) );
-    return ct;
-}
-
 ItemKeyFun TupleSchema_callbacks = {
     .hashcode = TupleSchema_hashcode,
     .haskey = TupleSchema_haskey,
-    .itemkey = tupleitem_itemkey,
-    .releasekey = tupleitem_releasekey,
-    .tostring = tupleitem_tostring
+    .itemkey = TupleSchema_itemkey,
+    .releasekey = TupleSchema_releasekey,
+    .tostring = TupleSchema_tostring
 };
 
-TupleSchema *TupleSchema_create(int arity,tuple *columns) {
+TupleSchema *TupleSchema_create(Tuple *columns) {
     TupleSchema *ts = (TupleSchema*) malloc( sizeof( TupleSchema ) );
     (*ts) = (TupleSchema) {
        .base = TupleSchema_callbacks,
-       .arity = arity,
-       .columns = (ItemKeyFun**) columns
+       .columns = columns
     };
     return ts;
 }
@@ -158,28 +143,17 @@ TupleSchema *TupleSchema_create(int arity,tuple *columns) {
 // 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 );
+    masked->columns = Tuple_clone( schema->columns );
     va_list ap;
     int i;
     va_start( ap, schema );
     for ( ;; ) {
        i = va_arg( ap, int );
-       if ( i < 0 || i >= schema->arity ) {
+       if ( i < 0 || i >= schema->columns->size ) {
            break;
        }
-       masked->columns[i] = 0;
+       masked->columns->elements[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;
-}