完成插入和删除操作

This commit is contained in:
邹晓航
2015-01-19 15:07:54 +08:00
parent c42b717783
commit 7727f398ad
2 changed files with 79 additions and 0 deletions

View File

@@ -173,6 +173,70 @@ namespace TinySTL{
Unordered_set<Key, Hash, KeyEqual, Allocator>::end(){
return iterator(buckets_.size() - 1, buckets_[buckets_.size() - 1].end(), this);
}
template<class Key, class Hash, class KeyEqual, class Allocator>
typename Unordered_set<Key, Hash, KeyEqual, Allocator>::local_iterator
Unordered_set<Key, Hash, KeyEqual, Allocator>::begin(size_type i){
return buckets_[i].begin();
}
template<class Key, class Hash, class KeyEqual, class Allocator>
typename Unordered_set<Key, Hash, KeyEqual, Allocator>::local_iterator
Unordered_set<Key, Hash, KeyEqual, Allocator>::end(size_type i){
return buckets_[i].end();
}
template<class Key, class Hash, class KeyEqual, class Allocator>
typename Unordered_set<Key, Hash, KeyEqual, Allocator>::iterator
Unordered_set<Key, Hash, KeyEqual, Allocator>::find(const key_type& key){
auto index = bucket_index(key);
for (auto it = begin(index); it != end(index); ++it){
if (key_equal()(key, *it))
return iterator(index, it, this);
}
return end();
}
template<class Key, class Hash, class KeyEqual, class Allocator>
typename Unordered_set<Key, Hash, KeyEqual, Allocator>::size_type
Unordered_set<Key, Hash, KeyEqual, Allocator>::count(const key_type& key){
auto it = find(key);
return it == end() ? 0 : 1;
}
template<class Key, class Hash, class KeyEqual, class Allocator>
TinySTL::pair<typename Unordered_set<Key, Hash, KeyEqual, Allocator>::iterator, bool>
Unordered_set<Key, Hash, KeyEqual, Allocator>::insert(const value_type& val){
if (!has_key(val)){
auto index = bucket_index(val);
buckets_[index].push_front(val);
++size_;
return TinySTL::pair<iterator, bool>(iterator(index, buckets_[index].begin(), this), true);
}
return TinySTL::pair<iterator, bool>(end(), false);
}
template<class Key, class Hash, class KeyEqual, class Allocator>
template<class InputIterator>
void Unordered_set<Key, Hash, KeyEqual, Allocator>::insert(InputIterator first, InputIterator last){
for (; first != last; ++first){
insert(*first);
}
}
template<class Key, class Hash, class KeyEqual, class Allocator>
typename Unordered_set<Key, Hash, KeyEqual, Allocator>::iterator
Unordered_set<Key, Hash, KeyEqual, Allocator>::erase(iterator position){
--size_;
auto t = position++;
auto index = t.bucket_index_;
auto it = buckets_[index].erase(t.iterator_);
return position;
}
template<class Key, class Hash, class KeyEqual, class Allocator>
typename Unordered_set<Key, Hash, KeyEqual, Allocator>::size_type
Unordered_set<Key, Hash, KeyEqual, Allocator>::erase(const key_type& key){
auto it = find(key);
if (it == end()){
return 0;
}else{
erase(it);
return 1;
}
}
}
#endif

View File

@@ -14,6 +14,9 @@ namespace TinySTL{
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:
template<class Key, class Hash, class KeyEqual, class Allocator>
friend class Unordered_set;
private:
typedef Unordered_set<Key, Hash, KeyEqual, Allocator>* cntrPtr;
size_t bucket_index_;
@@ -51,6 +54,7 @@ namespace TinySTL{
typedef Allocator allocator_type;
typedef value_type& reference;
typedef const value_type& const_reference;
typedef typename TinySTL::list<key_type>::iterator local_iterator;
typedef Detail::ust_iterator<Key, typename TinySTL::list<key_type>::iterator, Hash, KeyEqual, Allocator> iterator;
private:
TinySTL::vector<TinySTL::list<key_type>> buckets_;
@@ -73,6 +77,17 @@ namespace TinySTL{
iterator begin();
iterator end();
local_iterator begin(size_type i);
local_iterator end(size_type i);
iterator find(const key_type& key);
size_type count(const key_type& key);
TinySTL::pair<iterator, bool> insert(const value_type& val);
template<class InputIterator>
void insert(InputIterator first, InputIterator last);
iterator erase(iterator position);
size_type erase(const key_type& key);
haser hash_function()const;
key_equal key_eq()const;