mirror of
https://github.com/krahets/hello-algo.git
synced 2026-09-22 14:26:34 +08:00
feat: Traditional Chinese version (#1163)
* First commit * Update mkdocs.yml * Translate all the docs to traditional Chinese * Translate the code files. * Translate the docker file * Fix mkdocs.yml * Translate all the figures from SC to TC * 二叉搜尋樹 -> 二元搜尋樹 * Update terminology. * Update terminology * 构造函数/构造方法 -> 建構子 异或 -> 互斥或 * 擴充套件 -> 擴展 * constant - 常量 - 常數 * 類 -> 類別 * AVL -> AVL 樹 * 數組 -> 陣列 * 係統 -> 系統 斐波那契數列 -> 費波那契數列 運算元量 -> 運算量 引數 -> 參數 * 聯絡 -> 關聯 * 麵試 -> 面試 * 面向物件 -> 物件導向 歸併排序 -> 合併排序 范式 -> 範式 * Fix 算法 -> 演算法 * 錶示 -> 表示 反碼 -> 一補數 補碼 -> 二補數 列列尾部 -> 佇列尾部 區域性性 -> 區域性 一摞 -> 一疊 * Synchronize with main branch * 賬號 -> 帳號 推匯 -> 推導 * Sync with main branch * First commit * Update mkdocs.yml * Translate all the docs to traditional Chinese * Translate the code files. * Translate the docker file * Fix mkdocs.yml * Translate all the figures from SC to TC * 二叉搜尋樹 -> 二元搜尋樹 * Update terminology * 构造函数/构造方法 -> 建構子 异或 -> 互斥或 * 擴充套件 -> 擴展 * constant - 常量 - 常數 * 類 -> 類別 * AVL -> AVL 樹 * 數組 -> 陣列 * 係統 -> 系統 斐波那契數列 -> 費波那契數列 運算元量 -> 運算量 引數 -> 參數 * 聯絡 -> 關聯 * 麵試 -> 面試 * 面向物件 -> 物件導向 歸併排序 -> 合併排序 范式 -> 範式 * Fix 算法 -> 演算法 * 錶示 -> 表示 反碼 -> 一補數 補碼 -> 二補數 列列尾部 -> 佇列尾部 區域性性 -> 區域性 一摞 -> 一疊 * Synchronize with main branch * 賬號 -> 帳號 推匯 -> 推導 * Sync with main branch * Update terminology.md * 操作数量(num. of operations)-> 操作數量 * 字首和->前綴和 * Update figures * 歸 -> 迴 記憶體洩漏 -> 記憶體流失 * Fix the bug of the file filter * 支援 -> 支持 Add zh-Hant/README.md * Add the zh-Hant chapter covers. Bug fixes. * 外掛 -> 擴充功能 * Add the landing page for zh-Hant version * Unify the font of the chapter covers for the zh, en, and zh-Hant version * Move zh-Hant/ to zh-hant/ * Translate terminology.md to traditional Chinese
This commit is contained in:
@@ -0,0 +1,124 @@
|
||||
/**
|
||||
* File: array_hash_map.rs
|
||||
* Created Time: 2023-2-18
|
||||
* Author: xBLACICEx (xBLACKICEx@outlook.com)
|
||||
*/
|
||||
|
||||
/* 鍵值對 */
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
pub struct Pair {
|
||||
pub key: i32,
|
||||
pub val: String,
|
||||
}
|
||||
/* 基於陣列實現的雜湊表 */
|
||||
pub struct ArrayHashMap {
|
||||
buckets: Vec<Option<Pair>>,
|
||||
}
|
||||
|
||||
impl ArrayHashMap {
|
||||
pub fn new() -> ArrayHashMap {
|
||||
// 初始化陣列,包含 100 個桶
|
||||
Self {
|
||||
buckets: vec![None; 100],
|
||||
}
|
||||
}
|
||||
|
||||
/* 雜湊函式 */
|
||||
fn hash_func(&self, key: i32) -> usize {
|
||||
key as usize % 100
|
||||
}
|
||||
|
||||
/* 查詢操作 */
|
||||
pub fn get(&self, key: i32) -> Option<&String> {
|
||||
let index = self.hash_func(key);
|
||||
self.buckets[index].as_ref().map(|pair| &pair.val)
|
||||
}
|
||||
|
||||
/* 新增操作 */
|
||||
pub fn put(&mut self, key: i32, val: &str) {
|
||||
let index = self.hash_func(key);
|
||||
self.buckets[index] = Some(Pair {
|
||||
key,
|
||||
val: val.to_string(),
|
||||
});
|
||||
}
|
||||
|
||||
/* 刪除操作 */
|
||||
pub fn remove(&mut self, key: i32) {
|
||||
let index = self.hash_func(key);
|
||||
// 置為 None ,代表刪除
|
||||
self.buckets[index] = None;
|
||||
}
|
||||
|
||||
/* 獲取所有鍵值對 */
|
||||
pub fn entry_set(&self) -> Vec<&Pair> {
|
||||
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()
|
||||
}
|
||||
|
||||
/* 獲取所有值 */
|
||||
pub fn value_set(&self) -> Vec<&String> {
|
||||
self.buckets
|
||||
.iter()
|
||||
.filter_map(|pair| pair.as_ref().map(|pair| &pair.val))
|
||||
.collect()
|
||||
}
|
||||
|
||||
/* 列印雜湊表 */
|
||||
pub fn print(&self) {
|
||||
for pair in self.entry_set() {
|
||||
println!("{} -> {}", pair.key, pair.val);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
/* 初始化雜湊表 */
|
||||
let mut map = ArrayHashMap::new();
|
||||
/*新增操作 */
|
||||
// 在雜湊表中新增鍵值對(key, value)
|
||||
map.put(12836, "小哈");
|
||||
map.put(15937, "小囉");
|
||||
map.put(16750, "小算");
|
||||
map.put(13276, "小法");
|
||||
map.put(10583, "小鴨");
|
||||
println!("\n新增完成後,雜湊表為\nKey -> Value");
|
||||
map.print();
|
||||
|
||||
/* 查詢操作 */
|
||||
// 向雜湊表中輸入鍵 key ,得到值 value
|
||||
let name = map.get(15937).unwrap();
|
||||
println!("\n輸入學號 15937 ,查詢到姓名 {}", name);
|
||||
|
||||
/* 刪除操作 */
|
||||
// 在雜湊表中刪除鍵值對 (key, value)
|
||||
map.remove(10583);
|
||||
println!("\n刪除 10583 後,雜湊表為\nKey -> Value");
|
||||
map.print();
|
||||
|
||||
/* 走訪雜湊表 */
|
||||
println!("\n走訪鍵值對 Key->Value");
|
||||
for pair in map.entry_set() {
|
||||
println!("{} -> {}", pair.key, pair.val);
|
||||
}
|
||||
|
||||
println!("\n單獨走訪鍵 Key");
|
||||
for key in map.key_set() {
|
||||
println!("{}", key);
|
||||
}
|
||||
|
||||
println!("\n單獨走訪值 Value");
|
||||
for val in map.value_set() {
|
||||
println!("{}", val);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
/*
|
||||
* File: build_in_hash.rs
|
||||
* Created Time: 2023-7-6
|
||||
* Author: WSL0809 (wslzzy@outlook.com)
|
||||
*/
|
||||
|
||||
include!("../include/include.rs");
|
||||
|
||||
use crate::list_node::ListNode;
|
||||
use std::collections::hash_map::DefaultHasher;
|
||||
use std::hash::{Hash, Hasher};
|
||||
|
||||
/* Driver Code */
|
||||
fn main() {
|
||||
let num = 3;
|
||||
let mut num_hasher = DefaultHasher::new();
|
||||
num.hash(&mut num_hasher);
|
||||
let hash_num = num_hasher.finish();
|
||||
println!("整數 {} 的雜湊值為 {}", num, hash_num);
|
||||
|
||||
let bol = true;
|
||||
let mut bol_hasher = DefaultHasher::new();
|
||||
bol.hash(&mut bol_hasher);
|
||||
let hash_bol = bol_hasher.finish();
|
||||
println!("布林量 {} 的雜湊值為 {}", bol, hash_bol);
|
||||
|
||||
let dec: f32 = 3.14159;
|
||||
let mut dec_hasher = DefaultHasher::new();
|
||||
dec.to_bits().hash(&mut dec_hasher);
|
||||
let hash_dec = dec_hasher.finish();
|
||||
println!("小數 {} 的雜湊值為 {}", dec, hash_dec);
|
||||
|
||||
let str = "Hello 演算法";
|
||||
let mut str_hasher = DefaultHasher::new();
|
||||
str.hash(&mut str_hasher);
|
||||
let hash_str = str_hasher.finish();
|
||||
println!("字串 {} 的雜湊值為 {}", str, hash_str);
|
||||
|
||||
let arr = (&12836, &"小哈");
|
||||
let mut tup_hasher = DefaultHasher::new();
|
||||
arr.hash(&mut tup_hasher);
|
||||
let hash_tup = tup_hasher.finish();
|
||||
println!("元組 {:?} 的雜湊值為 {}", arr, hash_tup);
|
||||
|
||||
let node = ListNode::new(42);
|
||||
let mut hasher = DefaultHasher::new();
|
||||
node.borrow().val.hash(&mut hasher);
|
||||
let hash = hasher.finish();
|
||||
println!("節點物件 {:?} 的雜湊值為{}", node, hash);
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
/*
|
||||
* File: hash_map.rs
|
||||
* Created Time: 2023-02-05
|
||||
* Author: codingonion (coderonion@gmail.com)
|
||||
*/
|
||||
|
||||
include!("../include/include.rs");
|
||||
|
||||
use std::collections::HashMap;
|
||||
|
||||
/* Driver Code */
|
||||
pub fn main() {
|
||||
// 初始化雜湊表
|
||||
let mut map = HashMap::new();
|
||||
|
||||
// 新增操作
|
||||
// 在雜湊表中新增鍵值對 (key, value)
|
||||
map.insert(12836, "小哈");
|
||||
map.insert(15937, "小囉");
|
||||
map.insert(16750, "小算");
|
||||
map.insert(13276, "小法");
|
||||
map.insert(10583, "小鴨");
|
||||
println!("\n新增完成後,雜湊表為\nKey -> Value");
|
||||
print_util::print_hash_map(&map);
|
||||
|
||||
// 查詢操作
|
||||
// 向雜湊表中輸入鍵 key ,得到值 value
|
||||
let name = map.get(&15937).copied().unwrap();
|
||||
println!("\n輸入學號 15937 ,查詢到姓名 {name}");
|
||||
|
||||
// 刪除操作
|
||||
// 在雜湊表中刪除鍵值對 (key, value)
|
||||
_ = map.remove(&10583);
|
||||
println!("\n刪除 10583 後,雜湊表為\nKey -> Value");
|
||||
print_util::print_hash_map(&map);
|
||||
|
||||
// 走訪雜湊表
|
||||
println!("\n走訪鍵值對 Key->Value");
|
||||
print_util::print_hash_map(&map);
|
||||
println!("\n單獨走訪鍵 Key");
|
||||
for key in map.keys() {
|
||||
println!("{key}");
|
||||
}
|
||||
println!("\n單獨走訪值 value");
|
||||
for value in map.values() {
|
||||
println!("{value}");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,167 @@
|
||||
/*
|
||||
* File: hash_map_chaining.rs
|
||||
* Created Time: 2023-07-07
|
||||
* Author: WSL0809 (wslzzy@outlook.com)
|
||||
*/
|
||||
|
||||
#[derive(Clone)]
|
||||
/* 鍵值對 */
|
||||
struct Pair {
|
||||
key: i32,
|
||||
val: String,
|
||||
}
|
||||
|
||||
/* 鏈式位址雜湊表 */
|
||||
struct HashMapChaining {
|
||||
size: i32,
|
||||
capacity: i32,
|
||||
load_thres: f32,
|
||||
extend_ratio: i32,
|
||||
buckets: Vec<Vec<Pair>>,
|
||||
}
|
||||
|
||||
impl HashMapChaining {
|
||||
/* 建構子 */
|
||||
fn new() -> Self {
|
||||
Self {
|
||||
size: 0,
|
||||
capacity: 4,
|
||||
load_thres: 2.0 / 3.0,
|
||||
extend_ratio: 2,
|
||||
buckets: vec![vec![]; 4],
|
||||
}
|
||||
}
|
||||
|
||||
/* 雜湊函式 */
|
||||
fn hash_func(&self, key: i32) -> usize {
|
||||
key as usize % self.capacity as usize
|
||||
}
|
||||
|
||||
/* 負載因子 */
|
||||
fn load_factor(&self) -> f32 {
|
||||
self.size as f32 / self.capacity as f32
|
||||
}
|
||||
|
||||
/* 刪除操作 */
|
||||
fn remove(&mut self, key: i32) -> Option<String> {
|
||||
let index = self.hash_func(key);
|
||||
let bucket = &mut self.buckets[index];
|
||||
|
||||
// 走訪桶,從中刪除鍵值對
|
||||
for i in 0..bucket.len() {
|
||||
if bucket[i].key == key {
|
||||
let pair = bucket.remove(i);
|
||||
self.size -= 1;
|
||||
return Some(pair.val);
|
||||
}
|
||||
}
|
||||
|
||||
// 若未找到 key ,則返回 None
|
||||
None
|
||||
}
|
||||
|
||||
/* 擴容雜湊表 */
|
||||
fn extend(&mut self) {
|
||||
// 暫存原雜湊表
|
||||
let buckets_tmp = std::mem::replace(&mut self.buckets, vec![]);
|
||||
|
||||
// 初始化擴容後的新雜湊表
|
||||
self.capacity *= self.extend_ratio;
|
||||
self.buckets = vec![Vec::new(); self.capacity as usize];
|
||||
self.size = 0;
|
||||
|
||||
// 將鍵值對從原雜湊表搬運至新雜湊表
|
||||
for bucket in buckets_tmp {
|
||||
for pair in bucket {
|
||||
self.put(pair.key, pair.val);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* 列印雜湊表 */
|
||||
fn print(&self) {
|
||||
for bucket in &self.buckets {
|
||||
let mut res = Vec::new();
|
||||
for pair in bucket {
|
||||
res.push(format!("{} -> {}", pair.key, pair.val));
|
||||
}
|
||||
println!("{:?}", res);
|
||||
}
|
||||
}
|
||||
|
||||
/* 新增操作 */
|
||||
fn put(&mut self, key: i32, val: String) {
|
||||
// 當負載因子超過閾值時,執行擴容
|
||||
if self.load_factor() > self.load_thres {
|
||||
self.extend();
|
||||
}
|
||||
|
||||
let index = self.hash_func(key);
|
||||
let bucket = &mut self.buckets[index];
|
||||
|
||||
// 走訪桶,若遇到指定 key ,則更新對應 val 並返回
|
||||
for pair in bucket {
|
||||
if pair.key == key {
|
||||
pair.val = val.clone();
|
||||
return;
|
||||
}
|
||||
}
|
||||
let bucket = &mut self.buckets[index];
|
||||
|
||||
// 若無該 key ,則將鍵值對新增至尾部
|
||||
let pair = Pair {
|
||||
key,
|
||||
val: val.clone(),
|
||||
};
|
||||
bucket.push(pair);
|
||||
self.size += 1;
|
||||
}
|
||||
|
||||
/* 查詢操作 */
|
||||
fn get(&self, key: i32) -> Option<&str> {
|
||||
let index = self.hash_func(key);
|
||||
let bucket = &self.buckets[index];
|
||||
|
||||
// 走訪桶,若找到 key ,則返回對應 val
|
||||
for pair in bucket {
|
||||
if pair.key == key {
|
||||
return Some(&pair.val);
|
||||
}
|
||||
}
|
||||
|
||||
// 若未找到 key ,則返回 None
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
pub fn main() {
|
||||
/* 初始化雜湊表 */
|
||||
let mut map = HashMapChaining::new();
|
||||
|
||||
/* 新增操作 */
|
||||
// 在雜湊表中新增鍵值對 (key, value)
|
||||
map.put(12836, "小哈".to_string());
|
||||
map.put(15937, "小囉".to_string());
|
||||
map.put(16750, "小算".to_string());
|
||||
map.put(13276, "小法".to_string());
|
||||
map.put(10583, "小鴨".to_string());
|
||||
println!("\n新增完成後,雜湊表為\nKey -> Value");
|
||||
map.print();
|
||||
|
||||
/* 查詢操作 */
|
||||
// 向雜湊表中輸入鍵 key ,得到值 value
|
||||
println!(
|
||||
"\n輸入學號 13276,查詢到姓名 {}",
|
||||
match map.get(13276) {
|
||||
Some(value) => value,
|
||||
None => "Not a valid Key",
|
||||
}
|
||||
);
|
||||
|
||||
/* 刪除操作 */
|
||||
// 在雜湊表中刪除鍵值對 (key, value)
|
||||
map.remove(12836);
|
||||
println!("\n刪除 12836 後,雜湊表為\nKey -> Value");
|
||||
map.print();
|
||||
}
|
||||
@@ -0,0 +1,181 @@
|
||||
/*
|
||||
* File: hash_map_open_addressing.rs
|
||||
* Created Time: 2023-07-16
|
||||
* Author: WSL0809 (wslzzy@outlook.com), night-cruise (2586447362@qq.com)
|
||||
*/
|
||||
#![allow(non_snake_case)]
|
||||
#![allow(unused)]
|
||||
|
||||
mod array_hash_map;
|
||||
|
||||
use array_hash_map::Pair;
|
||||
|
||||
/* 開放定址雜湊表 */
|
||||
struct HashMapOpenAddressing {
|
||||
size: usize, // 鍵值對數量
|
||||
capacity: usize, // 雜湊表容量
|
||||
load_thres: f64, // 觸發擴容的負載因子閾值
|
||||
extend_ratio: usize, // 擴容倍數
|
||||
buckets: Vec<Option<Pair>>, // 桶陣列
|
||||
TOMBSTONE: Option<Pair>, // 刪除標記
|
||||
}
|
||||
|
||||
impl HashMapOpenAddressing {
|
||||
/* 建構子 */
|
||||
fn new() -> Self {
|
||||
Self {
|
||||
size: 0,
|
||||
capacity: 4,
|
||||
load_thres: 2.0 / 3.0,
|
||||
extend_ratio: 2,
|
||||
buckets: vec![None; 4],
|
||||
TOMBSTONE: Some(Pair {
|
||||
key: -1,
|
||||
val: "-1".to_string(),
|
||||
}),
|
||||
}
|
||||
}
|
||||
|
||||
/* 雜湊函式 */
|
||||
fn hash_func(&self, key: i32) -> usize {
|
||||
(key % self.capacity as i32) as usize
|
||||
}
|
||||
|
||||
/* 負載因子 */
|
||||
fn load_factor(&self) -> f64 {
|
||||
self.size as f64 / self.capacity as f64
|
||||
}
|
||||
|
||||
/* 搜尋 key 對應的桶索引 */
|
||||
fn find_bucket(&mut self, key: i32) -> usize {
|
||||
let mut index = self.hash_func(key);
|
||||
let mut first_tombstone = -1;
|
||||
// 線性探查,當遇到空桶時跳出
|
||||
while self.buckets[index].is_some() {
|
||||
// 若遇到 key,返回對應的桶索引
|
||||
if self.buckets[index].as_ref().unwrap().key == key {
|
||||
// 若之前遇到了刪除標記,則將建值對移動至該索引
|
||||
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 index; // 返回桶索引
|
||||
}
|
||||
// 記錄遇到的首個刪除標記
|
||||
if first_tombstone == -1 && self.buckets[index] == self.TOMBSTONE {
|
||||
first_tombstone = index as i32;
|
||||
}
|
||||
// 計算桶索引,越過尾部則返回頭部
|
||||
index = (index + 1) % self.capacity;
|
||||
}
|
||||
// 若 key 不存在,則返回新增點的索引
|
||||
if first_tombstone == -1 {
|
||||
index
|
||||
} else {
|
||||
first_tombstone as usize
|
||||
}
|
||||
}
|
||||
|
||||
/* 查詢操作 */
|
||||
fn get(&mut self, key: i32) -> Option<&str> {
|
||||
// 搜尋 key 對應的桶索引
|
||||
let index = self.find_bucket(key);
|
||||
// 若找到鍵值對,則返回對應 val
|
||||
if self.buckets[index].is_some() && self.buckets[index] != self.TOMBSTONE {
|
||||
return self.buckets[index].as_ref().map(|pair| &pair.val as &str);
|
||||
}
|
||||
// 若鍵值對不存在,則返回 null
|
||||
None
|
||||
}
|
||||
|
||||
/* 新增操作 */
|
||||
fn put(&mut self, key: i32, val: String) {
|
||||
// 當負載因子超過閾值時,執行擴容
|
||||
if self.load_factor() > self.load_thres {
|
||||
self.extend();
|
||||
}
|
||||
// 搜尋 key 對應的桶索引
|
||||
let index = self.find_bucket(key);
|
||||
// 若找到鍵值對,則覆蓋 val 並返回
|
||||
if self.buckets[index].is_some() && self.buckets[index] != self.TOMBSTONE {
|
||||
self.buckets[index].as_mut().unwrap().val = val;
|
||||
return;
|
||||
}
|
||||
// 若鍵值對不存在,則新增該鍵值對
|
||||
self.buckets[index] = Some(Pair { key, val });
|
||||
self.size += 1;
|
||||
}
|
||||
|
||||
/* 刪除操作 */
|
||||
fn remove(&mut self, key: i32) {
|
||||
// 搜尋 key 對應的桶索引
|
||||
let index = self.find_bucket(key);
|
||||
// 若找到鍵值對,則用刪除標記覆蓋它
|
||||
if self.buckets[index].is_some() && self.buckets[index] != self.TOMBSTONE {
|
||||
self.buckets[index] = self.TOMBSTONE.clone();
|
||||
self.size -= 1;
|
||||
}
|
||||
}
|
||||
|
||||
/* 擴容雜湊表 */
|
||||
fn extend(&mut self) {
|
||||
// 暫存原雜湊表
|
||||
let buckets_tmp = self.buckets.clone();
|
||||
// 初始化擴容後的新雜湊表
|
||||
self.capacity *= self.extend_ratio;
|
||||
self.buckets = vec![None; self.capacity];
|
||||
self.size = 0;
|
||||
|
||||
// 將鍵值對從原雜湊表搬運至新雜湊表
|
||||
for pair in buckets_tmp {
|
||||
if pair.is_none() || pair == self.TOMBSTONE {
|
||||
continue;
|
||||
}
|
||||
let pair = pair.unwrap();
|
||||
|
||||
self.put(pair.key, pair.val);
|
||||
}
|
||||
}
|
||||
/* 列印雜湊表 */
|
||||
fn print(&self) {
|
||||
for pair in &self.buckets {
|
||||
if pair.is_none() {
|
||||
println!("null");
|
||||
} else if pair == &self.TOMBSTONE {
|
||||
println!("TOMBSTONE");
|
||||
} else {
|
||||
let pair = pair.as_ref().unwrap();
|
||||
println!("{} -> {}", pair.key, pair.val);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
fn main() {
|
||||
/* 初始化雜湊表 */
|
||||
let mut hashmap = HashMapOpenAddressing::new();
|
||||
|
||||
/* 新增操作 */
|
||||
// 在雜湊表中新增鍵值對 (key, value)
|
||||
hashmap.put(12836, "小哈".to_string());
|
||||
hashmap.put(15937, "小囉".to_string());
|
||||
hashmap.put(16750, "小算".to_string());
|
||||
hashmap.put(13276, "小法".to_string());
|
||||
hashmap.put(10583, "小鴨".to_string());
|
||||
|
||||
println!("\n新增完成後,雜湊表為\nKey -> Value");
|
||||
hashmap.print();
|
||||
|
||||
/* 查詢操作 */
|
||||
// 向雜湊表中輸入鍵 key ,得到值 val
|
||||
let name = hashmap.get(13276).unwrap();
|
||||
println!("\n輸入學號 13276 ,查詢到姓名 {}", name);
|
||||
|
||||
/* 刪除操作 */
|
||||
// 在雜湊表中刪除鍵值對 (key, val)
|
||||
hashmap.remove(16750);
|
||||
println!("\n刪除 16750 後,雜湊表為\nKey -> Value");
|
||||
hashmap.print();
|
||||
}
|
||||
@@ -0,0 +1,70 @@
|
||||
/*
|
||||
* File: simple_hash.rs
|
||||
* Created Time: 2023-09-07
|
||||
* Author: night-cruise (2586447362@qq.com)
|
||||
*/
|
||||
|
||||
/* 加法雜湊 */
|
||||
fn add_hash(key: &str) -> i32 {
|
||||
let mut hash = 0_i64;
|
||||
const MODULUS: i64 = 1000000007;
|
||||
|
||||
for c in key.chars() {
|
||||
hash = (hash + c as i64) % MODULUS;
|
||||
}
|
||||
|
||||
hash as i32
|
||||
}
|
||||
|
||||
/* 乘法雜湊 */
|
||||
fn mul_hash(key: &str) -> i32 {
|
||||
let mut hash = 0_i64;
|
||||
const MODULUS: i64 = 1000000007;
|
||||
|
||||
for c in key.chars() {
|
||||
hash = (31 * hash + c as i64) % MODULUS;
|
||||
}
|
||||
|
||||
hash as i32
|
||||
}
|
||||
|
||||
/* 互斥或雜湊 */
|
||||
fn xor_hash(key: &str) -> i32 {
|
||||
let mut hash = 0_i64;
|
||||
const MODULUS: i64 = 1000000007;
|
||||
|
||||
for c in key.chars() {
|
||||
hash ^= c as i64;
|
||||
}
|
||||
|
||||
(hash & MODULUS) as i32
|
||||
}
|
||||
|
||||
/* 旋轉雜湊 */
|
||||
fn rot_hash(key: &str) -> i32 {
|
||||
let mut hash = 0_i64;
|
||||
const MODULUS: i64 = 1000000007;
|
||||
|
||||
for c in key.chars() {
|
||||
hash = ((hash << 4) ^ (hash >> 28) ^ c as i64) % MODULUS;
|
||||
}
|
||||
|
||||
hash as i32
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
fn main() {
|
||||
let key = "Hello 演算法";
|
||||
|
||||
let hash = add_hash(key);
|
||||
println!("加法雜湊值為 {hash}");
|
||||
|
||||
let hash = mul_hash(key);
|
||||
println!("乘法雜湊值為 {hash}");
|
||||
|
||||
let hash = xor_hash(key);
|
||||
println!("互斥或雜湊值為 {hash}");
|
||||
|
||||
let hash = rot_hash(key);
|
||||
println!("旋轉雜湊值為 {hash}");
|
||||
}
|
||||
Reference in New Issue
Block a user