major reorganisation
[rrq/rrqmisc.git] / logic / BindingTable.c
1 #include <BindingTable.h>
2 #include <string.h>
3 #include <stdlib.h>
4
5 BindingTable *BindingTable_create() {
6     BindingTable *this = (BindingTable*) malloc( sizeof( BindingTable ) );
7     (*this) = (HashVector) {
8         .table = (Vector) {
9             .variant = Nibble_index_levels, .size = 16, .entries = 0
10         }, .fill = 0, .holes = 0, .type = &Bindingitem
11     };
12     return this;
13 }
14
15 void BindingTable_release(BindingTable *bt) {
16     if ( bt ) {
17         Vector_resize( &bt->table, 0, Vector_free_any, 0 );
18         free( bt );
19     }
20 }
21
22 void BindingTable_set(BindingTable *bt,char *name,void *value) {
23     Binding *b = (Binding*) HashVector_find( bt, name );
24     if ( b == 0 ) {
25         b = (Binding*) malloc( sizeof( Binding ) );
26         b->name = name;
27         HashVector_add( bt, b );
28     }
29     b->value = value;
30 }
31
32 void *BindingTable_get(BindingTable *bt,char *name) {
33     Binding *b = (Binding*) HashVector_find( bt, name );
34     return b? b->value : 0;
35 }
36
37 void BindingTable_deref(BindingTable *bt,Tuple *t) {
38     unsigned long i;
39     for ( i = 0; i < t->size; i++ ) {
40         if ( t->elements[i] ) {
41             t->elements[i] = BindingTable_get( bt, t->elements[i] );
42         }
43     }
44 }
45
46 #if 0
47 int BindingTable_unify(
48     BindingTable *bt,char *n1,char *n2,int (*eq)(void*,void*)) {
49     void *v1 = BindingTable_get( bt, n1 );
50     void *v2 = BindingTable_get( bt, n2 );
51     if ( v2 && v1 == 0 ) {
52         BindingTable_set( bt, n1, v2 );
53     }
54     if ( v1 && v2 == 0 ) {
55         BindingTable_set( bt, n2, v1 );
56     }
57     return ( v1 && v2 )? ( eq? ( eq( v1, v2 ) == 0 ) : ( v1 == v2 ) ) : 1;
58 }
59 #endif
60
61 Tuple *BindingTable_get_all(BindingTable *bt,Tuple *t) {
62     Tuple *vt = Tuple_clone( t );
63     BindingTable_deref( bt, vt );
64     return vt;
65 }
66
67 void BindingTable_set_all(BindingTable *bt,Tuple *nm,Tuple *vs,int all) {
68     int i;
69     for ( i = 0; i < nm->size; i++ ) {
70         BindingTable_set( bt, nm->elements[i], vs->elements[i] );
71     }
72 }