Initial import of the CDE 2.1.30 sources from the Open Group.
[oweals/cde.git] / cde / programs / nsgmls / HashTable.h
1 /* $XConsortium: HashTable.h /main/1 1996/07/29 16:52:57 cde-hp $ */
2 // Copyright (c) 1994 James Clark
3 // See the file COPYING for copying permission.
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:
31   HashTable() { }
32   void insert(const K &key, const V &value, Boolean replace = 1);
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   }
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:
46   HashTableIter(const HashTable<K, V> &table) : iter_(table.table_) { }
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.C"
69 #endif