diff --git a/TinySTL/Detail/Unordered_set.impl.h b/TinySTL/Detail/Unordered_set.impl.h new file mode 100644 index 0000000..60e7754 --- /dev/null +++ b/TinySTL/Detail/Unordered_set.impl.h @@ -0,0 +1,56 @@ +#ifndef _UNORDERED_SET_IMPL_H_ +#define _UNORDERED_SET_IMPL_H_ + +namespace TinySTL{ + template + size_t Unordered_set::prime_list_[PRIME_LIST_SIZE] = { + 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 + 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(); + } + template + typename Unordered_set::size_type + Unordered_set::bucket_index(const key_type& key)const{ + return haser()(key) % buckets_.size(); + } + template + typename Unordered_set::size_type + Unordered_set::next_prime(size_type n)const{ + auto i = 0; + for (; i != PRIME_LIST_SIZE; ++i){ + if (n > prime_list_[i]) + continue; + else + break; + } + i = (i == PRIME_LIST_SIZE ? PRIME_LIST_SIZE - 1 : i); + return prime_list_[i]; + } + template + Unordered_set::Unordered_set(size_type bucket_count){ + bucket_count = next_prime(bucket_count); + buckets_.resize(bucket_count); + size_ = 0; + } + template + template + Unordered_set::Unordered_set(InputIterator first, InputIterator last){ + size_ = 0; + auto len = last - first; + buckets_.resize(len); + for (; first != last; ++first){ + auto index = bucket_index(*first); + if (!has_key(*first)){ + buckets_[index].push_front(*first); + ++size_; + } + } + } +} + +#endif \ No newline at end of file diff --git a/TinySTL/Unordered_set.h b/TinySTL/Unordered_set.h new file mode 100644 index 0000000..9b89d17 --- /dev/null +++ b/TinySTL/Unordered_set.h @@ -0,0 +1,43 @@ +#ifndef _UNORDERED_SET_H_ +#define _UNORDERED_SET_H_ + +#include "Allocator.h" +#include "Algorithm.h" +#include "Functional.h" +#include "List.h" +#include "Vector.h" +#include +#include + +namespace TinySTL{ + template, + class KeyEqual = TinySTL::equal_to, class Allocator = TinySTL::allocator < Key >> + class Unordered_set{ + public: + typedef Key key_type; + typedef Key value_type; + typedef size_t size_type; + typedef Hash haser; + typedef Allocator allocator_type; + typedef value_type& reference; + typedef const value_type& const_reference; + private: + TinySTL::vector> buckets_; + size_type size_; +#define PRIME_LIST_SIZE 28 + static size_t prime_list_[PRIME_LIST_SIZE]; + public: + explicit Unordered_set(size_t bucket_count); + template + Unordered_set(InputIterator first, InputIterator last); + //Unordered_set(const Unordered_set& ust); + //Unordered_set& operator = (const Unordered_set& ust); + private: + size_type next_prime(size_type n)const; + size_type bucket_index(const key_type& key)const; + bool has_key(const key_type& key); + }; +}//end of namespace TinySTL + +#include "Detail\Unordered_set.impl.h" +#endif \ No newline at end of file