1 ////////////////////////////////////////////////////////////////////////////////
2 //
3 // PlainHashSet.hh
4 //    produced: 01/09/97 jr
5 // last change: 24/01/99 jr
6 //
7 ////////////////////////////////////////////////////////////////////////////////
8 #ifndef PLAINHASHSET_HH
9 #define PLAINHASHSET_HH
10 
11 #include <stdlib.h>
12 #include <iostream>
13 #include <ctype.h>
14 #include <string.h>
15 
16 #include "Global.hh"
17 #include "RefCount.hh"
18 #include "PlainHashTable.hh"
19 
20 template<class Key>
21 class HashSetData;
22 
23 template<class Key>
24 class __hsd_reader;
25 
26 template<class Key>
27 inline std::istream& operator>>(std::istream& ist, __hsd_reader<Key>& r);
28 
29 template<class Key>
30 class __hsd_reader {
31 public:
32   Key  key;
33 public:
__hsd_reader()34   inline __hsd_reader() : key() {}
35   friend std::istream& operator>><>(std::istream& ist, __hsd_reader& r);
36 };
37 
38 template<class Key>
39 inline std::ostream& operator<<(std::ostream& ost, const HashSetData<Key>& hsd);
40 
41 template<class Key>
42 class HashSetData {
43 public:
44   typedef Key                  key_type;
45   typedef SmartPtr<Key>        keyptr_type;
46   typedef SmartPtr<const Key>  const_keyptr_type;
47   typedef __hsd_reader<Key>    reader;
48 private:
49   const const_keyptr_type _keyptr;
50 public:
51   // constructors:
HashSetData()52   inline HashSetData() : _keyptr() {}
HashSetData(const HashSetData & hsd)53   inline HashSetData(const HashSetData& hsd) : _keyptr(hsd._keyptr) {}
HashSetData(const Key & new_key)54   inline HashSetData(const Key& new_key) : _keyptr(new_key) {}
HashSetData(const reader & r)55   inline HashSetData(const reader& r) : _keyptr(r.key) {}
56   // destructor:
~HashSetData()57   inline ~HashSetData() {}
58 private:
59   // assignment:
operator =(const HashSetData & hsd)60   inline HashSetData& operator=(const HashSetData& hsd) {
61     if (this == &hsd) {
62       return *this;
63     }
64     _keyptr = hsd._keyptr;
65     return *this;
66   }
67 public:
68   // conversion:
operator const Key&() const69   inline operator const Key&() const { return *_keyptr; }
70   // keys for containers:
keyptr() const71   inline const keyptr_type& keyptr()  const { return _keyptr; }
key() const72   inline const Key&         key()     const { return *_keyptr; }
73   // comparison:
operator ==(const HashSetData<Key> & hsd) const74   inline const bool operator==(const HashSetData<Key>& hsd) const { return (key() == hsd.key()); }
operator !=(const HashSetData<Key> & hsd) const75   inline const bool operator!=(const HashSetData<Key>& hsd) const { return !(key() == hsd.key()); }
76   // iostream:
write(std::ostream & ost) const77   inline std::ostream& write(std::ostream& ost) const {
78     return ost << *_keyptr;
79   }
80   friend std::ostream& operator<<<>(std::ostream& ost, const HashSetData<Key>& hsd);
81 };
82 
83 template<class Key>
84 class PlainHashSet : public PlainHashTable< HashSetData<Key> > {
85 public:
86   typedef          PlainHashTable< HashSetData<Key> >       plainhashset_data;
87   typedef typename HashSetData<Key>::keyptr_type            keyptr_type;
88   typedef typename HashSetData<Key>::const_keyptr_type      const_keyptr_type;
89 public:
90   // constructors:
PlainHashSet()91   inline PlainHashSet() : plainhashset_data() {}
PlainHashSet(const PlainHashSet & phs)92   inline PlainHashSet(const PlainHashSet& phs) : plainhashset_data(phs) {}
PlainHashSet(const plainhashset_data & phsd)93   inline PlainHashSet(const plainhashset_data& phsd) : plainhashset_data(phsd) {}
PlainHashSet(const size_type init_size)94   inline PlainHashSet(const size_type init_size) : plainhashset_data(init_size) {}
95   // destructor:
~PlainHashSet()96   inline ~PlainHashSet() {}
97   // assignment:
operator =(const PlainHashSet & phs)98   inline PlainHashSet& operator=(const PlainHashSet& phs) {
99     plainhashset_data::operator=(phs);
100     return *this;
101   }
102   // functions:
insert(const Key & key)103   inline void insert(const Key& key)       { plainhashset_data::insert(key); }
member(const Key & key) const104   inline bool member(const Key& key) const { return plainhashset_data::member(key); }
105 };
106 
107 template<class Key>
operator >>(std::istream & ist,__hsd_reader<Key> & r)108 inline std::istream& operator>>(std::istream& ist, __hsd_reader<Key>& r) {
109   if (!(ist >> std::ws >> r.key)) {
110 #ifdef READ_DEBUG
111     std::cerr << "std::istream& operator>>(std::istream&, HashMapData<Key, Data>&): "
112 	 << "key not found." << std::endl;
113 #endif
114     ist.clear(std::ios::failbit);
115     return ist;
116   }
117   return ist;
118 }
119 
120 template<class Key>
operator <<(std::ostream & ost,const HashSetData<Key> & hsd)121 inline std::ostream& operator<<(std::ostream& ost, const HashSetData<Key>& hsd) {
122   return hsd.write(ost);
123 }
124 
125 #endif
126 // eof PlainHashSet.hh
127