完成rehash
This commit is contained in:
@@ -1,7 +1,7 @@
|
|||||||
#ifndef _UNORDERED_SET_IMPL_H_
|
#ifndef _UNORDERED_SET_IMPL_H_
|
||||||
#define _UNORDERED_SET_IMPL_H_
|
#define _UNORDERED_SET_IMPL_H_
|
||||||
|
|
||||||
#include <functional>
|
#include <functional>//for bind
|
||||||
|
|
||||||
namespace TinySTL{
|
namespace TinySTL{
|
||||||
namespace Detail{
|
namespace Detail{
|
||||||
@@ -128,12 +128,14 @@ namespace TinySTL{
|
|||||||
Unordered_set<Key, Hash, KeyEqual, Allocator>::Unordered_set(const Unordered_set& ust){
|
Unordered_set<Key, Hash, KeyEqual, Allocator>::Unordered_set(const Unordered_set& ust){
|
||||||
buckets_ = ust.buckets_;
|
buckets_ = ust.buckets_;
|
||||||
size_ = ust.size_;
|
size_ = ust.size_;
|
||||||
|
max_load_factor_ = ust.max_load_factor_;
|
||||||
}
|
}
|
||||||
template<class Key, class Hash, class KeyEqual, class Allocator>
|
template<class Key, class Hash, class KeyEqual, class Allocator>
|
||||||
Unordered_set<Key, Hash, KeyEqual, Allocator>& Unordered_set<Key, Hash, KeyEqual, Allocator>::operator = (const Unordered_set& ust){
|
Unordered_set<Key, Hash, KeyEqual, Allocator>& Unordered_set<Key, Hash, KeyEqual, Allocator>::operator = (const Unordered_set& ust){
|
||||||
if (this != &ust){
|
if (this != &ust){
|
||||||
buckets_ = ust.buckets_;
|
buckets_ = ust.buckets_;
|
||||||
size_ = ust.size_;
|
size_ = ust.size_;
|
||||||
|
max_load_factor_ = ust.max_load_factor_;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
template<class Key, class Hash, class KeyEqual, class Allocator>
|
template<class Key, class Hash, class KeyEqual, class Allocator>
|
||||||
@@ -141,11 +143,13 @@ namespace TinySTL{
|
|||||||
bucket_count = next_prime(bucket_count);
|
bucket_count = next_prime(bucket_count);
|
||||||
buckets_.resize(bucket_count);
|
buckets_.resize(bucket_count);
|
||||||
size_ = 0;
|
size_ = 0;
|
||||||
|
max_load_factor_ = 1.0;
|
||||||
}
|
}
|
||||||
template<class Key, class Hash, class KeyEqual, class Allocator>
|
template<class Key, class Hash, class KeyEqual, class Allocator>
|
||||||
template<class InputIterator>
|
template<class InputIterator>
|
||||||
Unordered_set<Key, Hash, KeyEqual, Allocator>::Unordered_set(InputIterator first, InputIterator last){
|
Unordered_set<Key, Hash, KeyEqual, Allocator>::Unordered_set(InputIterator first, InputIterator last){
|
||||||
size_ = 0;
|
size_ = 0;
|
||||||
|
max_load_factor_ = 1.0;
|
||||||
auto len = last - first;
|
auto len = last - first;
|
||||||
buckets_.resize(next_prime(len));
|
buckets_.resize(next_prime(len));
|
||||||
for (; first != last; ++first){
|
for (; first != last; ++first){
|
||||||
@@ -203,6 +207,8 @@ namespace TinySTL{
|
|||||||
TinySTL::pair<typename Unordered_set<Key, Hash, KeyEqual, Allocator>::iterator, bool>
|
TinySTL::pair<typename Unordered_set<Key, Hash, KeyEqual, Allocator>::iterator, bool>
|
||||||
Unordered_set<Key, Hash, KeyEqual, Allocator>::insert(const value_type& val){
|
Unordered_set<Key, Hash, KeyEqual, Allocator>::insert(const value_type& val){
|
||||||
if (!has_key(val)){
|
if (!has_key(val)){
|
||||||
|
if (load_factor() > max_load_factor())
|
||||||
|
rehash(next_prime(size()));
|
||||||
auto index = bucket_index(val);
|
auto index = bucket_index(val);
|
||||||
buckets_[index].push_front(val);
|
buckets_[index].push_front(val);
|
||||||
++size_;
|
++size_;
|
||||||
@@ -237,6 +243,31 @@ namespace TinySTL{
|
|||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
template<class Key, class Hash, class KeyEqual, class Allocator>
|
||||||
|
float Unordered_set<Key, Hash, KeyEqual, Allocator>::max_load_factor()const{
|
||||||
|
return max_load_factor_;
|
||||||
|
}
|
||||||
|
template<class Key, class Hash, class KeyEqual, class Allocator>
|
||||||
|
void Unordered_set<Key, Hash, KeyEqual, Allocator>::max_load_factor(float z){
|
||||||
|
max_load_factor_ = z;
|
||||||
|
}
|
||||||
|
template<class Key, class Hash, class KeyEqual, class Allocator>
|
||||||
|
void Unordered_set<Key, Hash, KeyEqual, Allocator>::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<class Key, class Hash, class KeyEqual, class Allocator>
|
||||||
|
void swap(Unordered_set<Key, Hash, KeyEqual, Allocator>& lhs,
|
||||||
|
Unordered_set<Key, Hash, KeyEqual, Allocator>& rhs){
|
||||||
|
TinySTL::swap(lhs.buckets_, rhs.buckets_);
|
||||||
|
TinySTL::swap(lhs.size_, rhs.size_);
|
||||||
|
TinySTL::swap(lhs.max_load_factor_, rhs.max_load_factor_);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
@@ -59,6 +59,7 @@ namespace TinySTL{
|
|||||||
private:
|
private:
|
||||||
TinySTL::vector<TinySTL::list<key_type>> buckets_;
|
TinySTL::vector<TinySTL::list<key_type>> buckets_;
|
||||||
size_type size_;
|
size_type size_;
|
||||||
|
float max_load_factor_;
|
||||||
#define PRIME_LIST_SIZE 28
|
#define PRIME_LIST_SIZE 28
|
||||||
static size_t prime_list_[PRIME_LIST_SIZE];
|
static size_t prime_list_[PRIME_LIST_SIZE];
|
||||||
public:
|
public:
|
||||||
@@ -74,6 +75,9 @@ namespace TinySTL{
|
|||||||
size_type bucket_size(size_type i)const;
|
size_type bucket_size(size_type i)const;
|
||||||
size_type bucket(const key_type& key)const;
|
size_type bucket(const key_type& key)const;
|
||||||
float load_factor()const;
|
float load_factor()const;
|
||||||
|
float max_load_factor()const;
|
||||||
|
void max_load_factor(float z);
|
||||||
|
void rehash(size_type n);
|
||||||
|
|
||||||
iterator begin();
|
iterator begin();
|
||||||
iterator end();
|
iterator end();
|
||||||
@@ -96,7 +100,12 @@ namespace TinySTL{
|
|||||||
size_type next_prime(size_type n)const;
|
size_type next_prime(size_type n)const;
|
||||||
size_type bucket_index(const key_type& key)const;
|
size_type bucket_index(const key_type& key)const;
|
||||||
bool has_key(const key_type& key);
|
bool has_key(const key_type& key);
|
||||||
|
public:
|
||||||
|
template<class Key, class Hash, class KeyEqual, class Allocator>
|
||||||
|
friend void swap(Unordered_set<Key, Hash, KeyEqual, Allocator>& lhs,
|
||||||
|
Unordered_set<Key, Hash, KeyEqual, Allocator>& rhs);
|
||||||
};
|
};
|
||||||
|
|
||||||
}//end of namespace TinySTL
|
}//end of namespace TinySTL
|
||||||
|
|
||||||
#include "Detail\Unordered_set.impl.h"
|
#include "Detail\Unordered_set.impl.h"
|
||||||
|
|||||||
Reference in New Issue
Block a user