refactoring
[rrq/rrqmisc.git] / vector / BindingTable.c
index e3ef2147e91bf3ea286546e74a5e104bdf9e0af0..2728516ec48b8564958da8fc0254c8fe5293f0e0 100644 (file)
 #include <string.h>
 #include <stdlib.h>
 
-/**
- * A Binding is an association between a name (char*) and a value
- * (void*).
- */
-typedef struct {
-    char *name;
-    void *value;
-} Binding;
-
-/**
- * This callback function should return the hashcode of a key. 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 binding_hashcode(void *this,void *key) {
-    char *name = (char *) key;
-    unsigned long n = strlen( name );
-    return HashVector_hashcode( (unsigned char *) name, n );
-}
-
-/**
- * This callback function should determine whether an item has a
- * given key or not.
- */
-static int binding_haskey(void *this,void *item,void *key) {
-    Binding *b = (Binding*) item;
-    char *name = (char *) key;
-    return strcmp( name, (char*) b->name ) == 0;
-}
-
-/**
- * This callback function should return a (possibly temporary) key
- * of an item. It can be anything (i.e., with or without internal
- * structure) as it is treated as an identifier that other
- * callbacks recognize. It is merely understood as a value in an
- * identity domain.
- */
-static void *binding_itemkey(void *this,void *item) {
-    Binding *b = (Binding*) item;
-    return b->name;
-}
-    
-
-/**
- * This callback function should handle a key obtained from the
- * itemkey callback function, e.g., reclaim temporary allocation.
- */
-static void binding_releasekey(void *this,void *key) {
-}
-
-/**
- * This callback function writes a representation of an item into
- * a character buffer.
- */
-static int binding_tostring(void *this,void *item,char *buffer,int limit) {
-    Binding *b = (Binding*) item;
-    return snprintf( buffer, limit, "{%s,%p}", b->name, b->value );
-}
-
-/**
- * This is the "item type" for Binding items.
- */
-ItemKeyFun bindingitem = {
-    .hashcode = binding_hashcode,
-    .haskey = binding_haskey,
-    .itemkey = binding_itemkey,
-    .releasekey = binding_releasekey,
-    .tostring = binding_tostring
-};
-
-BindingTable *BindingTable_create(BindingTable *next) {
+BindingTable *BindingTable_create() {
     BindingTable *this = (BindingTable*) malloc( sizeof( BindingTable ) );
-    this->table = (HashVector) {
+    (*this) = (HashVector) {
        .table = (Vector) {
            .variant = Nibble_index_levels, .size = 16, .entries = 0
-       }, .fill = 0, .holes = 0, .type = &bindingitem
+       }, .fill = 0, .holes = 0, .type = &Bindingitem
     };
-    this->next = next;
     return this;
 }
 
-BindingTable *BindingTable_release(BindingTable *bt) {
+void BindingTable_release(BindingTable *bt) {
     if ( bt ) {
-       BindingTable *next = bt->next;
-       Vector_resize( &bt->table.table, 0, Vector_free_any, 0 );
+       Vector_resize( &bt->table, 0, Vector_free_any, 0 );
        free( bt );
-       return next;
     }
-    return 0;
 }
 
 void BindingTable_set(BindingTable *bt,char *name,void *value) {
-    Binding *b = (Binding*) HashVector_find( &bt->table, name );
+    Binding *b = (Binding*) HashVector_find( bt, name );
     if ( b == 0 ) {
        b = (Binding*) malloc( sizeof( Binding ) );
        b->name = name;
-       HashVector_add( &bt->table, b );
+       HashVector_add( bt, b );
     }
     b->value = value;
 }
 
 void *BindingTable_get(BindingTable *bt,char *name) {
-    for ( ; bt; bt = bt->next ) {
-       Binding *b = (Binding*) HashVector_find( &bt->table, name );
-       if ( b ) {
-           return b->value;
-       }
-    }
-    return 0;
+    Binding *b = (Binding*) HashVector_find( bt, name );
+    return b? b->value : 0;
 }
 
-void BindingTable_deref(BindingTable *bt,int arity,Tuple *t) {
-    int i;
-    for ( i = 0; i < arity; i++ ) {
+void BindingTable_deref(BindingTable *bt,Tuple *t) {
+    unsigned long i;
+    for ( i = 0; i < t->size; i++ ) {
        if ( t->elements[i] ) {
            t->elements[i] = BindingTable_get( bt, t->elements[i] );
        }
     }
 }
 
+#if 0
 int BindingTable_unify(
     BindingTable *bt,char *n1,char *n2,int (*eq)(void*,void*)) {
     void *v1 = BindingTable_get( bt, n1 );
@@ -138,18 +56,17 @@ int BindingTable_unify(
     }
     return ( v1 && v2 )? ( eq? ( eq( v1, v2 ) == 0 ) : ( v1 == v2 ) ) : 1;
 }
+#endif
 
-#define COPYA(T,P,N) (T*) memcpy( malloc( N * sizeof(T) ), P, N * sizeof( T ) )
-
-Tuple *BindingTable_tuple_get(BindingTable *bt,int arity,Tuple *t) {
-    Tuple *vt = Tuple_clone( arity, t );
-    BindingTable_deref( bt, arity, vt );
+Tuple *BindingTable_get_all(BindingTable *bt,Tuple *t) {
+    Tuple *vt = Tuple_clone( t );
+    BindingTable_deref( bt, vt );
     return vt;
 }
 
-void BindingTable_tuple_set(BindingTable *bt,int arity,Tuple *nm,Tuple *vs) {
+void BindingTable_set_all(BindingTable *bt,Tuple *nm,Tuple *vs,int all) {
     int i;
-    for ( i = 0; i < arity; i++ ) {
+    for ( i = 0; i < nm->size; i++ ) {
        BindingTable_set( bt, nm->elements[i], vs->elements[i] );
     }
 }