2023-04-23 23:44:49 +08:00
|
|
|
/**
|
|
|
|
* File: array_hash_map.c
|
|
|
|
* Created Time: 2023-03-18
|
|
|
|
* Author: Guanngxu (446678850@qq.com)
|
|
|
|
*/
|
|
|
|
|
2023-04-24 04:11:18 +08:00
|
|
|
#include "../utils/common.h"
|
2023-04-23 23:44:49 +08:00
|
|
|
|
2023-10-18 02:16:26 +08:00
|
|
|
/* 哈希表默认大小 */
|
2023-09-02 23:54:38 +08:00
|
|
|
#define HASH_MAP_DEFAULT_SIZE 100
|
2023-04-23 23:44:49 +08:00
|
|
|
|
|
|
|
/* 键值对 int->string */
|
2023-10-18 02:16:26 +08:00
|
|
|
typedef struct {
|
2023-04-23 23:44:49 +08:00
|
|
|
int key;
|
|
|
|
char *val;
|
2023-10-18 02:16:26 +08:00
|
|
|
} Pair;
|
2023-04-23 23:44:49 +08:00
|
|
|
|
2023-10-18 02:16:26 +08:00
|
|
|
/* 键值对的集合 */
|
|
|
|
typedef struct {
|
2023-04-23 23:44:49 +08:00
|
|
|
void *set;
|
|
|
|
int len;
|
2023-10-18 02:16:26 +08:00
|
|
|
} MapSet;
|
2023-04-23 23:44:49 +08:00
|
|
|
|
|
|
|
/* 基于数组简易实现的哈希表 */
|
2023-10-18 02:16:26 +08:00
|
|
|
typedef struct {
|
|
|
|
Pair *buckets[HASH_MAP_DEFAULT_SIZE];
|
|
|
|
} ArrayHashMap;
|
2023-04-23 23:44:49 +08:00
|
|
|
|
|
|
|
/* 哈希表初始化函数 */
|
2023-10-18 02:16:26 +08:00
|
|
|
ArrayHashMap *newArrayHashMap() {
|
|
|
|
ArrayHashMap *map = malloc(sizeof(ArrayHashMap));
|
2023-09-26 10:57:53 +08:00
|
|
|
return map;
|
2023-04-23 23:44:49 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* 哈希函数 */
|
|
|
|
int hashFunc(int key) {
|
|
|
|
int index = key % HASH_MAP_DEFAULT_SIZE;
|
|
|
|
return index;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 查询操作 */
|
2023-10-18 02:16:26 +08:00
|
|
|
const char *get(const ArrayHashMap *d, const int key) {
|
2023-04-23 23:44:49 +08:00
|
|
|
int index = hashFunc(key);
|
2023-10-18 02:16:26 +08:00
|
|
|
const Pair *Pair = d->buckets[index];
|
|
|
|
if (Pair == NULL)
|
2023-04-23 23:44:49 +08:00
|
|
|
return NULL;
|
2023-10-18 02:16:26 +08:00
|
|
|
return Pair->val;
|
2023-04-23 23:44:49 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* 添加操作 */
|
2023-10-18 02:16:26 +08:00
|
|
|
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);
|
2023-04-23 23:44:49 +08:00
|
|
|
|
|
|
|
int index = hashFunc(key);
|
2023-10-18 02:16:26 +08:00
|
|
|
d->buckets[index] = Pair;
|
2023-04-23 23:44:49 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* 删除操作 */
|
2023-10-18 02:16:26 +08:00
|
|
|
void removeItem(ArrayHashMap *d, const int key) {
|
2023-04-23 23:44:49 +08:00
|
|
|
int index = hashFunc(key);
|
|
|
|
free(d->buckets[index]->val);
|
|
|
|
free(d->buckets[index]);
|
|
|
|
d->buckets[index] = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 获取所有键值对 */
|
2023-10-18 02:16:26 +08:00
|
|
|
void pairSet(ArrayHashMap *d, MapSet *set) {
|
|
|
|
Pair *entries;
|
2023-04-23 23:44:49 +08:00
|
|
|
int i = 0, index = 0;
|
|
|
|
int total = 0;
|
|
|
|
|
2023-06-14 02:01:06 +08:00
|
|
|
/* 统计有效键值对数量 */
|
2023-04-23 23:44:49 +08:00
|
|
|
for (i = 0; i < HASH_MAP_DEFAULT_SIZE; i++) {
|
|
|
|
if (d->buckets[i] != NULL) {
|
|
|
|
total++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-18 02:16:26 +08:00
|
|
|
entries = malloc(sizeof(Pair) * total);
|
2023-04-23 23:44:49 +08:00
|
|
|
for (i = 0; i < HASH_MAP_DEFAULT_SIZE; i++) {
|
|
|
|
if (d->buckets[i] != NULL) {
|
|
|
|
entries[index].key = d->buckets[i]->key;
|
|
|
|
entries[index].val = malloc(strlen(d->buckets[i]->val + 1));
|
|
|
|
strcpy(entries[index].val, d->buckets[i]->val);
|
|
|
|
index++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
set->set = entries;
|
|
|
|
set->len = total;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 获取所有键 */
|
2023-10-18 02:16:26 +08:00
|
|
|
void keySet(ArrayHashMap *d, MapSet *set) {
|
2023-04-23 23:44:49 +08:00
|
|
|
int *keys;
|
|
|
|
int i = 0, index = 0;
|
|
|
|
int total = 0;
|
|
|
|
|
2023-06-14 02:01:06 +08:00
|
|
|
/* 统计有效键值对数量 */
|
2023-04-23 23:44:49 +08:00
|
|
|
for (i = 0; i < HASH_MAP_DEFAULT_SIZE; i++) {
|
|
|
|
if (d->buckets[i] != NULL) {
|
|
|
|
total++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
keys = malloc(total * sizeof(int));
|
|
|
|
for (i = 0; i < HASH_MAP_DEFAULT_SIZE; i++) {
|
|
|
|
if (d->buckets[i] != NULL) {
|
|
|
|
keys[index] = d->buckets[i]->key;
|
|
|
|
index++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
set->set = keys;
|
|
|
|
set->len = total;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 获取所有值 */
|
2023-10-18 02:16:26 +08:00
|
|
|
void valueSet(ArrayHashMap *d, MapSet *set) {
|
2023-04-23 23:44:49 +08:00
|
|
|
char **vals;
|
|
|
|
int i = 0, index = 0;
|
|
|
|
int total = 0;
|
|
|
|
|
2023-06-14 02:01:06 +08:00
|
|
|
/* 统计有效键值对数量 */
|
2023-04-23 23:44:49 +08:00
|
|
|
for (i = 0; i < HASH_MAP_DEFAULT_SIZE; i++) {
|
|
|
|
if (d->buckets[i] != NULL) {
|
|
|
|
total++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
vals = malloc(total * sizeof(char *));
|
|
|
|
for (i = 0; i < HASH_MAP_DEFAULT_SIZE; i++) {
|
|
|
|
if (d->buckets[i] != NULL) {
|
|
|
|
vals[index] = d->buckets[i]->val;
|
|
|
|
index++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
set->set = vals;
|
|
|
|
set->len = total;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 打印哈希表 */
|
2023-10-18 02:16:26 +08:00
|
|
|
void print(ArrayHashMap *d) {
|
2023-04-23 23:44:49 +08:00
|
|
|
int i;
|
2023-10-18 02:16:26 +08:00
|
|
|
MapSet set;
|
2023-06-14 02:01:06 +08:00
|
|
|
pairSet(d, &set);
|
2023-10-18 02:16:26 +08:00
|
|
|
Pair *entries = (Pair *)set.set;
|
2023-04-23 23:44:49 +08:00
|
|
|
for (i = 0; i < set.len; i++) {
|
|
|
|
printf("%d -> %s\n", entries[i].key, entries[i].val);
|
|
|
|
}
|
|
|
|
free(set.set);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Driver Code */
|
|
|
|
int main() {
|
|
|
|
/* 初始化哈希表 */
|
2023-10-18 02:16:26 +08:00
|
|
|
ArrayHashMap *map = newArrayHashMap();
|
2023-04-23 23:44:49 +08:00
|
|
|
|
|
|
|
/* 添加操作 */
|
|
|
|
// 在哈希表中添加键值对 (key, value)
|
|
|
|
put(map, 12836, "小哈");
|
|
|
|
put(map, 15937, "小啰");
|
|
|
|
put(map, 16750, "小算");
|
|
|
|
put(map, 13276, "小法");
|
|
|
|
put(map, 10583, "小鸭");
|
|
|
|
printf("\n添加完成后,哈希表为\nKey -> Value\n");
|
|
|
|
print(map);
|
|
|
|
|
|
|
|
/* 查询操作 */
|
|
|
|
// 向哈希表输入键 key ,得到值 value
|
2023-09-02 23:54:38 +08:00
|
|
|
const char *name = get(map, 15937);
|
2023-04-23 23:44:49 +08:00
|
|
|
printf("\n输入学号 15937 ,查询到姓名 %s\n", name);
|
|
|
|
|
|
|
|
/* 删除操作 */
|
|
|
|
// 在哈希表中删除键值对 (key, value)
|
|
|
|
removeItem(map, 10583);
|
|
|
|
printf("\n删除 10583 后,哈希表为\nKey -> Value\n");
|
|
|
|
print(map);
|
|
|
|
|
|
|
|
/* 遍历哈希表 */
|
|
|
|
int i;
|
|
|
|
|
|
|
|
printf("\n遍历键值对 Key->Value\n");
|
|
|
|
print(map);
|
|
|
|
|
2023-10-18 02:16:26 +08:00
|
|
|
MapSet set;
|
2023-04-23 23:44:49 +08:00
|
|
|
|
|
|
|
keySet(map, &set);
|
2023-09-02 23:54:38 +08:00
|
|
|
int *keys = (int *)set.set;
|
2023-04-23 23:44:49 +08:00
|
|
|
printf("\n单独遍历键 Key\n");
|
|
|
|
for (i = 0; i < set.len; i++) {
|
|
|
|
printf("%d\n", keys[i]);
|
|
|
|
}
|
|
|
|
free(set.set);
|
|
|
|
|
|
|
|
valueSet(map, &set);
|
2023-09-02 23:54:38 +08:00
|
|
|
char **vals = (char **)set.set;
|
2023-04-23 23:44:49 +08:00
|
|
|
printf("\n单独遍历键 Value\n");
|
|
|
|
for (i = 0; i < set.len; i++) {
|
|
|
|
printf("%s\n", vals[i]);
|
|
|
|
}
|
|
|
|
free(set.set);
|
2023-09-02 23:54:38 +08:00
|
|
|
|
2023-04-23 23:44:49 +08:00
|
|
|
return 0;
|
|
|
|
}
|