update unordered_map.h

main
Shiina_cd 7 months ago
parent 427bfa3f8c
commit ebb9e68a7a

@ -1,4 +1,4 @@
#ifndef MYTINYSTL_UNORDERED_MAP_H_ #ifndef MYTINYSTL_UNORDERED_MAP_H_ // 预处理指令,防止头文件被重复包含
#define MYTINYSTL_UNORDERED_MAP_H_ #define MYTINYSTL_UNORDERED_MAP_H_
// 这个头文件包含两个模板类 unordered_map 和 unordered_multimap // 这个头文件包含两个模板类 unordered_map 和 unordered_multimap
@ -12,9 +12,9 @@
// * emplace_hint // * emplace_hint
// * insert // * insert
#include "hashtable.h" #include "hashtable.h" // 包含自定义的 hashtable 头文件
namespace mystl namespace mystl // 命名空间 mystl用于封装自定义的 STL 组件
{ {
// 模板类 unordered_map键值不允许重复 // 模板类 unordered_map键值不允许重复
@ -30,7 +30,7 @@ private:
public: public:
// 使用 hashtable 的型别 // 使用 hashtable 的型别
// 以下定义了与 hashtable 相关的类型别名,方便在 unordered_map 中使用
typedef typename base_type::allocator_type allocator_type; typedef typename base_type::allocator_type allocator_type;
typedef typename base_type::key_type key_type; typedef typename base_type::key_type key_type;
typedef typename base_type::mapped_type mapped_type; typedef typename base_type::mapped_type mapped_type;
@ -50,16 +50,17 @@ public:
typedef typename base_type::local_iterator local_iterator; typedef typename base_type::local_iterator local_iterator;
typedef typename base_type::const_local_iterator const_local_iterator; typedef typename base_type::const_local_iterator const_local_iterator;
allocator_type get_allocator() const { return ht_.get_allocator(); } allocator_type get_allocator() const { return ht_.get_allocator(); } // 获取分配器
public: public:
// 构造、复制、移动、析构函数 // 构造、复制、移动、析构函数
// 默认构造函数,初始化一个空的 unordered_map
unordered_map() unordered_map()
:ht_(100, Hash(), KeyEqual()) :ht_(100, Hash(), KeyEqual())
{ {
} }
// 带桶数量的构造函数,允许自定义哈希函数和键值比较函数
explicit unordered_map(size_type bucket_count, explicit unordered_map(size_type bucket_count,
const Hash& hash = Hash(), const Hash& hash = Hash(),
const KeyEqual& equal = KeyEqual()) const KeyEqual& equal = KeyEqual())
@ -67,6 +68,7 @@ public:
{ {
} }
// 范围构造函数,从给定的迭代器范围构造 unordered_map
template <class InputIterator> template <class InputIterator>
unordered_map(InputIterator first, InputIterator last, unordered_map(InputIterator first, InputIterator last,
const size_type bucket_count = 100, const size_type bucket_count = 100,
@ -78,6 +80,7 @@ public:
ht_.insert_unique_noresize(*first); ht_.insert_unique_noresize(*first);
} }
// 初始化列表构造函数,从给定的值初始化 unordered_map
unordered_map(std::initializer_list<value_type> ilist, unordered_map(std::initializer_list<value_type> ilist,
const size_type bucket_count = 100, const size_type bucket_count = 100,
const Hash& hash = Hash(), const Hash& hash = Hash(),
@ -88,185 +91,295 @@ public:
ht_.insert_unique_noresize(*first); ht_.insert_unique_noresize(*first);
} }
// 复制构造函数
unordered_map(const unordered_map& rhs) unordered_map(const unordered_map& rhs)
:ht_(rhs.ht_) :ht_(rhs.ht_)
{ {
} }
// 移动构造函数
unordered_map(unordered_map&& rhs) noexcept unordered_map(unordered_map&& rhs) noexcept
:ht_(mystl::move(rhs.ht_)) :ht_(mystl::move(rhs.ht_))
{ {
} }
}
// 赋值运算符重载
// 复制赋值运算符,将右侧对象的值赋给当前对象
unordered_map& operator=(const unordered_map& rhs) unordered_map& operator=(const unordered_map& rhs)
{ {
ht_ = rhs.ht_; ht_ = rhs.ht_;
return *this; return *this;
} }
// 移动赋值运算符,将右侧对象的值移动到当前对象
unordered_map& operator=(unordered_map&& rhs) unordered_map& operator=(unordered_map&& rhs)
{ {
ht_ = mystl::move(rhs.ht_); ht_ = mystl::move(rhs.ht_);
return *this; return *this;
} }
// 从初始化列表赋值
unordered_map& operator=(std::initializer_list<value_type> ilist) unordered_map& operator=(std::initializer_list<value_type> ilist)
{ {
ht_.clear(); ht_.clear(); // 清空当前容器
ht_.reserve(ilist.size()); ht_.reserve(ilist.size()); // 预留足够的空间
for (auto first = ilist.begin(), last = ilist.end(); first != last; ++first) for (auto first = ilist.begin(), last = ilist.end(); first != last; ++first)
ht_.insert_unique_noresize(*first); ht_.insert_unique_noresize(*first); // 插入元素,不调整桶大小
return *this; return *this;
} }
// 默认析构函数
~unordered_map() = default; ~unordered_map() = default;
// 迭代器相关 // 迭代器相关
// 返回指向第一个元素的迭代器
iterator begin() noexcept iterator begin() noexcept
{ return ht_.begin(); } {
return ht_.begin();
}
// 返回指向第一个元素的常量迭代器
const_iterator begin() const noexcept const_iterator begin() const noexcept
{ return ht_.begin(); } {
return ht_.begin();
}
// 返回指向最后一个元素的迭代器
iterator end() noexcept iterator end() noexcept
{ return ht_.end(); } {
return ht_.end();
}
// 返回指向最后一个元素的常量迭代器
const_iterator end() const noexcept const_iterator end() const noexcept
{ return ht_.end(); } {
return ht_.end();
}
// 返回指向第一个元素的常量迭代器cbegin 和 cend 是 C++11 新增的,用于明确表达“常量迭代器”)
const_iterator cbegin() const noexcept const_iterator cbegin() const noexcept
{ return ht_.cbegin(); } {
return ht_.cbegin();
}
const_iterator cend() const noexcept const_iterator cend() const noexcept
{ return ht_.cend(); } {
return ht_.cend();
}
// 容量相关 // 容量相关
// 检查容器是否为空
bool empty() const noexcept { return ht_.empty(); } bool empty() const noexcept { return ht_.empty(); }
// 返回容器中的元素数量
size_type size() const noexcept { return ht_.size(); } size_type size() const noexcept { return ht_.size(); }
// 返回容器能容纳的最大元素数量
size_type max_size() const noexcept { return ht_.max_size(); } size_type max_size() const noexcept { return ht_.max_size(); }
// 修改容器操作 // 修改容器操作
// empalce / empalce_hint // emplace / emplace_hint
// 就地构造元素,返回插入的迭代器和是否插入的布尔值
template <class ...Args> template <class ...Args>
pair<iterator, bool> emplace(Args&& ...args) pair<iterator, bool> emplace(Args&& ...args)
{ return ht_.emplace_unique(mystl::forward<Args>(args)...); } {
return ht_.emplace_unique(mystl::forward<Args>(args)...);
}
// 就地构造元素,并使用 hint 提示插入位置,返回插入的迭代器
template <class ...Args> template <class ...Args>
iterator emplace_hint(const_iterator hint, Args&& ...args) iterator emplace_hint(const_iterator hint, Args&& ...args)
{ return ht_.emplace_unique_use_hint(hint, mystl::forward<Args>(args)...); } {
return ht_.emplace_unique_use_hint(hint, mystl::forward<Args>(args)...);
}
// insert // insert
// 插入元素,返回插入的迭代器和是否插入的布尔值
pair<iterator, bool> insert(const value_type& value) pair<iterator, bool> insert(const value_type& value)
{ return ht_.insert_unique(value); } {
return ht_.insert_unique(value);
}
// 插入移动构造的元素
pair<iterator, bool> insert(value_type&& value) pair<iterator, bool> insert(value_type&& value)
{ return ht_.emplace_unique(mystl::move(value)); } {
return ht_.emplace_unique(mystl::move(value));
}
// 使用 hint 提示插入位置
iterator insert(const_iterator hint, const value_type& value) iterator insert(const_iterator hint, const value_type& value)
{ return ht_.insert_unique_use_hint(hint, value); } {
return ht_.insert_unique_use_hint(hint, value);
}
// 使用 hint 提示插入位置,并插入移动构造的元素
iterator insert(const_iterator hint, value_type&& value) iterator insert(const_iterator hint, value_type&& value)
{ return ht_.emplace_unique_use_hint(hint, mystl::move(value)); } {
return ht_.emplace_unique_use_hint(hint, mystl::move(value));
}
// 从迭代器范围插入元素
template <class InputIterator> template <class InputIterator>
void insert(InputIterator first, InputIterator last) void insert(InputIterator first, InputIterator last)
{ ht_.insert_unique(first, last); } {
ht_.insert_unique(first, last);
}
// erase / clear // erase / clear
// 删除指定迭代器位置的元素
void erase(iterator it) void erase(iterator it)
{ ht_.erase(it); } {
ht_.erase(it);
}
// 删除指定范围的元素
void erase(iterator first, iterator last) void erase(iterator first, iterator last)
{ ht_.erase(first, last); } {
ht_.erase(first, last);
}
// 删除指定键的元素,并返回被删除的元素数量
size_type erase(const key_type& key) size_type erase(const key_type& key)
{ return ht_.erase_unique(key); } {
return ht_.erase_unique(key);
}
// 清空容器
void clear() void clear()
{ ht_.clear(); } {
ht_.clear();
}
// 交换两个 unordered_map 对象的内容
void swap(unordered_map& other) noexcept void swap(unordered_map& other) noexcept
{ ht_.swap(other.ht_); } {
ht_.swap(other.ht_);
}
// 查找相关 // 查找相关
// 返回指定键的值的引用,如果键不存在则抛出异常
mapped_type& at(const key_type& key) mapped_type& at(const key_type& key)
{ {
iterator it = ht_.find(key); iterator it = ht_.find(key);
THROW_OUT_OF_RANGE_IF(it.node == nullptr, "unordered_map<Key, T> no such element exists"); THROW_OUT_OF_RANGE_IF(it.node == nullptr, "unordered_map<Key, T> no such element exists");
return it->second; return it->second;
} }
// 返回指定键的值的常量引用,如果键不存在则抛出异常
const mapped_type& at(const key_type& key) const const mapped_type& at(const key_type& key) const
{ {
iterator it = ht_.find(key); iterator it = ht_.find(key);
THROW_OUT_OF_RANGE_IF(it.node == nullptr, "unordered_map<Key, T> no such element exists"); THROW_OUT_OF_RANGE_IF(it.node == nullptr, "unordered_map<Key, T> no such element exists");
return it->second; return it->second;
} }
// 下标操作符重载
// 通过键查找或创建元素,并返回元素的引用
mapped_type& operator[](const key_type& key) mapped_type& operator[](const key_type& key)
{ {
iterator it = ht_.find(key); iterator it = ht_.find(key);
if (it.node == nullptr) if (it.node == nullptr)
it = ht_.emplace_unique(key, T{}).first; it = ht_.emplace_unique(key, T{}).first; // 如果元素不存在,则插入新元素
return it->second; return it->second;
} }
// 下标操作符重载,用于移动构造的键
mapped_type& operator[](key_type&& key) mapped_type& operator[](key_type&& key)
{ {
iterator it = ht_.find(key); iterator it = ht_.find(key);
if (it.node == nullptr) if (it.node == nullptr)
it = ht_.emplace_unique(mystl::move(key), T{}).first; it = ht_.emplace_unique(mystl::move(key), T{}).first; // 如果元素不存在,则插入新元素
return it->second; return it->second;
} }
// 计算给定键的元素数量
size_type count(const key_type& key) const size_type count(const key_type& key) const
{ return ht_.count(key); } {
return ht_.count(key);
}
// 查找元素
iterator find(const key_type& key) iterator find(const key_type& key)
{ return ht_.find(key); } {
return ht_.find(key);
}
const_iterator find(const key_type& key) const const_iterator find(const key_type& key) const
{ return ht_.find(key); } {
return ht_.find(key);
}
// 返回给定键的元素范围
pair<iterator, iterator> equal_range(const key_type& key) pair<iterator, iterator> equal_range(const key_type& key)
{ return ht_.equal_range_unique(key); } {
return ht_.equal_range_unique(key);
}
pair<const_iterator, const_iterator> equal_range(const key_type& key) const pair<const_iterator, const_iterator> equal_range(const key_type& key) const
{ return ht_.equal_range_unique(key); } {
return ht_.equal_range_unique(key);
}
// bucket interface // 桶接口
// 返回指定桶的本地迭代器
local_iterator begin(size_type n) noexcept local_iterator begin(size_type n) noexcept
{ return ht_.begin(n); } {
return ht_.begin(n);
}
const_local_iterator begin(size_type n) const noexcept const_local_iterator begin(size_type n) const noexcept
{ return ht_.begin(n); } {
return ht_.begin(n);
}
const_local_iterator cbegin(size_type n) const noexcept const_local_iterator cbegin(size_type n) const noexcept
{ return ht_.cbegin(n); } {
return ht_.cbegin(n);
}
// 返回指定桶的本地迭代器
local_iterator end(size_type n) noexcept local_iterator end(size_type n) noexcept
{ return ht_.end(n); } {
return ht_.end(n);
}
const_local_iterator end(size_type n) const noexcept const_local_iterator end(size_type n) const noexcept
{ return ht_.end(n); } {
return ht_.end(n);
}
const_local_iterator cend(size_type n) const noexcept const_local_iterator cend(size_type n) const noexcept
{ return ht_.cend(n); } {
return ht_.cend(n);
}
// 返回桶的数量
size_type bucket_count() const noexcept size_type bucket_count() const noexcept
{ return ht_.bucket_count(); } {
return ht_.bucket_count();
}
size_type max_bucket_count() const noexcept size_type max_bucket_count() const noexcept
{ return ht_.max_bucket_count(); } {
return ht_.max_bucket_count();
}
// 返回指定桶的大小
size_type bucket_size(size_type n) const noexcept size_type bucket_size(size_type n) const noexcept
{ return ht_.bucket_size(n); } {
return ht_.bucket_size(n);
}
// 返回给定键的元素所在的桶编号
size_type bucket(const key_type& key) const size_type bucket(const key_type& key) const
{ return ht_.bucket(key); } {
return ht_.bucket(key);
}
// hash policy // 哈希策略
// 返回当前负载因子
float load_factor() const noexcept { return ht_.load_factor(); } float load_factor() const noexcept { return ht_.load_factor(); }
// 返回最大负载因子
float max_load_factor() const noexcept { return ht_.max_load_factor(); } float max_load_factor() const noexcept { return ht_.max_load_factor(); }
// 设置最大负载因子
void max_load_factor(float ml) { ht_.max_load_factor(ml); } void max_load_factor(float ml) { ht_.max_load_factor(ml); }
// 重新哈希,调整桶的数量
void rehash(size_type count) { ht_.rehash(count); } void rehash(size_type count) { ht_.rehash(count); }
// 调整桶的数量以至少容纳指定数量的元素
void reserve(size_type count) { ht_.reserve(count); } void reserve(size_type count) { ht_.reserve(count); }
// 返回哈希函数
hasher hash_fcn() const { return ht_.hash_fcn(); } hasher hash_fcn() const { return ht_.hash_fcn(); }
// 返回键比较函数
key_equal key_eq() const { return ht_.key_eq(); } key_equal key_eq() const { return ht_.key_eq(); }
// 友元函数,重载比较操作符
public: public:
friend bool operator==(const unordered_map& lhs, const unordered_map& rhs) friend bool operator==(const unordered_map& lhs, const unordered_map& rhs)
{ {
@ -293,16 +406,13 @@ bool operator!=(const unordered_map<Key, T, Hash, KeyEqual>& lhs,
return lhs != rhs; return lhs != rhs;
} }
// 重载 mystl 的 swap // 重载 mystl 的 swap 函数
template <class Key, class T, class Hash, class KeyEqual> template <class Key, class T, class Hash, class KeyEqual>
void swap(unordered_map<Key, T, Hash, KeyEqual>& lhs, void swap(unordered_map<Key, T, Hash, KeyEqual>& lhs,
unordered_map<Key, T, Hash, KeyEqual>& rhs) unordered_map<Key, T, Hash, KeyEqual>& rhs)
{ {
lhs.swap(rhs); lhs.swap(rhs);
} }
/*****************************************************************************************/
// 模板类 unordered_multimap键值允许重复 // 模板类 unordered_multimap键值允许重复
// 参数一代表键值类型,参数二代表实值类型,参数三代表哈希函数,缺省使用 mystl::hash // 参数一代表键值类型,参数二代表实值类型,参数三代表哈希函数,缺省使用 mystl::hash
// 参数四代表键值比较方式,缺省使用 mystl::equal_to // 参数四代表键值比较方式,缺省使用 mystl::equal_to
@ -316,6 +426,7 @@ private:
public: public:
// 使用 hashtable 的型别 // 使用 hashtable 的型别
// 以下定义了与 hashtable 相关的类型别名,方便在 unordered_multimap 中使用
typedef typename base_type::allocator_type allocator_type; typedef typename base_type::allocator_type allocator_type;
typedef typename base_type::key_type key_type; typedef typename base_type::key_type key_type;
typedef typename base_type::mapped_type mapped_type; typedef typename base_type::mapped_type mapped_type;
@ -335,16 +446,18 @@ public:
typedef typename base_type::local_iterator local_iterator; typedef typename base_type::local_iterator local_iterator;
typedef typename base_type::const_local_iterator const_local_iterator; typedef typename base_type::const_local_iterator const_local_iterator;
// 获取分配器
allocator_type get_allocator() const { return ht_.get_allocator(); } allocator_type get_allocator() const { return ht_.get_allocator(); }
public: public:
// 构造、复制、移动函数 // 构造、复制、移动函数
// 默认构造函数,初始化一个空的 unordered_multimap
unordered_multimap() unordered_multimap()
:ht_(100, Hash(), KeyEqual()) :ht_(100, Hash(), KeyEqual())
{ {
} }
// 带桶数量的构造函数,允许自定义哈希函数和键值比较函数
explicit unordered_multimap(size_type bucket_count, explicit unordered_multimap(size_type bucket_count,
const Hash& hash = Hash(), const Hash& hash = Hash(),
const KeyEqual& equal = KeyEqual()) const KeyEqual& equal = KeyEqual())
@ -352,6 +465,7 @@ public:
{ {
} }
// 范围构造函数,从给定的迭代器范围构造 unordered_multimap
template <class InputIterator> template <class InputIterator>
unordered_multimap(InputIterator first, InputIterator last, unordered_multimap(InputIterator first, InputIterator last,
const size_type bucket_count = 100, const size_type bucket_count = 100,
@ -360,9 +474,10 @@ public:
: ht_(mystl::max(bucket_count, static_cast<size_type>(mystl::distance(first, last))), hash, equal) : ht_(mystl::max(bucket_count, static_cast<size_type>(mystl::distance(first, last))), hash, equal)
{ {
for (; first != last; ++first) for (; first != last; ++first)
ht_.insert_multi_noresize(*first); ht_.insert_multi_noresize(*first); // 插入元素,不调整桶大小
} }
// 初始化列表构造函数,从给定的值初始化 unordered_multimap
unordered_multimap(std::initializer_list<value_type> ilist, unordered_multimap(std::initializer_list<value_type> ilist,
const size_type bucket_count = 100, const size_type bucket_count = 100,
const Hash& hash = Hash(), const Hash& hash = Hash(),
@ -370,18 +485,22 @@ public:
:ht_(mystl::max(bucket_count, static_cast<size_type>(ilist.size())), hash, equal) :ht_(mystl::max(bucket_count, static_cast<size_type>(ilist.size())), hash, equal)
{ {
for (auto first = ilist.begin(), last = ilist.end(); first != last; ++first) for (auto first = ilist.begin(), last = ilist.end(); first != last; ++first)
ht_.insert_multi_noresize(*first); ht_.insert_multi_noresize(*first); // 插入元素,不调整桶大小
} }
// 复制构造函数
unordered_multimap(const unordered_multimap& rhs) unordered_multimap(const unordered_multimap& rhs)
:ht_(rhs.ht_) :ht_(rhs.ht_)
{ {
} }
// 移动构造函数
unordered_multimap(unordered_multimap&& rhs) noexcept unordered_multimap(unordered_multimap&& rhs) noexcept
:ht_(mystl::move(rhs.ht_)) :ht_(mystl::move(rhs.ht_))
{ {
} }
// 赋值运算符重载
unordered_multimap& operator=(const unordered_multimap& rhs) unordered_multimap& operator=(const unordered_multimap& rhs)
{ {
ht_ = rhs.ht_; ht_ = rhs.ht_;
@ -393,171 +512,249 @@ public:
return *this; return *this;
} }
// 从初始化列表赋值
unordered_multimap& operator=(std::initializer_list<value_type> ilist) unordered_multimap& operator=(std::initializer_list<value_type> ilist)
{ {
ht_.clear(); ht_.clear(); // 清空当前容器
ht_.reserve(ilist.size()); ht_.reserve(ilist.size()); // 预留足够的空间
for (auto first = ilist.begin(), last = ilist.end(); first != last; ++first) for (auto first = ilist.begin(), last = ilist.end(); first != last; ++first)
ht_.insert_multi_noresize(*first); ht_.insert_multi_noresize(*first); // 插入元素,不调整桶大小
return *this; return *this;
} }
// 默认析构函数
~unordered_multimap() = default; ~unordered_multimap() = default;
};
// 迭代器相关 // 迭代器访问
// 返回指向第一个元素的迭代器
iterator begin() noexcept iterator begin() noexcept { return ht_.begin(); }
{ return ht_.begin(); } // 返回指向第一个元素的常量迭代器
const_iterator begin() const noexcept const_iterator begin() const noexcept { return ht_.begin(); }
{ return ht_.begin(); } // 返回指向最后一个元素的迭代器
iterator end() noexcept iterator end() noexcept { return ht_.end(); }
{ return ht_.end(); } // 返回指向最后一个元素的常量迭代器
const_iterator end() const noexcept const_iterator end() const noexcept { return ht_.end(); }
{ return ht_.end(); }
// 返回指向第一个元素的常量迭代器cbegin 和 cend 是 C++11 新增的,用于明确表达“常量迭代器”)
const_iterator cbegin() const noexcept const_iterator cbegin() const noexcept { return ht_.cbegin(); }
{ return ht_.cbegin(); } const_iterator cend() const noexcept { return ht_.cend(); }
const_iterator cend() const noexcept
{ return ht_.cend(); }
// 容量相关 // 容量相关
// 检查容器是否为空
bool empty() const noexcept { return ht_.empty(); } bool empty() const noexcept { return ht_.empty(); }
// 返回容器中的元素数量
size_type size() const noexcept { return ht_.size(); } size_type size() const noexcept { return ht_.size(); }
// 返回容器能容纳的最大元素数量
size_type max_size() const noexcept { return ht_.max_size(); } size_type max_size() const noexcept { return ht_.max_size(); }
// 修改容器相关 // 修改容器操作
// emplace / emplace_hint // emplace / emplace_hint
// 就地构造元素,返回插入的迭代器
template <class ...Args> template <class ...Args>
iterator emplace(Args&& ...args) iterator emplace(Args&& ...args) {
{ return ht_.emplace_multi(mystl::forward<Args>(args)...); } return ht_.emplace_multi(mystl::forward<Args>(args)...);
}
// 就地构造元素,并使用 hint 提示插入位置,返回插入的迭代器
template <class ...Args> template <class ...Args>
iterator emplace_hint(const_iterator hint, Args&& ...args) iterator emplace_hint(const_iterator hint, Args&& ...args) {
{ return ht_.emplace_multi_use_hint(hint, mystl::forward<Args>(args)...); } return ht_.emplace_multi_use_hint(hint, mystl::forward<Args>(args)...);
}
// insert // insert
// 插入元素,返回插入的迭代器
iterator insert(const value_type& value) {
return ht_.insert_multi(value);
}
// 插入移动构造的元素
iterator insert(value_type&& value) {
return ht_.emplace_multi(mystl::move(value));
}
iterator insert(const value_type& value) // 使用 hint 提示插入位置
{ return ht_.insert_multi(value); } iterator insert(const_iterator hint, const value_type& value) {
iterator insert(value_type&& value) return ht_.insert_multi_use_hint(hint, value);
{ return ht_.emplace_multi(mystl::move(value)); } }
// 使用 hint 提示插入位置,并插入移动构造的元素
iterator insert(const_iterator hint, const value_type& value) iterator insert(const_iterator hint, value_type&& value) {
{ return ht_.insert_multi_use_hint(hint, value); } return ht_.emplace_multi_use_hint(hint, mystl::move(value));
iterator insert(const_iterator hint, value_type&& value) }
{ return ht_.emplace_multi_use_hint(hint, mystl::move(value)); }
// 从迭代器范围插入元素
template <class InputIterator> template <class InputIterator>
void insert(InputIterator first, InputIterator last) void insert(InputIterator first, InputIterator last) {
{ ht_.insert_multi(first, last); } ht_.insert_multi(first, last);
}
// erase / clear // erase / clear
// 删除指定迭代器位置的元素
void erase(iterator it) {
ht_.erase(it);
}
// 删除指定范围的元素
void erase(iterator first, iterator last) {
ht_.erase(first, last);
}
void erase(iterator it) // 删除指定键的所有元素,并返回被删除的元素数量
{ ht_.erase(it); } size_type erase(const key_type& key) {
void erase(iterator first, iterator last) return ht_.erase_multi(key);
{ ht_.erase(first, last); } }
size_type erase(const key_type& key)
{ return ht_.erase_multi(key); }
void clear() // 清空容器
{ ht_.clear(); } void clear() {
ht_.clear();
}
void swap(unordered_multimap& other) noexcept // 交换两个 unordered_multimap 对象的内容
{ ht_.swap(other.ht_); } void swap(unordered_multimap& other) noexcept {
ht_.swap(other.ht_);
}
// 查找相关 // 查找相关
// 返回指定键的元素数量
size_type count(const key_type& key) const {
return ht_.count(key);
}
size_type count(const key_type& key) const // 查找元素
{ return ht_.count(key); } iterator find(const key_type& key) {
return ht_.find(key);
iterator find(const key_type& key) }
{ return ht_.find(key); } const_iterator find(const key_type& key) const {
const_iterator find(const key_type& key) const return ht_.find(key);
{ return ht_.find(key); } }
pair<iterator, iterator> equal_range(const key_type& key)
{ return ht_.equal_range_multi(key); }
pair<const_iterator, const_iterator> equal_range(const key_type& key) const
{ return ht_.equal_range_multi(key); }
// bucket interface // 返回给定键的元素范围
pair<iterator, iterator> equal_range(const key_type& key) {
return ht_.equal_range_multi(key);
}
pair<const_iterator, const_iterator> equal_range(const key_type& key) const {
return ht_.equal_range_multi(key);
}
local_iterator begin(size_type n) noexcept // 桶接口
{ return ht_.begin(n); }
const_local_iterator begin(size_type n) const noexcept
{ return ht_.begin(n); }
const_local_iterator cbegin(size_type n) const noexcept
{ return ht_.cbegin(n); }
local_iterator end(size_type n) noexcept // 返回指定桶的本地迭代器
{ return ht_.end(n); } local_iterator begin(size_type n) noexcept {
const_local_iterator end(size_type n) const noexcept return ht_.begin(n);
{ return ht_.end(n); } }
const_local_iterator cend(size_type n) const noexcept const_local_iterator begin(size_type n) const noexcept {
{ return ht_.cend(n); } return ht_.begin(n);
}
const_local_iterator cbegin(size_type n) const noexcept {
return ht_.cbegin(n);
}
size_type bucket_count() const noexcept // 返回指定桶的本地迭代器
{ return ht_.bucket_count(); } local_iterator end(size_type n) noexcept {
size_type max_bucket_count() const noexcept return ht_.end(n);
{ return ht_.max_bucket_count(); } }
const_local_iterator end(size_type n) const noexcept {
return ht_.end(n);
}
const_local_iterator cend(size_type n) const noexcept {
return ht_.cend(n);
}
size_type bucket_size(size_type n) const noexcept // 返回桶的数量
{ return ht_.bucket_size(n); } size_type bucket_count() const noexcept {
size_type bucket(const key_type& key) const return ht_.bucket_count();
{ return ht_.bucket(key); } }
size_type max_bucket_count() const noexcept {
return ht_.max_bucket_count();
}
// hash policy // 返回指定桶的大小
size_type bucket_size(size_type n) const noexcept {
return ht_.bucket_size(n);
}
// 返回给定键的元素所在的桶编号
size_type bucket(const key_type& key) const {
return ht_.bucket(key);
}
float load_factor() const noexcept { return ht_.load_factor(); } // 哈希策略
float max_load_factor() const noexcept { return ht_.max_load_factor(); } // 返回当前负载因子
void max_load_factor(float ml) { ht_.max_load_factor(ml); } float load_factor() const noexcept {
return ht_.load_factor();
}
void rehash(size_type count) { ht_.rehash(count); } // 返回最大负载因子
void reserve(size_type count) { ht_.reserve(count); } float max_load_factor() const noexcept {
return ht_.max_load_factor();
}
// 设置最大负载因子
void max_load_factor(float ml) {
ht_.max_load_factor(ml);
}
hasher hash_fcn() const { return ht_.hash_fcn(); } // 重新哈希,调整桶的数量
key_equal key_eq() const { return ht_.key_eq(); } void rehash(size_type count) {
ht_.rehash(count);
}
// 调整桶的数量以至少容纳指定数量的元素
void reserve(size_type count) {
ht_.reserve(count);
}
public: // 返回哈希函数
hasher hash_fcn() const {
return ht_.hash_fcn();
}
// 返回键比较函数
key_equal key_eq() const {
return ht_.key_eq();
}
// 友元函数,重载比较操作符 ==
// 比较两个 unordered_multimap 对象是否相等
friend bool operator==(const unordered_multimap& lhs, const unordered_multimap& rhs) friend bool operator==(const unordered_multimap& lhs, const unordered_multimap& rhs)
{ {
// 调用底层 hashtable 的 equal_range_multi 方法比较两个容器的元素是否相等
return lhs.ht_.equal_range_multi(rhs.ht_); return lhs.ht_.equal_range_multi(rhs.ht_);
} }
// 友元函数,重载比较操作符 !=
// 比较两个 unordered_multimap 对象是否不相等
friend bool operator!=(const unordered_multimap& lhs, const unordered_multimap& rhs) friend bool operator!=(const unordered_multimap& lhs, const unordered_multimap& rhs)
{ {
// 直接返回 == 操作符的相反结果
return !lhs.ht_.equal_range_multi(rhs.ht_); return !lhs.ht_.equal_range_multi(rhs.ht_);
} }
};
// 重载比较操作符 // 重载比较操作符 ==
// 模板特化,使得可以不写明完整类型定义直接比较两个 unordered_multimap 对象是否相等
template <class Key, class T, class Hash, class KeyEqual> template <class Key, class T, class Hash, class KeyEqual>
bool operator==(const unordered_multimap<Key, T, Hash, KeyEqual>& lhs, bool operator==(const unordered_multimap<Key, T, Hash, KeyEqual>& lhs,
const unordered_multimap<Key, T, Hash, KeyEqual>& rhs) const unordered_multimap<Key, T, Hash, KeyEqual>& rhs)
{ {
// 调用友元函数实现的比较逻辑
return lhs == rhs; return lhs == rhs;
} }
// 重载比较操作符 !=
// 模板特化,使得可以不写明完整类型定义直接比较两个 unordered_multimap 对象是否不相等
template <class Key, class T, class Hash, class KeyEqual> template <class Key, class T, class Hash, class KeyEqual>
bool operator!=(const unordered_multimap<Key, T, Hash, KeyEqual>& lhs, bool operator!=(const unordered_multimap<Key, T, Hash, KeyEqual>& lhs,
const unordered_multimap<Key, T, Hash, KeyEqual>& rhs) const unordered_multimap<Key, T, Hash, KeyEqual>& rhs)
{ {
// 调用友元函数实现的比较逻辑
return lhs != rhs; return lhs != rhs;
} }
// 重载 mystl 的 swap // 重载 mystl 的 swap 函数
// 模板特化,使得可以不写明完整类型定义直接交换两个 unordered_multimap 对象的内容
template <class Key, class T, class Hash, class KeyEqual> template <class Key, class T, class Hash, class KeyEqual>
void swap(unordered_multimap<Key, T, Hash, KeyEqual>& lhs, void swap(unordered_multimap<Key, T, Hash, KeyEqual>& lhs,
unordered_multimap<Key, T, Hash, KeyEqual>& rhs) unordered_multimap<Key, T, Hash, KeyEqual>& rhs)
{ {
// 调用 unordered_multimap 类的 swap 成员函数
lhs.swap(rhs); lhs.swap(rhs);
} }
} // namespace mystl } // namespace mystl 结束命名空间 mystl
#endif // !MYTINYSTL_UNORDERED_MAP_H_
#endif // !MYTINYSTL_UNORDERED_MAP_H_ 结束头文件保护

Loading…
Cancel
Save