STL仅仅规定接口和复杂度,对于详细实现不作要求。set大多以红黑树实现,但STL在标准规格之外提供了一个所谓的hash_set,以hash table实现。hash_set的接口,hash_table都提供了,所以差点儿全部的hash_set操作都是直接调用hash_table的函数而已。

除了hash_set,还有hash_multiset,它们两个的关系就像set和multiset的关系,一个不同意键值反复,另外一个同意键值反复。其它实现一样。

  1. G++ 2.91.57cygnus\cygwin-b20\include\g++\stl_hash_set.h 完整列表
  2. /*
  3. * Copyright (c) 1996
  4. * Silicon Graphics Computer Systems, Inc.
  5. *
  6. * Permission to use, copy, modify, distribute and sell this software
  7. * and its documentation for any purpose is hereby granted without fee,
  8. * provided that the above copyright notice appear in all copies and
  9. * that both that copyright notice and this permission notice appear
  10. * in supporting documentation. Silicon Graphics makes no
  11. * representations about the suitability of this software for any
  12. * purpose. It is provided "as is" without express or implied warranty.
  13. *
  14. *
  15. * Copyright (c) 1994
  16. * Hewlett-Packard Company
  17. *
  18. * Permission to use, copy, modify, distribute and sell this software
  19. * and its documentation for any purpose is hereby granted without fee,
  20. * provided that the above copyright notice appear in all copies and
  21. * that both that copyright notice and this permission notice appear
  22. * in supporting documentation. Hewlett-Packard Company makes no
  23. * representations about the suitability of this software for any
  24. * purpose. It is provided "as is" without express or implied warranty.
  25. *
  26. */
  27.  
  28. /* NOTE: This is an internal header file, included by other STL headers.
  29. * You should not attempt to use it directly.
  30. */
  31.  
  32. #ifndef __SGI_STL_INTERNAL_HASH_SET_H
  33. #define __SGI_STL_INTERNAL_HASH_SET_H
  34.  
  35. __STL_BEGIN_NAMESPACE
  36.  
  37. #if defined(__sgi) && !defined(__GNUC__) && (_MIPS_SIM != _MIPS_SIM_ABI32)
  38. #pragma set woff 1174
  39. #endif
  40.  
  41. #ifndef __STL_LIMITED_DEFAULT_TEMPLATES
  42. // hash<>是个 function object,定义于 <stl_hash_fun.h>中
  43. //例:hash<int>::operator()(intx)const{return x;}
  44. template <class Value, class HashFcn = hash<Value>,
  45. class EqualKey = equal_to<Value>,
  46. class Alloc = alloc>
  47. #else
  48. template <class Value, class HashFcn, class EqualKey, class Alloc = alloc>
  49. #endif
  50. class hash_set
  51. {
  52. private:
  53. // 下面使用的 identity<> 定义于 <stl_function.h> 中。
  54. typedef hashtable<Value, Value, HashFcn, identity<Value>,
  55. EqualKey, Alloc> ht;
  56. ht rep; // 底层机制以 hash table 完毕
  57.  
  58. public:
  59. typedef typename ht::key_type key_type;
  60. typedef typename ht::value_type value_type;
  61. typedef typename ht::hasher hasher;
  62. typedef typename ht::key_equal key_equal;
  63.  
  64. typedef typename ht::size_type size_type;
  65. typedef typename ht::difference_type difference_type;
  66. typedef typename ht::const_pointer pointer;
  67. typedef typename ht::const_pointer const_pointer;
  68. typedef typename ht::const_reference reference;
  69. typedef typename ht::const_reference const_reference;
  70.  
  71. typedef typename ht::const_iterator iterator;
  72. typedef typename ht::const_iterator const_iterator;
  73.  
  74. hasher hash_funct() const { return rep.hash_funct(); }
  75. key_equal key_eq() const { return rep.key_eq(); }
  76.  
  77. public:
  78. //预设使用大小为100的vector buckets。将会自己主动调整为比100大的质数
  79. hash_set() : rep(100, hasher(), key_equal()) {}
  80. explicit hash_set(size_type n) : rep(n, hasher(), key_equal()) {}
  81. hash_set(size_type n, const hasher& hf) : rep(n, hf, key_equal()) {}
  82. hash_set(size_type n, const hasher& hf, const key_equal& eql)
  83. : rep(n, hf, eql) {}
  84.  
  85. // 插入所有调用 insert_unique(),不同意键值反复
  86. #ifdef __STL_MEMBER_TEMPLATES
  87. template <class InputIterator>
  88. hash_set(InputIterator f, InputIterator l)
  89. : rep(100, hasher(), key_equal()) { rep.insert_unique(f, l); }
  90. template <class InputIterator>
  91. hash_set(InputIterator f, InputIterator l, size_type n)
  92. : rep(n, hasher(), key_equal()) { rep.insert_unique(f, l); }
  93. template <class InputIterator>
  94. hash_set(InputIterator f, InputIterator l, size_type n,
  95. const hasher& hf)
  96. : rep(n, hf, key_equal()) { rep.insert_unique(f, l); }
  97. template <class InputIterator>
  98. hash_set(InputIterator f, InputIterator l, size_type n,
  99. const hasher& hf, const key_equal& eql)
  100. : rep(n, hf, eql) { rep.insert_unique(f, l); }
  101. #else
  102.  
  103. hash_set(const value_type* f, const value_type* l)
  104. : rep(100, hasher(), key_equal()) { rep.insert_unique(f, l); }
  105. hash_set(const value_type* f, const value_type* l, size_type n)
  106. : rep(n, hasher(), key_equal()) { rep.insert_unique(f, l); }
  107. hash_set(const value_type* f, const value_type* l, size_type n,
  108. const hasher& hf)
  109. : rep(n, hf, key_equal()) { rep.insert_unique(f, l); }
  110. hash_set(const value_type* f, const value_type* l, size_type n,
  111. const hasher& hf, const key_equal& eql)
  112. : rep(n, hf, eql) { rep.insert_unique(f, l); }
  113.  
  114. hash_set(const_iterator f, const_iterator l)
  115. : rep(100, hasher(), key_equal()) { rep.insert_unique(f, l); }
  116. hash_set(const_iterator f, const_iterator l, size_type n)
  117. : rep(n, hasher(), key_equal()) { rep.insert_unique(f, l); }
  118. hash_set(const_iterator f, const_iterator l, size_type n,
  119. const hasher& hf)
  120. : rep(n, hf, key_equal()) { rep.insert_unique(f, l); }
  121. hash_set(const_iterator f, const_iterator l, size_type n,
  122. const hasher& hf, const key_equal& eql)
  123. : rep(n, hf, eql) { rep.insert_unique(f, l); }
  124. #endif /*__STL_MEMBER_TEMPLATES */
  125.  
  126. public:
  127. // 所有操作差点儿都是调用 hash table的函数。
  128. size_type size() const { return rep.size(); }
  129. size_type max_size() const { return rep.max_size(); }
  130. bool empty() const { return rep.empty(); }
  131. void swap(hash_set& hs) { rep.swap(hs.rep); }
  132. friend bool operator== __STL_NULL_TMPL_ARGS (const hash_set&,
  133. const hash_set&);
  134.  
  135. iterator begin() const { return rep.begin(); }
  136. iterator end() const { return rep.end(); }
  137.  
  138. public:
  139. pair<iterator, bool> insert(const value_type& obj)
  140. {
  141. pair<typename ht::iterator, bool> p = rep.insert_unique(obj);
  142. return pair<iterator, bool>(p.first, p.second);
  143. }
  144. #ifdef __STL_MEMBER_TEMPLATES
  145. template <class InputIterator>
  146. void insert(InputIterator f, InputIterator l) { rep.insert_unique(f,l); }
  147. #else
  148. void insert(const value_type* f, const value_type* l) {
  149. rep.insert_unique(f,l);
  150. }
  151. void insert(const_iterator f, const_iterator l) {rep.insert_unique(f, l); }
  152. #endif /*__STL_MEMBER_TEMPLATES */
  153. pair<iterator, bool> insert_noresize(const value_type& obj)
  154. {
  155. pair<typename ht::iterator, bool> p = rep.insert_unique_noresize(obj);
  156. return pair<iterator, bool>(p.first, p.second);
  157. }
  158.  
  159. iterator find(const key_type& key) const { return rep.find(key); }
  160.  
  161. size_type count(const key_type& key) const { return rep.count(key); }
  162.  
  163. pair<iterator, iterator> equal_range(const key_type& key) const
  164. { return rep.equal_range(key); }
  165.  
  166. size_type erase(const key_type& key) {return rep.erase(key); }
  167. void erase(iterator it) { rep.erase(it); }
  168. void erase(iterator f, iterator l) { rep.erase(f, l); }
  169. void clear() { rep.clear(); }
  170.  
  171. public:
  172. void resize(size_type hint) { rep.resize(hint); }
  173. size_type bucket_count() const { return rep.bucket_count(); }
  174. size_type max_bucket_count() const { return rep.max_bucket_count(); }
  175. size_type elems_in_bucket(size_type n) const
  176. { return rep.elems_in_bucket(n); }
  177. };
  178.  
  179. template <class Value, class HashFcn, class EqualKey, class Alloc>
  180. inline bool operator==(const hash_set<Value, HashFcn, EqualKey, Alloc>& hs1,
  181. const hash_set<Value, HashFcn, EqualKey, Alloc>& hs2)
  182. {
  183. return hs1.rep == hs2.rep;
  184. }
  185.  
  186. #ifdef __STL_FUNCTION_TMPL_PARTIAL_ORDER
  187.  
  188. template <class Val, class HashFcn, class EqualKey, class Alloc>
  189. inline void swap(hash_set<Val, HashFcn, EqualKey, Alloc>& hs1,
  190. hash_set<Val, HashFcn, EqualKey, Alloc>& hs2) {
  191. hs1.swap(hs2);
  192. }
  193.  
  194. #endif /* __STL_FUNCTION_TMPL_PARTIAL_ORDER */
  195.  
  196. //hash_multiset同意键值反复,其它与hash_set一样
  197. #ifndef __STL_LIMITED_DEFAULT_TEMPLATES
  198. template <class Value, class HashFcn = hash<Value>,
  199. class EqualKey = equal_to<Value>,
  200. class Alloc = alloc>
  201. #else
  202. template <class Value, class HashFcn, class EqualKey, class Alloc = alloc>
  203. #endif
  204. class hash_multiset
  205. {
  206. private:
  207. typedef hashtable<Value, Value, HashFcn, identity<Value>,
  208. EqualKey, Alloc> ht;
  209. ht rep;
  210.  
  211. public:
  212. typedef typename ht::key_type key_type;
  213. typedef typename ht::value_type value_type;
  214. typedef typename ht::hasher hasher;
  215. typedef typename ht::key_equal key_equal;
  216.  
  217. typedef typename ht::size_type size_type;
  218. typedef typename ht::difference_type difference_type;
  219. typedef typename ht::const_pointer pointer;
  220. typedef typename ht::const_pointer const_pointer;
  221. typedef typename ht::const_reference reference;
  222. typedef typename ht::const_reference const_reference;
  223.  
  224. typedef typename ht::const_iterator iterator;
  225. typedef typename ht::const_iterator const_iterator;
  226.  
  227. hasher hash_funct() const { return rep.hash_funct(); }
  228. key_equal key_eq() const { return rep.key_eq(); }
  229.  
  230. public:
  231. hash_multiset() : rep(100, hasher(), key_equal()) {}
  232. explicit hash_multiset(size_type n) : rep(n, hasher(), key_equal()) {}
  233. hash_multiset(size_type n, const hasher& hf) : rep(n, hf, key_equal()) {}
  234. hash_multiset(size_type n, const hasher& hf, const key_equal& eql)
  235. : rep(n, hf, eql) {}
  236.  
  237. // 下面,插入所有使用 insert_equal(),同意键值反复
  238. #ifdef __STL_MEMBER_TEMPLATES
  239. template <class InputIterator>
  240. hash_multiset(InputIterator f, InputIterator l)
  241. : rep(100, hasher(), key_equal()) { rep.insert_equal(f, l); }
  242. template <class InputIterator>
  243. hash_multiset(InputIterator f, InputIterator l, size_type n)
  244. : rep(n, hasher(), key_equal()) { rep.insert_equal(f, l); }
  245. template <class InputIterator>
  246. hash_multiset(InputIterator f, InputIterator l, size_type n,
  247. const hasher& hf)
  248. : rep(n, hf, key_equal()) { rep.insert_equal(f, l); }
  249. template <class InputIterator>
  250. hash_multiset(InputIterator f, InputIterator l, size_type n,
  251. const hasher& hf, const key_equal& eql)
  252. : rep(n, hf, eql) { rep.insert_equal(f, l); }
  253. #else
  254.  
  255. hash_multiset(const value_type* f, const value_type* l)
  256. : rep(100, hasher(), key_equal()) { rep.insert_equal(f, l); }
  257. hash_multiset(const value_type* f, const value_type* l, size_type n)
  258. : rep(n, hasher(), key_equal()) { rep.insert_equal(f, l); }
  259. hash_multiset(const value_type* f, const value_type* l, size_type n,
  260. const hasher& hf)
  261. : rep(n, hf, key_equal()) { rep.insert_equal(f, l); }
  262. hash_multiset(const value_type* f, const value_type* l, size_type n,
  263. const hasher& hf, const key_equal& eql)
  264. : rep(n, hf, eql) { rep.insert_equal(f, l); }
  265.  
  266. hash_multiset(const_iterator f, const_iterator l)
  267. : rep(100, hasher(), key_equal()) { rep.insert_equal(f, l); }
  268. hash_multiset(const_iterator f, const_iterator l, size_type n)
  269. : rep(n, hasher(), key_equal()) { rep.insert_equal(f, l); }
  270. hash_multiset(const_iterator f, const_iterator l, size_type n,
  271. const hasher& hf)
  272. : rep(n, hf, key_equal()) { rep.insert_equal(f, l); }
  273. hash_multiset(const_iterator f, const_iterator l, size_type n,
  274. const hasher& hf, const key_equal& eql)
  275. : rep(n, hf, eql) { rep.insert_equal(f, l); }
  276. #endif /*__STL_MEMBER_TEMPLATES */
  277.  
  278. public:
  279. size_type size() const { return rep.size(); }
  280. size_type max_size() const { return rep.max_size(); }
  281. bool empty() const { return rep.empty(); }
  282. void swap(hash_multiset& hs) { rep.swap(hs.rep); }
  283. friend bool operator== __STL_NULL_TMPL_ARGS (const hash_multiset&,
  284. const hash_multiset&);
  285.  
  286. iterator begin() const { return rep.begin(); }
  287. iterator end() const { return rep.end(); }
  288.  
  289. public:
  290. iterator insert(const value_type& obj) { return rep.insert_equal(obj); }
  291. #ifdef __STL_MEMBER_TEMPLATES
  292. template <class InputIterator>
  293. void insert(InputIterator f, InputIterator l) { rep.insert_equal(f,l); }
  294. #else
  295. void insert(const value_type* f, const value_type* l) {
  296. rep.insert_equal(f,l);
  297. }
  298. void insert(const_iterator f, const_iterator l) { rep.insert_equal(f, l); }
  299. #endif /*__STL_MEMBER_TEMPLATES */
  300. iterator insert_noresize(const value_type& obj)
  301. { return rep.insert_equal_noresize(obj); }
  302.  
  303. iterator find(const key_type& key) const { return rep.find(key); }
  304.  
  305. size_type count(const key_type& key) const { return rep.count(key); }
  306.  
  307. pair<iterator, iterator> equal_range(const key_type& key) const
  308. { return rep.equal_range(key); }
  309.  
  310. size_type erase(const key_type& key) {return rep.erase(key); }
  311. void erase(iterator it) { rep.erase(it); }
  312. void erase(iterator f, iterator l) { rep.erase(f, l); }
  313. void clear() { rep.clear(); }
  314.  
  315. public:
  316. void resize(size_type hint) { rep.resize(hint); }
  317. size_type bucket_count() const { return rep.bucket_count(); }
  318. size_type max_bucket_count() const { return rep.max_bucket_count(); }
  319. size_type elems_in_bucket(size_type n) const
  320. { return rep.elems_in_bucket(n); }
  321. };
  322.  
  323. template <class Val, class HashFcn, class EqualKey, class Alloc>
  324. inline bool operator==(const hash_multiset<Val, HashFcn, EqualKey, Alloc>& hs1,
  325. const hash_multiset<Val, HashFcn, EqualKey, Alloc>& hs2)
  326. {
  327. return hs1.rep == hs2.rep;
  328. }
  329.  
  330. #ifdef __STL_FUNCTION_TMPL_PARTIAL_ORDER
  331.  
  332. template <class Val, class HashFcn, class EqualKey, class Alloc>
  333. inline void swap(hash_multiset<Val, HashFcn, EqualKey, Alloc>& hs1,
  334. hash_multiset<Val, HashFcn, EqualKey, Alloc>& hs2)
  335. {
  336. hs1.swap(hs2);
  337. }
  338.  
  339. #endif /* __STL_FUNCTION_TMPL_PARTIAL_ORDER */
  340.  
  341. #if defined(__sgi) && !defined(__GNUC__) && (_MIPS_SIM != _MIPS_SIM_ABI32)
  342. #pragma reset woff 1174
  343. #endif
  344.  
  345. __STL_END_NAMESPACE
  346.  
  347. #endif /* __SGI_STL_INTERNAL_HASH_SET_H */
  348.  
  349. // Local Variables:
  350. // mode:C++
  351. // End:

