1 ////////////////////////////////////////////////////////////////////////////////
2 //
3 // HashSet.hh
4 //    produced: 01/09/97 jr
5 // last change: 10/10/97 jr
6 //
7 ////////////////////////////////////////////////////////////////////////////////
8 #ifndef HASHSET_HH
9 #define HASHSET_HH
10 
11 #include <stdlib.h>
12 #include <iostream>
13 #include <ctype.h>
14 #include <string.h>
15 
16 #include "RefCount.hh"
17 #include "PlainHashSet.hh"
18 
19 template<class Key>
20 class HashSet;
21 
22 template<class Key>
23 inline std::istream& operator>>(std::istream& ist, HashSet<Key>& ht);
24 
25 template<class Key>
26 inline std::ostream& operator<<(std::ostream& ost, const HashSet<Key>& ht);
27 
28 template<class Key>
29 class HashSet {
30   typedef SmartPtr< PlainHashSet<Key> >    data_type;
31 private:
32   static const PlainHashSet<Key> default_PlainHashSet;
33 private:
34   data_type _data;
35 public:
36   // constructors:
HashSet()37   inline HashSet() : _data(default_PlainHashSet) {}
HashSet(const size_type new_size)38   inline HashSet(const size_type new_size) : _data(PlainHashSet<Key>(new_size)) {}
HashSet(const HashSet<Key> & ht)39   inline HashSet(const HashSet<Key>& ht) : _data(ht._data) {}
40   // destructor:
~HashSet()41   inline ~HashSet() {}
42   // assignment:
operator =(const HashSet<Key> & ht)43   inline HashSet& operator=(const HashSet<Key>& ht) { _data = ht._data; return *this; }
44   // comparison:
operator ==(const HashSet<Key> & ht) const45   inline bool operator==(const HashSet<Key>& ht) const {
46     return (*_data == *ht._data);
47   }
operator !=(const HashSet<Key> & ht) const48   inline bool operator!=(const HashSet<Key>& ht) const {
49     return !((*this) == ht);
50   }
51   // keys for containers:
keysize() const52   inline const size_type keysize()              const { return _data->keysize(); }
key(const size_type n) const53   inline const size_type key(const size_type n) const { return _data->key(n); }
54   // accessors:
is_empty() const55   inline const bool           is_empty() const { return _data->is_empty(); }
card() const56   inline const size_type      card()     const { return _data->load(); }
size() const57   inline const size_type      size()     const { return _data->size(); }
load() const58   inline const size_type      load()     const { return _data->load(); }
59 #ifdef WATCH_MAXCHAINLEN
maxchainlen()60   inline static const size_type      maxchainlen() { return PlainHashSet<Key>::maxchainlen(); }
61 #endif
62   // operations:
member(const Key & key) const63   inline bool member(const Key& key) const { return _data->member(key); }
insert(const Key & key)64   inline void insert(const Key& key)       { _data->insert(key); }
erase(const Key & key)65   inline void erase(const Key& key)        { _data->erase(key); }
erase_random()66   inline void erase_random()               { _data->erase_random(); }
clear()67   inline void clear()                      { _data->clear(); }
68   // iterator types:
69   typedef typename PlainHashSet<Key>::const_iterator const_iterator;
70   typedef const_iterator                    iterator;
71   // iterator functions:
begin() const72   inline const_iterator begin()              const {return _data->begin();}
end() const73   inline const_iterator end()                const {return _data->end();}
find(const Key & key) const74   inline const_iterator find(const Key& key) const { return _data->find(key); }
75   // iostream:
read(std::istream & ist)76   inline std::istream& read(std::istream& ist) {
77     return ist >> *_data;
78   }
write(std::ostream & ost) const79   inline std::ostream& write(std::ostream& ost) const {
80     return ost << *_data;
81   }
82   friend std::istream& operator>><>(std::istream& ist, HashSet<Key>& ht);
83   friend std::ostream& operator<<<>(std::ostream& ost, const HashSet<Key>& ht);
84 };
85 
86 template<class Key>
87 const PlainHashSet<Key> HashSet<Key>::default_PlainHashSet;
88 
89 // friends:
90 template<class Key>
operator >>(std::istream & ist,HashSet<Key> & ht)91 inline std::istream& operator>>(std::istream& ist, HashSet<Key>& ht) {
92   return ht.read(ist);
93 }
94 
95 template<class Key>
operator <<(std::ostream & ost,const HashSet<Key> & ht)96 inline std::ostream& operator<<(std::ostream& ost, const HashSet<Key>& ht) {
97   return ht.write(ost);
98 }
99 
100 #endif
101 // eof HashSet.hh
102