完成相关函数
This commit is contained in:
@@ -1,7 +1,52 @@
|
||||
#ifndef _UNORDERED_SET_IMPL_H_
|
||||
#define _UNORDERED_SET_IMPL_H_
|
||||
|
||||
#include <functional>
|
||||
|
||||
namespace TinySTL{
|
||||
template<class Key, class Hash, class KeyEqual, class Allocator>
|
||||
typename Unordered_set<Key, Hash, KeyEqual, Allocator>::size_type
|
||||
Unordered_set<Key, Hash, KeyEqual, Allocator>::size()const{
|
||||
return size_;
|
||||
}
|
||||
template<class Key, class Hash, class KeyEqual, class Allocator>
|
||||
bool Unordered_set<Key, Hash, KeyEqual, Allocator>::empty()const{
|
||||
return size() == 0;
|
||||
}
|
||||
template<class Key, class Hash, class KeyEqual, class Allocator>
|
||||
typename Unordered_set<Key, Hash, KeyEqual, Allocator>::size_type
|
||||
Unordered_set<Key, Hash, KeyEqual, Allocator>::bucket_count()const{
|
||||
return buckets_.size();
|
||||
}
|
||||
template<class Key, class Hash, class KeyEqual, class Allocator>
|
||||
typename Unordered_set<Key, Hash, KeyEqual, Allocator>::size_type
|
||||
Unordered_set<Key, Hash, KeyEqual, Allocator>::bucket_size(size_type i)const{
|
||||
return buckets_[i].size();
|
||||
}
|
||||
template<class Key, class Hash, class KeyEqual, class Allocator>
|
||||
typename Unordered_set<Key, Hash, KeyEqual, Allocator>::size_type
|
||||
Unordered_set<Key, Hash, KeyEqual, Allocator>::bucket(const key_type& key)const{
|
||||
return bucket_index(key);
|
||||
}
|
||||
template<class Key, class Hash, class KeyEqual, class Allocator>
|
||||
float Unordered_set<Key, Hash, KeyEqual, Allocator>::load_factor()const{
|
||||
return (float)size() / (float)bucket_count();
|
||||
}
|
||||
template<class Key, class Hash, class KeyEqual, class Allocator>
|
||||
typename Unordered_set<Key, Hash, KeyEqual, Allocator>::haser
|
||||
Unordered_set<Key, Hash, KeyEqual, Allocator>::hash_function()const{
|
||||
return haser();
|
||||
}
|
||||
template<class Key, class Hash, class KeyEqual, class Allocator>
|
||||
typename Unordered_set<Key, Hash, KeyEqual, Allocator>::key_equal
|
||||
Unordered_set<Key, Hash, KeyEqual, Allocator>::key_eq()const{
|
||||
return key_equal();
|
||||
}
|
||||
template<class Key, class Hash, class KeyEqual, class Allocator>
|
||||
typename Unordered_set<Key, Hash, KeyEqual, Allocator>::allocator_type
|
||||
Unordered_set<Key, Hash, KeyEqual, Allocator>::get_allocator()const{
|
||||
return allocator_type();
|
||||
}
|
||||
template<class Key, class Hash, class KeyEqual, class Allocator>
|
||||
size_t Unordered_set<Key, Hash, KeyEqual, Allocator>::prime_list_[PRIME_LIST_SIZE] = {
|
||||
53, 97, 193, 389, 769, 1543, 3079, 6151, 12289, 24593, 49157, 98317, 196613, 393241,
|
||||
@@ -11,7 +56,8 @@ namespace TinySTL{
|
||||
template<class Key, class Hash, class KeyEqual, class Allocator>
|
||||
bool Unordered_set<Key, Hash, KeyEqual, Allocator>::has_key(const key_type& key){
|
||||
auto& list = buckets_[bucket_index(key)];
|
||||
return TinySTL::find(list.begin(), list.end(), key) != list.end();
|
||||
auto pred = std::bind(KeyEqual(), key, std::placeholders::_1);
|
||||
return TinySTL::find_if(list.begin(), list.end(), pred) != list.end();
|
||||
}
|
||||
template<class Key, class Hash, class KeyEqual, class Allocator>
|
||||
typename Unordered_set<Key, Hash, KeyEqual, Allocator>::size_type
|
||||
@@ -42,7 +88,7 @@ namespace TinySTL{
|
||||
Unordered_set<Key, Hash, KeyEqual, Allocator>::Unordered_set(InputIterator first, InputIterator last){
|
||||
size_ = 0;
|
||||
auto len = last - first;
|
||||
buckets_.resize(len);
|
||||
buckets_.resize(next_prime(len));
|
||||
for (; first != last; ++first){
|
||||
auto index = bucket_index(*first);
|
||||
if (!has_key(*first)){
|
||||
|
||||
@@ -18,6 +18,7 @@ namespace TinySTL{
|
||||
typedef Key value_type;
|
||||
typedef size_t size_type;
|
||||
typedef Hash haser;
|
||||
typedef KeyEqual key_equal;
|
||||
typedef Allocator allocator_type;
|
||||
typedef value_type& reference;
|
||||
typedef const value_type& const_reference;
|
||||
@@ -32,6 +33,17 @@ namespace TinySTL{
|
||||
Unordered_set(InputIterator first, InputIterator last);
|
||||
//Unordered_set(const Unordered_set& ust);
|
||||
//Unordered_set& operator = (const Unordered_set& ust);
|
||||
|
||||
size_type size()const;
|
||||
bool empty()const;
|
||||
size_type bucket_count()const;
|
||||
size_type bucket_size(size_type i)const;
|
||||
size_type bucket(const key_type& key)const;
|
||||
float load_factor()const;
|
||||
|
||||
haser hash_function()const;
|
||||
key_equal key_eq()const;
|
||||
allocator_type get_allocator()const;
|
||||
private:
|
||||
size_type next_prime(size_type n)const;
|
||||
size_type bucket_index(const key_type& key)const;
|
||||
|
||||
Reference in New Issue
Block a user