1 // $TOG: cc_hdict.C /main/5 1998/04/17 11:45:00 mgreess $
3 #include "dti_cc/cc_exceptions.h"
6 template <class K, class V> CC_Boolean kv_pair<K, V>::f_needRemove = FALSE;
9 template <class K, class V>
10 kv_pair<K, V>::~kv_pair()
12 if ( f_needRemove == TRUE ) {
19 template <class K, class V>
20 ostream& kv_pair<K, V>::print(ostream& out)
22 return out << *f_key << " " << *f_value << "\n";
26 template <class K, class V>
27 unsigned int kv_pair<K, V>::operator==(const kv_pair<K, V>& kv)
29 if ( f_key == 0 || kv.f_key == 0 )
30 throw(ccStringException("kv_pair::operator==(): null pointer(s)."));
32 return ( *f_key == (*kv.f_key) ) ? 1 : 0;
35 ///////////////////////////////////
37 ///////////////////////////////////
39 template <class K, class V>
40 hashTable<K,V>::hashTable(const hashTable<K, V>& h) :
41 f_hash_func_ptr(h.f_hash_func_ptr), f_buckets(h.f_buckets),
44 cerr << "Warning: hashTable(const hashTable&) called" << endl;
48 template <class K, class V>
49 hashTable<K,V>::hashTable(unsigned (*f)(const K&), size_t init_bucket_num) :
50 f_hash_func_ptr(f), f_buckets(init_bucket_num),
55 template <class K, class V>
56 hashTable<K,V>::~hashTable()
58 CC_TPtrSlist<kv_pair<K, V> > * b = 0;
59 kv_pair<K, V> * r = 0;
61 for (size_t i=0; i<f_buckets.length(); i++ ) {
66 while ( (r = b -> removeFirst()) )
74 template <class K, class V>
75 void hashTable<K,V>::clearAndDestroy()
77 kv_pair<K, V>::f_needRemove = TRUE;
79 CC_TPtrSlist<kv_pair<K, V> >* b = 0;
81 for (size_t i=0; i<f_buckets.length(); i++ ) {
85 b -> clearAndDestroy();
93 kv_pair<K, V>::f_needRemove = FALSE;
96 template <class K, class V>
97 CC_Boolean hashTable<K,V>::contains(const K* k) const
106 template <class K, class V>
107 kv_pair<K, V>* hashTable<K,V>::_find(const K* k) const
109 size_t i = (*f_hash_func_ptr)(*k) % f_buckets.length();
111 CC_TPtrSlist<kv_pair<K, V> >* b = f_buckets[i];
116 kv_pair<K, V> key((K*)k);
118 return b -> find(&key);
121 template <class K, class V>
122 V* hashTable<K,V>::findValue(const K* k) const
124 kv_pair<K, V>* p = _find(k);
131 template <class K, class V>
132 K* hashTable<K,V>::findKeyAndValue(const K* k, V*& v) const
134 kv_pair<K, V>* p = _find(k);
142 template <class K, class V>
143 void hashTable<K,V>::insertKeyAndValue(K* k, V* v)
145 size_t i = (*f_hash_func_ptr)(*k) % f_buckets.length();
147 CC_TPtrSlist<kv_pair<K, V> >* b = f_buckets[i];
150 f_buckets[i] = new CC_TPtrSlist<kv_pair<K, V> >;
153 kv_pair<K, V>* p = new kv_pair<K, V>(k, v);
154 f_buckets[i] -> insert(p);
159 template <class K, class V>
160 K* hashTable<K,V>::remove(const K* k)
162 size_t i = (*f_hash_func_ptr)(*k) % f_buckets.length();
164 CC_TPtrSlist<kv_pair<K, V> >* b = f_buckets[i];
169 kv_pair<K, V> key((K*)k, 0);
170 kv_pair<K, V>* result = b -> remove(&key);
175 K* kr = result -> f_key;
185 template <class K, class V>
186 ostream& hashTable<K,V>::print(ostream& out)
188 CC_TPtrSlist<kv_pair<K, V> >* b = 0;
190 for (int i=0; i<f_buckets.length(); i++ ) {
194 cerr << "bucket num = " << i << "\n";
196 CC_TPtrSlistIterator<kv_pair<K, V> > next(*b);
199 out << ' ' << *next.key() ;
208 ////////////////////////////////////////
209 template <class K, class V>
210 hashTableIterator<K,V>::hashTableIterator(hashTable<K, V>& b) :
211 f_bucket_num(0), f_pos(0), f_rec(0), f_hashTable(b)
215 template <class K, class V>
216 hashTableIterator<K,V>::~hashTableIterator()
220 template <class K, class V>
221 CC_Boolean hashTableIterator<K,V>::_findNonEmptyBucket()
223 CC_TPtrSlist<kv_pair<K, V> >* b = 0;
225 for (; f_bucket_num<f_hashTable.f_buckets.length(); f_bucket_num++ ) {
226 if ( (b=f_hashTable.f_buckets[f_bucket_num]) && b->entries() > 0 ) {
235 template <class K, class V>
236 CC_Boolean hashTableIterator<K,V>::operator++()
238 if ( f_rec == 0 ) { // first call to this op.
239 if ( _findNonEmptyBucket() == FALSE )
243 if ( _findNextRecord() == FALSE ) {
245 if ( _findNonEmptyBucket() == FALSE )
250 //fprintf(stderr, "in operator++: f_bucket_num= %d, f_pos = %d\n",
251 //f_bucket_num, f_pos);
253 f_rec = f_hashTable.f_buckets[f_bucket_num] -> at(f_pos);
257 template <class K, class V>
258 CC_Boolean hashTableIterator<K,V>::_findNextRecord()
262 //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() );
264 if ( f_hashTable.f_buckets[f_bucket_num] -> entries() <= f_pos )
270 template <class K, class V>
271 K* hashTableIterator<K,V>::key()
273 return f_rec -> f_key;
276 template <class K, class V>
277 V* hashTableIterator<K,V>::value() const
279 return f_rec -> f_value;