This commit is contained in:
krahets
2023-11-27 02:32:06 +08:00
parent 32d5bd97aa
commit a4a23e2488
31 changed files with 179 additions and 213 deletions
+1 -1
View File
@@ -731,7 +731,7 @@ $$
int hashStr = str.GetHashCode();
// 字符串 Hello 算法 的哈希值为 -586107568;
object[] arr = { 12836, "小哈" };
object[] arr = [12836, "小哈"];
int hashTup = arr.GetHashCode();
// 数组 [12836, 小哈] 的哈希值为 42931033;
+18 -18
View File
@@ -359,8 +359,8 @@ comments: true
class HashMapChaining {
int size; // 键值对数量
int capacity; // 哈希表容量
readonly double loadThres; // 触发扩容的负载因子阈值
readonly int extendRatio; // 扩容倍数
double loadThres; // 触发扩容的负载因子阈值
int extendRatio; // 扩容倍数
List<List<Pair>> buckets; // 桶数组
/* 构造方法 */
@@ -371,17 +371,17 @@ comments: true
extendRatio = 2;
buckets = new List<List<Pair>>(capacity);
for (int i = 0; i < capacity; i++) {
buckets.Add(new List<Pair>());
buckets.Add([]);
}
}
/* 哈希函数 */
private int HashFunc(int key) {
int HashFunc(int key) {
return key % capacity;
}
/* 负载因子 */
private double LoadFactor() {
double LoadFactor() {
return (double)size / capacity;
}
@@ -431,14 +431,14 @@ comments: true
}
/* 扩容哈希表 */
private void Extend() {
void Extend() {
// 暂存原哈希表
List<List<Pair>> bucketsTmp = buckets;
// 初始化扩容后的新哈希表
capacity *= extendRatio;
buckets = new List<List<Pair>>(capacity);
for (int i = 0; i < capacity; i++) {
buckets.Add(new List<Pair>());
buckets.Add([]);
}
size = 0;
// 将键值对从原哈希表搬运至新哈希表
@@ -452,7 +452,7 @@ comments: true
/* 打印哈希表 */
public void Print() {
foreach (List<Pair> bucket in buckets) {
List<string> res = new();
List<string> res = [];
foreach (Pair pair in bucket) {
res.Add(pair.key + " -> " + pair.val);
}
@@ -1727,12 +1727,12 @@ comments: true
```csharp title="hash_map_open_addressing.cs"
/* 开放寻址哈希表 */
class HashMapOpenAddressing {
private int size; // 键值对数量
private int capacity = 4; // 哈希表容量
private readonly double loadThres = 2.0 / 3.0; // 触发扩容的负载因子阈值
private readonly int extendRatio = 2; // 扩容倍数
private Pair[] buckets; // 桶数组
private readonly Pair TOMBSTONE = new(-1, "-1"); // 删除标记
int size; // 键值对数量
int capacity = 4; // 哈希表容量
double loadThres = 2.0 / 3.0; // 触发扩容的负载因子阈值
int extendRatio = 2; // 扩容倍数
Pair[] buckets; // 桶数组
Pair TOMBSTONE = new(-1, "-1"); // 删除标记
/* 构造方法 */
public HashMapOpenAddressing() {
@@ -1741,17 +1741,17 @@ comments: true
}
/* 哈希函数 */
private int HashFunc(int key) {
int HashFunc(int key) {
return key % capacity;
}
/* 负载因子 */
private double LoadFactor() {
double LoadFactor() {
return (double)size / capacity;
}
/* 搜索 key 对应的桶索引 */
private int FindBucket(int key) {
int FindBucket(int key) {
int index = HashFunc(key);
int firstTombstone = -1;
// 线性探测,当遇到空桶时跳出
@@ -1819,7 +1819,7 @@ comments: true
}
/* 扩容哈希表 */
private void Extend() {
void Extend() {
// 暂存原哈希表
Pair[] bucketsTmp = buckets;
// 初始化扩容后的新哈希表
+10 -14
View File
@@ -776,28 +776,24 @@ index = hash(key) % capacity
```csharp title="array_hash_map.cs"
/* 键值对 int->string */
class Pair {
public int key;
public string val;
public Pair(int key, string val) {
this.key = key;
this.val = val;
}
class Pair(int key, string val) {
public int key = key;
public string val = val;
}
/* 基于数组简易实现的哈希表 */
class ArrayHashMap {
private readonly List<Pair?> buckets;
List<Pair?> buckets;
public ArrayHashMap() {
// 初始化数组,包含 100 个桶
buckets = new();
buckets = [];
for (int i = 0; i < 100; i++) {
buckets.Add(null);
}
}
/* 哈希函数 */
private int HashFunc(int key) {
int HashFunc(int key) {
int index = key % 100;
return index;
}
@@ -826,7 +822,7 @@ index = hash(key) % capacity
/* 获取所有键值对 */
public List<Pair> PairSet() {
List<Pair> pairSet = new();
List<Pair> pairSet = [];
foreach (Pair? pair in buckets) {
if (pair != null)
pairSet.Add(pair);
@@ -836,7 +832,7 @@ index = hash(key) % capacity
/* 获取所有键 */
public List<int> KeySet() {
List<int> keySet = new();
List<int> keySet = [];
foreach (Pair? pair in buckets) {
if (pair != null)
keySet.Add(pair.key);
@@ -846,7 +842,7 @@ index = hash(key) % capacity
/* 获取所有值 */
public List<string> ValueSet() {
List<string> valueSet = new();
List<string> valueSet = [];
foreach (Pair? pair in buckets) {
if (pair != null)
valueSet.Add(pair.val);
@@ -1462,7 +1458,7 @@ index = hash(key) % capacity
for (i = 0; i < HASHTABLE_CAPACITY; i++) {
if (hmap->buckets[i] != NULL) {
entries[index].key = hmap->buckets[i]->key;
entries[index].val = malloc(strlen(hmap->buckets[i]->val + 1));
entries[index].val = malloc(strlen(hmap->buckets[i]->val) + 1);
strcpy(entries[index].val, hmap->buckets[i]->val);
index++;
}