This commit is contained in:
krahets
2023-10-18 02:16:55 +08:00
parent 64c5d13051
commit d2ba55fcd6
22 changed files with 374 additions and 436 deletions

View File

@@ -1150,27 +1150,23 @@ comments: true
```c title="hash_map_chaining.c"
/* 链表节点 */
struct node {
typedef struct Node {
Pair *pair;
struct node *next;
};
typedef struct node Node;
struct Node *next;
} Node;
/* 链式地址哈希表 */
struct hashMapChaining {
typedef struct {
int size; // 键值对数量
int capacity; // 哈希表容量
double loadThres; // 触发扩容的负载因子阈值
int extendRatio; // 扩容倍数
Node **buckets; // 桶数组
};
typedef struct hashMapChaining hashMapChaining;
} HashMapChaining;
/* 构造方法 */
hashMapChaining *initHashMapChaining() {
hashMapChaining *hashMap = (hashMapChaining *)malloc(sizeof(hashMapChaining));
HashMapChaining *initHashMapChaining() {
HashMapChaining *hashMap = (HashMapChaining *)malloc(sizeof(HashMapChaining));
hashMap->size = 0;
hashMap->capacity = 4;
hashMap->loadThres = 2.0 / 3.0;
@@ -1183,7 +1179,7 @@ comments: true
}
/* 析构方法 */
void freeHashMapChaining(hashMapChaining *hashMap) {
void freeHashMapChaining(HashMapChaining *hashMap) {
for (int i = 0; i < hashMap->capacity; i++) {
Node *cur = hashMap->buckets[i];
while (cur) {
@@ -1198,17 +1194,17 @@ comments: true
}
/* 哈希函数 */
int hashFunc(hashMapChaining *hashMap, int key) {
int hashFunc(HashMapChaining *hashMap, int key) {
return key % hashMap->capacity;
}
/* 负载因子 */
double loadFactor(hashMapChaining *hashMap) {
double loadFactor(HashMapChaining *hashMap) {
return (double)hashMap->size / (double)hashMap->capacity;
}
/* 查询操作 */
char *get(hashMapChaining *hashMap, int key) {
char *get(HashMapChaining *hashMap, int key) {
int index = hashFunc(hashMap, key);
// 遍历桶,若找到 key 则返回对应 val
Node *cur = hashMap->buckets[index];
@@ -1222,7 +1218,7 @@ comments: true
}
/* 添加操作 */
void put(hashMapChaining *hashMap, int key, const char *val) {
void put(HashMapChaining *hashMap, int key, const char *val) {
// 当负载因子超过阈值时,执行扩容
if (loadFactor(hashMap) > hashMap->loadThres) {
extend(hashMap);
@@ -1249,7 +1245,7 @@ comments: true
}
/* 扩容哈希表 */
void extend(hashMapChaining *hashMap) {
void extend(HashMapChaining *hashMap) {
// 暂存原哈希表
int oldCapacity = hashMap->capacity;
Node **oldBuckets = hashMap->buckets;
@@ -1277,7 +1273,7 @@ comments: true
}
/* 删除操作 */
void removeKey(hashMapChaining *hashMap, int key) {
void removeKey(HashMapChaining *hashMap, int key) {
int index = hashFunc(hashMap, key);
Node *cur = hashMap->buckets[index];
Node *pre = NULL;
@@ -1301,7 +1297,7 @@ comments: true
}
/* 打印哈希表 */
void print(hashMapChaining *hashMap) {
void print(HashMapChaining *hashMap) {
for (int i = 0; i < hashMap->capacity; i++) {
Node *cur = hashMap->buckets[i];
printf("[");
@@ -2647,20 +2643,18 @@ comments: true
```c title="hash_map_open_addressing.c"
/* 开放寻址哈希表 */
struct hashMapOpenAddressing {
typedef struct {
int size; // 键值对数量
int capacity; // 哈希表容量
double loadThres; // 触发扩容的负载因子阈值
int extendRatio; // 扩容倍数
Pair **buckets; // 桶数组
Pair *TOMBSTONE; // 删除标记
};
typedef struct hashMapOpenAddressing hashMapOpenAddressing;
} HashMapOpenAddressing;
/* 构造方法 */
hashMapOpenAddressing *newHashMapOpenAddressing() {
hashMapOpenAddressing *hashMap = (hashMapOpenAddressing *)malloc(sizeof(hashMapOpenAddressing));
HashMapOpenAddressing *newHashMapOpenAddressing() {
HashMapOpenAddressing *hashMap = (HashMapOpenAddressing *)malloc(sizeof(HashMapOpenAddressing));
hashMap->size = 0;
hashMap->capacity = 4;
hashMap->loadThres = 2.0 / 3.0;
@@ -2674,7 +2668,7 @@ comments: true
}
/* 析构方法 */
void delHashMapOpenAddressing(hashMapOpenAddressing *hashMap) {
void delHashMapOpenAddressing(HashMapOpenAddressing *hashMap) {
for (int i = 0; i < hashMap->capacity; i++) {
Pair *pair = hashMap->buckets[i];
if (pair != NULL && pair != hashMap->TOMBSTONE) {
@@ -2685,17 +2679,17 @@ comments: true
}
/* 哈希函数 */
int hashFunc(hashMapOpenAddressing *hashMap, int key) {
int hashFunc(HashMapOpenAddressing *hashMap, int key) {
return key % hashMap->capacity;
}
/* 负载因子 */
double loadFactor(hashMapOpenAddressing *hashMap) {
double loadFactor(HashMapOpenAddressing *hashMap) {
return (double)hashMap->size / (double)hashMap->capacity;
}
/* 搜索 key 对应的桶索引 */
int findBucket(hashMapOpenAddressing *hashMap, int key) {
int findBucket(HashMapOpenAddressing *hashMap, int key) {
int index = hashFunc(hashMap, key);
int firstTombstone = -1;
// 线性探测,当遇到空桶时跳出
@@ -2722,7 +2716,7 @@ comments: true
}
/* 查询操作 */
char *get(hashMapOpenAddressing *hashMap, int key) {
char *get(HashMapOpenAddressing *hashMap, int key) {
// 搜索 key 对应的桶索引
int index = findBucket(hashMap, key);
// 若找到键值对,则返回对应 val
@@ -2734,7 +2728,7 @@ comments: true
}
/* 添加操作 */
void put(hashMapOpenAddressing *hashMap, int key, char *val) {
void put(HashMapOpenAddressing *hashMap, int key, char *val) {
// 当负载因子超过阈值时,执行扩容
if (loadFactor(hashMap) > hashMap->loadThres) {
extend(hashMap);
@@ -2761,7 +2755,7 @@ comments: true
}
/* 删除操作 */
void removeItem(hashMapOpenAddressing *hashMap, int key) {
void removeItem(HashMapOpenAddressing *hashMap, int key) {
// 搜索 key 对应的桶索引
int index = findBucket(hashMap, key);
// 若找到键值对,则用删除标记覆盖它
@@ -2775,7 +2769,7 @@ comments: true
}
/* 扩容哈希表 */
void extend(hashMapOpenAddressing *hashMap) {
void extend(HashMapOpenAddressing *hashMap) {
// 暂存原哈希表
Pair **bucketsTmp = hashMap->buckets;
int oldCapacity = hashMap->capacity;
@@ -2796,7 +2790,7 @@ comments: true
}
/* 打印哈希表 */
void print(hashMapOpenAddressing *hashMap) {
void print(HashMapOpenAddressing *hashMap) {
for (int i = 0; i < hashMap->capacity; i++) {
Pair *pair = hashMap->buckets[i];
if (pair == NULL) {
@@ -2820,7 +2814,7 @@ comments: true
平方探测与线性探测类似,都是开放寻址的常见策略之一。当发生冲突时,平方探测不是简单地跳过一个固定的步数,而是跳过“探测次数的平方”的步数,即 $1, 4, 9, \dots$ 步。
平方探测主要具有以下优势。
平方探测主要具有以下优势。
- 平方探测通过跳过平方的距离,试图缓解线性探测的聚集效应。
- 平方探测会跳过更大的距离来寻找空位置,有助于数据分布得更加均匀。

View File

@@ -1405,39 +1405,35 @@ index = hash(key) % capacity
```c title="array_hash_map.c"
/* 键值对 int->string */
struct pair {
typedef struct {
int key;
char *val;
};
typedef struct pair pair;
} Pair;
/* 基于数组简易实现的哈希表 */
struct arrayHashMap {
pair *buckets[HASH_MAP_DEFAULT_SIZE];
};
typedef struct arrayHashMap arrayHashMap;
typedef struct {
Pair *buckets[HASH_MAP_DEFAULT_SIZE];
} ArrayHashMap;
/* 哈希表初始化函数 */
arrayHashMap *newArrayHashMap() {
arrayHashMap *map = malloc(sizeof(arrayHashMap));
ArrayHashMap *newArrayHashMap() {
ArrayHashMap *map = malloc(sizeof(ArrayHashMap));
return map;
}
/* 添加操作 */
void put(arrayHashMap *d, const int key, const char *val) {
pair *pair = malloc(sizeof(pair));
pair->key = key;
pair->val = malloc(strlen(val) + 1);
strcpy(pair->val, val);
void put(ArrayHashMap *d, const int key, const char *val) {
Pair *Pair = malloc(sizeof(Pair));
Pair->key = key;
Pair->val = malloc(strlen(val) + 1);
strcpy(Pair->val, val);
int index = hashFunc(key);
d->buckets[index] = pair;
d->buckets[index] = Pair;
}
/* 删除操作 */
void removeItem(arrayHashMap *d, const int key) {
void removeItem(ArrayHashMap *d, const int key) {
int index = hashFunc(key);
free(d->buckets[index]->val);
free(d->buckets[index]);
@@ -1445,8 +1441,8 @@ index = hash(key) % capacity
}
/* 获取所有键值对 */
void pairSet(arrayHashMap *d, mapSet *set) {
pair *entries;
void pairSet(ArrayHashMap *d, MapSet *set) {
Pair *entries;
int i = 0, index = 0;
int total = 0;
@@ -1457,7 +1453,7 @@ index = hash(key) % capacity
}
}
entries = malloc(sizeof(pair) * total);
entries = malloc(sizeof(Pair) * total);
for (i = 0; i < HASH_MAP_DEFAULT_SIZE; i++) {
if (d->buckets[i] != NULL) {
entries[index].key = d->buckets[i]->key;
@@ -1472,7 +1468,7 @@ index = hash(key) % capacity
}
/* 获取所有键 */
void keySet(arrayHashMap *d, mapSet *set) {
void keySet(ArrayHashMap *d, MapSet *set) {
int *keys;
int i = 0, index = 0;
int total = 0;
@@ -1497,7 +1493,7 @@ index = hash(key) % capacity
}
/* 获取所有值 */
void valueSet(arrayHashMap *d, mapSet *set) {
void valueSet(ArrayHashMap *d, MapSet *set) {
char **vals;
int i = 0, index = 0;
int total = 0;
@@ -1522,11 +1518,11 @@ index = hash(key) % capacity
}
/* 打印哈希表 */
void print(arrayHashMap *d) {
void print(ArrayHashMap *d) {
int i;
mapSet set;
MapSet set;
pairSet(d, &set);
pair *entries = (pair *)set.set;
Pair *entries = (Pair *)set.set;
for (i = 0; i < set.len; i++) {
printf("%d -> %s\n", entries[i].key, entries[i].val);
}