完成迭代器

This commit is contained in:
邹晓航
2015-01-19 14:31:42 +08:00
parent c625045183
commit c42b717783
2 changed files with 113 additions and 4 deletions

View File

@@ -4,6 +4,53 @@
#include <functional>
namespace TinySTL{
namespace Detail{
template<class Key, class ListIterator, class Hash, class KeyEqual, class Allocator>
ust_iterator<Key, ListIterator, Hash, KeyEqual, Allocator>::ust_iterator(size_t index, ListIterator it, cntrPtr ptr)
:bucket_index_(index), iterator_(it), container_(ptr){}
template<class Key, class ListIterator, class Hash, class KeyEqual, class Allocator>
ust_iterator<Key, ListIterator, Hash, KeyEqual, Allocator>&
ust_iterator<Key, ListIterator, Hash, KeyEqual, Allocator>::operator ++(){
++iterator_;
//<2F><><EFBFBD><EFBFBD>ǰ<EFBFBD><C7B0>һλ<D2BB>󵽴<EFBFBD><F3B5BDB4><EFBFBD>list<73><74>ĩβ<C4A9><CEB2><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҫ<EFBFBD><D2AA>ת<EFBFBD><D7AA><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD>item<65><6D>bucket<65><74>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())){//<2F><>list<73><74>Ϊ<EFBFBD><CEAA>
iterator_ = container_->buckets_[bucket_index_].begin();
break;
}
}
}
}
return *this;
}
template<class Key, class ListIterator, class Hash, class KeyEqual, class Allocator>
ust_iterator<Key, ListIterator, Hash, KeyEqual, Allocator>
ust_iterator<Key, ListIterator, Hash, KeyEqual, Allocator>::operator ++(int){
auto res = *this;
++*this;
return res;
}
template<class Key, class ListIterator, class Hash, class KeyEqual, class Allocator>
bool operator ==(const ust_iterator<Key, ListIterator, Hash, KeyEqual, Allocator>& lhs,
const ust_iterator<Key, ListIterator, Hash, KeyEqual, Allocator>& rhs){
return lhs.bucket_index_ == rhs.bucket_index_ &&
lhs.iterator_ == rhs.iterator_ &&
lhs.container_ == rhs.container_;
}
template<class Key, class ListIterator, class Hash, class KeyEqual, class Allocator>
bool operator !=(const ust_iterator<Key, ListIterator, Hash, KeyEqual, Allocator>& lhs,
const ust_iterator<Key, ListIterator, Hash, KeyEqual, Allocator>& rhs){
return !(lhs == rhs);
}
}//end of Detail namespace
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{
@@ -78,6 +125,18 @@ namespace TinySTL{
return prime_list_[i];
}
template<class Key, class Hash, class KeyEqual, class Allocator>
Unordered_set<Key, Hash, KeyEqual, Allocator>::Unordered_set(const Unordered_set& ust){
buckets_ = ust.buckets_;
size_ = ust.size_;
}
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){
if (this != &ust){
buckets_ = ust.buckets_;
size_ = ust.size_;
}
}
template<class Key, class Hash, class KeyEqual, class Allocator>
Unordered_set<Key, Hash, KeyEqual, Allocator>::Unordered_set(size_type bucket_count){
bucket_count = next_prime(bucket_count);
buckets_.resize(bucket_count);
@@ -97,6 +156,23 @@ namespace TinySTL{
}
}
}
template<class Key, class Hash, class KeyEqual, class Allocator>
typename Unordered_set<Key, Hash, KeyEqual, Allocator>::iterator
Unordered_set<Key, Hash, KeyEqual, Allocator>::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<class Key, class Hash, class KeyEqual, class Allocator>
typename Unordered_set<Key, Hash, KeyEqual, Allocator>::iterator
Unordered_set<Key, Hash, KeyEqual, Allocator>::end(){
return iterator(buckets_.size() - 1, buckets_[buckets_.size() - 1].end(), this);
}
}
#endif

View File

@@ -6,13 +6,42 @@
#include "Functional.h"
#include "List.h"
#include "Vector.h"
#include <list>
#include <vector>
namespace TinySTL{
template<class Key, class Hash, class KeyEqual, class Allocator>
class Unordered_set;
namespace Detail{
template<class Key, class ListIterator, class Hash = std::hash<Key>,
class KeyEqual = TinySTL::equal_to<Key>, class Allocator = TinySTL::allocator < Key >>
class ust_iterator{
private:
typedef Unordered_set<Key, Hash, KeyEqual, Allocator>* 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<class Key, class ListIterator, class Hash, class KeyEqual, class Allocator>
friend bool operator ==(const ust_iterator<Key, ListIterator, Hash, KeyEqual, Allocator>& lhs,
const ust_iterator<Key, ListIterator, Hash, KeyEqual, Allocator>& rhs);
template<class Key, class ListIterator, class Hash, class KeyEqual, class Allocator>
friend bool operator !=(const ust_iterator<Key, ListIterator, Hash, KeyEqual, Allocator>& lhs,
const ust_iterator<Key, ListIterator, Hash, KeyEqual, Allocator>& rhs);
};
}//end of namespace Detail
template<class Key, class Hash = std::hash<Key>,
class KeyEqual = TinySTL::equal_to<Key>, class Allocator = TinySTL::allocator < Key >>
class Unordered_set{
private:
template<class Key, class ListIterator, class Hash, class KeyEqual, class Allocator>
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<Key, typename TinySTL::list<key_type>::iterator, Hash, KeyEqual, Allocator> iterator;
private:
TinySTL::vector<TinySTL::list<key_type>> buckets_;
size_type size_;
@@ -31,8 +61,8 @@ namespace TinySTL{
explicit Unordered_set(size_t bucket_count);
template<class InputIterator>
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;