《STL源代码剖析》---stl_hash_set.h阅读笔记的更多相关文章

  1. 《STL源代码剖析》---stl_deque.h阅读笔记(2)

    看完,<STL源代码剖析>---stl_deque.h阅读笔记(1)后.再看代码: G++ 2.91.57,cygnus\cygwin-b20\include\g++\stl_deque. ...

  2. STL源代码剖析——STL算法stl_algo.h

    前言 在前面的博文中剖析了STL的数值算法.基本算法和set集合算法.本文剖析STL其它的算法,比如排序算法.合并算法.查找算法等等.在剖析的时候.会针对函数给出一些样例说明函数的使用.源代码出自SG ...

  3. STL源代码剖析——基本算法stl_algobase.h

    前言 在STL中.算法是常常被使用的,算法在整个STL中起到很关键的数据.本节介绍的是一些基本算法,包括equal.fill.fill_n,iter_swap.lexicographical_comp ...

  4. STL源代码剖析 读书总结

    <<STL源代码剖析>> 侯捷著 非常早就买了这本书, 一直没看, 如今在实验室师兄代码的时候发现里面使用了大量泛型编程的内容, 让我有了先看看这本书的想法. 看之前我对于泛型 ...

  5. STL源代码剖析(一) - 内存分配

    Allocaor allocator 指的是空间配置器,用于分配内存.STL中默认使用SGI STL alloc作为STL的内存分配器,尽管未能符合标准规格,但效率上更好.SGI STL也定义有一个符 ...

  6. STL源代码剖析——STL算法之set集合算法

    前言 本节介绍set集合的相关算法,各自是并集set_union,差集set_difference,交集set_intersection 和对称差集set_symmetric_difference.这 ...

  7. STL源代码剖析(二) - 迭代器与traits技法

    提要 先看一段用迭代器的代码: int a[] = {1, 2, 3, 4, 5}; vector<int> v1( a, a+5); vector<int>::iterato ...

  8. STL源代码剖析 容器 stl_map.h

    本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie map ------------------------------------------ ...

  9. 《STL源代码剖析》---stl_alloc.h阅读笔记

    这一节是讲空间的配置与释放,但不涉及对象的构造和析构,仅仅是解说对象构造前空前的申请以及对象析构后空间怎么释放. SGI版本号的STL对空间的的申请和释放做了例如以下考虑: 1.向堆申请空间 2.考虑 ...

