|
|
|
@ -31,39 +31,39 @@ private:
|
|
|
|
|
public:
|
|
|
|
|
// 使用 hashtable 的型别
|
|
|
|
|
|
|
|
|
|
typedef typename base_type::allocator_type allocator_type;
|
|
|
|
|
typedef typename base_type::key_type key_type;
|
|
|
|
|
typedef typename base_type::mapped_type mapped_type;
|
|
|
|
|
typedef typename base_type::value_type value_type;
|
|
|
|
|
typedef typename base_type::hasher hasher;
|
|
|
|
|
typedef typename base_type::key_equal key_equal;
|
|
|
|
|
|
|
|
|
|
typedef typename base_type::size_type size_type;
|
|
|
|
|
typedef typename base_type::difference_type difference_type;
|
|
|
|
|
typedef typename base_type::pointer pointer;
|
|
|
|
|
typedef typename base_type::const_pointer const_pointer;
|
|
|
|
|
typedef typename base_type::reference reference;
|
|
|
|
|
typedef typename base_type::const_reference const_reference;
|
|
|
|
|
|
|
|
|
|
typedef typename base_type::iterator iterator;
|
|
|
|
|
typedef typename base_type::const_iterator const_iterator;
|
|
|
|
|
typedef typename base_type::local_iterator local_iterator;
|
|
|
|
|
typedef typename base_type::const_local_iterator const_local_iterator;
|
|
|
|
|
|
|
|
|
|
allocator_type get_allocator() const { return ht_.get_allocator(); }
|
|
|
|
|
typedef typename base_type::allocator_type allocator_type; // 分配器类型
|
|
|
|
|
typedef typename base_type::key_type key_type; // 键类型
|
|
|
|
|
typedef typename base_type::mapped_type mapped_type; // 值类型
|
|
|
|
|
typedef typename base_type::value_type value_type; // 键值对类型
|
|
|
|
|
typedef typename base_type::hasher hasher; // 哈希函数类型
|
|
|
|
|
typedef typename base_type::key_equal key_equal; // 键比较函数类型
|
|
|
|
|
|
|
|
|
|
typedef typename base_type::size_type size_type; // 大小类型
|
|
|
|
|
typedef typename base_type::difference_type difference_type;// 差值类型
|
|
|
|
|
typedef typename base_type::pointer pointer; // 指针类型
|
|
|
|
|
typedef typename base_type::const_pointer const_pointer; // 常量指针类型
|
|
|
|
|
typedef typename base_type::reference reference; // 引用类型
|
|
|
|
|
typedef typename base_type::const_reference const_reference;// 常量引用类型
|
|
|
|
|
|
|
|
|
|
typedef typename base_type::iterator iterator; // 迭代器类型
|
|
|
|
|
typedef typename base_type::const_iterator const_iterator;// 常量迭代器类型
|
|
|
|
|
typedef typename base_type::local_iterator local_iterator;// 本地迭代器类型
|
|
|
|
|
typedef typename base_type::const_local_iterator const_local_iterator;// 常量本地迭代器类型
|
|
|
|
|
|
|
|
|
|
allocator_type get_allocator() const { return ht_.get_allocator(); } // 获取分配器
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
// 构造、复制、移动、析构函数
|
|
|
|
|
|
|
|
|
|
unordered_map()
|
|
|
|
|
:ht_(100, Hash(), KeyEqual())
|
|
|
|
|
:ht_(100, Hash(), KeyEqual()) // 默认构造函数,初始化哈希表,桶的数量为100
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
explicit unordered_map(size_type bucket_count,
|
|
|
|
|
const Hash& hash = Hash(),
|
|
|
|
|
const KeyEqual& equal = KeyEqual())
|
|
|
|
|
:ht_(bucket_count, hash, equal)
|
|
|
|
|
:ht_(bucket_count, hash, equal) // 构造函数,指定桶的数量和哈希函数、键比较函数
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -72,7 +72,7 @@ public:
|
|
|
|
|
const size_type bucket_count = 100,
|
|
|
|
|
const Hash& hash = Hash(),
|
|
|
|
|
const KeyEqual& equal = KeyEqual())
|
|
|
|
|
: 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)
|
|
|
|
|
ht_.insert_unique_noresize(*first);
|
|
|
|
@ -82,33 +82,33 @@ public:
|
|
|
|
|
const size_type bucket_count = 100,
|
|
|
|
|
const Hash& hash = Hash(),
|
|
|
|
|
const KeyEqual& equal = KeyEqual())
|
|
|
|
|
: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)
|
|
|
|
|
ht_.insert_unique_noresize(*first);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
unordered_map(const unordered_map& rhs)
|
|
|
|
|
:ht_(rhs.ht_)
|
|
|
|
|
:ht_(rhs.ht_) // 拷贝构造函数
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
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_;
|
|
|
|
|
return *this;
|
|
|
|
|
}
|
|
|
|
|
unordered_map& operator=(unordered_map&& rhs)
|
|
|
|
|
unordered_map& operator=(unordered_map&& rhs) // 移动赋值运算符
|
|
|
|
|
{
|
|
|
|
|
ht_ = mystl::move(rhs.ht_);
|
|
|
|
|
return *this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
unordered_map& operator=(std::initializer_list<value_type> ilist)
|
|
|
|
|
unordered_map& operator=(std::initializer_list<value_type> ilist) // 赋值运算符,从初始化列表赋值
|
|
|
|
|
{
|
|
|
|
|
ht_.clear();
|
|
|
|
|
ht_.reserve(ilist.size());
|
|
|
|
@ -117,77 +117,113 @@ public:
|
|
|
|
|
return *this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
~unordered_map() = default;
|
|
|
|
|
~unordered_map() = default; // 析构函数
|
|
|
|
|
|
|
|
|
|
// 迭代器相关
|
|
|
|
|
|
|
|
|
|
iterator begin() noexcept
|
|
|
|
|
{ return ht_.begin(); }
|
|
|
|
|
{
|
|
|
|
|
return ht_.begin();
|
|
|
|
|
} // 返回指向第一个元素的迭代器
|
|
|
|
|
const_iterator begin() const noexcept
|
|
|
|
|
{ return ht_.begin(); }
|
|
|
|
|
{
|
|
|
|
|
return ht_.begin();
|
|
|
|
|
} // 返回指向第一个元素的常量迭代器
|
|
|
|
|
iterator end() noexcept
|
|
|
|
|
{ return ht_.end(); }
|
|
|
|
|
{
|
|
|
|
|
return ht_.end();
|
|
|
|
|
} // 返回指向最后一个元素的迭代器
|
|
|
|
|
const_iterator end() const noexcept
|
|
|
|
|
{ return ht_.end(); }
|
|
|
|
|
{
|
|
|
|
|
return ht_.end();
|
|
|
|
|
} // 返回指向最后一个元素的常量迭代器
|
|
|
|
|
|
|
|
|
|
const_iterator cbegin() const noexcept
|
|
|
|
|
{ return ht_.cbegin(); }
|
|
|
|
|
{
|
|
|
|
|
return ht_.cbegin();
|
|
|
|
|
} // 返回指向第一个元素的常量迭代器
|
|
|
|
|
const_iterator cend() const noexcept
|
|
|
|
|
{ return ht_.cend(); }
|
|
|
|
|
{
|
|
|
|
|
return ht_.cend();
|
|
|
|
|
} // 返回指向最后一个元素的常量迭代器
|
|
|
|
|
|
|
|
|
|
// 容量相关
|
|
|
|
|
|
|
|
|
|
bool empty() const noexcept { return ht_.empty(); }
|
|
|
|
|
size_type size() const noexcept { return ht_.size(); }
|
|
|
|
|
size_type max_size() const noexcept { return ht_.max_size(); }
|
|
|
|
|
bool empty() const noexcept { return ht_.empty(); } // 检查容器是否为空
|
|
|
|
|
size_type size() const noexcept { return ht_.size(); } // 获取容器的大小
|
|
|
|
|
size_type max_size() const noexcept { return ht_.max_size(); } // 获取容器的最大可能大小
|
|
|
|
|
|
|
|
|
|
// 修改容器操作
|
|
|
|
|
|
|
|
|
|
// empalce / empalce_hint
|
|
|
|
|
// emplace / emplace_hint
|
|
|
|
|
|
|
|
|
|
template <class ...Args>
|
|
|
|
|
pair<iterator, bool> emplace(Args&& ...args)
|
|
|
|
|
{ return ht_.emplace_unique(mystl::forward<Args>(args)...); }
|
|
|
|
|
pair<iterator, bool> emplace(Args&& ...args) // 就地构造元素并插入
|
|
|
|
|
{
|
|
|
|
|
return ht_.emplace_unique(mystl::forward<Args>(args)...);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template <class ...Args>
|
|
|
|
|
iterator emplace_hint(const_iterator hint, Args&& ...args)
|
|
|
|
|
{ return ht_.emplace_unique_use_hint(hint, mystl::forward<Args>(args)...); }
|
|
|
|
|
iterator emplace_hint(const_iterator hint, Args&& ...args) // 就地构造元素并插入,使用hint作为插入位置的提示
|
|
|
|
|
{
|
|
|
|
|
return ht_.emplace_unique_use_hint(hint, mystl::forward<Args>(args)...);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// insert
|
|
|
|
|
|
|
|
|
|
pair<iterator, bool> insert(const value_type& value)
|
|
|
|
|
{ return ht_.insert_unique(value); }
|
|
|
|
|
pair<iterator, bool> insert(value_type&& value)
|
|
|
|
|
{ return ht_.emplace_unique(mystl::move(value)); }
|
|
|
|
|
pair<iterator, bool> insert(const value_type& value) // 插入元素
|
|
|
|
|
{
|
|
|
|
|
return ht_.insert_unique(value);
|
|
|
|
|
}
|
|
|
|
|
pair<iterator, bool> insert(value_type&& value) // 插入右值引用元素
|
|
|
|
|
{
|
|
|
|
|
return ht_.emplace_unique(mystl::move(value));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
iterator insert(const_iterator hint, const value_type& value)
|
|
|
|
|
{ return ht_.insert_unique_use_hint(hint, value); }
|
|
|
|
|
iterator insert(const_iterator hint, value_type&& value)
|
|
|
|
|
{ return ht_.emplace_unique_use_hint(hint, mystl::move(value)); }
|
|
|
|
|
iterator insert(const_iterator hint, const value_type& value) // 在hint位置插入元素
|
|
|
|
|
{
|
|
|
|
|
return ht_.insert_unique_use_hint(hint, value);
|
|
|
|
|
}
|
|
|
|
|
iterator insert(const_iterator hint, value_type&& value) // 在hint位置插入右值引用元素
|
|
|
|
|
{
|
|
|
|
|
return ht_.emplace_unique_use_hint(hint, mystl::move(value));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template <class InputIterator>
|
|
|
|
|
void insert(InputIterator first, InputIterator last)
|
|
|
|
|
{ ht_.insert_unique(first, last); }
|
|
|
|
|
void insert(InputIterator first, InputIterator last) // 从迭代器范围插入元素
|
|
|
|
|
{
|
|
|
|
|
ht_.insert_unique(first, last);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 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);
|
|
|
|
|
}
|
|
|
|
|
void erase(iterator first, iterator last) // 擦除[first, last)范围内的元素
|
|
|
|
|
{
|
|
|
|
|
ht_.erase(first, last);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
size_type erase(const key_type& key)
|
|
|
|
|
{ return ht_.erase_unique(key); }
|
|
|
|
|
size_type erase(const key_type& key) // 擦除指定键的元素,并返回擦除的元素数量
|
|
|
|
|
{
|
|
|
|
|
return ht_.erase_unique(key);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void clear()
|
|
|
|
|
{ ht_.clear(); }
|
|
|
|
|
void clear() // 清空容器
|
|
|
|
|
{
|
|
|
|
|
ht_.clear();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void swap(unordered_map& other) noexcept
|
|
|
|
|
{ ht_.swap(other.ht_); }
|
|
|
|
|
void swap(unordered_map& other) noexcept // 交换两个unordered_map的内容
|
|
|
|
|
{
|
|
|
|
|
ht_.swap(other.ht_);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 查找相关
|
|
|
|
|
|
|
|
|
|
mapped_type& at(const key_type& key)
|
|
|
|
|
mapped_type& at(const key_type& key) // 获取指定键的值,如果不存在则抛出异常
|
|
|
|
|
{
|
|
|
|
|
iterator it = ht_.find(key);
|
|
|
|
|
THROW_OUT_OF_RANGE_IF(it.node == nullptr, "unordered_map<Key, T> no such element exists");
|
|
|
|
@ -195,86 +231,116 @@ public:
|
|
|
|
|
}
|
|
|
|
|
const mapped_type& at(const key_type& key) const
|
|
|
|
|
{
|
|
|
|
|
iterator it = ht_.find(key);
|
|
|
|
|
THROW_OUT_OF_RANGE_IF(it.node == nullptr, "unordered_map<Key, T> no such element exists");
|
|
|
|
|
return it->second;
|
|
|
|
|
iterator it = ht_.find(key); // 在哈希表中查找键
|
|
|
|
|
THROW_OUT_OF_RANGE_IF(it.node == nullptr, "unordered_map<Key, T> no such element exists"); // 如果找不到,抛出异常
|
|
|
|
|
return it->second; // 返回找到的值
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
mapped_type& operator[](const key_type& key)
|
|
|
|
|
{
|
|
|
|
|
iterator it = ht_.find(key);
|
|
|
|
|
if (it.node == nullptr)
|
|
|
|
|
it = ht_.emplace_unique(key, T{}).first;
|
|
|
|
|
return it->second;
|
|
|
|
|
iterator it = ht_.find(key); // 在哈希表中查找键
|
|
|
|
|
if (it.node == nullptr) // 如果找不到
|
|
|
|
|
it = ht_.emplace_unique(key, T{}).first; // 插入键,并就地构造值
|
|
|
|
|
return it->second; // 返回找到或插入的值
|
|
|
|
|
}
|
|
|
|
|
mapped_type& operator[](key_type&& key)
|
|
|
|
|
{
|
|
|
|
|
iterator it = ht_.find(key);
|
|
|
|
|
if (it.node == nullptr)
|
|
|
|
|
it = ht_.emplace_unique(mystl::move(key), T{}).first;
|
|
|
|
|
return it->second;
|
|
|
|
|
iterator it = ht_.find(key); // 在哈希表中查找键
|
|
|
|
|
if (it.node == nullptr) // 如果找不到
|
|
|
|
|
it = ht_.emplace_unique(mystl::move(key), T{}).first; // 插入键,并就地构造值,键使用移动语义
|
|
|
|
|
return it->second; // 返回找到或插入的值
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
size_type count(const key_type& key) const
|
|
|
|
|
{ return ht_.count(key); }
|
|
|
|
|
{
|
|
|
|
|
return ht_.count(key);
|
|
|
|
|
} // 返回键出现的次数
|
|
|
|
|
|
|
|
|
|
iterator find(const key_type& key)
|
|
|
|
|
{ return ht_.find(key); }
|
|
|
|
|
{
|
|
|
|
|
return ht_.find(key);
|
|
|
|
|
} // 返回指向第一个匹配键的迭代器
|
|
|
|
|
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_unique(key); }
|
|
|
|
|
{
|
|
|
|
|
return ht_.equal_range_unique(key);
|
|
|
|
|
} // 返回范围,包含所有匹配的键值对
|
|
|
|
|
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
|
|
|
|
|
{ return ht_.begin(n); }
|
|
|
|
|
{
|
|
|
|
|
return ht_.begin(n);
|
|
|
|
|
} // 返回指定桶的开始迭代器
|
|
|
|
|
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
|
|
|
|
|
{ return ht_.cbegin(n); }
|
|
|
|
|
{
|
|
|
|
|
return ht_.cbegin(n);
|
|
|
|
|
} // 返回指定桶的开始常量迭代器
|
|
|
|
|
|
|
|
|
|
local_iterator end(size_type n) noexcept
|
|
|
|
|
{ return ht_.end(n); }
|
|
|
|
|
{
|
|
|
|
|
return ht_.end(n);
|
|
|
|
|
} // 返回指定桶的结束迭代器
|
|
|
|
|
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
|
|
|
|
|
{ return ht_.cend(n); }
|
|
|
|
|
{
|
|
|
|
|
return ht_.cend(n);
|
|
|
|
|
} // 返回指定桶的结束常量迭代器
|
|
|
|
|
|
|
|
|
|
size_type bucket_count() const noexcept
|
|
|
|
|
{ return ht_.bucket_count(); }
|
|
|
|
|
{
|
|
|
|
|
return ht_.bucket_count();
|
|
|
|
|
} // 返回桶的数量
|
|
|
|
|
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
|
|
|
|
|
{ return ht_.bucket_size(n); }
|
|
|
|
|
{
|
|
|
|
|
return ht_.bucket_size(n);
|
|
|
|
|
} // 返回指定桶中的元素数量
|
|
|
|
|
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(); }
|
|
|
|
|
void max_load_factor(float ml) { ht_.max_load_factor(ml); }
|
|
|
|
|
float max_load_factor() const noexcept { return ht_.max_load_factor(); } // 返回最大装载因子
|
|
|
|
|
void max_load_factor(float ml) { ht_.max_load_factor(ml); } // 设置最大装载因子
|
|
|
|
|
|
|
|
|
|
void rehash(size_type count) { ht_.rehash(count); }
|
|
|
|
|
void reserve(size_type count) { ht_.reserve(count); }
|
|
|
|
|
void rehash(size_type count) { ht_.rehash(count); } // 重新哈希,至少包含count个桶
|
|
|
|
|
void reserve(size_type count) { ht_.reserve(count); } // 重新哈希,以容纳至少count个元素
|
|
|
|
|
|
|
|
|
|
hasher hash_fcn() const { return ht_.hash_fcn(); }
|
|
|
|
|
key_equal key_eq() const { return ht_.key_eq(); }
|
|
|
|
|
hasher hash_fcn() const { return ht_.hash_fcn(); } // 返回哈希函数
|
|
|
|
|
key_equal key_eq() const { return ht_.key_eq(); } // 返回键比较函数
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
friend bool operator==(const unordered_map& lhs, const unordered_map& rhs)
|
|
|
|
|
{
|
|
|
|
|
return lhs.ht_.equal_range_unique(rhs.ht_);
|
|
|
|
|
return lhs.ht_.equal_range_unique(rhs.ht_); // 比较两个unordered_map是否相等
|
|
|
|
|
}
|
|
|
|
|
friend bool operator!=(const unordered_map& lhs, const unordered_map& rhs)
|
|
|
|
|
{
|
|
|
|
|
return !lhs.ht_.equal_range_unique(rhs.ht_);
|
|
|
|
|
return !lhs.ht_.equal_range_unique(rhs.ht_); // 比较两个unordered_map是否不相等
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
@ -283,14 +349,14 @@ template <class Key, class T, class Hash, class KeyEqual>
|
|
|
|
|
bool operator==(const unordered_map<Key, T, Hash, KeyEqual>& lhs,
|
|
|
|
|
const unordered_map<Key, T, Hash, KeyEqual>& rhs)
|
|
|
|
|
{
|
|
|
|
|
return lhs == rhs;
|
|
|
|
|
return lhs == rhs; // 比较两个unordered_map是否相等
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template <class Key, class T, class Hash, class KeyEqual>
|
|
|
|
|
bool operator!=(const unordered_map<Key, T, Hash, KeyEqual>& lhs,
|
|
|
|
|
const unordered_map<Key, T, Hash, KeyEqual>& rhs)
|
|
|
|
|
{
|
|
|
|
|
return lhs != rhs;
|
|
|
|
|
return lhs != rhs; // 比较两个unordered_map是否不相等
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 重载 mystl 的 swap
|
|
|
|
@ -298,7 +364,7 @@ template <class Key, class T, class Hash, class KeyEqual>
|
|
|
|
|
void swap(unordered_map<Key, T, Hash, KeyEqual>& lhs,
|
|
|
|
|
unordered_map<Key, T, Hash, KeyEqual>& rhs)
|
|
|
|
|
{
|
|
|
|
|
lhs.swap(rhs);
|
|
|
|
|
lhs.swap(rhs); // 交换两个unordered_map的内容
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*****************************************************************************************/
|
|
|
|
@ -341,14 +407,14 @@ public:
|
|
|
|
|
// 构造、复制、移动函数
|
|
|
|
|
|
|
|
|
|
unordered_multimap()
|
|
|
|
|
:ht_(100, Hash(), KeyEqual())
|
|
|
|
|
:ht_(100, Hash(), KeyEqual()) // 默认构造函数,初始化哈希表,桶的数量为100
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
explicit unordered_multimap(size_type bucket_count,
|
|
|
|
|
const Hash& hash = Hash(),
|
|
|
|
|
const KeyEqual& equal = KeyEqual())
|
|
|
|
|
:ht_(bucket_count, hash, equal)
|
|
|
|
|
:ht_(bucket_count, hash, equal) // 构造函数,指定桶的数量和哈希函数、键比较函数
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -357,28 +423,28 @@ public:
|
|
|
|
|
const size_type bucket_count = 100,
|
|
|
|
|
const Hash& hash = Hash(),
|
|
|
|
|
const KeyEqual& equal = KeyEqual())
|
|
|
|
|
: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)
|
|
|
|
|
ht_.insert_multi_noresize(*first);
|
|
|
|
|
ht_.insert_multi_noresize(*first); // 插入元素,不调整桶大小
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
unordered_multimap(std::initializer_list<value_type> ilist,
|
|
|
|
|
const size_type bucket_count = 100,
|
|
|
|
|
const Hash& hash = Hash(),
|
|
|
|
|
const KeyEqual& equal = KeyEqual())
|
|
|
|
|
: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)
|
|
|
|
|
ht_.insert_multi_noresize(*first);
|
|
|
|
|
ht_.insert_multi_noresize(*first); // 插入元素,不调整桶大小
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
unordered_multimap(const unordered_multimap& rhs)
|
|
|
|
|
:ht_(rhs.ht_)
|
|
|
|
|
:ht_(rhs.ht_) // 拷贝构造函数
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
unordered_multimap(unordered_multimap&& rhs) noexcept
|
|
|
|
|
:ht_(mystl::move(rhs.ht_))
|
|
|
|
|
:ht_(mystl::move(rhs.ht_)) // 移动构造函数
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -387,44 +453,52 @@ public:
|
|
|
|
|
ht_ = rhs.ht_;
|
|
|
|
|
return *this;
|
|
|
|
|
}
|
|
|
|
|
unordered_multimap& operator=(unordered_multimap&& rhs)
|
|
|
|
|
{
|
|
|
|
|
ht_ = mystl::move(rhs.ht_);
|
|
|
|
|
return *this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
```c+ +
|
|
|
|
|
unordered_multimap & operator=(std::initializer_list<value_type> ilist)
|
|
|
|
|
{
|
|
|
|
|
ht_.clear();
|
|
|
|
|
ht_.reserve(ilist.size());
|
|
|
|
|
ht_.clear(); // 清空哈希表
|
|
|
|
|
ht_.reserve(ilist.size()); // 预留足够的空间
|
|
|
|
|
for (auto first = ilist.begin(), last = ilist.end(); first != last; ++first)
|
|
|
|
|
ht_.insert_multi_noresize(*first);
|
|
|
|
|
return *this;
|
|
|
|
|
ht_.insert_multi_noresize(*first); // 插入元素,不调整桶大小
|
|
|
|
|
return *this; // 返回当前对象的引用
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
~unordered_multimap() = default;
|
|
|
|
|
~unordered_multimap() = default; // 默认析构函数
|
|
|
|
|
|
|
|
|
|
// 迭代器相关
|
|
|
|
|
|
|
|
|
|
iterator begin() noexcept
|
|
|
|
|
{ return ht_.begin(); }
|
|
|
|
|
{
|
|
|
|
|
return ht_.begin();
|
|
|
|
|
} // 返回全局开始的迭代器
|
|
|
|
|
const_iterator begin() const noexcept
|
|
|
|
|
{ return ht_.begin(); }
|
|
|
|
|
{
|
|
|
|
|
return ht_.begin();
|
|
|
|
|
} // 返回全局开始的常量迭代器
|
|
|
|
|
iterator end() noexcept
|
|
|
|
|
{ return ht_.end(); }
|
|
|
|
|
{
|
|
|
|
|
return ht_.end();
|
|
|
|
|
} // 返回全局结束的迭代器
|
|
|
|
|
const_iterator end() const noexcept
|
|
|
|
|
{ return ht_.end(); }
|
|
|
|
|
{
|
|
|
|
|
return ht_.end();
|
|
|
|
|
} // 返回全局结束的常量迭代器
|
|
|
|
|
|
|
|
|
|
const_iterator cbegin() const noexcept
|
|
|
|
|
{ return ht_.cbegin(); }
|
|
|
|
|
{
|
|
|
|
|
return ht_.cbegin();
|
|
|
|
|
} // 返回全局开始的常量迭代器
|
|
|
|
|
const_iterator cend() const noexcept
|
|
|
|
|
{ return ht_.cend(); }
|
|
|
|
|
{
|
|
|
|
|
return ht_.cend();
|
|
|
|
|
} // 返回全局结束的常量迭代器
|
|
|
|
|
|
|
|
|
|
// 容量相关
|
|
|
|
|
|
|
|
|
|
bool empty() const noexcept { return ht_.empty(); }
|
|
|
|
|
size_type size() const noexcept { return ht_.size(); }
|
|
|
|
|
size_type max_size() const noexcept { return ht_.max_size(); }
|
|
|
|
|
bool empty() const noexcept { return ht_.empty(); } // 检查是否为空
|
|
|
|
|
size_type size() const noexcept { return ht_.size(); } // 获取大小
|
|
|
|
|
size_type max_size() const noexcept { return ht_.max_size(); } // 获取最大可能大小
|
|
|
|
|
|
|
|
|
|
// 修改容器相关
|
|
|
|
|
|
|
|
|
@ -432,106 +506,160 @@ public:
|
|
|
|
|
|
|
|
|
|
template <class ...Args>
|
|
|
|
|
iterator emplace(Args&& ...args)
|
|
|
|
|
{ return ht_.emplace_multi(mystl::forward<Args>(args)...); }
|
|
|
|
|
{
|
|
|
|
|
return ht_.emplace_multi(mystl::forward<Args>(args)...);
|
|
|
|
|
} // 就地构造并插入元素
|
|
|
|
|
|
|
|
|
|
template <class ...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)...);
|
|
|
|
|
} // 就地构造并插入元素,使用hint作为插入位置的提示
|
|
|
|
|
|
|
|
|
|
// insert
|
|
|
|
|
|
|
|
|
|
iterator insert(const value_type & value)
|
|
|
|
|
{ return ht_.insert_multi(value); }
|
|
|
|
|
{
|
|
|
|
|
return ht_.insert_multi(value);
|
|
|
|
|
} // 插入元素
|
|
|
|
|
iterator insert(value_type && value)
|
|
|
|
|
{ return ht_.emplace_multi(mystl::move(value)); }
|
|
|
|
|
{
|
|
|
|
|
return ht_.emplace_multi(mystl::move(value));
|
|
|
|
|
} // 插入右值引用元素
|
|
|
|
|
|
|
|
|
|
iterator insert(const_iterator hint, const value_type & value)
|
|
|
|
|
{ return ht_.insert_multi_use_hint(hint, value); }
|
|
|
|
|
{
|
|
|
|
|
return ht_.insert_multi_use_hint(hint, value);
|
|
|
|
|
} // 在hint位置插入元素
|
|
|
|
|
iterator insert(const_iterator hint, value_type && value)
|
|
|
|
|
{ return ht_.emplace_multi_use_hint(hint, mystl::move(value)); }
|
|
|
|
|
{
|
|
|
|
|
return ht_.emplace_multi_use_hint(hint, mystl::move(value));
|
|
|
|
|
} // 在hint位置插入右值引用元素
|
|
|
|
|
|
|
|
|
|
template <class InputIterator>
|
|
|
|
|
void insert(InputIterator first, InputIterator last)
|
|
|
|
|
{ ht_.insert_multi(first, last); }
|
|
|
|
|
{
|
|
|
|
|
ht_.insert_multi(first, last);
|
|
|
|
|
} // 从迭代器范围插入元素
|
|
|
|
|
|
|
|
|
|
// erase / clear
|
|
|
|
|
|
|
|
|
|
void erase(iterator it)
|
|
|
|
|
{ ht_.erase(it); }
|
|
|
|
|
{
|
|
|
|
|
ht_.erase(it);
|
|
|
|
|
} // 擦除指定位置的元素
|
|
|
|
|
void erase(iterator first, iterator last)
|
|
|
|
|
{ ht_.erase(first, last); }
|
|
|
|
|
{
|
|
|
|
|
ht_.erase(first, last);
|
|
|
|
|
} // 擦除[first, last)范围内的元素
|
|
|
|
|
|
|
|
|
|
size_type erase(const key_type & key)
|
|
|
|
|
{ return ht_.erase_multi(key); }
|
|
|
|
|
{
|
|
|
|
|
return ht_.erase_multi(key);
|
|
|
|
|
} // 擦除指定键的所有元素,并返回擦除的元素数量
|
|
|
|
|
|
|
|
|
|
void clear()
|
|
|
|
|
{ ht_.clear(); }
|
|
|
|
|
{
|
|
|
|
|
ht_.clear();
|
|
|
|
|
} // 清空容器
|
|
|
|
|
|
|
|
|
|
void swap(unordered_multimap & other) noexcept
|
|
|
|
|
{ ht_.swap(other.ht_); }
|
|
|
|
|
{
|
|
|
|
|
ht_.swap(other.ht_);
|
|
|
|
|
} // 交换两个unordered_multimap的内容
|
|
|
|
|
|
|
|
|
|
// 查找相关
|
|
|
|
|
|
|
|
|
|
size_type count(const key_type & key) const
|
|
|
|
|
{ return ht_.count(key); }
|
|
|
|
|
{
|
|
|
|
|
return ht_.count(key);
|
|
|
|
|
} // 返回键出现的次数
|
|
|
|
|
|
|
|
|
|
iterator find(const key_type & key)
|
|
|
|
|
{ return ht_.find(key); }
|
|
|
|
|
{
|
|
|
|
|
return ht_.find(key);
|
|
|
|
|
} // 返回指向第一个匹配键的迭代器
|
|
|
|
|
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); }
|
|
|
|
|
{
|
|
|
|
|
return ht_.equal_range_multi(key);
|
|
|
|
|
} // 返回范围,包含所有匹配的键值对
|
|
|
|
|
pair<const_iterator, const_iterator> equal_range(const key_type & key) const
|
|
|
|
|
{ return ht_.equal_range_multi(key); }
|
|
|
|
|
{
|
|
|
|
|
return ht_.equal_range_multi(key);
|
|
|
|
|
} // 返回范围,包含所有匹配的键值对(常量迭代器)
|
|
|
|
|
|
|
|
|
|
// bucket interface
|
|
|
|
|
|
|
|
|
|
local_iterator begin(size_type n) noexcept
|
|
|
|
|
{ return ht_.begin(n); }
|
|
|
|
|
{
|
|
|
|
|
return ht_.begin(n);
|
|
|
|
|
} // 返回指定桶的开始迭代器
|
|
|
|
|
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
|
|
|
|
|
{ return ht_.cbegin(n); }
|
|
|
|
|
{
|
|
|
|
|
return ht_.cbegin(n);
|
|
|
|
|
} // 返回指定桶的开始常量迭代器
|
|
|
|
|
|
|
|
|
|
local_iterator end(size_type n) noexcept
|
|
|
|
|
{ return ht_.end(n); }
|
|
|
|
|
{
|
|
|
|
|
return ht_.end(n);
|
|
|
|
|
} // 返回指定桶的结束迭代器
|
|
|
|
|
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
|
|
|
|
|
{ return ht_.cend(n); }
|
|
|
|
|
{
|
|
|
|
|
return ht_.cend(n);
|
|
|
|
|
} // 返回指定桶的结束常量迭代器
|
|
|
|
|
|
|
|
|
|
size_type bucket_count() const noexcept
|
|
|
|
|
{ return ht_.bucket_count(); }
|
|
|
|
|
{
|
|
|
|
|
return ht_.bucket_count();
|
|
|
|
|
} // 返回桶的数量
|
|
|
|
|
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
|
|
|
|
|
{ return ht_.bucket_size(n); }
|
|
|
|
|
{
|
|
|
|
|
return ht_.bucket_size(n);
|
|
|
|
|
} // 返回指定桶中的元素数量
|
|
|
|
|
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(); }
|
|
|
|
|
void max_load_factor(float ml) { ht_.max_load_factor(ml); }
|
|
|
|
|
float max_load_factor() const noexcept { return ht_.max_load_factor(); } // 返回最大装载因子
|
|
|
|
|
void max_load_factor(float ml) { ht_.max_load_factor(ml); } // 设置最大装载因子
|
|
|
|
|
|
|
|
|
|
void rehash(size_type count) { ht_.rehash(count); }
|
|
|
|
|
void reserve(size_type count) { ht_.reserve(count); }
|
|
|
|
|
void rehash(size_type count) { ht_.rehash(count); } // 重新哈希,至少包含count个桶
|
|
|
|
|
void reserve(size_type count) { ht_.reserve(count); } // 重新哈希,以容纳至少count个元素
|
|
|
|
|
|
|
|
|
|
hasher hash_fcn() const { return ht_.hash_fcn(); }
|
|
|
|
|
key_equal key_eq() const { return ht_.key_eq(); }
|
|
|
|
|
hasher hash_fcn() const { return ht_.hash_fcn(); } // 返回哈希函数
|
|
|
|
|
key_equal key_eq() const { return ht_.key_eq(); } // 返回键比较函数
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
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是否相等
|
|
|
|
|
}
|
|
|
|
|
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是否不相等
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
@ -540,14 +668,14 @@ template <class Key, class T, class Hash, class KeyEqual>
|
|
|
|
|
bool operator==(const unordered_multimap<Key, T, Hash, KeyEqual>& lhs,
|
|
|
|
|
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>
|
|
|
|
|
bool operator!=(const unordered_multimap<Key, T, Hash, KeyEqual>& lhs,
|
|
|
|
|
const unordered_multimap<Key, T, Hash, KeyEqual>& rhs)
|
|
|
|
|
{
|
|
|
|
|
return lhs != rhs;
|
|
|
|
|
return lhs != rhs; // 比较两个unordered_multimap是否不相等
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 重载 mystl 的 swap
|
|
|
|
@ -555,9 +683,11 @@ template <class Key, class T, class Hash, class KeyEqual>
|
|
|
|
|
void swap(unordered_multimap<Key, T, Hash, KeyEqual>& lhs,
|
|
|
|
|
unordered_multimap<Key, T, Hash, KeyEqual>& rhs)
|
|
|
|
|
{
|
|
|
|
|
lhs.swap(rhs);
|
|
|
|
|
lhs.swap(rhs); // 交换两个unordered_multimap的内容
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // namespace mystl
|
|
|
|
|
#endif // !MYTINYSTL_UNORDERED_MAP_H_
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|