This commit is contained in:
krahets
2023-10-08 01:43:28 +08:00
parent 3d2d669b43
commit baac2d11a7
52 changed files with 999 additions and 625 deletions
+5 -5
View File
@@ -180,7 +180,7 @@ index = hash(key) % capacity
```csharp title="simple_hash.cs"
/* 加法哈希 */
int addHash(string key) {
int AddHash(string key) {
long hash = 0;
const int MODULUS = 1000000007;
foreach (char c in key) {
@@ -190,7 +190,7 @@ index = hash(key) % capacity
}
/* 乘法哈希 */
int mulHash(string key) {
int MulHash(string key) {
long hash = 0;
const int MODULUS = 1000000007;
foreach (char c in key) {
@@ -200,7 +200,7 @@ index = hash(key) % capacity
}
/* 异或哈希 */
int xorHash(string key) {
int XorHash(string key) {
int hash = 0;
const int MODULUS = 1000000007;
foreach (char c in key) {
@@ -210,7 +210,7 @@ index = hash(key) % capacity
}
/* 旋转哈希 */
int rotHash(string key) {
int RotHash(string key) {
long hash = 0;
const int MODULUS = 1000000007;
foreach (char c in key) {
@@ -735,7 +735,7 @@ $$
int hashTup = arr.GetHashCode();
// 数组 [12836, 小哈] 的哈希值为 42931033;
ListNode obj = new ListNode(0);
ListNode obj = new(0);
int hashObj = obj.GetHashCode();
// 节点对象 0 的哈希值为 39053774;
```
+34 -34
View File
@@ -359,8 +359,8 @@ comments: true
class HashMapChaining {
int size; // 键值对数量
int capacity; // 哈希表容量
double loadThres; // 触发扩容的负载因子阈值
int extendRatio; // 扩容倍数
readonly double loadThres; // 触发扩容的负载因子阈值
readonly int extendRatio; // 扩容倍数
List<List<Pair>> buckets; // 桶数组
/* 构造方法 */
@@ -376,18 +376,18 @@ comments: true
}
/* 哈希函数 */
private int hashFunc(int key) {
private int HashFunc(int key) {
return key % capacity;
}
/* 负载因子 */
private double loadFactor() {
private double LoadFactor() {
return (double)size / capacity;
}
/* 查询操作 */
public string? get(int key) {
int index = hashFunc(key);
public string? Get(int key) {
int index = HashFunc(key);
// 遍历桶,若找到 key 则返回对应 val
foreach (Pair pair in buckets[index]) {
if (pair.key == key) {
@@ -399,12 +399,12 @@ comments: true
}
/* 添加操作 */
public void put(int key, string val) {
public void Put(int key, string val) {
// 当负载因子超过阈值时,执行扩容
if (loadFactor() > loadThres) {
extend();
if (LoadFactor() > loadThres) {
Extend();
}
int index = hashFunc(key);
int index = HashFunc(key);
// 遍历桶,若遇到指定 key ,则更新对应 val 并返回
foreach (Pair pair in buckets[index]) {
if (pair.key == key) {
@@ -418,8 +418,8 @@ comments: true
}
/* 删除操作 */
public void remove(int key) {
int index = hashFunc(key);
public void Remove(int key) {
int index = HashFunc(key);
// 遍历桶,从中删除键值对
foreach (Pair pair in buckets[index].ToList()) {
if (pair.key == key) {
@@ -431,7 +431,7 @@ comments: true
}
/* 扩容哈希表 */
private void extend() {
private void Extend() {
// 暂存原哈希表
List<List<Pair>> bucketsTmp = buckets;
// 初始化扩容后的新哈希表
@@ -444,15 +444,15 @@ comments: true
// 将键值对从原哈希表搬运至新哈希表
foreach (List<Pair> bucket in bucketsTmp) {
foreach (Pair pair in bucket) {
put(pair.key, pair.val);
Put(pair.key, pair.val);
}
}
}
/* 打印哈希表 */
public void print() {
public void Print() {
foreach (List<Pair> bucket in buckets) {
List<string> res = new List<string>();
List<string> res = new();
foreach (Pair pair in bucket) {
res.Add(pair.key + " -> " + pair.val);
}
@@ -1743,10 +1743,10 @@ comments: true
class HashMapOpenAddressing {
private int size; // 键值对数量
private int capacity = 4; // 哈希表容量
private double loadThres = 2.0 / 3.0; // 触发扩容的负载因子阈值
private int extendRatio = 2; // 扩容倍数
private readonly double loadThres = 2.0 / 3.0; // 触发扩容的负载因子阈值
private readonly int extendRatio = 2; // 扩容倍数
private Pair[] buckets; // 桶数组
private Pair TOMBSTONE = new Pair(-1, "-1"); // 删除标记
private readonly Pair TOMBSTONE = new(-1, "-1"); // 删除标记
/* 构造方法 */
public HashMapOpenAddressing() {
@@ -1755,18 +1755,18 @@ comments: true
}
/* 哈希函数 */
private int hashFunc(int key) {
private int HashFunc(int key) {
return key % capacity;
}
/* 负载因子 */
private double loadFactor() {
private double LoadFactor() {
return (double)size / capacity;
}
/* 搜索 key 对应的桶索引 */
private int findBucket(int key) {
int index = hashFunc(key);
private int FindBucket(int key) {
int index = HashFunc(key);
int firstTombstone = -1;
// 线性探测,当遇到空桶时跳出
while (buckets[index] != null) {
@@ -1792,9 +1792,9 @@ comments: true
}
/* 查询操作 */
public string? get(int key) {
public string? Get(int key) {
// 搜索 key 对应的桶索引
int index = findBucket(key);
int index = FindBucket(key);
// 若找到键值对,则返回对应 val
if (buckets[index] != null && buckets[index] != TOMBSTONE) {
return buckets[index].val;
@@ -1804,13 +1804,13 @@ comments: true
}
/* 添加操作 */
public void put(int key, string val) {
public void Put(int key, string val) {
// 当负载因子超过阈值时,执行扩容
if (loadFactor() > loadThres) {
extend();
if (LoadFactor() > loadThres) {
Extend();
}
// 搜索 key 对应的桶索引
int index = findBucket(key);
int index = FindBucket(key);
// 若找到键值对,则覆盖 val 并返回
if (buckets[index] != null && buckets[index] != TOMBSTONE) {
buckets[index].val = val;
@@ -1822,9 +1822,9 @@ comments: true
}
/* 删除操作 */
public void remove(int key) {
public void Remove(int key) {
// 搜索 key 对应的桶索引
int index = findBucket(key);
int index = FindBucket(key);
// 若找到键值对,则用删除标记覆盖它
if (buckets[index] != null && buckets[index] != TOMBSTONE) {
buckets[index] = TOMBSTONE;
@@ -1833,7 +1833,7 @@ comments: true
}
/* 扩容哈希表 */
private void extend() {
private void Extend() {
// 暂存原哈希表
Pair[] bucketsTmp = buckets;
// 初始化扩容后的新哈希表
@@ -1843,13 +1843,13 @@ comments: true
// 将键值对从原哈希表搬运至新哈希表
foreach (Pair pair in bucketsTmp) {
if (pair != null && pair != TOMBSTONE) {
put(pair.key, pair.val);
Put(pair.key, pair.val);
}
}
}
/* 打印哈希表 */
public void print() {
public void Print() {
foreach (Pair pair in buckets) {
if (pair == null) {
Console.WriteLine("null");
+24 -24
View File
@@ -109,19 +109,19 @@ comments: true
```csharp title="hash_map.cs"
/* 初始化哈希表 */
Dictionary<int, String> map = new ();
/* 添加操作 */
// 在哈希表中添加键值对 (key, value)
map.Add(12836, "小哈");
map.Add(15937, "小啰");
map.Add(16750, "小算");
map.Add(13276, "小法");
map.Add(10583, "小鸭");
Dictionary<int, string> map = new() {
/* 添加操作 */
// 在哈希表中添加键值对 (key, value)
{ 12836, "小哈" },
{ 15937, "小啰" },
{ 16750, "小算" },
{ 13276, "小法" },
{ 10583, "小鸭" }
};
/* 查询操作 */
// 向哈希表输入键 key ,得到值 value
String name = map[15937];
string name = map[15937];
/* 删除操作 */
// 在哈希表中删除键值对 (key, value)
@@ -791,7 +791,7 @@ index = hash(key) % capacity
/* 基于数组简易实现的哈希表 */
class ArrayHashMap {
private List<Pair?> buckets;
private readonly List<Pair?> buckets;
public ArrayHashMap() {
// 初始化数组,包含 100 个桶
buckets = new();
@@ -801,35 +801,35 @@ index = hash(key) % capacity
}
/* 哈希函数 */
private int hashFunc(int key) {
private int HashFunc(int key) {
int index = key % 100;
return index;
}
/* 查询操作 */
public string? get(int key) {
int index = hashFunc(key);
public string? Get(int key) {
int index = HashFunc(key);
Pair? pair = buckets[index];
if (pair == null) return null;
return pair.val;
}
/* 添加操作 */
public void put(int key, string val) {
Pair pair = new Pair(key, val);
int index = hashFunc(key);
public void Put(int key, string val) {
Pair pair = new(key, val);
int index = HashFunc(key);
buckets[index] = pair;
}
/* 删除操作 */
public void remove(int key) {
int index = hashFunc(key);
public void Remove(int key) {
int index = HashFunc(key);
// 置为 null ,代表删除
buckets[index] = null;
}
/* 获取所有键值对 */
public List<Pair> pairSet() {
public List<Pair> PairSet() {
List<Pair> pairSet = new();
foreach (Pair? pair in buckets) {
if (pair != null)
@@ -839,7 +839,7 @@ index = hash(key) % capacity
}
/* 获取所有键 */
public List<int> keySet() {
public List<int> KeySet() {
List<int> keySet = new();
foreach (Pair? pair in buckets) {
if (pair != null)
@@ -849,7 +849,7 @@ index = hash(key) % capacity
}
/* 获取所有值 */
public List<string> valueSet() {
public List<string> ValueSet() {
List<string> valueSet = new();
foreach (Pair? pair in buckets) {
if (pair != null)
@@ -859,8 +859,8 @@ index = hash(key) % capacity
}
/* 打印哈希表 */
public void print() {
foreach (Pair kv in pairSet()) {
public void Print() {
foreach (Pair kv in PairSet()) {
Console.WriteLine(kv.key + " -> " + kv.val);
}
}