mirror of
https://github.com/krahets/hello-algo.git
synced 2026-04-14 02:10:37 +08:00
build
This commit is contained in:
@@ -1741,21 +1741,17 @@ comments: true
|
||||
```csharp title="hash_map_open_addressing.cs"
|
||||
/* 开放寻址哈希表 */
|
||||
class HashMapOpenAddressing {
|
||||
int size; // 键值对数量
|
||||
int capacity; // 哈希表容量
|
||||
double loadThres; // 触发扩容的负载因子阈值
|
||||
int extendRatio; // 扩容倍数
|
||||
Pair[] buckets; // 桶数组
|
||||
Pair removed; // 删除标记
|
||||
private int size; // 键值对数量
|
||||
private int capacity = 4; // 哈希表容量
|
||||
private double loadThres = 2.0 / 3; // 触发扩容的负载因子阈值
|
||||
private int extendRatio = 2; // 扩容倍数
|
||||
private Pair[] buckets; // 桶数组
|
||||
private Pair TOMBSTONE = new Pair(-1, "-1"); // 删除标记
|
||||
|
||||
/* 构造方法 */
|
||||
public HashMapOpenAddressing() {
|
||||
size = 0;
|
||||
capacity = 4;
|
||||
loadThres = 2.0 / 3.0;
|
||||
extendRatio = 2;
|
||||
buckets = new Pair[capacity];
|
||||
removed = new Pair(-1, "-1");
|
||||
}
|
||||
|
||||
/* 哈希函数 */
|
||||
@@ -1768,20 +1764,42 @@ comments: true
|
||||
return (double)size / capacity;
|
||||
}
|
||||
|
||||
/* 搜索 key 对应的桶索引 */
|
||||
private int findBucket(int key) {
|
||||
int index = hashFunc(key);
|
||||
int firstTombstone = -1;
|
||||
// 线性探测,当遇到空桶时跳出
|
||||
while (buckets[index] != null) {
|
||||
// 若遇到 key ,返回对应桶索引
|
||||
if (buckets[index].key == key) {
|
||||
// 若之前遇到了删除标记,则将键值对移动至该索引
|
||||
if (firstTombstone != -1) {
|
||||
buckets[firstTombstone] = buckets[index];
|
||||
buckets[index] = TOMBSTONE;
|
||||
return firstTombstone; // 返回移动后的桶索引
|
||||
}
|
||||
return index; // 返回桶索引
|
||||
}
|
||||
// 记录遇到的首个删除标记
|
||||
if (firstTombstone == -1 && buckets[index] == TOMBSTONE) {
|
||||
firstTombstone = index;
|
||||
}
|
||||
// 计算桶索引,越过尾部返回头部
|
||||
index = (index + 1) % capacity;
|
||||
}
|
||||
// 若 key 不存在,则返回添加点的索引
|
||||
return firstTombstone == -1 ? index : firstTombstone;
|
||||
}
|
||||
|
||||
/* 查询操作 */
|
||||
public string get(int key) {
|
||||
int index = hashFunc(key);
|
||||
// 线性探测,从 index 开始向后遍历
|
||||
for (int i = 0; i < capacity; i++) {
|
||||
// 计算桶索引,越过尾部返回头部
|
||||
int j = (index + i) % capacity;
|
||||
// 若遇到空桶,说明无此 key ,则返回 null
|
||||
if (buckets[j] == null)
|
||||
return null;
|
||||
// 若遇到指定 key ,则返回对应 val
|
||||
if (buckets[j].key == key && buckets[j] != removed)
|
||||
return buckets[j].val;
|
||||
// 搜索 key 对应的桶索引
|
||||
int index = findBucket(key);
|
||||
// 若找到键值对,则返回对应 val
|
||||
if (buckets[index] != null && buckets[index] != TOMBSTONE) {
|
||||
return buckets[index].val;
|
||||
}
|
||||
// 若键值对不存在,则返回 null
|
||||
return null;
|
||||
}
|
||||
|
||||
@@ -1791,42 +1809,26 @@ comments: true
|
||||
if (loadFactor() > loadThres) {
|
||||
extend();
|
||||
}
|
||||
int index = hashFunc(key);
|
||||
// 线性探测,从 index 开始向后遍历
|
||||
for (int i = 0; i < capacity; i++) {
|
||||
// 计算桶索引,越过尾部返回头部
|
||||
int j = (index + i) % capacity;
|
||||
// 若遇到空桶、或带有删除标记的桶,则将键值对放入该桶
|
||||
if (buckets[j] == null || buckets[j] == removed) {
|
||||
buckets[j] = new Pair(key, val);
|
||||
size += 1;
|
||||
return;
|
||||
}
|
||||
// 若遇到指定 key ,则更新对应 val
|
||||
if (buckets[j].key == key) {
|
||||
buckets[j].val = val;
|
||||
return;
|
||||
}
|
||||
// 搜索 key 对应的桶索引
|
||||
int index = findBucket(key);
|
||||
// 若找到键值对,则覆盖 val 并返回
|
||||
if (buckets[index] != null && buckets[index] != TOMBSTONE) {
|
||||
buckets[index].val = val;
|
||||
return;
|
||||
}
|
||||
// 若键值对不存在,则添加该键值对
|
||||
buckets[index] = new Pair(key, val);
|
||||
size++;
|
||||
}
|
||||
|
||||
/* 删除操作 */
|
||||
public void remove(int key) {
|
||||
int index = hashFunc(key);
|
||||
// 线性探测,从 index 开始向后遍历
|
||||
for (int i = 0; i < capacity; i++) {
|
||||
// 计算桶索引,越过尾部返回头部
|
||||
int j = (index + i) % capacity;
|
||||
// 若遇到空桶,说明无此 key ,则直接返回
|
||||
if (buckets[j] == null) {
|
||||
return;
|
||||
}
|
||||
// 若遇到指定 key ,则标记删除并返回
|
||||
if (buckets[j].key == key) {
|
||||
buckets[j] = removed;
|
||||
size -= 1;
|
||||
return;
|
||||
}
|
||||
// 搜索 key 对应的桶索引
|
||||
int index = findBucket(key);
|
||||
// 若找到键值对,则用删除标记覆盖它
|
||||
if (buckets[index] != null && buckets[index] != TOMBSTONE) {
|
||||
buckets[index] = TOMBSTONE;
|
||||
size--;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1840,7 +1842,7 @@ comments: true
|
||||
size = 0;
|
||||
// 将键值对从原哈希表搬运至新哈希表
|
||||
foreach (Pair pair in bucketsTmp) {
|
||||
if (pair != null && pair != removed) {
|
||||
if (pair != null && pair != TOMBSTONE) {
|
||||
put(pair.key, pair.val);
|
||||
}
|
||||
}
|
||||
@@ -1849,10 +1851,12 @@ comments: true
|
||||
/* 打印哈希表 */
|
||||
public void print() {
|
||||
foreach (Pair pair in buckets) {
|
||||
if (pair != null) {
|
||||
Console.WriteLine(pair.key + " -> " + pair.val);
|
||||
} else {
|
||||
if (pair == null) {
|
||||
Console.WriteLine("null");
|
||||
} else if (pair == TOMBSTONE) {
|
||||
Console.WriteLine("TOMBSTONE");
|
||||
} else {
|
||||
Console.WriteLine(pair.key + " -> " + pair.val);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -2503,12 +2507,12 @@ comments: true
|
||||
```rust title="hash_map_open_addressing.rs"
|
||||
/* 开放寻址哈希表 */
|
||||
struct HashMapOpenAddressing {
|
||||
size: usize,
|
||||
capacity: usize,
|
||||
load_thres: f32,
|
||||
extend_ratio: usize,
|
||||
buckets: Vec<Option<Pair>>,
|
||||
removed: Pair,
|
||||
size: usize, // 键值对数量
|
||||
capacity: usize, // 哈希表容量
|
||||
load_thres: f64, // 触发扩容的负载因子阈值
|
||||
extend_ratio: usize, // 扩容倍数
|
||||
buckets: Vec<Option<Pair>>, // 桶数组
|
||||
TOMBSTONE: Option<Pair>, // 删除标记
|
||||
}
|
||||
|
||||
|
||||
@@ -2521,10 +2525,7 @@ comments: true
|
||||
load_thres: 2.0 / 3.0,
|
||||
extend_ratio: 2,
|
||||
buckets: vec![None; 4],
|
||||
removed: Pair {
|
||||
key: -1,
|
||||
val: "-1".to_string(),
|
||||
},
|
||||
TOMBSTONE: Some(Pair {key: -1, val: "-1".to_string()}),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2534,27 +2535,46 @@ comments: true
|
||||
}
|
||||
|
||||
/* 负载因子 */
|
||||
fn load_factor(&self) -> f32 {
|
||||
self.size as f32 / self.capacity as f32
|
||||
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(&self, key: i32) -> Option<&str> {
|
||||
let mut index = self.hash_func(key);
|
||||
let capacity = self.capacity;
|
||||
// 线性探测,从 index 开始向后遍历
|
||||
for _ in 0..capacity {
|
||||
// 计算桶索引,越过尾部返回头部
|
||||
let j = (index + 1) % capacity;
|
||||
match &self.buckets[j] {
|
||||
// 若遇到空桶,说明无此 key ,则返回 None
|
||||
None => return None,
|
||||
// 若遇到指定 key ,则返回对应 val
|
||||
Some(pair) if pair.key == key && pair != &self.removed => return Some(&pair.val),
|
||||
_ => index = j,
|
||||
}
|
||||
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
|
||||
}
|
||||
|
||||
@@ -2564,57 +2584,29 @@ comments: true
|
||||
if self.load_factor() > self.load_thres {
|
||||
self.extend();
|
||||
}
|
||||
|
||||
let mut index = self.hash_func(key);
|
||||
let capacity = self.capacity;
|
||||
|
||||
// 线性探测,从 index 开始向后遍历
|
||||
for _ in 0..capacity {
|
||||
//计算桶索引,越过尾部返回头部
|
||||
let j = (index + 1) % capacity;
|
||||
// 若遇到空桶、或带有删除标记的桶,则将键值对放入该桶
|
||||
match &mut self.buckets[j] {
|
||||
bucket @ &mut None | bucket @ &mut Some(Pair { key: -1, .. }) => {
|
||||
*bucket = Some(Pair { key, val });
|
||||
self.size += 1;
|
||||
return;
|
||||
}
|
||||
// 若遇到指定 key ,则更新对应 val
|
||||
Some(pair) if pair.key == key => {
|
||||
pair.val = val;
|
||||
return;
|
||||
}
|
||||
_ => index = j,
|
||||
}
|
||||
// 搜索 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) {
|
||||
let mut index = self.hash_func(key);
|
||||
let capacity = self.capacity;
|
||||
|
||||
// 遍历桶,从中删除键值对
|
||||
for _ in 0..capacity {
|
||||
let j = (index + 1) % capacity;
|
||||
match &mut self.buckets[j] {
|
||||
// 若遇到空桶,说明无此 key ,则直接返回
|
||||
None => return,
|
||||
// 若遇到指定 key ,则标记删除并返回
|
||||
Some(pair) if pair.key == key => {
|
||||
*pair = Pair {
|
||||
key: -1,
|
||||
val: "-1".to_string(),
|
||||
};
|
||||
self.size -= 1;
|
||||
return;
|
||||
}
|
||||
_ => index = j,
|
||||
}
|
||||
// 搜索 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) {
|
||||
// 暂存原哈希表
|
||||
@@ -2626,17 +2618,24 @@ comments: true
|
||||
|
||||
// 将键值对从原哈希表搬运至新哈希表
|
||||
for pair in buckets_tmp {
|
||||
if let Some(pair) = pair {
|
||||
self.put(pair.key, pair.val);
|
||||
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 {
|
||||
match pair {
|
||||
Some(pair) => println!("{} -> {}", pair.key, pair.val),
|
||||
None => println!("None"),
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1329,6 +1329,7 @@ index = hash(key) % capacity
|
||||
|
||||
```rust title="array_hash_map.rs"
|
||||
/* 键值对 */
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
pub struct Pair {
|
||||
pub key: i32,
|
||||
pub val: String,
|
||||
|
||||
Reference in New Issue
Block a user