1 // Copyright (c) 1994 James Clark 2 // See the file COPYING for copying permission. 3 #pragma ident "%Z%%M% %I% %E% SMI" 4 5 #ifndef HashTable_INCLUDED 6 #define HashTable_INCLUDED 1 7 8 #include <stddef.h> 9 #include "OwnerTable.h" 10 #include "Hash.h" 11 #include "Boolean.h" 12 #include "HashTableItemBase.h" 13 14 #ifdef SP_NAMESPACE 15 namespace SP_NAMESPACE { 16 #endif 17 18 template<class K, class V> 19 class HashTableItem : public HashTableItemBase<K> { 20 public: 21 HashTableItem(const K &k, const V &v); 22 HashTableItemBase<K> *copy() const; 23 V value; 24 }; 25 26 template<class K, class V> class HashTableIter; 27 28 template<class K, class V> 29 class HashTable { 30 public: HashTable()31 HashTable() { } 32 void insert(const K &key, const V &value, Boolean replace = 1); lookup(const K & key)33 const V *lookup(const K &key) const { 34 HashTableItem<K, V> *tem = (HashTableItem<K, V> *)table_.lookup(key); 35 return tem ? &tem->value : 0; 36 } count()37 size_t count() const { return table_.count(); } 38 private: 39 CopyOwnerTable<HashTableItemBase<K>, K, Hash, HashTableKeyFunction<K> > table_; 40 friend class HashTableIter<K,V>; 41 }; 42 43 template<class K, class V> 44 class HashTableIter { 45 public: HashTableIter(const HashTable<K,V> & table)46 HashTableIter(const HashTable<K, V> &table) : iter_(table.table_) { } next(const K * & key,const V * & value)47 Boolean next(const K *&key, const V *&value) { 48 HashTableItem<K, V> *p = (HashTableItem<K, V> *)iter_.next(); 49 if (p) { 50 key = &p->key; 51 value = &p->value; 52 return 1; 53 } 54 else 55 return 0; 56 } 57 private: 58 OwnerTableIter<HashTableItemBase<K>, K, Hash, HashTableKeyFunction<K> > iter_; 59 }; 60 61 #ifdef SP_NAMESPACE 62 } 63 #endif 64 65 #endif /* not HashTable_INCLUDED */ 66 67 #ifdef SP_DEFINE_TEMPLATES 68 #include "HashTable.cxx" 69 #endif 70