STL关联式容器之hash_set、hash_map、hash_multiset、hash_multimap

目录

hash_set

hash_map

hash_multiset

hash_multimap


hash_set

        虽然STL只规范复杂度与接口,并不规范实现方法,但STL set多半以RB-tree为底层机制。SGI则是在STL标准规格之外另又提供了一个所谓的hash_set,以hashtable为底层机制。由于hash_set所提供的操作接口,hashtable都提供了,所以几乎所有的hash_set操作行为,都只是转调用hashtable的操作行为而已。

        运用set,为的是能够快速搜寻元素。这一点,不论底层是RB-tree或是hashtable,都可以达成任务。但是请注意,RB-tree有自动排序功能而hashtable没有,反应出来的结果就是,set的元素有自动排序功能而hash_set没有。

        set的元素不像map那样可以同时拥有实值(value)和键值(key),set元素的键值也是实值,实值就是键值。这一点hash_set也是一样的。

        hash_set的使用方式,与set完全相同。

        下面是hash_set的源代码摘录,其中的注释几乎说明了接口的调用方式。

        请注意,STL关联式容器之hashtable-CSDN博客 提到,hashtable有一些无法处理的型别(除非用户为那些型别撰写 hash function),凡是hashtable无法处理的,hash_set也无法处理。

template<class Value, 
         class HashFcn = hash<Value>,
         class EqualKey = equal_to<Value>,
         class Alloc = alloc>
class hash_set {
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_set(): rep(10, hasher(), key_equal()) {}
    explicit hash_set(size_type n) : rep(n, hashes(), 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) {}
    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);
    }
    
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_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 = p.insert_unique(obj);
        return pair<iterator, bool>(p.first, p.second);
    }

    template<class InputIterator>
    void insert(InputIterator f, InputIterator l) {
        rep.insert_unique(f, l);
    }

    pair<iterator, bool> insert_noresize(const value& 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) {
        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 elms_in_bucket(size_type n) {
        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;
}

hash_set的使用实例这里就不一一列举了;读者可以自行测试验证。

hash_map

        SGI在STL标准规格之外,另提供了一个所谓的hash_map,以hash_table为底层机制。由于hash_map所供应的操作接口,hashtable都提供了,所以几乎所有的hash_map的操作行为,都只是转调用了hashtable的操作行为而已。

        运用map,为的是能够根据键值快速搜寻元素。这一点,不论其底层是RB-tree或是hashtable,都可以达成任务。但是请注意,RB-tree有自动排序功能而hashtable没有,反应出来的结果就是,map的元素有自动排序功能而hashmap没有。

        map的特性是,没有个元素同时拥有实值(value)和一个键值(key)。这一点再hash_map是一样的。hash_map和map的使用方式完全相同。

        下面是hash_map的源代码摘录;源码几乎能够自解析

template  <class Key,
           class T,
           class HashFcn = Hash<Key>,
           class EqualKey = equal_to<Key>,
           class Allocl = alloc>
class hash_map {
private:
    typedef hashtable<pair<const Key, T>, Key, HashFcn, select1st<pair<const Key, T>>, EqualKey, Alloc> ht;
    ht rep;

public:
    typedef typename ht::key_type key_type;
    typedef T data_type;
    typedef T mapped_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::pointer pointer;
    typedef typename ht::const_pointer const_pointer;
    typedef typename ht::reference reference;
    typedef typename ht::const_reference const_reference;

    typedef typename ht::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_map() : rep(100, hasher(), key_qual()) {}
    explicit hash_map(size_type n)  : rep(n, hasher(), key_qual()) {}
    
    hash_map(size_type n, const hasher&hf)  : rep(n, hf, key_qual()) {}
    hash_map(size_type n, const hasher&hf, const key_equal& eql)  : rep(n, hf, eql) {}

template <class InputIterator> 
    hash_map(InputIterator f, InputIterator l) : rep(100, hasher(), key_equal()) {
        rep.insert_unique(f, l);
    }

    template <class InputIterator> 
    hash_map(InputIterator f, InputIterator l, size_type n) : rep(n, hasher(), key_equal()) {
        rep.insert_unique(f, l);
    }

    template <class InputIterator> 
    hash_map(InputIterator f, InputIterator l, size_type n,const hasher& hf ) : rep(n, hf, key_equal()) {
        rep.insert_unique(f, l);
    }

    template <class InputIterator> 
    hash_map(InputIterator f, InputIterator l, size_type n,const hasher& hf , const key_equal& eql) : rep(n, hf, eql) {
        rep.insert_unique(f, l);
    }

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_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 = p.insert_unique(obj);
        return pair<iterator, bool>(p.first, p.second);
    }

    template<class InputIterator>
    void insert(InputIterator f, InputIterator l) {
        rep.insert_unique(f, l);
    }

    pair<iterator, bool> insert_noresize(const value& 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); }

    T& operator[] (const key_type&key) {
        return rep.find_or_insert(value_type(key, T())).second;
    }
    
    size_type count(const key_type& key) const { return rep.count(key); }

    pair<iterator, iterator> equal_range(const key_type&key) {
        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 elms_in_bucket(size_type n) {
        return rep.elems_in_bucket(n);
    }


};

template <class Key, class T, class HashFcn, class EqualKey, class Alloc> 
inline bool operator ==(const hash_map<Key,T, HashFcn, EqualKey, Alloc> &hm1,
                        const hash_map<Key, T, HashFcn, EqualKey, Alloc> &hm2) {
    return hm1.rep == hm2.rep;
}

hash_multiset

        hash_multiset的特性与multiset完全一样,参见STL关联式容器之multiset及multimap-CSDN博客 ,唯一的区别是它的底层机制是hashtable。也因此,hash_multiset的元素并不会自动排序。

        hash_multiset和hash_set实现上唯一差别在于,前者的元素插入采用底层机制hashtable的insert_euqal(),后者则是采用insert_unique();

        其源码就不一一展示了; 有兴趣的读者,可以自行查阅STL源码。

hash_multimap

        hash_multimap的特性与multimap完全相同,可参见STL关联式容器之multiset及multimap-CSDN博客 ;唯一差别是在于它的底层机制为hashtable。也因此,hash_multimap的元素并不会被自动排序。

        hash_multimap和hash_map实现上唯一的差别在于,前者的元素插入操作采用底层机制hashtable的insert_equal(),后者则采用insert_unique();

        其源码就不一一展示了;有兴趣的读者,可以自行查阅STL源码。

参考文档《STL源码剖析》--侯捷

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

大明__

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值