// $TOG: cc_hdict.C /main/5 1998/04/17 11:45:00 mgreess $ #include "dti_cc/cc_exceptions.h" #if !defined(__osf__) template CC_Boolean kv_pair::f_needRemove = FALSE; #endif template kv_pair::~kv_pair() { if ( f_needRemove == TRUE ) { delete f_key; delete f_value; } } #ifdef DEBUG template ostream& kv_pair::print(ostream& out) { return out << *f_key << " " << *f_value << "\n"; } #endif template unsigned int kv_pair::operator==(const kv_pair& kv) { if ( f_key == 0 || kv.f_key == 0 ) throw(ccStringException("kv_pair::operator==(): null pointer(s).")); return ( *f_key == (*kv.f_key) ) ? 1 : 0; } /////////////////////////////////// // /////////////////////////////////// template hashTable::hashTable(const hashTable& h) : f_hash_func_ptr(h.f_hash_func_ptr), f_buckets(h.f_buckets), f_items(h.f_items) { cerr << "Warning: hashTable(const hashTable&) called" << endl; exit(-1); } template hashTable::hashTable(unsigned (*f)(const K&), size_t init_bucket_num) : f_hash_func_ptr(f), f_buckets(init_bucket_num), f_items(0) { } template hashTable::~hashTable() { CC_TPtrSlist > * b = 0; kv_pair * r = 0; for (size_t i=0; i removeFirst()) ) delete r; delete b; } } } template void hashTable::clearAndDestroy() { kv_pair::f_needRemove = TRUE; CC_TPtrSlist >* b = 0; for (size_t i=0; i clearAndDestroy(); delete b; f_buckets[i] = 0; } } f_items = 0; kv_pair::f_needRemove = FALSE; } template CC_Boolean hashTable::contains(const K* k) const { if ( findValue(k) ) return TRUE; else return FALSE; } template kv_pair* hashTable::_find(const K* k) const { size_t i = (*f_hash_func_ptr)(*k) % f_buckets.length(); CC_TPtrSlist >* b = f_buckets[i]; if ( b == 0 ) return 0; kv_pair key((K*)k); return b -> find(&key); } template V* hashTable::findValue(const K* k) const { kv_pair* p = _find(k); if ( p ) return p -> f_value; else return 0; } template K* hashTable::findKeyAndValue(const K* k, V*& v) const { kv_pair* p = _find(k); if ( p ) { v = p -> f_value; return p -> f_key; } else return 0; } template void hashTable::insertKeyAndValue(K* k, V* v) { size_t i = (*f_hash_func_ptr)(*k) % f_buckets.length(); CC_TPtrSlist >* b = f_buckets[i]; if ( b == 0 ) { f_buckets[i] = new CC_TPtrSlist >; } kv_pair* p = new kv_pair(k, v); f_buckets[i] -> insert(p); f_items ++; } template K* hashTable::remove(const K* k) { size_t i = (*f_hash_func_ptr)(*k) % f_buckets.length(); CC_TPtrSlist >* b = f_buckets[i]; if ( b == 0 ) return 0; kv_pair key((K*)k, 0); kv_pair* result = b -> remove(&key); if ( result == 0 ) return 0; K* kr = result -> f_key; delete result; f_items --; return kr; } #ifdef DEBUG template ostream& hashTable::print(ostream& out) { CC_TPtrSlist >* b = 0; for (int i=0; i > next(*b); while (++next) { out << ' ' << *next.key() ; } } } return out; } #endif //////////////////////////////////////// template hashTableIterator::hashTableIterator(hashTable& b) : f_bucket_num(0), f_pos(0), f_rec(0), f_hashTable(b) { } template hashTableIterator::~hashTableIterator() { } template CC_Boolean hashTableIterator::_findNonEmptyBucket() { CC_TPtrSlist >* b = 0; for (; f_bucket_numentries() > 0 ) { f_pos = 0; return TRUE; } } return FALSE; } template CC_Boolean hashTableIterator::operator++() { if ( f_rec == 0 ) { // first call to this op. if ( _findNonEmptyBucket() == FALSE ) return FALSE; } else { if ( _findNextRecord() == FALSE ) { f_bucket_num++; if ( _findNonEmptyBucket() == FALSE ) return FALSE; } } //fprintf(stderr, "in operator++: f_bucket_num= %d, f_pos = %d\n", //f_bucket_num, f_pos); f_rec = f_hashTable.f_buckets[f_bucket_num] -> at(f_pos); return TRUE; } template CC_Boolean hashTableIterator::_findNextRecord() { f_pos++; //fprintf(stderr, "f_bucket_num= %d, f_pos = %d, entries() = %d\n", f_bucket_num, f_pos, f_hashTable.f_buckets[f_bucket_num] -> entries() ); if ( f_hashTable.f_buckets[f_bucket_num] -> entries() <= f_pos ) return FALSE; else return TRUE; } template K* hashTableIterator::key() { return f_rec -> f_key; } template V* hashTableIterator::value() const { return f_rec -> f_value; }