#include "tmtopicset.h" #include "tmmem.h" #include "tmtrace.h" #include "tmassert.h" struct TMTopicSet { TMPool pool; int count; int size; TMTopic *topics; }; TMTopicSet tm_topicset_new(TMPool pool,int size_hint) { /* ONCE HINT IS IMPLEMENTED, assert(hint) to break 0 values used now */ TMTopicSet s; TM_NEW(pool,s); s->pool = pool; s->count = 0; s->size = 10; s->topics = (TMTopic*)TM_ALLOC(pool,10 * sizeof(TMTopic)); assert(s->topics); return s; } void tm_topicset_delete(TMTopicSet *pself) { TMPool pool; assert(pself && *pself); pool = (*pself)->pool; TM_FREE(pool,(*pself)->topics); TM_FREE(pool,*pself); } void tm_topicset_clear(TMTopicSet self) { assert(self); self->count = 0; } void tm_topicset_print(TMTopicSet self, FILE* f) { int i; if(self->count == 0) { fprintf(f,"[]"); return; } fprintf(f,"["); for(i = 0; i < self->count-1; i++) fprintf(f,"%d,",self->topics[i] ); fprintf(f,"%d]",self->topics[self->count-1] ); /* fprintf(f,"]"); */ } int tm_topicset_empty(TMTopicSet self) { assert(self); return(self->count == 0); } int tm_topicset_size(TMTopicSet self) { assert(self); TMTRACE(TM_X_TRACE,"tm_topicset_size(): size=%d\n" _ self->count); return(self->count); } void tm_topicset_add(TMTopicSet self, TMTopic topic) { int i; assert(self); i=0; /* fprintf(stderr,"NODESET: adding topic %d\n",topic); */ for(i = 0 ; i < self->count; i++) if(self->topics[i] == topic) return; if(self->count >= self->size) { self->size *= 2; TM_RESIZE(self->pool,self->topics,self->size * sizeof(TMTopic) ); } /* TBD: sort !!! */ self->topics[self->count] = topic; self->count++; } int tm_topicset_remove(TMTopicSet self, TMTopic topic) { int i; assert(self); i=0; for(i = 0 ; i < self->count; i++) { if(self->topics[i] == topic) { int j; for(j=i;jcount-1;j++) { self->topics[j] = self->topics[j+1]; } self->count--; return 1; } } return (0); } TMTopic tm_topicset_get(TMTopicSet self, int i) { assert(self); assert(i >= 0 && i < self->count); return self->topics[i]; } int tm_topicset_contains(TMTopicSet self, TMTopic topic) { int i; assert(self); assert(topic); TMTRACE(TM_X_TRACE,"enter\n"); for(i = 0 ; i < self->count; i++) if(self->topics[i] == topic) { TMTRACE(TM_X_TRACE,"exit does contain\n"); return 1; } TMTRACE(TM_X_TRACE,"exit not contains\n"); return 0; } int tm_topicset_disjoint(TMTopicSet self, TMTopicSet other) { int i; assert(self); assert(other); TMTRACE(TM_X_TRACE,"enter\n"); for(i = 0 ; i < other->count; i++) { if(tm_topicset_contains(self,other->topics[i]) ) { TMTRACE(TM_X_TRACE,"exit (not disjoint)\n"); return 0; } } TMTRACE(TM_X_TRACE,"exit (are disjoint)\n"); return 1; } int tm_topicset_disjoint_list(TMTopicSet self, TMList list) { assert(self); TMTRACE(TM_X_TRACE,"enter\n"); for( ; list; list=list->next) { if(tm_topicset_contains(self,(TMTopic)list->content) ) { TMTRACE(TM_X_TRACE,"exit (not disjoint)\n"); return 0; } } TMTRACE(TM_X_TRACE,"exit (are disjoint)\n"); return 1; } void tm_topicset_join(TMTopicSet self, TMTopicSet other) { int i; assert(self); assert(other); for(i = 0 ; i < other->count; i++) tm_topicset_add(self,other->topics[i]); } int tm_topicset_equal(TMTopicSet self, TMTopicSet other) { int i; assert(self); assert(other); if(self->count != other->count) return 0; for(i = 0 ; i < other->count; i++) { if(! tm_topicset_contains(self,other->topics[i]) ) return 0; } return 1; } TMTopicSet tm_topicset_copy(TMPool pool,TMTopicSet self) { TMTopicSet cpy; TM_NEW(pool,cpy); cpy->count = self->count; cpy->size = self->size; cpy->topics = (TMTopic*)TM_ALLOC(pool,self->size * sizeof(TMTopic)); assert(cpy->topics); memcpy(cpy->topics,self->topics, self->count * sizeof(TMTopic)); assert( tm_topicset_equal(self,cpy)); assert( tm_topicset_equal(cpy,self)); return cpy; } #ifdef TEST int main(int argc, char** args) { GW gw; TMTopicSet s; printf("\nUnit tests for %s:\n",__FILE__); gw = GW_init(__FILE__,"1.0"); GWTEST(gw != NULL); s = tm_topicset_new(gw); tm_topicset_push(s,(void*)1); GWTEST(tm_topicset_top(s) == (void*)1); tm_topicset_push(s,(void*)2); GWTEST(tm_topicset_top(s) == (void*)2); GWTEST(tm_topicset_pop(s) == (void*)2); GWTEST(tm_topicset_top(s) == (void*)1); tm_topicset_pop(s); GWTEST(tm_topicset_size(s) == 0); tm_topicset_delete(&s); GWTEST(s == NULL); return 0; } #endif