From ae6b655669fba741480013a589a881d1fd0b362e Mon Sep 17 00:00:00 2001 From: greyhawk Date: Mon, 28 Mar 2022 13:54:51 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20hashmap.md=20=E7=AC=AC=E4=B8=80=E9=A2=98?= =?UTF-8?q?=E5=8D=95=E8=AF=8D=E6=8B=BC=E5=86=99=E9=94=99=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- zh-CN/src/collections/hashmap.md | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/zh-CN/src/collections/hashmap.md b/zh-CN/src/collections/hashmap.md index 0ddecd2..b60159a 100644 --- a/zh-CN/src/collections/hashmap.md +++ b/zh-CN/src/collections/hashmap.md @@ -1,12 +1,14 @@ # HashMap + `HashMap` 默认使用 `SipHash 1-3` 哈希算法,该算法对于抵抗 `HashDos` 攻击非常有效。在性能方面,如果你的 key 是中型大小的,那该算法非常不错,但是如果是小型的 key( 例如整数 )亦或是大型的 key ( 例如字符串 ),那你需要采用社区提供的其它算法来提高性能。 哈希表的算法是基于 Google 的 [SwissTable](https://abseil.io/blog/20180927-swisstables),你可以在[这里](https://github.com/abseil/abseil-cpp/blob/master/absl/container/internal/raw_hash_set.h)找到 C++ 的实现,同时在 [CppCon talk](https://www.youtube.com/watch?v=ncHmEUmJZf4) 上也有关于算法如何工作的演讲。 ### 基本操作 + 1. 🌟🌟 -```rust,editbale +```rust,editable // 填空并修复错误 use std::collections::HashMap; @@ -37,6 +39,7 @@ fn main() { ``` 2. 🌟🌟 + ```rust,editable use std::collections::HashMap; @@ -63,6 +66,7 @@ fn main() { ``` 3. 🌟🌟 + ```rust,editable // 填空 @@ -95,18 +99,19 @@ fn random_stat_buff() -> u8 { ``` ### HashMap key 的限制 + 任何实现了 `Eq` 和 `Hash` 特征的类型都可以用于 `HashMap` 的 key,包括: - `bool` (虽然很少用到,因为它只能表达两种 key) - `int`, `uint` 以及它们的变体,例如 `u8`、`i32` 等 - `String` 和 `&str` (提示: `HashMap` 的 key 是 `String` 类型时,你其实可以使用 `&str` 配合 `get` 方法进行查询 - + 需要注意的是,`f32` 和 `f64` 并没有实现 `Hash`,原因是 [浮点数精度](https://en.wikipedia.org/wiki/Floating-point_arithmetic#Accuracy_problems) 的问题会导致它们无法进行相等比较。 如果一个集合类型的所有字段都实现了 `Eq` 和 `Hash`,那该集合类型会自动实现 `Eq` 和 `Hash`。例如 `Vect` 要实现 `Hash`,那么首先需要 `T` 实现 `Hash`。 - 4. 🌟🌟 + ```rust,editable // 修复错误 @@ -143,9 +148,11 @@ fn main() { ``` ### 容量 + 关于容量,我们在之前的 [Vector](https://zh.practice.rs/collections/vector.html#容量) 中有详细的介绍,而 `HashMap` 也可以调整容量: 你可以通过 `HashMap::with_capacity(uint)` 使用指定的容量来初始化,或者使用 `HashMap::new()` ,后者会提供一个默认的初始化容量。 #### 示例 + ```rust,editable use std::collections::HashMap; @@ -169,10 +176,11 @@ fn main() { ``` ### 所有权 + 对于实现了 `Copy` 特征的类型,例如 `i32`,那类型的值会被拷贝到 `HashMap` 中。而对于有所有权的类型,例如 `String`,它们的值的所有权将被转移到 `HashMap` 中。 - 5. 🌟🌟 + ```rust,editable // 修复错误,尽可能少的去修改代码 // 不要移除任何代码行! @@ -187,7 +195,7 @@ fn main() { let mut m2 = HashMap::new(); // 所有权在这里发生了转移 m2.insert(v2, v1); - + assert_eq!(v2, "hello"); println!("Success!") @@ -195,6 +203,7 @@ fn main() { ``` ### 三方库 Hash 库 + 在开头,我们提到过如果现有的 `SipHash 1-3` 的性能无法满足你的需求,那么可以使用社区提供的替代算法。 例如其中一个社区库的使用方式如下: @@ -211,4 +220,4 @@ hash.insert(42, "the answer"); assert_eq!(hash.get(&42), Some(&"the answer")); ``` -> You can find the solutions [here](https://github.com/sunface/rust-by-practice)(under the solutions path), but only use it when you need it \ No newline at end of file +> You can find the solutions [here](https://github.com/sunface/rust-by-practice)(under the solutions path), but only use it when you need it