完成插入和删除操作
This commit is contained in:
@@ -173,6 +173,70 @@ namespace TinySTL{
|
|||||||
Unordered_set<Key, Hash, KeyEqual, Allocator>::end(){
|
Unordered_set<Key, Hash, KeyEqual, Allocator>::end(){
|
||||||
return iterator(buckets_.size() - 1, buckets_[buckets_.size() - 1].end(), this);
|
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
|
#endif
|
||||||
@@ -14,6 +14,9 @@ namespace TinySTL{
|
|||||||
template<class Key, class ListIterator, class Hash = std::hash<Key>,
|
template<class Key, class ListIterator, class Hash = std::hash<Key>,
|
||||||
class KeyEqual = TinySTL::equal_to<Key>, class Allocator = TinySTL::allocator < Key >>
|
class KeyEqual = TinySTL::equal_to<Key>, class Allocator = TinySTL::allocator < Key >>
|
||||||
class ust_iterator{
|
class ust_iterator{
|
||||||
|
private:
|
||||||
|
template<class Key, class Hash, class KeyEqual, class Allocator>
|
||||||
|
friend class Unordered_set;
|
||||||
private:
|
private:
|
||||||
typedef Unordered_set<Key, Hash, KeyEqual, Allocator>* cntrPtr;
|
typedef Unordered_set<Key, Hash, KeyEqual, Allocator>* cntrPtr;
|
||||||
size_t bucket_index_;
|
size_t bucket_index_;
|
||||||
@@ -51,6 +54,7 @@ namespace TinySTL{
|
|||||||
typedef Allocator allocator_type;
|
typedef Allocator allocator_type;
|
||||||
typedef value_type& reference;
|
typedef value_type& reference;
|
||||||
typedef const value_type& const_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;
|
typedef Detail::ust_iterator<Key, typename TinySTL::list<key_type>::iterator, Hash, KeyEqual, Allocator> iterator;
|
||||||
private:
|
private:
|
||||||
TinySTL::vector<TinySTL::list<key_type>> buckets_;
|
TinySTL::vector<TinySTL::list<key_type>> buckets_;
|
||||||
@@ -73,6 +77,17 @@ namespace TinySTL{
|
|||||||
|
|
||||||
iterator begin();
|
iterator begin();
|
||||||
iterator end();
|
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;
|
haser hash_function()const;
|
||||||
key_equal key_eq()const;
|
key_equal key_eq()const;
|
||||||
|
|||||||
Reference in New Issue
Block a user