#include "ptrset.h" #include "ktransition.h" #include "klocation.h" #include "lstring.h" #include template class PtrSet; template class PtrSet; template class PtrSetEl; template class PtrSetEl; template PtrSetEl::PtrSetEl(T *newitem, const PtrSetEl *f /* = NULL */) { item = newitem; child[0] = child[1] = NULL; father = f; } template PtrSetEl::~PtrSetEl() { if ( NULL != child[0] ) delete child[0]; if ( NULL != child[1] ) delete child[1]; if ( NULL != item ) delete item; } template PtrSetEl::PtrSetEl(const PtrSetEl ©, const PtrSetEl *f /* = NULL */) { item = new T(*copy.item); if ( NULL == copy.child[0] ) child[0] = NULL; else child[0] = new PtrSetEl(*copy.child[0], this); if ( NULL == copy.child[1] ) child[1] = NULL; else child[1] = new PtrSetEl(*copy.child[1], this); father = f; } template T *PtrSetEl::search(T *key, mode notfound) { if ( *key == *item ) return item; int i = *item < *key; T *result = NULL; if ( NULL != child[i] ) result = child[i]->search(key, notfound); else if ( SInsert == notfound ) child[i] = new PtrSetEl(key, this); return NULL != result || i != notfound ? result : item; } template void PtrSetEl::print(int depth) { if ( NULL != child[0] ) child[0]->print(depth + 1); printf("%*c%s:\n", depth, ':', ((string *)item)->getstr()); if ( NULL != child[1] ) child[1]->print(depth + 1); } /*--------------------------------------------------------------------------*/ template PtrSet::PtrSet() :root(NULL) { } template PtrSet::~PtrSet() { if ( NULL != root ) delete root; } template PtrSet::PtrSet(const PtrSet ©) { if ( NULL == copy.root ) root = (PtrSetEl *) NULL; else root = new PtrSetEl(*copy.root); } template T *PtrSet::search(T *key, mode notfound) { if ( NULL != root ) return root->search(key, notfound); if ( SInsert == notfound ) root = new PtrSetEl(key); return NULL; } template bool PtrSet::first(mode dir /* = SNext */) { current = root; if ( NULL == current ) return False; while ( NULL != current->child[dir] ) current = current->child[dir]; return True; } template bool PtrSet::next(mode dir /* = SNext */) { if ( NULL == current ) return False; register const PtrSetEl *temp = current->child[1 - dir]; if ( NULL != temp ) { do current = temp; while ( NULL != (temp = temp->child[dir]) ); return True; } do { temp = current; if ( NULL == (current = temp->father) ) return False; } while (current->child[dir] != temp); return True; } template void PtrSet::print() { if ( NULL != root ) root->print(0); }