From d7380c5c3592825624434e73bff64b09a6bc8027 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=82=B9=E6=99=93=E8=88=AA?= <1210603696@qq.com> Date: Mon, 19 Jan 2015 15:28:08 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E6=88=90rehash?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- TinySTL/Detail/Unordered_set.impl.h | 33 ++++++++++++++++++++++++++++- TinySTL/Unordered_set.h | 9 ++++++++ 2 files changed, 41 insertions(+), 1 deletion(-) diff --git a/TinySTL/Detail/Unordered_set.impl.h b/TinySTL/Detail/Unordered_set.impl.h index 4e700f1..45e9302 100644 --- a/TinySTL/Detail/Unordered_set.impl.h +++ b/TinySTL/Detail/Unordered_set.impl.h @@ -1,7 +1,7 @@ #ifndef _UNORDERED_SET_IMPL_H_ #define _UNORDERED_SET_IMPL_H_ -#include +#include //for bind namespace TinySTL{ namespace Detail{ @@ -128,12 +128,14 @@ namespace TinySTL{ Unordered_set::Unordered_set(const Unordered_set& ust){ buckets_ = ust.buckets_; size_ = ust.size_; + max_load_factor_ = ust.max_load_factor_; } template Unordered_set& Unordered_set::operator = (const Unordered_set& ust){ if (this != &ust){ buckets_ = ust.buckets_; size_ = ust.size_; + max_load_factor_ = ust.max_load_factor_; } } template @@ -141,11 +143,13 @@ namespace TinySTL{ bucket_count = next_prime(bucket_count); buckets_.resize(bucket_count); size_ = 0; + max_load_factor_ = 1.0; } template template Unordered_set::Unordered_set(InputIterator first, InputIterator last){ size_ = 0; + max_load_factor_ = 1.0; auto len = last - first; buckets_.resize(next_prime(len)); for (; first != last; ++first){ @@ -203,6 +207,8 @@ namespace TinySTL{ TinySTL::pair::iterator, bool> Unordered_set::insert(const value_type& val){ if (!has_key(val)){ + if (load_factor() > max_load_factor()) + rehash(next_prime(size())); auto index = bucket_index(val); buckets_[index].push_front(val); ++size_; @@ -237,6 +243,31 @@ namespace TinySTL{ return 1; } } + template + float Unordered_set::max_load_factor()const{ + return max_load_factor_; + } + template + void Unordered_set::max_load_factor(float z){ + max_load_factor_ = z; + } + template + void Unordered_set::rehash(size_type n){ + if (n <= buckets_.size()) + return; + Unordered_set temp(next_prime(n)); + for (auto& val : *this){ + temp.insert(val); + } + TinySTL::swap(*this, temp); + } + template + void swap(Unordered_set& lhs, + Unordered_set& rhs){ + TinySTL::swap(lhs.buckets_, rhs.buckets_); + TinySTL::swap(lhs.size_, rhs.size_); + TinySTL::swap(lhs.max_load_factor_, rhs.max_load_factor_); + } } #endif \ No newline at end of file diff --git a/TinySTL/Unordered_set.h b/TinySTL/Unordered_set.h index 1a35418..52e707f 100644 --- a/TinySTL/Unordered_set.h +++ b/TinySTL/Unordered_set.h @@ -59,6 +59,7 @@ namespace TinySTL{ private: TinySTL::vector> buckets_; size_type size_; + float max_load_factor_; #define PRIME_LIST_SIZE 28 static size_t prime_list_[PRIME_LIST_SIZE]; public: @@ -74,6 +75,9 @@ namespace TinySTL{ size_type bucket_size(size_type i)const; size_type bucket(const key_type& key)const; float load_factor()const; + float max_load_factor()const; + void max_load_factor(float z); + void rehash(size_type n); iterator begin(); iterator end(); @@ -96,7 +100,12 @@ namespace TinySTL{ size_type next_prime(size_type n)const; size_type bucket_index(const key_type& key)const; bool has_key(const key_type& key); + public: + template + friend void swap(Unordered_set& lhs, + Unordered_set& rhs); }; + }//end of namespace TinySTL #include "Detail\Unordered_set.impl.h" -- GitLab