cargo fmt rust code (#1131)

* cargo fmt code

* Add empty line to seperate unrelated comments

* Fix review

* Update bubble_sort.rs

* Update merge_sort.rs

---------

Co-authored-by: Yudong Jin <krahets@163.com>
This commit is contained in:
rongyi
2024-03-16 02:13:41 +08:00
committed by GitHub
parent 54ceef3443
commit 7b1094318b
70 changed files with 1021 additions and 836 deletions

View File

@@ -12,13 +12,15 @@ pub struct Pair {
}
/* 基于数组实现的哈希表 */
pub struct ArrayHashMap {
buckets: Vec<Option<Pair>>
buckets: Vec<Option<Pair>>,
}
impl ArrayHashMap {
pub fn new() -> ArrayHashMap {
// 初始化数组,包含 100 个桶
Self { buckets: vec![None; 100] }
Self {
buckets: vec![None; 100],
}
}
/* 哈希函数 */
@@ -50,17 +52,26 @@ impl ArrayHashMap {
/* 获取所有键值对 */
pub fn entry_set(&self) -> Vec<&Pair> {
self.buckets.iter().filter_map(|pair| pair.as_ref()).collect()
self.buckets
.iter()
.filter_map(|pair| pair.as_ref())
.collect()
}
/* 获取所有键 */
pub fn key_set(&self) -> Vec<&i32> {
self.buckets.iter().filter_map(|pair| pair.as_ref().map(|pair| &pair.key)).collect()
self.buckets
.iter()
.filter_map(|pair| pair.as_ref().map(|pair| &pair.key))
.collect()
}
/* 获取所有值 */
pub fn value_set(&self) -> Vec<&String> {
self.buckets.iter().filter_map(|pair| pair.as_ref().map(|pair| &pair.val)).collect()
self.buckets
.iter()
.filter_map(|pair| pair.as_ref().map(|pair| &pair.val))
.collect()
}
/* 打印哈希表 */

View File

@@ -151,10 +151,13 @@ pub fn main() {
/* 查询操作 */
// 向哈希表中输入键 key ,得到值 value
println!("\n输入学号 13276,查询到姓名 {}", match map.get(13276) {
Some(value) => value,
None => "Not a valid Key"
});
println!(
"\n输入学号 13276,查询到姓名 {}",
match map.get(13276) {
Some(value) => value,
None => "Not a valid Key",
}
);
/* 删除操作 */
// 在哈希表中删除键值对 (key, value)

View File

@@ -12,15 +12,14 @@ use array_hash_map::Pair;
/* 开放寻址哈希表 */
struct HashMapOpenAddressing {
size: usize, // 键值对数量
capacity: usize, // 哈希表容量
load_thres: f64, // 触发扩容的负载因子阈值
extend_ratio: usize, // 扩容倍数
buckets: Vec<Option<Pair>>, // 桶数组
TOMBSTONE: Option<Pair>, // 删除标记
size: usize, // 键值对数量
capacity: usize, // 哈希表容量
load_thres: f64, // 触发扩容的负载因子阈值
extend_ratio: usize, // 扩容倍数
buckets: Vec<Option<Pair>>, // 桶数组
TOMBSTONE: Option<Pair>, // 删除标记
}
impl HashMapOpenAddressing {
/* 构造方法 */
fn new() -> Self {
@@ -30,7 +29,10 @@ impl HashMapOpenAddressing {
load_thres: 2.0 / 3.0,
extend_ratio: 2,
buckets: vec![None; 4],
TOMBSTONE: Some(Pair {key: -1, val: "-1".to_string()}),
TOMBSTONE: Some(Pair {
key: -1,
val: "-1".to_string(),
}),
}
}
@@ -56,9 +58,9 @@ impl HashMapOpenAddressing {
if first_tombstone != -1 {
self.buckets[first_tombstone as usize] = self.buckets[index].take();
self.buckets[index] = self.TOMBSTONE.clone();
return first_tombstone as usize; // 返回移动后的桶索引
return first_tombstone as usize; // 返回移动后的桶索引
}
return index; // 返回桶索引
return index; // 返回桶索引
}
// 记录遇到的首个删除标记
if first_tombstone == -1 && self.buckets[index] == self.TOMBSTONE {
@@ -68,7 +70,11 @@ impl HashMapOpenAddressing {
index = (index + 1) % self.capacity;
}
// 若 key 不存在,则返回添加点的索引
if first_tombstone == -1 { index } else { first_tombstone as usize }
if first_tombstone == -1 {
index
} else {
first_tombstone as usize
}
}
/* 查询操作 */

View File

@@ -4,7 +4,6 @@
* Author: night-cruise (2586447362@qq.com)
*/
/* 加法哈希 */
fn add_hash(key: &str) -> i32 {
let mut hash = 0_i64;
@@ -15,7 +14,7 @@ fn add_hash(key: &str) -> i32 {
}
hash as i32
}
}
/* 乘法哈希 */
fn mul_hash(key: &str) -> i32 {
@@ -68,4 +67,4 @@ fn main() {
let hash = rot_hash(key);
println!("旋转哈希值为 {hash}");
}
}