static const int _stl_num_primes = 28;
template<typename T, typename Hash = xhash<T>>
class hashtable; ministl::vector<int> prime_list =
{
53, 97, 193, 389, 769, 1543, 3079, 6151, 12289, 24593, 49157, 98317, 196613, 393241,
786433, 1572869, 3145739, 6291469, 12582917, 25165843, 50331653, 100663319, 201326611,
402653189, 805306457, 1610612741, 3221225473, 4294967291,
}; template<typename T>
class hash_iterator : public bidirectional_iterator<T>
{
public:
hashtable<T>* hash_ptr;
list_iterator<T> node_ptr;
list<T>* bucket_ptr;
hash_iterator(hashtable<T>* hp, list<T>* bp, list_iterator<T> np) :hash_ptr(hp), bucket_ptr(bp), node_ptr(np) {} hash_iterator& operator++()
{
if (++node_ptr == bucket_ptr->end())
{
auto tmp = node_ptr;
--tmp;
size_t cur = hash_ptr->hash(*tmp, hash_ptr->bucket_size()) + 1;
while (cur < hash_ptr->bucket_size() && (*hash_ptr).data[cur].empty())
cur++;
bucket_ptr = cur < hash_ptr->bucket_size() ? &(hash_ptr->data[cur]) : hash_ptr->data.end();
node_ptr = (bucket_ptr == hash_ptr->data.end() ? nullptr : bucket_ptr->begin());
}
return *this;
} hash_iterator operator++(int)
{
hash_iterator tmp = *this;
++*this;
return tmp;
} hash_iterator& operator--()
{
if (node_ptr-- = bucket_ptr->begin())
{
size_t cur = hash_ptr->hash(*node_ptr, hash_ptr->bucket_size()) - 1;
if (cur < 0) cur = 0;
else
{
while (cur >= 0 && (*hash_ptr).data[cur].empty())
cur--;
}
bucket_ptr = &(hash_ptr->data[cur]);
node_ptr = bucket_ptr->begin();
}
return *this;
} hash_iterator operator--(int)
{
hash_iterator tmp = *this;
--*this;
return tmp;
} T& operator* ()
{
return *node_ptr;
} bool operator==(const hash_iterator& rhs)
{
return hash_ptr == rhs.hash_ptr && node_ptr == rhs.node_ptr && bucket_ptr == rhs.bucket_ptr;
} bool operator!=(const hash_iterator& rhs)
{
return !(*this == rhs);
}
}; template<typename T,typename Hash = xhash<T> >
class hashtable
{
friend class hash_iterator<T>;
public:
typedef T value_type;
typedef Hash hash_type;
typedef vector<list<T>> table;
typedef hash_iterator<T> iterator;
private:
Hash hash;
table data;
size_t elem_num;//the sum of all elements
pair<iterator, bool> _insert_unique_noresize(T val)
{
size_t index = hash(val, data.size()); for (auto it = data[index].begin(); it != data[index].end(); it++)
{
if (*it == val)
return make_pair<iterator, bool >(iterator(this, &data[index], it), false);
} data[index].push_front(val);
elem_num++;
return make_pair<iterator, bool>(iterator(this, &data[index], data[index].begin()), true);
} iterator _insert_equal_noresize(T val)
{
size_t index = hash(val, data.size());
for (auto it = data[index].begin(); it != data[index].end(); it++)
{
if (*it == val)
{
return iterator(this, &data[index], data[index].insert(it, val));
}
}
data[index].push_front(val);
elem_num++;
return iterator(this, &data[index], data[index].begin());
} void initialize_buckets(size_t n)
{
data.resize(n);
elem_num = 0;
} public:
hashtable()
{
initialize_buckets(53);
}
hashtable(size_t n)
{
initialize_buckets(next_prime(n));
} void clear()
{
for (auto& it : data)
{
it.clear();
}
elem_num = 0;
} bool empty()
{
return elem_num == 0;
} pair<iterator, bool>find(const T& val)
{
size_t index = hash(val, data.size()); for (auto it = data[index].begin(); it != data[index].end(); it++)
{
if (*it == val)
return ministl::make_pair<iterator, bool >(iterator(this, &data[index], it), true);
} return ministl::make_pair<iterator, bool >(end(), false);
} iterator begin() const
{
auto it = data.begin();
while (it != data.end() && it->empty())
it++;
return iterator(const_cast<hashtable<T>*>(this), it, it == data.end() ? nullptr : it->begin());
} iterator begin()
{
auto it = data.begin();
while (it != data.end() && it->empty())
it++;
return iterator(this, it, it == data.end() ? nullptr : it->begin());
} iterator end() const
{
return iterator(const_cast<hashtable<T>*>(this), data.end(), nullptr);
} iterator end()
{
return iterator(this, data.end(), nullptr);
} inline unsigned long next_prime(unsigned long n)
{
for (auto it : prime_list)
{
if (it >= n)
return n;
}
return prime_list[_stl_num_primes - 1];
} size_t bucket_size()
{
return data.size();
} size_t bucket_size() const
{
return data.size();
} size_t size()
{
return elem_num;
}
size_t hash_val(const T& val)
{
return hash(val, data.size());
}
pair<iterator, bool> insert_unique(T val)
{
rehash(elem_num + 1);
return _insert_unique_noresize(val);
} iterator insert_equal(T val)
{
rehash(elem_num + 1);
return _insert_equal_noresize(val);
} iterator erase_iter(iterator pos)
{
iterator ret = pos;
ret++;
pos.bucket_ptr->erase(pos.node_ptr);
return ret;
} void erase_val(const T& val)
{
size_t index = hash(val, data.size());
for (auto it = data[index].begin(); it != data[index].end();)
{
if (*it == val)
it = data[index].erase(it);
else
it++; }
} void rehash(size_t n)
{
if (n > bucket_size())
{
elem_num = elem_num ? elem_num:1;
while (n > elem_num) elem_num *= 2;
size_t new_size = next_prime(elem_num);
hashtable new_table(new_size);
if (!empty())
{
iterator it = begin();
for (; it != end(); it++)
{
//std::cout << *it << std::endl;
new_table.insert_equal(*it);
}
}
swap(*this, new_table);
}
} hashtable& operator=(const hashtable& rhs)
{
clear();
for (auto it = rhs.begin(); it != rhs.end(); it++)
{
insert_equal(*it);
}
return *this;
}
}; template<typename T>
void swap(const hashtable<T>& lhs, const hashtable<T>& rhs)
{
using ministl::swap;
swap(lhs.data, rhs.data);
swap(lhs.elem_num, rhs.elem_num);
swap(lhs.hash, rhs.hash);
} template<typename T, typename Hash = xhash<T> >
class unordered_set
{
public:
typedef T value_type;
typedef Hash hash_type;
typedef vector<list<T>> table;
typedef hash_iterator<T> iterator;
typedef hashtable<T> seq;
private:
seq con;
public:
unordered_set() :con() {}
unordered_set(std::initializer_list<T> l)
{
unordered_set();
insert(l.begin(), l.end());
}
iterator begin()
{
return con.begin();
} iterator end()
{
return con.end();
} iterator find(const value_type& key)
{
return con.find(key).first;
} iterator insert(const value_type& key)
{
return con.insert_unique(key).first;
} iterator insert(value_type& key)
{
return con.insert_unique(key);
}
template <class InputIterator>
void insert(InputIterator first, InputIterator last)
{
for (auto it = first; it != last; it++)
insert(*it);
} size_t erase(const value_type& key)
{
size_t num = count(key);
if(num)
con.erase_val(key);
return num;
} iterator erase(const iterator it)
{
return con.erase_iter(it);
} iterator erase(const iterator first, const iterator last)
{
iterator it = first;
for (; it != last;)
it = con.erase_iter(it);
return it;
} size_t count(const value_type& val)
{
return (con.find(val).second == true) ? 1 : 0;
} bool empty()
{
return con.empty();
} size_t size()
{
return con.size();
} void clear()
{
con.clear();
} void swap(unordered_set<T>& rhs)
{
using ministl::swap;
swap(con, rhs.con);
} size_t bucket_count()
{
return con.bucket_size();
} void rehash()
{
con.rehash(con.elem_num + 1);
} double load_factor()
{
return 1.0 * con.elem_num / bucket_count();
} }; //Todo: map_pair的hashval
//模板特化无法匹配?
template<typename K , typename T, typename Hash = xhash<map_pair<K,T>> >
class unordered_map
{
public:
typedef K key_type;
typedef T value_type;
typedef Hash hash_type;
typedef vector<list<map_pair<K,T>>> table;
typedef hash_iterator<map_pair<K,T>> iterator;
typedef hashtable<map_pair<K, T>, xhash<map_pair<K, T>> > seq;
private:
seq con;
public:
unordered_map() :con() {} iterator begin()
{
return con.begin();
}
iterator begin() const
{
return con.begin();
} iterator end()
{
return con.end();
}
iterator end() const
{
return con.end();
} value_type& operator[](const key_type& key)
{
map_pair<K, T> p(key, T());
auto tmp = con.find(p);
iterator it = tmp.first;
if (tmp.second == false)
{
it = con.insert_equal(p);
}
return (*it).second; } value_type& at(const key_type& key)
{
auto tmp = con.find(key);
if (tmp.second == false)
{
std::cerr << "unordered_map out of range \n";
std::exit(1);
}
return (*it).second;
} const value_type& at(const key_type& key) const
{
auto tmp = con.find(key);
if (tmp.second == false)
{
std::cerr << "unordered_map out of range \n";
std::exit(1);
}
return (*it).second;
} iterator find(const key_type& key)
{
return con.find(key).first;
} pair<iterator, iterator>
equal_range(const key_type& k)
{
auto first = con.find(k).first;
if (first == con.end())
return make_pair<iterator, iterator>(first, first);
else
{
iterator last = ++first;
while (last != con.end() && (*last == *first))
{
last++;
}
return make_pair<iterator, iterator>(first, last);
}
} size_t count(const key_type& k)
{
auto first = con.find(k).first;
if (first == con.end())
return 0;
else
{
iterator last = ++first;
size_t cnt = 1;
while (last != con.end() && (*last == *first))
{
last++, cnt++;
}
return cnt;
}
} auto insert(const map_pair<K, T> p)
{
return con.insert_unique(p);
} size_t erase(const value_type& key)
{
size_t num = count(key);
if (num)
con.erase_val(key);
return num;
} iterator erase(const iterator it)
{
return con.erase_iter(it);
} iterator erase(const iterator first, const iterator last)
{
iterator it = first;
for (; it != last;)
it = con.erase_iter(it);
return it;
} void rehash(size_t n)
{
con.rehash(n);
} };

