Update test_hash.c

This commit is contained in:
kyt_2002 2023-10-05 19:01:15 +08:00
parent a6505f3731
commit b20768ff97
1 changed files with 320 additions and 322 deletions

View File

@ -1,15 +1,7 @@
/**
* @file: test_hash.c
* @brief: a application of test hash function
* @version: 3.0
* @author: Yao wenying
* @date: 2023/05/26
*/
#include <transform.h> #include <transform.h>
#include"test_hash.h" #include "test_hash.h"
// 默认的哈希函数,将字符串键转换为哈希码
int defaultHashCode(HashMap hashMap, let key) { int defaultHashCode(HashMap hashMap, let key) {
char * k = (char *)key; char * k = (char *)key;
unsigned long h = 0; unsigned long h = 0;
@ -24,12 +16,13 @@ int defaultHashCode(HashMap hashMap, let key) {
return h % hashMap->listSize; return h % hashMap->listSize;
} }
// 默认的键相等判断函数,比较两个字符串是否相等
Boolean defaultEqual(let key1, let key2) { Boolean defaultEqual(let key1, let key2) {
return strcmp((string)key1, (string)key2) ? False : True; return strcmp((string)key1, (string)key2) ? False : True;
} }
// 重置哈希表的大小
void resetHashMap(HashMap hashMap, int listSize) { void resetHashMap(HashMap hashMap, int listSize) {
if (listSize < 8) return; if (listSize < 8) return;
// 键值对临时存储空间 // 键值对临时存储空间
@ -83,6 +76,7 @@ void resetHashMap(HashMap hashMap, int listSize) {
free(tempList); free(tempList);
} }
// 默认的键值对插入函数
void defaultPut(HashMap hashMap, let key, let value) { void defaultPut(HashMap hashMap, let key, let value) {
// 获取哈希值 // 获取哈希值
int index = hashMap->hashCode(hashMap, key); int index = hashMap->hashCode(hashMap, key);
@ -94,7 +88,6 @@ void defaultPut(HashMap hashMap, let key, let value) {
hashMap->list[index].value = value; hashMap->list[index].value = value;
} }
else { else {
Entry current = &hashMap->list[index]; Entry current = &hashMap->list[index];
while (current != NULL) { while (current != NULL) {
if (hashMap->equal(key, current->key)) { if (hashMap->equal(key, current->key)) {
@ -115,17 +108,12 @@ void defaultPut(HashMap hashMap, let key, let value) {
} }
if (hashMap->autoAssign && hashMap->size >= hashMap->listSize) { if (hashMap->autoAssign && hashMap->size >= hashMap->listSize) {
// 内存扩充至原来的两倍 // 内存扩充至原来的两倍
// *注: 扩充时考虑的是当前存储元素数量与存储空间的大小关系,而不是存储空间是否已经存满,
// 例如: 存储空间为10存入了10个键值对但是全部冲突了所以存储空间空着9个其余的全部挂在一个上面
// 这样检索的时候和遍历查询没有什么区别了可以简单这样理解当我存入第11个键值对的时候一定会发生冲突
// 这是由哈希函数本身的特性(取模)决定的,冲突就会导致检索变慢,所以这时候扩充存储空间,对原有键值对进行
// 再次散列,会把冲突的数据再次分散开,加快索引定位速度。
resetHashMap(hashMap, hashMap->listSize * 2); resetHashMap(hashMap, hashMap->listSize * 2);
} }
} }
// 默认的获取值函数
let defaultGet(HashMap hashMap, let key) { let defaultGet(HashMap hashMap, let key) {
if (hashMap->exists(hashMap, key)) { if (hashMap->exists(hashMap, key)) {
int index = hashMap->hashCode(hashMap, key); int index = hashMap->hashCode(hashMap, key);
@ -140,6 +128,7 @@ let defaultGet(HashMap hashMap, let key) {
return NULL; return NULL;
} }
// 默认的删除键值对函数
let defaultRemove(HashMap hashMap, let key) { let defaultRemove(HashMap hashMap, let key) {
int index = hashMap->hashCode(hashMap, key); int index = hashMap->hashCode(hashMap, key);
Entry entry = &hashMap->list[index]; Entry entry = &hashMap->list[index];
@ -186,6 +175,7 @@ let defaultRemove(HashMap hashMap, let key) {
return entryKey; return entryKey;
} }
// 默认的判断键是否存在函数
Boolean defaultExists(HashMap hashMap, let key) { Boolean defaultExists(HashMap hashMap, let key) {
int index = hashMap->hashCode(hashMap, key); int index = hashMap->hashCode(hashMap, key);
Entry entry = &hashMap->list[index]; Entry entry = &hashMap->list[index];
@ -203,6 +193,7 @@ Boolean defaultExists(HashMap hashMap, let key) {
} }
} }
// 默认的清空哈希表函数
void defaultClear(HashMap hashMap) { void defaultClear(HashMap hashMap) {
for (int i = 0; i < hashMap->listSize; i++) { for (int i = 0; i < hashMap->listSize; i++) {
// 释放冲突值内存 // 释放冲突值内存
@ -221,6 +212,7 @@ void defaultClear(HashMap hashMap) {
hashMap->listSize = 0; hashMap->listSize = 0;
} }
// 创建一个哈希表
HashMap createHashMap(HashCode hashCode, Equal equal) { HashMap createHashMap(HashCode hashCode, Equal equal) {
HashMap hashMap = newHashMap(); HashMap hashMap = newHashMap();
if (hashMap == NULL) { if (hashMap == NULL) {
@ -249,6 +241,7 @@ HashMap createHashMap(HashCode hashCode, Equal equal) {
return hashMap; return hashMap;
} }
// 创建一个哈希表迭代器
HashMapIterator createHashMapIterator(HashMap hashMap) { HashMapIterator createHashMapIterator(HashMap hashMap) {
HashMapIterator iterator = newHashMapIterator(); HashMapIterator iterator = newHashMapIterator();
if (iterator == NULL) { if (iterator == NULL) {
@ -261,10 +254,12 @@ HashMapIterator createHashMapIterator(HashMap hashMap) {
return iterator; return iterator;
} }
// 判断迭代器是否还有下一个元素
Boolean hasNextHashMapIterator(HashMapIterator iterator) { Boolean hasNextHashMapIterator(HashMapIterator iterator) {
return iterator->count < iterator->hashMap->size ? True : False; return iterator->count < iterator->hashMap->size ? True : False;
} }
// 获取迭代器的下一个元素
HashMapIterator nextHashMapIterator(HashMapIterator iterator) { HashMapIterator nextHashMapIterator(HashMapIterator iterator) {
if (hasNextHashMapIterator(iterator)) { if (hasNextHashMapIterator(iterator)) {
if (iterator->entry != NULL && iterator->entry->next != NULL) { if (iterator->entry != NULL && iterator->entry->next != NULL) {
@ -284,11 +279,14 @@ HashMapIterator nextHashMapIterator(HashMapIterator iterator) {
return iterator; return iterator;
} }
// 释放哈希表迭代器
void freeHashMapIterator(HashMapIterator * iterator) { void freeHashMapIterator(HashMapIterator * iterator) {
free(*iterator); free(*iterator);
*iterator = NULL; *iterator = NULL;
} }
// 定义简化的宏,用于方便操作哈希表
#define Put(map, key, value) map->put(map, (void *)key, (void *)value); #define Put(map, key, value) map->put(map, (void *)key, (void *)value);
#define Get(map, key) (char *)map->get(map, (void *)key) #define Get(map, key) (char *)map->get(map, (void *)key)
#define Remove(map, key) map->remove(map, (void *)key) #define Remove(map, key) map->remove(map, (void *)key)