diff --git a/src/MyTinySTL-master/MyTinySTL-master/MyTinySTL/unordered_map.h b/src/MyTinySTL-master/MyTinySTL-master/MyTinySTL/unordered_map.h index 5838a21..99c651b 100755 --- a/src/MyTinySTL-master/MyTinySTL-master/MyTinySTL/unordered_map.h +++ b/src/MyTinySTL-master/MyTinySTL-master/MyTinySTL/unordered_map.h @@ -17,288 +17,354 @@ namespace mystl { -// 模板类 unordered_map,键值不允许重复 -// 参数一代表键值类型,参数二代表实值类型,参数三代表哈希函数,缺省使用 mystl::hash -// 参数四代表键值比较方式,缺省使用 mystl::equal_to -template , class KeyEqual = mystl::equal_to> -class unordered_map -{ -private: - // 使用 hashtable 作为底层机制 - typedef hashtable, Hash, KeyEqual> base_type; - base_type ht_; - -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(); } + // 模板类 unordered_map,键值不允许重复 + // 参数一代表键值类型,参数二代表实值类型,参数三代表哈希函数,缺省使用 mystl::hash + // 参数四代表键值比较方式,缺省使用 mystl::equal_to + template , class KeyEqual = mystl::equal_to> + class unordered_map + { + private: + // 使用 hashtable 作为底层机制 + typedef hashtable, Hash, KeyEqual> base_type; + base_type ht_; + + 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(); } // 获取分配器 + + public: + // 构造、复制、移动、析构函数 + + unordered_map() + :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) // 构造函数,指定桶的数量和哈希函数、键比较函数 + { + } + + template + unordered_map(InputIterator first, InputIterator last, + const size_type bucket_count = 100, + const Hash& hash = Hash(), + const KeyEqual& equal = KeyEqual()) + : ht_(mystl::max(bucket_count, static_cast(mystl::distance(first, last))), hash, equal) // 构造函数,从迭代器范围构造 + { + for (; first != last; ++first) + ht_.insert_unique_noresize(*first); + } + + unordered_map(std::initializer_list ilist, + const size_type bucket_count = 100, + const Hash& hash = Hash(), + const KeyEqual& equal = KeyEqual()) + :ht_(mystl::max(bucket_count, static_cast(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_) // 拷贝构造函数 + { + } + unordered_map(unordered_map&& rhs) noexcept + :ht_(mystl::move(rhs.ht_)) // 移动构造函数 + { + } + + unordered_map& operator=(const unordered_map& rhs) // 拷贝赋值运算符 + { + ht_ = rhs.ht_; + return *this; + } + unordered_map& operator=(unordered_map&& rhs) // 移动赋值运算符 + { + ht_ = mystl::move(rhs.ht_); + return *this; + } + + unordered_map& operator=(std::initializer_list ilist) // 赋值运算符,从初始化列表赋值 + { + ht_.clear(); + ht_.reserve(ilist.size()); + for (auto first = ilist.begin(), last = ilist.end(); first != last; ++first) + ht_.insert_unique_noresize(*first); + return *this; + } + + ~unordered_map() = default; // 析构函数 + + // 迭代器相关 + + iterator begin() noexcept + { + return ht_.begin(); + } // 返回指向第一个元素的迭代器 + const_iterator begin() const noexcept + { + return ht_.begin(); + } // 返回指向第一个元素的常量迭代器 + iterator end() noexcept + { + return ht_.end(); + } // 返回指向最后一个元素的迭代器 + const_iterator end() const noexcept + { + return ht_.end(); + } // 返回指向最后一个元素的常量迭代器 + + const_iterator cbegin() const noexcept + { + return ht_.cbegin(); + } // 返回指向第一个元素的常量迭代器 + const_iterator cend() const noexcept + { + 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(); } // 获取容器的最大可能大小 + + // 修改容器操作 + + // emplace / emplace_hint + + template + pair emplace(Args&& ...args) // 就地构造元素并插入 + { + return ht_.emplace_unique(mystl::forward(args)...); + } + + template + iterator emplace_hint(const_iterator hint, Args&& ...args) // 就地构造元素并插入,使用hint作为插入位置的提示 + { + return ht_.emplace_unique_use_hint(hint, mystl::forward(args)...); + } + + // insert + + pair insert(const value_type& value) // 插入元素 + { + return ht_.insert_unique(value); + } + pair insert(value_type&& value) // 插入右值引用元素 + { + return ht_.emplace_unique(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 + 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) // 擦除[first, last)范围内的元素 + { + ht_.erase(first, last); + } + + size_type erase(const key_type& key) // 擦除指定键的元素,并返回擦除的元素数量 + { + return ht_.erase_unique(key); + } + + void clear() // 清空容器 + { + ht_.clear(); + } + + void swap(unordered_map& other) noexcept // 交换两个unordered_map的内容 + { + ht_.swap(other.ht_); + } + + // 查找相关 + + mapped_type& at(const key_type& key) // 获取指定键的值,如果不存在则抛出异常 + { + iterator it = ht_.find(key); + THROW_OUT_OF_RANGE_IF(it.node == nullptr, "unordered_map no such element exists"); + return it->second; + } + const mapped_type& at(const key_type& key) const + { + iterator it = ht_.find(key); // 在哈希表中查找键 + THROW_OUT_OF_RANGE_IF(it.node == nullptr, "unordered_map 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; // 返回找到或插入的值 + } + 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; // 返回找到或插入的值 + } + + size_type count(const key_type& key) const + { + return ht_.count(key); + } // 返回键出现的次数 + + iterator find(const key_type& key) + { + return ht_.find(key); + } // 返回指向第一个匹配键的迭代器 + const_iterator find(const key_type& key) const + { + return ht_.find(key); + } // 返回指向第一个匹配键的常量迭代器 + + pair equal_range(const key_type& key) + { + return ht_.equal_range_unique(key); + } // 返回范围,包含所有匹配的键值对 + pair equal_range(const key_type& key) const + { + return ht_.equal_range_unique(key); + } // 返回范围,包含所有匹配的键值对(常量迭代器) + +// bucket interface + + 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); + } // 返回指定桶的结束迭代器 + 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_count() const noexcept + { + return ht_.bucket_count(); + } // 返回桶的数量 + size_type max_bucket_count() const noexcept + { + return ht_.max_bucket_count(); + } // 返回最大桶的数量 + + 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); + } // 返回键所属的桶的索引 + +// hash policy + + 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); } // 设置最大装载因子 + + 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(); } // 返回键比较函数 public: - // 构造、复制、移动、析构函数 - - unordered_map() - :ht_(100, Hash(), KeyEqual()) - { - } - - explicit unordered_map(size_type bucket_count, - const Hash& hash = Hash(), - const KeyEqual& equal = KeyEqual()) - :ht_(bucket_count, hash, equal) - { - } - - template - unordered_map(InputIterator first, InputIterator last, - const size_type bucket_count = 100, - const Hash& hash = Hash(), - const KeyEqual& equal = KeyEqual()) - : ht_(mystl::max(bucket_count, static_cast(mystl::distance(first, last))), hash, equal) - { - for (; first != last; ++first) - ht_.insert_unique_noresize(*first); - } - - unordered_map(std::initializer_list ilist, - const size_type bucket_count = 100, - const Hash& hash = Hash(), - const KeyEqual& equal = KeyEqual()) - :ht_(mystl::max(bucket_count, static_cast(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_) - { - } - unordered_map(unordered_map&& rhs) noexcept - :ht_(mystl::move(rhs.ht_)) - { - } - - unordered_map& operator=(const unordered_map& rhs) - { - ht_ = rhs.ht_; - return *this; - } - unordered_map& operator=(unordered_map&& rhs) - { - ht_ = mystl::move(rhs.ht_); - return *this; - } - - unordered_map& operator=(std::initializer_list ilist) - { - ht_.clear(); - ht_.reserve(ilist.size()); - for (auto first = ilist.begin(), last = ilist.end(); first != last; ++first) - ht_.insert_unique_noresize(*first); - return *this; - } - - ~unordered_map() = default; - - // 迭代器相关 - - iterator begin() noexcept - { return ht_.begin(); } - const_iterator begin() const noexcept - { return ht_.begin(); } - iterator end() noexcept - { return ht_.end(); } - const_iterator end() const noexcept - { return ht_.end(); } - - const_iterator cbegin() const noexcept - { return ht_.cbegin(); } - const_iterator cend() const noexcept - { 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(); } - - // 修改容器操作 - - // empalce / empalce_hint - - template - pair emplace(Args&& ...args) - { return ht_.emplace_unique(mystl::forward(args)...); } - - template - iterator emplace_hint(const_iterator hint, Args&& ...args) - { return ht_.emplace_unique_use_hint(hint, mystl::forward(args)...); } - - // insert - - pair insert(const value_type& value) - { return ht_.insert_unique(value); } - pair 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)); } - - template - 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); } - - size_type erase(const key_type& key) - { return ht_.erase_unique(key); } - - void clear() - { ht_.clear(); } - - void swap(unordered_map& other) noexcept - { ht_.swap(other.ht_); } - - // 查找相关 - - mapped_type& at(const key_type& key) - { - iterator it = ht_.find(key); - THROW_OUT_OF_RANGE_IF(it.node == nullptr, "unordered_map no such element exists"); - return it->second; - } - const mapped_type& at(const key_type& key) const - { - iterator it = ht_.find(key); - THROW_OUT_OF_RANGE_IF(it.node == nullptr, "unordered_map 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; - } - 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; - } - - size_type count(const key_type& key) const - { return ht_.count(key); } - - iterator find(const key_type& key) - { return ht_.find(key); } - const_iterator find(const key_type& key) const - { return ht_.find(key); } - - pair equal_range(const key_type& key) - { return ht_.equal_range_unique(key); } - pair equal_range(const key_type& key) const - { return ht_.equal_range_unique(key); } - - // bucket interface - - 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); } - 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_count() const noexcept - { return ht_.bucket_count(); } - size_type max_bucket_count() const noexcept - { return ht_.max_bucket_count(); } - - 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); } - - // hash policy - - 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); } - - void rehash(size_type count) { ht_.rehash(count); } - void reserve(size_type count) { ht_.reserve(count); } - - 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_); - } - friend bool operator!=(const unordered_map& lhs, const unordered_map& rhs) - { - return !lhs.ht_.equal_range_unique(rhs.ht_); - } + friend bool operator==(const unordered_map& lhs, const unordered_map& rhs) + { + 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_); // 比较两个unordered_map是否不相等 + } }; // 重载比较操作符 template bool operator==(const unordered_map& lhs, - const unordered_map& rhs) + const unordered_map& rhs) { - return lhs == rhs; + return lhs == rhs; // 比较两个unordered_map是否相等 } template bool operator!=(const unordered_map& lhs, - const unordered_map& rhs) + const unordered_map& rhs) { - return lhs != rhs; + return lhs != rhs; // 比较两个unordered_map是否不相等 } // 重载 mystl 的 swap template void swap(unordered_map& lhs, - unordered_map& rhs) + unordered_map& rhs) { - lhs.swap(rhs); + lhs.swap(rhs); // 交换两个unordered_map的内容 } /*****************************************************************************************/ @@ -310,254 +376,318 @@ template , class KeyEqual = my class unordered_multimap { private: - // 使用 hashtable 作为底层机制 - typedef hashtable, Hash, KeyEqual> base_type; - base_type ht_; + // 使用 hashtable 作为底层机制 + typedef hashtable, Hash, KeyEqual> base_type; + base_type ht_; 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(); } + // 使用 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(); } public: - // 构造、复制、移动函数 - - unordered_multimap() - :ht_(100, Hash(), KeyEqual()) - { - } - - explicit unordered_multimap(size_type bucket_count, - const Hash& hash = Hash(), - const KeyEqual& equal = KeyEqual()) - :ht_(bucket_count, hash, equal) - { - } - - template - unordered_multimap(InputIterator first, InputIterator last, - const size_type bucket_count = 100, - const Hash& hash = Hash(), - const KeyEqual& equal = KeyEqual()) - :ht_(mystl::max(bucket_count, static_cast(mystl::distance(first, last))), hash, equal) - { - for (; first != last; ++first) - ht_.insert_multi_noresize(*first); - } - - unordered_multimap(std::initializer_list ilist, - const size_type bucket_count = 100, - const Hash& hash = Hash(), - const KeyEqual& equal = KeyEqual()) - :ht_(mystl::max(bucket_count, static_cast(ilist.size())), hash, equal) - { - for (auto first = ilist.begin(), last = ilist.end(); first != last; ++first) - ht_.insert_multi_noresize(*first); - } - - unordered_multimap(const unordered_multimap& rhs) - :ht_(rhs.ht_) - { - } - unordered_multimap(unordered_multimap&& rhs) noexcept - :ht_(mystl::move(rhs.ht_)) - { - } - - unordered_multimap& operator=(const unordered_multimap& rhs) - { - ht_ = rhs.ht_; - return *this; - } - unordered_multimap& operator=(unordered_multimap&& rhs) - { - ht_ = mystl::move(rhs.ht_); - return *this; - } - - unordered_multimap& operator=(std::initializer_list ilist) - { - ht_.clear(); - ht_.reserve(ilist.size()); - for (auto first = ilist.begin(), last = ilist.end(); first != last; ++first) - ht_.insert_multi_noresize(*first); - return *this; - } - - ~unordered_multimap() = default; - - // 迭代器相关 - - iterator begin() noexcept - { return ht_.begin(); } - const_iterator begin() const noexcept - { return ht_.begin(); } - iterator end() noexcept - { return ht_.end(); } - const_iterator end() const noexcept - { return ht_.end(); } - - const_iterator cbegin() const noexcept - { return ht_.cbegin(); } - const_iterator cend() const noexcept - { 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(); } - - // 修改容器相关 - - // emplace / emplace_hint - - template - iterator emplace(Args&& ...args) - { return ht_.emplace_multi(mystl::forward(args)...); } - - template - iterator emplace_hint(const_iterator hint, Args&& ...args) - { return ht_.emplace_multi_use_hint(hint, mystl::forward(args)...); } - - // 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_iterator hint, const value_type& value) - { return ht_.insert_multi_use_hint(hint, value); } - iterator insert(const_iterator hint, value_type&& value) - { return ht_.emplace_multi_use_hint(hint, mystl::move(value)); } - - template - void insert(InputIterator first, InputIterator last) - { ht_.insert_multi(first, last); } - - // erase / clear - - void erase(iterator it) - { ht_.erase(it); } - void erase(iterator first, iterator last) - { ht_.erase(first, last); } - - size_type erase(const key_type& key) - { return ht_.erase_multi(key); } - - void clear() - { ht_.clear(); } - - void swap(unordered_multimap& other) noexcept - { ht_.swap(other.ht_); } - - // 查找相关 - - size_type count(const key_type& key) const - { return ht_.count(key); } - - iterator find(const key_type& key) - { return ht_.find(key); } - const_iterator find(const key_type& key) const - { return ht_.find(key); } - - pair equal_range(const key_type& key) - { return ht_.equal_range_multi(key); } - pair equal_range(const key_type& key) const - { return ht_.equal_range_multi(key); } - - // bucket interface - - 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); } - 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_count() const noexcept - { return ht_.bucket_count(); } - size_type max_bucket_count() const noexcept - { return ht_.max_bucket_count(); } - - 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); } - - // hash policy - - 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); } - - void rehash(size_type count) { ht_.rehash(count); } - void reserve(size_type count) { ht_.reserve(count); } - - hasher hash_fcn() const { return ht_.hash_fcn(); } - key_equal key_eq() const { return ht_.key_eq(); } + // 构造、复制、移动函数 + + unordered_multimap() + :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) // 构造函数,指定桶的数量和哈希函数、键比较函数 + { + } + + template + unordered_multimap(InputIterator first, InputIterator last, + const size_type bucket_count = 100, + const Hash& hash = Hash(), + const KeyEqual& equal = KeyEqual()) + : ht_(mystl::max(bucket_count, static_cast(mystl::distance(first, last))), hash, equal) // 构造函数,从迭代器范围构造 + { + for (; first != last; ++first) + ht_.insert_multi_noresize(*first); // 插入元素,不调整桶大小 + } + + unordered_multimap(std::initializer_list ilist, + const size_type bucket_count = 100, + const Hash& hash = Hash(), + const KeyEqual& equal = KeyEqual()) + :ht_(mystl::max(bucket_count, static_cast(ilist.size())), hash, equal) // 构造函数,从初始化列表构造 + { + for (auto first = ilist.begin(), last = ilist.end(); first != last; ++first) + ht_.insert_multi_noresize(*first); // 插入元素,不调整桶大小 + } + + unordered_multimap(const unordered_multimap& rhs) + :ht_(rhs.ht_) // 拷贝构造函数 + { + } + unordered_multimap(unordered_multimap&& rhs) noexcept + :ht_(mystl::move(rhs.ht_)) // 移动构造函数 + { + } + + unordered_multimap& operator=(const unordered_multimap& rhs) + { + ht_ = rhs.ht_; + return *this; + } + + ```c+ + + unordered_multimap & operator=(std::initializer_list ilist) + { + ht_.clear(); // 清空哈希表 + ht_.reserve(ilist.size()); // 预留足够的空间 + for (auto first = ilist.begin(), last = ilist.end(); first != last; ++first) + ht_.insert_multi_noresize(*first); // 插入元素,不调整桶大小 + return *this; // 返回当前对象的引用 + } + + ~unordered_multimap() = default; // 默认析构函数 + + // 迭代器相关 + + iterator begin() noexcept + { + return ht_.begin(); + } // 返回全局开始的迭代器 + const_iterator begin() const noexcept + { + return ht_.begin(); + } // 返回全局开始的常量迭代器 + iterator end() noexcept + { + return ht_.end(); + } // 返回全局结束的迭代器 + const_iterator end() const noexcept + { + return ht_.end(); + } // 返回全局结束的常量迭代器 + + const_iterator cbegin() const noexcept + { + return ht_.cbegin(); + } // 返回全局开始的常量迭代器 + const_iterator cend() const noexcept + { + 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(); } // 获取最大可能大小 + + // 修改容器相关 + + // emplace / emplace_hint + + template + iterator emplace(Args&& ...args) + { + return ht_.emplace_multi(mystl::forward(args)...); + } // 就地构造并插入元素 + + template + iterator emplace_hint(const_iterator hint, Args&& ...args) + { + return ht_.emplace_multi_use_hint(hint, mystl::forward(args)...); + } // 就地构造并插入元素,使用hint作为插入位置的提示 + +// 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_iterator hint, const value_type & 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)); + } // 在hint位置插入右值引用元素 + + template + void insert(InputIterator first, InputIterator last) + { + ht_.insert_multi(first, last); + } // 从迭代器范围插入元素 + +// erase / clear + + void erase(iterator it) + { + ht_.erase(it); + } // 擦除指定位置的元素 + void erase(iterator first, iterator last) + { + ht_.erase(first, last); + } // 擦除[first, last)范围内的元素 + + size_type erase(const key_type & key) + { + return ht_.erase_multi(key); + } // 擦除指定键的所有元素,并返回擦除的元素数量 + + void clear() + { + ht_.clear(); + } // 清空容器 + + void swap(unordered_multimap & other) noexcept + { + ht_.swap(other.ht_); + } // 交换两个unordered_multimap的内容 + +// 查找相关 + + size_type count(const key_type & key) const + { + return ht_.count(key); + } // 返回键出现的次数 + + iterator find(const key_type & key) + { + return ht_.find(key); + } // 返回指向第一个匹配键的迭代器 + const_iterator find(const key_type & key) const + { + return ht_.find(key); + } // 返回指向第一个匹配键的常量迭代器 + + pair equal_range(const key_type & key) + { + return ht_.equal_range_multi(key); + } // 返回范围,包含所有匹配的键值对 + pair equal_range(const key_type & key) const + { + return ht_.equal_range_multi(key); + } // 返回范围,包含所有匹配的键值对(常量迭代器) + +// bucket interface + + 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); + } // 返回指定桶的结束迭代器 + 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_count() const noexcept + { + return ht_.bucket_count(); + } // 返回桶的数量 + size_type max_bucket_count() const noexcept + { + return ht_.max_bucket_count(); + } // 返回最大桶的数量 + + 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); + } // 返回键所属的桶的索引 + +// hash policy + + 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); } // 设置最大装载因子 + + 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(); } // 返回键比较函数 public: - friend bool operator==(const unordered_multimap& lhs, const unordered_multimap& rhs) - { - return lhs.ht_.equal_range_multi(rhs.ht_); - } - friend bool operator!=(const unordered_multimap& lhs, const unordered_multimap& rhs) - { - return !lhs.ht_.equal_range_multi(rhs.ht_); - } + friend bool operator==(const unordered_multimap & lhs, const unordered_multimap & rhs) + { + 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_); // 比较两个unordered_multimap是否不相等 + } }; // 重载比较操作符 template bool operator==(const unordered_multimap& lhs, - const unordered_multimap& rhs) + const unordered_multimap& rhs) { - return lhs == rhs; + return lhs == rhs; // 比较两个unordered_multimap是否相等 } template bool operator!=(const unordered_multimap& lhs, - const unordered_multimap& rhs) + const unordered_multimap& rhs) { - return lhs != rhs; + return lhs != rhs; // 比较两个unordered_multimap是否不相等 } // 重载 mystl 的 swap template void swap(unordered_multimap& lhs, - unordered_multimap& rhs) + unordered_multimap& rhs) { - lhs.swap(rhs); + lhs.swap(rhs); // 交换两个unordered_multimap的内容 } } // namespace mystl #endif // !MYTINYSTL_UNORDERED_MAP_H_ +``` +