随机推荐

  1. 由Java代码运行shell命令/脚本

    JDK有两种方法自带通Runtime.getRuntime().exec()和ProcessBuilder课上做, 后者是JDK1.5引进后,,政府还提出要放弃使用Runtime顺便做.现的时候就是採 ...

  2. WP8加入自己定义铃声

    1.下载千千静听绿色版,下载地址:http://pan.baidu.com/s/1ntuSNOH 2.将目标铃声加入到千千静听,选中铃声,右键选文件属性. 3.改动文件属性,将流派改为"Ri ...

  3. [模拟] hdu 4452 Running Rabbits

    意甲冠军: 两个人在一个人(1,1),一个人(N,N) 要人人搬家每秒的速度v.而一个s代表移动s左转方向秒 特别值得注意的是假设壁,反弹.改变方向 例如,在(1,1),采取的一个步骤,以左(1,0) ...

  4. Oracle经常使用函数

    Oracle经常使用函数 --TRUNC,TO_DATE,TO_CHAR,TO_NUMBER, SUBSTR,REPLACE.NVL .TRIM,wm_concat,upper, lower,leng ...

  5. 我异常-VS2012“System.AccessViolationException: 试图读取或写入保护内存。”

       我的系统是win7 64位 系统 安装的是 Visual studio 2012.从SVN上下载了源代码.一个B/S的系统.能正常载入登陆页,但在输入登录信息要登录的时候,就报错了:     - ...

  6. opengl 扳回一球

    本文介绍了使用两种方法opengl画一个球体,一个是一个球形点位置计算,然后绘制出,还有一个glut套件自带功能. 一.直接绘制法 直接贴代码,解释都写在凝视里了.绘制时先移动好坐标系.然后调用这方法 ...

  7. react.js 从零开始(二)组件的生命周期

    什么是生命周期? 组件本质上是一个状态机,输入确定,输出一定确定. 当状态改变的时候 会触发不同的钩子函数,可以让开发者做出响应.. 一个组件的生命周期可以概括为 初始化:状态下 可以自定义的函数 g ...

  8. JavaEE(4) - JMS实现企业PTP消息处理

    1. 在Weblogic服务器上配置PTP消息目的 配置持久化: Services-->Persistence Stores-->New(Create FileStore, Create ...

  9. cocos2d0基础知识三个音符

    1.触摸屏事件: bool HelloWorld::init() { //省略的代码的最后位 this->schedule(schedule_selector(HelloWorld::usecr ...

  10. matlab配置Libsvm 防止备忘录

    1 首先我们要下载一个Libsvm 工具箱 其中,这一切都可以被下载到 2 我们解包 我解压在桌面上 住址C:\Users\Administrator\Desktop\libsvm 3打开matlab ...