hash_set

2 篇文章 0 订阅
1 // Filename:    stl_hash_set.h
  2 
  3 // Comment By: watson
  4 
  6 
  7 // hash_set和hash_multiset是对hashtable的简单包装, 很容易理解
  8 
  9 /*
 10  * Copyright (c) 1996
 11  * Silicon Graphics Computer Systems, Inc.
 12  *
 13  * Permission to use, copy, modify, distribute and sell this software
 14  * and its documentation for any purpose is hereby granted without fee,
 15  * provided that the above copyright notice appear in all copies and
 16  * that both that copyright notice and this permission notice appear
 17  * in supporting documentation.  Silicon Graphics makes no
 18  * representations about the suitability of this software for any
 19  * purpose.  It is provided "as is" without express or implied warranty.
 20  *
 21  *
 22  * Copyright (c) 1994
 23  * Hewlett-Packard Company
 24  *
 25  * Permission to use, copy, modify, distribute and sell this software
 26  * and its documentation for any purpose is hereby granted without fee,
 27  * provided that the above copyright notice appear in all copies and
 28  * that both that copyright notice and this permission notice appear
 29  * in supporting documentation.  Hewlett-Packard Company makes no
 30  * representations about the suitability of this software for any
 31  * purpose.  It is provided "as is" without express or implied warranty.
 32  *
 33  */
 34 
 35 /* NOTE: This is an internal header file, included by other STL headers.
 36  *   You should not attempt to use it directly.
 37  */
 38 
 39 #ifndef __SGI_STL_INTERNAL_HASH_SET_H
 40 #define __SGI_STL_INTERNAL_HASH_SET_H
 41 
 42 __STL_BEGIN_NAMESPACE
 43 
 44 #if defined(__sgi) && !defined(__GNUC__) && (_MIPS_SIM != _MIPS_SIM_ABI32)
 45 #pragma set woff 1174
 46 #endif
 47 
 48 // 如果编译器不能根据前面模板参数推导出后面使用的默认参数类型,
 49 // 那么就需要手工指定, 并且对于基本的数据类型, 在<stl_hash_fun.h>
 50 // 中都提供hash函数
 51 #ifndef __STL_LIMITED_DEFAULT_TEMPLATES
 52 template <class Value, class HashFcn = hash<Value>,
 53           class EqualKey = equal_to<Value>,
 54           class Alloc = alloc>
 55 #else
 56 template <class Value, class HashFcn, class EqualKey, class Alloc = alloc>
 57 #endif
 58 class hash_set
 59 {
 60 private:
 61   // identity<Value>用于析出Value
 62   typedef hashtable<Value, Value, HashFcn, identity<Value>,
 63                     EqualKey, Alloc> ht;
 64   ht rep;       // 其实hash_set就是hashtable的简单封装
 65 
 66 public:
 67   typedef typename ht::key_type key_type;
 68   typedef typename ht::value_type value_type;
 69   typedef typename ht::hasher hasher;
 70   typedef typename ht::key_equal key_equal;
 71 
 72   // 注意: reference, pointer, iterator都为const, 因为不能修改hashtable
 73   // 内部的元素, 否则会导致hashtable失效
 74   typedef typename ht::size_type size_type;
 75   typedef typename ht::difference_type difference_type;
 76   typedef typename ht::const_pointer pointer;
 77   typedef typename ht::const_pointer const_pointer;
 78   typedef typename ht::const_reference reference;
 79   typedef typename ht::const_reference const_reference;
 80 
 81   typedef typename ht::const_iterator iterator;
 82   typedef typename ht::const_iterator const_iterator;
 83 
 84   // 返回hash相关函数
 85   hasher hash_funct() const { return rep.hash_funct(); }
 86   key_equal key_eq() const { return rep.key_eq(); }
 87 
 88 public:
 89   hash_set() : rep(100, hasher(), key_equal()) {}
 90   explicit hash_set(size_type n) : rep(n, hasher(), key_equal()) {}
 91   hash_set(size_type n, const hasher& hf) : rep(n, hf, key_equal()) {}
 92   hash_set(size_type n, const hasher& hf, const key_equal& eql)
 93     : rep(n, hf, eql) {}
 94 
 95 #ifdef __STL_MEMBER_TEMPLATES
 96   template <class InputIterator>
 97   hash_set(InputIterator f, InputIterator l)
 98     : rep(100, hasher(), key_equal()) { rep.insert_unique(f, l); }
 99   template <class InputIterator>
100   hash_set(InputIterator f, InputIterator l, size_type n)
101     : rep(n, hasher(), key_equal()) { rep.insert_unique(f, l); }
102   template <class InputIterator>
103   hash_set(InputIterator f, InputIterator l, size_type n,
104            const hasher& hf)
105     : rep(n, hf, key_equal()) { rep.insert_unique(f, l); }
106   template <class InputIterator>
107   hash_set(InputIterator f, InputIterator l, size_type n,
108            const hasher& hf, const key_equal& eql)
109     : rep(n, hf, eql) { rep.insert_unique(f, l); }
110 #else
111 
112   hash_set(const value_type* f, const value_type* l)
113     : rep(100, hasher(), key_equal()) { rep.insert_unique(f, l); }
114   hash_set(const value_type* f, const value_type* l, size_type n)
115     : rep(n, hasher(), key_equal()) { rep.insert_unique(f, l); }
116   hash_set(const value_type* f, const value_type* l, size_type n,
117            const hasher& hf)
118     : rep(n, hf, key_equal()) { rep.insert_unique(f, l); }
119   hash_set(const value_type* f, const value_type* l, size_type n,
120            const hasher& hf, const key_equal& eql)
121     : rep(n, hf, eql) { rep.insert_unique(f, l); }
122 
123   hash_set(const_iterator f, const_iterator l)
124     : rep(100, hasher(), key_equal()) { rep.insert_unique(f, l); }
125   hash_set(const_iterator f, const_iterator l, size_type n)
126     : rep(n, hasher(), key_equal()) { rep.insert_unique(f, l); }
127   hash_set(const_iterator f, const_iterator l, size_type n,
128            const hasher& hf)
129     : rep(n, hf, key_equal()) { rep.insert_unique(f, l); }
130   hash_set(const_iterator f, const_iterator l, size_type n,
131            const hasher& hf, const key_equal& eql)
132     : rep(n, hf, eql) { rep.insert_unique(f, l); }
133 #endif /*__STL_MEMBER_TEMPLATES */
134 
135 public:
136   // 下面都是对hashtable的简单封装, 见<stl_hashtable.h>
137   size_type size() const { return rep.size(); }
138   size_type max_size() const { return rep.max_size(); }
139   bool empty() const { return rep.empty(); }
140   void swap(hash_set& hs) { rep.swap(hs.rep); }
141 
142   friend bool operator== __STL_NULL_TMPL_ARGS (const hash_set&,
143                                                const hash_set&);
144   iterator begin() const { return rep.begin(); }
145   iterator end() const { return rep.end(); }
146 
147 public:
148   pair<iterator, bool> insert(const value_type& obj)
149     {
150       pair<typename ht::iterator, bool> p = rep.insert_unique(obj);
151       r
152       eturn pair<iterator, bool>(p.first, p.second);
153     }
154 #ifdef __STL_MEMBER_TEMPLATES
155   template <class InputIterator>
156   void insert(InputIterator f, InputIterator l) { rep.insert_unique(f,l); }
157 #else
158   void insert(const value_type* f, const value_type* l) {
159     rep.insert_unique(f,l);
160   }
161   void insert(const_iterator f, const_iterator l) {rep.insert_unique(f, l); }
162 #endif /*__STL_MEMBER_TEMPLATES */
163 
164   // hash_set和set一样, 都不允许key重复
165   pair<iterator, bool> insert_noresize(const value_type& obj)
166   {
167     pair<typename ht::iterator, bool> p = rep.insert_unique_noresize(obj);
168     return pair<iterator, bool>(p.first, p.second);
169   }
170 
171   iterator find(const key_type& key) const { return rep.find(key); }
172 
173   size_type count(const key_type& key) const { return rep.count(key); }
174 
175   pair<iterator, iterator> equal_range(const key_type& key) const
176     { return rep.equal_range(key); }
177 
178   size_type erase(const key_type& key) {return rep.erase(key); }
179   void erase(iterator it) { rep.erase(it); }
180   void erase(iterator f, iterator l) { rep.erase(f, l); }
181   void clear() { rep.clear(); }
182 
183 public:
184   void resize(size_type hint) { rep.resize(hint); }
185   size_type bucket_count() const { return rep.bucket_count(); }
186   size_type max_bucket_count() const { return rep.max_bucket_count(); }
187   size_type elems_in_bucket(size_type n) const
188     { return rep.elems_in_bucket(n); }
189 };
190 
191 template <class Value, class HashFcn, class EqualKey, class Alloc>
192 inline bool operator==(const hash_set<Value, HashFcn, EqualKey, Alloc>& hs1,
193                        const hash_set<Value, HashFcn, EqualKey, Alloc>& hs2)
194 {
195   return hs1.rep == hs2.rep;
196 }
197 
198 // 如果编译器支持模板函数特化优先级
199 // 那么将全局的swap实现为使用hash_set私有的swap以提高效率
200 #ifdef __STL_FUNCTION_TMPL_PARTIAL_ORDER
201 
202 template <class Val, class HashFcn, class EqualKey, class Alloc>
203 inline void swap(hash_set<Val, HashFcn, EqualKey, Alloc>& hs1,
204                  hash_set<Val, HashFcn, EqualKey, Alloc>& hs2)
205 {
206   hs1.swap(hs2);
207 }
208 
209 #endif /* __STL_FUNCTION_TMPL_PARTIAL_ORDER */
210 
211 // hash_multiset和hash_set除去允许key重复外, 其余性质一致
212 #ifndef __STL_LIMITED_DEFAULT_TEMPLATES
213 template <class Value, class HashFcn = hash<Value>,
214           class EqualKey = equal_to<Value>,
215           class Alloc = alloc>
216 #else
217 template <class Value, class HashFcn, class EqualKey, class Alloc = alloc>
218 #endif
219 class hash_multiset
220 {
221 private:
222   typedef hashtable<Value, Value, HashFcn, identity<Value>,
223                     EqualKey, Alloc> ht;
224   ht rep;
225 
226 public:
227   typedef typename ht::key_type key_type;
228   typedef typename ht::value_type value_type;
229   typedef typename ht::hasher hasher;
230   typedef typename ht::key_equal key_equal;
231 
232   typedef typename ht::size_type size_type;
233   typedef typename ht::difference_type difference_type;
234   typedef typename ht::const_pointer pointer;
235   typedef typename ht::const_pointer const_pointer;
236   typedef typename ht::const_reference reference;
237   typedef typename ht::const_reference const_reference;
238 
239   typedef typename ht::const_iterator iterator;
240   typedef typename ht::const_iterator const_iterator;
241 
242   hasher hash_funct() const { return rep.hash_funct(); }
243   key_equal key_eq() const { return rep.key_eq(); }
244 
245 public:
246   hash_multiset() : rep(100, hasher(), key_equal()) {}
247   explicit hash_multiset(size_type n) : rep(n, hasher(), key_equal()) {}
248   hash_multiset(size_type n, const hasher& hf) : rep(n, hf, key_equal()) {}
249   hash_multiset(size_type n, const hasher& hf, const key_equal& eql)
250     : rep(n, hf, eql) {}
251 
252 #ifdef __STL_MEMBER_TEMPLATES
253   template <class InputIterator>
254   hash_multiset(InputIterator f, InputIterator l)
255     : rep(100, hasher(), key_equal()) { rep.insert_equal(f, l); }
256   template <class InputIterator>
257   hash_multiset(InputIterator f, InputIterator l, size_type n)
258     : rep(n, hasher(), key_equal()) { rep.insert_equal(f, l); }
259   template <class InputIterator>
260   hash_multiset(InputIterator f, InputIterator l, size_type n,
261                 const hasher& hf)
262     : rep(n, hf, key_equal()) { rep.insert_equal(f, l); }
263   template <class InputIterator>
264   hash_multiset(InputIterator f, InputIterator l, size_type n,
265                 const hasher& hf, const key_equal& eql)
266     : rep(n, hf, eql) { rep.insert_equal(f, l); }
267 #else
268 
269   hash_multiset(const value_type* f, const value_type* l)
270     : rep(100, hasher(), key_equal()) { rep.insert_equal(f, l); }
271   hash_multiset(const value_type* f, const value_type* l, size_type n)
272     : rep(n, hasher(), key_equal()) { rep.insert_equal(f, l); }
273   hash_multiset(const value_type* f, const value_type* l, size_type n,
274                 const hasher& hf)
275     : rep(n, hf, key_equal()) { rep.insert_equal(f, l); }
276   hash_multiset(const value_type* f, const value_type* l, size_type n,
277                 const hasher& hf, const key_equal& eql)
278     : rep(n, hf, eql) { rep.insert_equal(f, l); }
279 
280   hash_multiset(const_iterator f, const_iterator l)
281     : rep(100, hasher(), key_equal()) { rep.insert_equal(f, l); }
282   hash_multiset(const_iterator f, const_iterator l, size_type n)
283     : rep(n, hasher(), key_equal()) { rep.insert_equal(f, l); }
284   hash_multiset(const_iterator f, const_iterator l, size_type n,
285                 const hasher& hf)
286     : rep(n, hf, key_equal()) { rep.insert_equal(f, l); }
287   hash_multiset(const_iterator f, const_iterator l, size_type n,
288                 const hasher& hf, const key_equal& eql)
289     : rep(n, hf, eql) { rep.insert_equal(f, l); }
290 #endif /*__STL_MEMBER_TEMPLATES */
291 
292 public:
293   size_type size() const { return rep.size(); }
294   size_type max_size() const { return rep.max_size(); }
295   bool empty() const { return rep.empty(); }
296   void swap(hash_multiset& hs) { rep.swap(hs.rep); }
297   friend bool operator== __STL_NULL_TMPL_ARGS (const hash_multiset&,
298                                                const hash_multiset&);
299 
300   iterator begin() const { return rep.begin(); }
301   iterator end() const { return rep.end(); }
302 
303 public:
304   iterator insert(const value_type& obj) { return rep.insert_equal(obj); }
305 #ifdef __STL_MEMBER_TEMPLATES
306   template <class InputIterator>
307   void insert(InputIterator f, InputIterator l) { rep.insert_equal(f,l); }
308 #else
309   void insert(const value_type* f, const value_type* l) {
310     rep.insert_equal(f,l);
311   }
312   void insert(const_iterator f, const_iterator l) { rep.insert_equal(f, l); }
313 #endif /*__STL_MEMBER_TEMPLATES */
314   iterator insert_noresize(const value_type& obj)
315     { return rep.insert_equal_noresize(obj); }
316 
317   iterator find(const key_type& key) const { return rep.find(key); }
318 
319   size_type count(const key_type& key) const { return rep.count(key); }
320 
321   pair<iterator, iterator> equal_range(const key_type& key) const
322     { return rep.equal_range(key); }
323 
324   size_type erase(const key_type& key) {return rep.erase(key); }
325   void erase(iterator it) { rep.erase(it); }
326   void erase(iterator f, iterator l) { rep.erase(f, l); }
327   void clear() { rep.clear(); }
328 
329 public:
330   void resize(size_type hint) { rep.resize(hint); }
331   size_type bucket_count() const { return rep.bucket_count(); }
332   size_type max_bucket_count() const { return rep.max_bucket_count(); }
333   size_type elems_in_bucket(size_type n) const
334     { return rep.elems_in_bucket(n); }
335 };
336 
337 template <class Val, class HashFcn, class EqualKey, class Alloc>
338 inline bool operator==(const hash_multiset<Val, HashFcn, EqualKey, Alloc>& hs1,
339                        const hash_multiset<Val, HashFcn, EqualKey, Alloc>& hs2)
340 {
341   return hs1.rep == hs2.rep;
342 }
343 
344 // 如果编译器支持模板函数特化优先级
345 // 那么将全局的swap实现为使用hash_multiset私有的swap以提高效率
346 #ifdef __STL_FUNCTION_TMPL_PARTIAL_ORDER
347 
348 template <class Val, class HashFcn, class EqualKey, class Alloc>
349 inline void swap(hash_multiset<Val, HashFcn, EqualKey, Alloc>& hs1,
350                  hash_multiset<Val, HashFcn, EqualKey, Alloc>& hs2)
351 {
352   hs1.swap(hs2);
353 }
354 
355 #endif /* __STL_FUNCTION_TMPL_PARTIAL_ORDER */
356 
357 #if defined(__sgi) && !defined(__GNUC__) && (_MIPS_SIM != _MIPS_SIM_ABI32)
358 #pragma reset woff 1174
359 #endif
360 
361 __STL_END_NAMESPACE
362 
363 #endif /* __SGI_STL_INTERNAL_HASH_SET_H */
364 
365 // Local Variables:
366 // mode:C++
367 // End:
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值