diff --git a/TinySTL/Detail/Unordered_set.impl.h b/TinySTL/Detail/Unordered_set.impl.h index 60e7754..2bda7f0 100644 --- a/TinySTL/Detail/Unordered_set.impl.h +++ b/TinySTL/Detail/Unordered_set.impl.h @@ -1,7 +1,52 @@ #ifndef _UNORDERED_SET_IMPL_H_ #define _UNORDERED_SET_IMPL_H_ +#include + namespace TinySTL{ + template + typename Unordered_set::size_type + Unordered_set::size()const{ + return size_; + } + template + bool Unordered_set::empty()const{ + return size() == 0; + } + template + typename Unordered_set::size_type + Unordered_set::bucket_count()const{ + return buckets_.size(); + } + template + typename Unordered_set::size_type + Unordered_set::bucket_size(size_type i)const{ + return buckets_[i].size(); + } + template + typename Unordered_set::size_type + Unordered_set::bucket(const key_type& key)const{ + return bucket_index(key); + } + template + float Unordered_set::load_factor()const{ + return (float)size() / (float)bucket_count(); + } + template + typename Unordered_set::haser + Unordered_set::hash_function()const{ + return haser(); + } + template + typename Unordered_set::key_equal + Unordered_set::key_eq()const{ + return key_equal(); + } + template + typename Unordered_set::allocator_type + Unordered_set::get_allocator()const{ + return allocator_type(); + } template size_t Unordered_set::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 bool Unordered_set::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 typename Unordered_set::size_type @@ -42,7 +88,7 @@ namespace TinySTL{ Unordered_set::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)){ diff --git a/TinySTL/Unordered_set.h b/TinySTL/Unordered_set.h index 9b89d17..78f43c3 100644 --- a/TinySTL/Unordered_set.h +++ b/TinySTL/Unordered_set.h @@ -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;