From: Ralph Ronnquist Date: Sun, 10 Jul 2022 14:40:16 +0000 (+1000) Subject: use hashvector for knockouts X-Git-Url: https://git.rrq.au/?a=commitdiff_plain;h=76dbbc2320962d0808e6cdc715a3ffc7b5e41a22;p=rrq%2Frrqmisc.git use hashvector for knockouts --- diff --git a/vector/relation.c b/vector/relation.c index a6dd312..945c991 100644 --- a/vector/relation.c +++ b/vector/relation.c @@ -51,7 +51,7 @@ int relation_add_constraint(relation *r,...) { // Iteration context for adding or querying a relation typedef struct { relation *rel; - vector knockouts; + hashvector knockouts; void *item; } knockout; @@ -64,21 +64,15 @@ static int knockout_check(vector_index index,void *item,void *data) { for ( ; i < ((hashvector*) item)->table.size; i++ ) { void *old = hashvector_next( (hashvector*) item, &i, kod->item ); if ( old ) { - vector_append( &kod->knockouts, old ); + hashvector_add( &kod->knockouts, old ); } } return 0; } // delete the (tuple*)item from the (hashvector*)data -static int knockout_delete_item(vector_index index,void *item,void *data) { - hashvector_delete( (hashvector*)data, item ); - return 0; -} - -// delete the tuples of (vector*)data from the (hashvector*)item static int knockout_delete(vector_index index,void *item,void *data) { - vector_iterate( (vector*)data, 0, knockout_delete_item, item ); + hashvector_delete( (hashvector*) item, data ); return 0; } @@ -95,23 +89,31 @@ static int knockout_clear(knockout *this,relation *r,tuple *item,int add) { (*this) = (knockout) { .rel = r, .knockouts = { - .variant = bitpair_index_levels, - .size = 0, .entries = 0 + .table = { + .variant = nibble_index_levels, .size = 16, .entries = 0 + }, + .fill = 0, .holes = 0, .type = r->content.type, }, .item = item }; knockout_check( 0, &r->content, this ); - if ( add && this->knockouts.size > 0 ) { - return 0; + if ( add ) { + if ( this->knockouts.fill > 0 ) { + return 0; + } + // Find all constraint knockouts for addition + vector_iterate( &r->constraints, 0, knockout_check, this ); } - // Find all constraint knockouts - vector_iterate( &r->constraints, 0, knockout_check, this ); - if ( this->knockouts.size > 0 ) { + if ( this->knockouts.fill > 0 ) { // Delete them from all tables - vector_iterate( - &this->knockouts, 0, knockout_delete_item, &r->content ); - vector_iterate( - &r->constraints, 0, knockout_delete, &this->knockouts ); + vector_index i; + for ( i = 0; i < this->knockouts.table.size; i++ ) { + void *t = hashvector_next( &this->knockouts, &i, 0 ); + if ( t ) { + hashvector_delete( &r->content, t ); + vector_iterate( &r->constraints, 0, knockout_delete, t ); + } + } } return 1; } @@ -124,9 +126,7 @@ vector *relation_add(relation *r,tuple *item) { // Add the new tuple hashvector_add( &r->content, item ); vector_iterate( &r->constraints, 0, knockout_add, item ); - if ( data.knockouts.size > 0 ) { - return vector_clone( single_index_level, &data.knockouts ); - } + return hashvector_contents( &data.knockouts, single_index_level, 0 ); } return 0; } @@ -134,10 +134,7 @@ vector *relation_add(relation *r,tuple *item) { vector *relation_delete(relation *r,tuple *item) { knockout data; (void) knockout_clear( &data, r, item, 0 ); - if ( data.knockouts.size > 0 ) { - return vector_clone( single_index_level, &data.knockouts ); - } - return 0; + return hashvector_contents( &data.knockouts, single_index_level, 0 ); } void *relation_next(relation *r,vector_index *index,tuple *query) {