diff --git a/TinySTL/Detail/Unordered_set.impl.h b/TinySTL/Detail/Unordered_set.impl.h index 2bda7f0..1ba76e5 100644 --- a/TinySTL/Detail/Unordered_set.impl.h +++ b/TinySTL/Detail/Unordered_set.impl.h @@ -4,6 +4,53 @@ #include namespace TinySTL{ + namespace Detail{ + template + ust_iterator::ust_iterator(size_t index, ListIterator it, cntrPtr ptr) + :bucket_index_(index), iterator_(it), container_(ptr){} + template + ust_iterator& + ust_iterator::operator ++(){ + ++iterator_; + //如果前进一位后到达了list的末尾,则需要跳转到下一个有item的bucket的list + if (iterator_ == container_->buckets_[bucket_index_].end()){ + for (;;){ + if (bucket_index_ == container_->buckets_.size() - 1){ + *this = container_->end(); + break; + } + else{ + ++bucket_index_; + if (!(container_->buckets_[bucket_index_].empty())){//此list不为空 + iterator_ = container_->buckets_[bucket_index_].begin(); + break; + } + } + } + } + return *this; + } + template + ust_iterator + ust_iterator::operator ++(int){ + auto res = *this; + ++*this; + return res; + } + template + bool operator ==(const ust_iterator& lhs, + const ust_iterator& rhs){ + return lhs.bucket_index_ == rhs.bucket_index_ && + lhs.iterator_ == rhs.iterator_ && + lhs.container_ == rhs.container_; + } + template + bool operator !=(const ust_iterator& lhs, + const ust_iterator& rhs){ + return !(lhs == rhs); + } + }//end of Detail namespace + template typename Unordered_set::size_type Unordered_set::size()const{ @@ -78,6 +125,18 @@ namespace TinySTL{ return prime_list_[i]; } template + Unordered_set::Unordered_set(const Unordered_set& ust){ + buckets_ = ust.buckets_; + size_ = ust.size_; + } + template + Unordered_set& Unordered_set::operator = (const Unordered_set& ust){ + if (this != &ust){ + buckets_ = ust.buckets_; + size_ = ust.size_; + } + } + template Unordered_set::Unordered_set(size_type bucket_count){ bucket_count = next_prime(bucket_count); buckets_.resize(bucket_count); @@ -97,6 +156,23 @@ namespace TinySTL{ } } } + template + typename Unordered_set::iterator + Unordered_set::begin(){ + size_type index = 0; + for (; index != buckets_.size(); ++index){ + if (!(buckets_[index].empty())) + break; + } + if (index == buckets_.size()) + return end(); + return iterator(index, buckets_[index].begin(), this); + } + template + typename Unordered_set::iterator + Unordered_set::end(){ + return iterator(buckets_.size() - 1, buckets_[buckets_.size() - 1].end(), this); + } } #endif \ No newline at end of file diff --git a/TinySTL/Unordered_set.h b/TinySTL/Unordered_set.h index 78f43c3..151b516 100644 --- a/TinySTL/Unordered_set.h +++ b/TinySTL/Unordered_set.h @@ -6,13 +6,42 @@ #include "Functional.h" #include "List.h" #include "Vector.h" -#include -#include namespace TinySTL{ + template + class Unordered_set; + namespace Detail{ + template, + class KeyEqual = TinySTL::equal_to, class Allocator = TinySTL::allocator < Key >> + class ust_iterator{ + private: + typedef Unordered_set* cntrPtr; + size_t bucket_index_; + ListIterator iterator_; + cntrPtr container_; + public: + ust_iterator(size_t index, ListIterator it, cntrPtr ptr); + ust_iterator& operator ++(); + ust_iterator operator ++(int); + Key& operator*(){ return *iterator_; } + Key* operator->(){ return &(operator*()); } + private: + template + friend bool operator ==(const ust_iterator& lhs, + const ust_iterator& rhs); + template + friend bool operator !=(const ust_iterator& lhs, + const ust_iterator& rhs); + }; + }//end of namespace Detail + + template, class KeyEqual = TinySTL::equal_to, class Allocator = TinySTL::allocator < Key >> class Unordered_set{ + private: + template + friend class Detail::ust_iterator; public: typedef Key key_type; typedef Key value_type; @@ -22,6 +51,7 @@ namespace TinySTL{ typedef Allocator allocator_type; typedef value_type& reference; typedef const value_type& const_reference; + typedef Detail::ust_iterator::iterator, Hash, KeyEqual, Allocator> iterator; private: TinySTL::vector> buckets_; size_type size_; @@ -31,8 +61,8 @@ namespace TinySTL{ 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); + Unordered_set(const Unordered_set& ust); + Unordered_set& operator = (const Unordered_set& ust); size_type size()const; bool empty()const; @@ -41,6 +71,9 @@ namespace TinySTL{ size_type bucket(const key_type& key)const; float load_factor()const; + iterator begin(); + iterator end(); + haser hash_function()const; key_equal key_eq()const; allocator_type get_allocator()const;