hash_set和hash_multiset

 1 #include "MyHashTable.h"
 2 template <typename Tp>
 3 struct Identity 
 4 {
 5     const Tp& operator()(const Tp& x) const { return x; }
 6 };
 7 template <typename Value, typename Hash = std::hash<Value>, typename Equal = std::equal_to<Value> >
 8 class MyHashSet
 9 {
10 private:
11     typedef MyHashtable<Value, Value, Hash, Identity<Value>, Equal> Hashtable;
12     Hashtable m_hashtable;
13 public:
14     typedef typename Hashtable::key_type key_type;
15     typedef typename Hashtable::value_type value_type;
16     typedef typename Hashtable::reference reference;
17     typedef typename Hashtable::iterator iterator;
18 public:
19     MyHashSet() : m_hashtable(100) {}
20     explicit MyHashSet(size_t n) : m_hashtable(n) {}
21 
22 public:
23     size_t size() const { return m_hashtable.size(); }
24     size_t max_size() const { return m_hashtable.max_size(); }
25     bool empty() const { return m_hashtable.empty(); }
26     void swap(MyHashSet& hs) { m_hashtable.swap(hs.m_hashtable); }
27     iterator begin() const { return m_hashtable.begin(); }
28     iterator end() const { return m_hashtable.end(); }
29 
30 public:
31     std::pair<iterator, bool> insert(const value_type& obj)
32     {
33         return m_hashtable.insert_unique(obj);
34     }
35     iterator find(const key_type& key) { return m_hashtable.find(key); }
36     size_t count(const key_type& key) const { return m_hashtable.count(key); }
37     size_t erase(const key_type& key) { return m_hashtable.erase(key); }
38     void erase(iterator it) { m_hashtable.erase(it); }
39     void clear() { m_hashtable.clear(); }
40     void resize(size_t hint) { m_hashtable.resize(hint); }
41     size_t bucket_count() const { return m_hashtable.bucket_count(); }
42 };
43 
44 template <typename Value, typename Hash = std::hash<Value>, typename Equal = std::equal_to<Value> >
45 class MyHashMultiset
46 {
47 private:
48     typedef MyHashtable<Value, Value, Hash, Identity<Value>,Equal> Hashtable;
49     Hashtable m_hashtable;
50 
51 public:
52     typedef typename Hashtable::key_type key_type;
53     typedef typename Hashtable::value_type value_type;
54     typedef typename Hashtable::reference reference;
55     typedef typename Hashtable::iterator iterator;
56 public:
57     MyHashMultiset() : m_hashtable(100) {}
58     explicit MyHashMultiset(size_t n) : m_hashtable(n) {}
59 public:
60     size_t size() const { return m_hashtable.size(); }
61     size_t max_size() const { return m_hashtable.max_size(); }
62     bool empty() const { return m_hashtable.empty(); }
63     void swap(MyHashMultiset& hs) { m_hashtable.swap(hs.m_hashtable); }
64     iterator begin() const { return m_hashtable.begin(); }
65     iterator end() const { return m_hashtable.end(); }
66 public:
67     iterator insert(const value_type& obj)
68     {
69         return m_hashtable.insert_equal(obj);
70     }
71     iterator find(const key_type& key) { return m_hashtable.find(key); }
72     size_t count(const key_type& key) const { return m_hashtable.count(key); }
73     size_t erase(const key_type& key) { return m_hashtable.erase(key); }
74     void erase(iterator it) { m_hashtable.erase(it); }
75     void clear() { m_hashtable.clear(); }
76     void resize(size_t hint) { m_hashtable.resize(hint); }
77     size_t bucket_count() const { return m_hashtable.bucket_count(); }
78 };

 

posted @ 2021-02-25 20:43  ho966  阅读(63)  评论(0编辑  收藏  举报