《STL源代码剖析》---stl_hash_set.h阅读笔记
STL仅仅规定接口和复杂度,对于详细实现不作要求。set大多以红黑树实现,但STL在标准规格之外提供了一个所谓的hash_set,以hash table实现。hash_set的接口,hash_table都提供了,所以差点儿全部的hash_set操作都是直接调用hash_table的函数而已。
除了hash_set,还有hash_multiset,它们两个的关系就像set和multiset的关系,一个不同意键值反复,另外一个同意键值反复。其它实现一样。
- G++ 2.91.57,cygnus\cygwin-b20\include\g++\stl_hash_set.h 完整列表
- /*
- * Copyright (c) 1996
- * Silicon Graphics Computer Systems, Inc.
- *
- * Permission to use, copy, modify, distribute and sell this software
- * and its documentation for any purpose is hereby granted without fee,
- * provided that the above copyright notice appear in all copies and
- * that both that copyright notice and this permission notice appear
- * in supporting documentation. Silicon Graphics makes no
- * representations about the suitability of this software for any
- * purpose. It is provided "as is" without express or implied warranty.
- *
- *
- * Copyright (c) 1994
- * Hewlett-Packard Company
- *
- * Permission to use, copy, modify, distribute and sell this software
- * and its documentation for any purpose is hereby granted without fee,
- * provided that the above copyright notice appear in all copies and
- * that both that copyright notice and this permission notice appear
- * in supporting documentation. Hewlett-Packard Company makes no
- * representations about the suitability of this software for any
- * purpose. It is provided "as is" without express or implied warranty.
- *
- */
- /* NOTE: This is an internal header file, included by other STL headers.
- * You should not attempt to use it directly.
- */
- #ifndef __SGI_STL_INTERNAL_HASH_SET_H
- #define __SGI_STL_INTERNAL_HASH_SET_H
- __STL_BEGIN_NAMESPACE
- #if defined(__sgi) && !defined(__GNUC__) && (_MIPS_SIM != _MIPS_SIM_ABI32)
- #pragma set woff 1174
- #endif
- #ifndef __STL_LIMITED_DEFAULT_TEMPLATES
- // hash<>是个 function object,定义于 <stl_hash_fun.h>中
- //例:hash<int>::operator()(intx)const{return x;}
- template <class Value, class HashFcn = hash<Value>,
- class EqualKey = equal_to<Value>,
- class Alloc = alloc>
- #else
- template <class Value, class HashFcn, class EqualKey, class Alloc = alloc>
- #endif
- class hash_set
- {
- private:
- // 下面使用的 identity<> 定义于 <stl_function.h> 中。
- typedef hashtable<Value, Value, HashFcn, identity<Value>,
- EqualKey, Alloc> ht;
- ht rep; // 底层机制以 hash table 完毕
- public:
- typedef typename ht::key_type key_type;
- typedef typename ht::value_type value_type;
- typedef typename ht::hasher hasher;
- typedef typename ht::key_equal key_equal;
- typedef typename ht::size_type size_type;
- typedef typename ht::difference_type difference_type;
- typedef typename ht::const_pointer pointer;
- typedef typename ht::const_pointer const_pointer;
- typedef typename ht::const_reference reference;
- typedef typename ht::const_reference const_reference;
- typedef typename ht::const_iterator iterator;
- typedef typename ht::const_iterator const_iterator;
- hasher hash_funct() const { return rep.hash_funct(); }
- key_equal key_eq() const { return rep.key_eq(); }
- public:
- //预设使用大小为100的vector buckets。将会自己主动调整为比100大的质数
- hash_set() : rep(100, hasher(), key_equal()) {}
- explicit hash_set(size_type n) : rep(n, hasher(), key_equal()) {}
- hash_set(size_type n, const hasher& hf) : rep(n, hf, key_equal()) {}
- hash_set(size_type n, const hasher& hf, const key_equal& eql)
- : rep(n, hf, eql) {}
- // 插入所有调用 insert_unique(),不同意键值反复
- #ifdef __STL_MEMBER_TEMPLATES
- template <class InputIterator>
- hash_set(InputIterator f, InputIterator l)
- : rep(100, hasher(), key_equal()) { rep.insert_unique(f, l); }
- template <class InputIterator>
- hash_set(InputIterator f, InputIterator l, size_type n)
- : rep(n, hasher(), key_equal()) { rep.insert_unique(f, l); }
- template <class InputIterator>
- hash_set(InputIterator f, InputIterator l, size_type n,
- const hasher& hf)
- : rep(n, hf, key_equal()) { rep.insert_unique(f, l); }
- template <class InputIterator>
- hash_set(InputIterator f, InputIterator l, size_type n,
- const hasher& hf, const key_equal& eql)
- : rep(n, hf, eql) { rep.insert_unique(f, l); }
- #else
- hash_set(const value_type* f, const value_type* l)
- : rep(100, hasher(), key_equal()) { rep.insert_unique(f, l); }
- hash_set(const value_type* f, const value_type* l, size_type n)
- : rep(n, hasher(), key_equal()) { rep.insert_unique(f, l); }
- hash_set(const value_type* f, const value_type* l, size_type n,
- const hasher& hf)
- : rep(n, hf, key_equal()) { rep.insert_unique(f, l); }
- hash_set(const value_type* f, const value_type* l, size_type n,
- const hasher& hf, const key_equal& eql)
- : rep(n, hf, eql) { rep.insert_unique(f, l); }
- hash_set(const_iterator f, const_iterator l)
- : rep(100, hasher(), key_equal()) { rep.insert_unique(f, l); }
- hash_set(const_iterator f, const_iterator l, size_type n)
- : rep(n, hasher(), key_equal()) { rep.insert_unique(f, l); }
- hash_set(const_iterator f, const_iterator l, size_type n,
- const hasher& hf)
- : rep(n, hf, key_equal()) { rep.insert_unique(f, l); }
- hash_set(const_iterator f, const_iterator l, size_type n,
- const hasher& hf, const key_equal& eql)
- : rep(n, hf, eql) { rep.insert_unique(f, l); }
- #endif /*__STL_MEMBER_TEMPLATES */
- public:
- // 所有操作差点儿都是调用 hash table的函数。
- size_type size() const { return rep.size(); }
- size_type max_size() const { return rep.max_size(); }
- bool empty() const { return rep.empty(); }
- void swap(hash_set& hs) { rep.swap(hs.rep); }
- friend bool operator== __STL_NULL_TMPL_ARGS (const hash_set&,
- const hash_set&);
- iterator begin() const { return rep.begin(); }
- iterator end() const { return rep.end(); }
- public:
- pair<iterator, bool> insert(const value_type& obj)
- {
- pair<typename ht::iterator, bool> p = rep.insert_unique(obj);
- return pair<iterator, bool>(p.first, p.second);
- }
- #ifdef __STL_MEMBER_TEMPLATES
- template <class InputIterator>
- void insert(InputIterator f, InputIterator l) { rep.insert_unique(f,l); }
- #else
- void insert(const value_type* f, const value_type* l) {
- rep.insert_unique(f,l);
- }
- void insert(const_iterator f, const_iterator l) {rep.insert_unique(f, l); }
- #endif /*__STL_MEMBER_TEMPLATES */
- pair<iterator, bool> insert_noresize(const value_type& obj)
- {
- pair<typename ht::iterator, bool> p = rep.insert_unique_noresize(obj);
- return pair<iterator, bool>(p.first, p.second);
- }
- iterator find(const key_type& key) const { return rep.find(key); }
- size_type count(const key_type& key) const { return rep.count(key); }
- pair<iterator, iterator> equal_range(const key_type& key) const
- { return rep.equal_range(key); }
- size_type erase(const key_type& key) {return rep.erase(key); }
- void erase(iterator it) { rep.erase(it); }
- void erase(iterator f, iterator l) { rep.erase(f, l); }
- void clear() { rep.clear(); }
- public:
- void resize(size_type hint) { rep.resize(hint); }
- size_type bucket_count() const { return rep.bucket_count(); }
- size_type max_bucket_count() const { return rep.max_bucket_count(); }
- size_type elems_in_bucket(size_type n) const
- { return rep.elems_in_bucket(n); }
- };
- template <class Value, class HashFcn, class EqualKey, class Alloc>
- inline bool operator==(const hash_set<Value, HashFcn, EqualKey, Alloc>& hs1,
- const hash_set<Value, HashFcn, EqualKey, Alloc>& hs2)
- {
- return hs1.rep == hs2.rep;
- }
- #ifdef __STL_FUNCTION_TMPL_PARTIAL_ORDER
- template <class Val, class HashFcn, class EqualKey, class Alloc>
- inline void swap(hash_set<Val, HashFcn, EqualKey, Alloc>& hs1,
- hash_set<Val, HashFcn, EqualKey, Alloc>& hs2) {
- hs1.swap(hs2);
- }
- #endif /* __STL_FUNCTION_TMPL_PARTIAL_ORDER */
- //hash_multiset同意键值反复,其它与hash_set一样
- #ifndef __STL_LIMITED_DEFAULT_TEMPLATES
- template <class Value, class HashFcn = hash<Value>,
- class EqualKey = equal_to<Value>,
- class Alloc = alloc>
- #else
- template <class Value, class HashFcn, class EqualKey, class Alloc = alloc>
- #endif
- class hash_multiset
- {
- private:
- typedef hashtable<Value, Value, HashFcn, identity<Value>,
- EqualKey, Alloc> ht;
- ht rep;
- public:
- typedef typename ht::key_type key_type;
- typedef typename ht::value_type value_type;
- typedef typename ht::hasher hasher;
- typedef typename ht::key_equal key_equal;
- typedef typename ht::size_type size_type;
- typedef typename ht::difference_type difference_type;
- typedef typename ht::const_pointer pointer;
- typedef typename ht::const_pointer const_pointer;
- typedef typename ht::const_reference reference;
- typedef typename ht::const_reference const_reference;
- typedef typename ht::const_iterator iterator;
- typedef typename ht::const_iterator const_iterator;
- hasher hash_funct() const { return rep.hash_funct(); }
- key_equal key_eq() const { return rep.key_eq(); }
- public:
- hash_multiset() : rep(100, hasher(), key_equal()) {}
- explicit hash_multiset(size_type n) : rep(n, hasher(), key_equal()) {}
- hash_multiset(size_type n, const hasher& hf) : rep(n, hf, key_equal()) {}
- hash_multiset(size_type n, const hasher& hf, const key_equal& eql)
- : rep(n, hf, eql) {}
- // 下面,插入所有使用 insert_equal(),同意键值反复
- #ifdef __STL_MEMBER_TEMPLATES
- template <class InputIterator>
- hash_multiset(InputIterator f, InputIterator l)
- : rep(100, hasher(), key_equal()) { rep.insert_equal(f, l); }
- template <class InputIterator>
- hash_multiset(InputIterator f, InputIterator l, size_type n)
- : rep(n, hasher(), key_equal()) { rep.insert_equal(f, l); }
- template <class InputIterator>
- hash_multiset(InputIterator f, InputIterator l, size_type n,
- const hasher& hf)
- : rep(n, hf, key_equal()) { rep.insert_equal(f, l); }
- template <class InputIterator>
- hash_multiset(InputIterator f, InputIterator l, size_type n,
- const hasher& hf, const key_equal& eql)
- : rep(n, hf, eql) { rep.insert_equal(f, l); }
- #else
- hash_multiset(const value_type* f, const value_type* l)
- : rep(100, hasher(), key_equal()) { rep.insert_equal(f, l); }
- hash_multiset(const value_type* f, const value_type* l, size_type n)
- : rep(n, hasher(), key_equal()) { rep.insert_equal(f, l); }
- hash_multiset(const value_type* f, const value_type* l, size_type n,
- const hasher& hf)
- : rep(n, hf, key_equal()) { rep.insert_equal(f, l); }
- hash_multiset(const value_type* f, const value_type* l, size_type n,
- const hasher& hf, const key_equal& eql)
- : rep(n, hf, eql) { rep.insert_equal(f, l); }
- hash_multiset(const_iterator f, const_iterator l)
- : rep(100, hasher(), key_equal()) { rep.insert_equal(f, l); }
- hash_multiset(const_iterator f, const_iterator l, size_type n)
- : rep(n, hasher(), key_equal()) { rep.insert_equal(f, l); }
- hash_multiset(const_iterator f, const_iterator l, size_type n,
- const hasher& hf)
- : rep(n, hf, key_equal()) { rep.insert_equal(f, l); }
- hash_multiset(const_iterator f, const_iterator l, size_type n,
- const hasher& hf, const key_equal& eql)
- : rep(n, hf, eql) { rep.insert_equal(f, l); }
- #endif /*__STL_MEMBER_TEMPLATES */
- public:
- size_type size() const { return rep.size(); }
- size_type max_size() const { return rep.max_size(); }
- bool empty() const { return rep.empty(); }
- void swap(hash_multiset& hs) { rep.swap(hs.rep); }
- friend bool operator== __STL_NULL_TMPL_ARGS (const hash_multiset&,
- const hash_multiset&);
- iterator begin() const { return rep.begin(); }
- iterator end() const { return rep.end(); }
- public:
- iterator insert(const value_type& obj) { return rep.insert_equal(obj); }
- #ifdef __STL_MEMBER_TEMPLATES
- template <class InputIterator>
- void insert(InputIterator f, InputIterator l) { rep.insert_equal(f,l); }
- #else
- void insert(const value_type* f, const value_type* l) {
- rep.insert_equal(f,l);
- }
- void insert(const_iterator f, const_iterator l) { rep.insert_equal(f, l); }
- #endif /*__STL_MEMBER_TEMPLATES */
- iterator insert_noresize(const value_type& obj)
- { return rep.insert_equal_noresize(obj); }
- iterator find(const key_type& key) const { return rep.find(key); }
- size_type count(const key_type& key) const { return rep.count(key); }
- pair<iterator, iterator> equal_range(const key_type& key) const
- { return rep.equal_range(key); }
- size_type erase(const key_type& key) {return rep.erase(key); }
- void erase(iterator it) { rep.erase(it); }
- void erase(iterator f, iterator l) { rep.erase(f, l); }
- void clear() { rep.clear(); }
- public:
- void resize(size_type hint) { rep.resize(hint); }
- size_type bucket_count() const { return rep.bucket_count(); }
- size_type max_bucket_count() const { return rep.max_bucket_count(); }
- size_type elems_in_bucket(size_type n) const
- { return rep.elems_in_bucket(n); }
- };
- template <class Val, class HashFcn, class EqualKey, class Alloc>
- inline bool operator==(const hash_multiset<Val, HashFcn, EqualKey, Alloc>& hs1,
- const hash_multiset<Val, HashFcn, EqualKey, Alloc>& hs2)
- {
- return hs1.rep == hs2.rep;
- }
- #ifdef __STL_FUNCTION_TMPL_PARTIAL_ORDER
- template <class Val, class HashFcn, class EqualKey, class Alloc>
- inline void swap(hash_multiset<Val, HashFcn, EqualKey, Alloc>& hs1,
- hash_multiset<Val, HashFcn, EqualKey, Alloc>& hs2)
- {
- hs1.swap(hs2);
- }
- #endif /* __STL_FUNCTION_TMPL_PARTIAL_ORDER */
- #if defined(__sgi) && !defined(__GNUC__) && (_MIPS_SIM != _MIPS_SIM_ABI32)
- #pragma reset woff 1174
- #endif
- __STL_END_NAMESPACE
- #endif /* __SGI_STL_INTERNAL_HASH_SET_H */
- // Local Variables:
- // mode:C++
- // End:
《STL源代码剖析》---stl_hash_set.h阅读笔记的更多相关文章
- 《STL源代码剖析》---stl_deque.h阅读笔记(2)
看完,<STL源代码剖析>---stl_deque.h阅读笔记(1)后.再看代码: G++ 2.91.57,cygnus\cygwin-b20\include\g++\stl_deque. ...
- STL源代码剖析——STL算法stl_algo.h
前言 在前面的博文中剖析了STL的数值算法.基本算法和set集合算法.本文剖析STL其它的算法,比如排序算法.合并算法.查找算法等等.在剖析的时候.会针对函数给出一些样例说明函数的使用.源代码出自SG ...
- STL源代码剖析——基本算法stl_algobase.h
前言 在STL中.算法是常常被使用的,算法在整个STL中起到很关键的数据.本节介绍的是一些基本算法,包括equal.fill.fill_n,iter_swap.lexicographical_comp ...
- STL源代码剖析 读书总结
<<STL源代码剖析>> 侯捷著 非常早就买了这本书, 一直没看, 如今在实验室师兄代码的时候发现里面使用了大量泛型编程的内容, 让我有了先看看这本书的想法. 看之前我对于泛型 ...
- STL源代码剖析(一) - 内存分配
Allocaor allocator 指的是空间配置器,用于分配内存.STL中默认使用SGI STL alloc作为STL的内存分配器,尽管未能符合标准规格,但效率上更好.SGI STL也定义有一个符 ...
- STL源代码剖析——STL算法之set集合算法
前言 本节介绍set集合的相关算法,各自是并集set_union,差集set_difference,交集set_intersection 和对称差集set_symmetric_difference.这 ...
- STL源代码剖析(二) - 迭代器与traits技法
提要 先看一段用迭代器的代码: int a[] = {1, 2, 3, 4, 5}; vector<int> v1( a, a+5); vector<int>::iterato ...
- STL源代码剖析 容器 stl_map.h
本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie map ------------------------------------------ ...
- 《STL源代码剖析》---stl_alloc.h阅读笔记
这一节是讲空间的配置与释放,但不涉及对象的构造和析构,仅仅是解说对象构造前空前的申请以及对象析构后空间怎么释放. SGI版本号的STL对空间的的申请和释放做了例如以下考虑: 1.向堆申请空间 2.考虑 ...
随机推荐
- 由Java代码运行shell命令/脚本
JDK有两种方法自带通Runtime.getRuntime().exec()和ProcessBuilder课上做, 后者是JDK1.5引进后,,政府还提出要放弃使用Runtime顺便做.现的时候就是採 ...
- WP8加入自己定义铃声
1.下载千千静听绿色版,下载地址:http://pan.baidu.com/s/1ntuSNOH 2.将目标铃声加入到千千静听,选中铃声,右键选文件属性. 3.改动文件属性,将流派改为"Ri ...
- [模拟] hdu 4452 Running Rabbits
意甲冠军: 两个人在一个人(1,1),一个人(N,N) 要人人搬家每秒的速度v.而一个s代表移动s左转方向秒 特别值得注意的是假设壁,反弹.改变方向 例如,在(1,1),采取的一个步骤,以左(1,0) ...
- Oracle经常使用函数
Oracle经常使用函数 --TRUNC,TO_DATE,TO_CHAR,TO_NUMBER, SUBSTR,REPLACE.NVL .TRIM,wm_concat,upper, lower,leng ...
- 我异常-VS2012“System.AccessViolationException: 试图读取或写入保护内存。”
我的系统是win7 64位 系统 安装的是 Visual studio 2012.从SVN上下载了源代码.一个B/S的系统.能正常载入登陆页,但在输入登录信息要登录的时候,就报错了: - ...
- opengl
扳回一球
本文介绍了使用两种方法opengl画一个球体,一个是一个球形点位置计算,然后绘制出,还有一个glut套件自带功能. 一.直接绘制法 直接贴代码,解释都写在凝视里了.绘制时先移动好坐标系.然后调用这方法 ...
- react.js 从零开始(二)组件的生命周期
什么是生命周期? 组件本质上是一个状态机,输入确定,输出一定确定. 当状态改变的时候 会触发不同的钩子函数,可以让开发者做出响应.. 一个组件的生命周期可以概括为 初始化:状态下 可以自定义的函数 g ...
- JavaEE(4) - JMS实现企业PTP消息处理
1. 在Weblogic服务器上配置PTP消息目的 配置持久化: Services-->Persistence Stores-->New(Create FileStore, Create ...
- cocos2d0基础知识三个音符
1.触摸屏事件: bool HelloWorld::init() { //省略的代码的最后位 this->schedule(schedule_selector(HelloWorld::usecr ...
- matlab配置Libsvm 防止备忘录
1 首先我们要下载一个Libsvm 工具箱 其中,这一切都可以被下载到 2 我们解包 我解压在桌面上 住址C:\Users\Administrator\Desktop\libsvm 3打开matlab ...