从零开始写STL—哈希表的更多相关文章

  1. 从零开始写STL - 智能指针

    从零开始写STL - 智能指针 智能指针的分类及其特点: scoped_ptr:初始化获得资源控制权,在作用域结束释放资源 shared_ptr: 引用计数来控制共享资源,最后一个资源的引用被释放的时 ...

  2. 从零开始写STL—栈和队列

    从零开始写STL-栈和队列 适配器模式 意图:将一个类的接口转换成客户希望的另外一个接口.适配器模式使得原本由于接口不兼容而不能一起工作的那些类可以一起工作. 主要解决:主要解决在软件系统中,常常要将 ...

  3. 从零开始写STL—容器—vector

    从0开始写STL-容器-vector vector又称为动态数组,那么动态体现在哪里?vector和一般的数组又有什么区别?vector中各个函数的实现原理是怎样的,我们怎样使用会更高效? 以上内容我 ...

  4. 从零开始写STL—模板元编程之any

    any class any; (since C++17) The class any describes a type-safe container for single values of any ...

  5. 从零开始写STL—functional

    function C++11 将任意类型的可调用(Callable)对象与函数调用的特征封装到一起. 这里的类是对函数策略的封装,将函数的性质抽象成组件,便于和algorithm库配合使用 基本运算符 ...

  6. 从零开始写STL—模板元编程之tuple

    tuple Class template std::tuple is a fixed-size collection of heterogeneous values. It is a generali ...

  7. 从零开始写STL—set/map

    这一部分只要把搜索树中暴露的接口封装一下,做一些改动. set源码剖析 template<typename T> class set { public: typedef T key_typ ...

  8. [Codevs 1230]元素查找(手写哈希表)

    题目连接:http://codevs.cn/problem/1230/ 说白了就是要我们自己手写一个哈希表的数据结构来实现加入和查找功能.map也能直接过(我第一次写就是用map骗AC的) 提一下个人 ...

  9. C++ STL中哈希表Map 与 hash_map 介绍

    0 为什么需要hash_map 用过map吧?map提供一个很常用的功能,那就是提供key-value的存储和查找功能.例如,我要记录一个人名和相应的存储,而且随时增加,要快速查找和修改: 岳不群-华 ...

随机推荐

  1. spring中junit 提示Failed to load ApplicationContext

    错误提示: 1:java.lang.IllegalStateException: Failed to load ApplicationContext 2:Error creating bean wit ...

  2. mysql出错排查

    1,例如:Can't connect to local MySQL server through socket '/tmp/mysql-5.5.37.sock' (2) Mysql链接出错,请配置/A ...

  3. NIO入门之轻松读取大文件

    NIO入门之轻松读取大文件 今天同事碰到了一个问题,从游戏服务器下载下来的输出log有一个多G大.用记事本打不开,EditPlus也打不开,都提示文件太大.用word也打不开,提示文件大于512M.打 ...

  4. web页面调用IOS的事件

    /** * js 调用ios的方法 * @param callback */ function connectWebViewJavascriptBridge(callback) { if (windo ...

  5. makefile vpath变量

    在讲vpath之前,我们首先了解以下makefile文件. 在类Unix系统中,当我们使用源码编译某个软件的时候,我们会使用confiure,make,make install这三个命令,其中cofi ...

  6. (译)IOS block编程指南 1 介绍

    Introduction(介绍) Block objects are a C-level syntactic and runtime feature. They are similar to stan ...

  7. jeecms

    ===标签=== <!-- 显示一级栏目对应的二级栏目 --> <!-- [@cms_channel_list parentId=c.id] [#if tag_list?size&g ...

  8. Postman 安装及使用入门教程 | 前后台 写接口的 徐工给的

    https://www.cnblogs.com/mafly/p/postman.html

  9. 这就是那个feature map256 256向量

    http://blog.csdn.net/XZZPPP/article/details/51582810 在这个特征图上使用3*3的卷积核(滑动窗口)与特征图进行卷积,那么这个3*3的区域卷积后可以获 ...

  10. Swift语言Storyboard教程:第二部

    本文由CocoaChina翻译小组@TurtleFromMars翻译自raywenderlich,原文:Storyboards Tutorial in Swift: Part 2 更新记录:该Stor ...