5 Star 31 Fork 6

方瑾/async

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
hashMap.h 2.44 KB
一键复制 编辑 原始数据 按行查看 历史
方瑾 提交于 2019-04-16 17:39 . 解决内存泄露问题
#ifndef __HASHMAP_H__
#define __HASHMAP_H__
#include"dataStruct.h"
#include"array.h"
typedef struct entry {
let key; // 键
let value; // 值
struct entry * next; // 冲突链表
}*Entry;
#define newEntry() NEW(struct entry)
#define newEntryList(length) (Entry)malloc(length * sizeof(struct entry))
// 哈希结构
typedef struct hashMap *HashMap;
#define newHashMap() NEW(struct hashMap)
// 哈希函数类型
typedef int(*HashCode)(HashMap, let key);
// 判等函数类型
typedef Boolean(*Equal)(let key1, let key2);
// 添加键函数类型
typedef void(*Put)(HashMap hashMap, let key, let value);
// 获取键对应值的函数类型
typedef let(*Get)(HashMap hashMap, let key);
// 删除键的函数类型
typedef let(*Remove)(HashMap hashMap, let key);
// 清空Map的函数类型
typedef void(*Clear)(HashMap hashMap);
// 判断键值是否存在的函数类型
typedef Boolean(*Exists)(HashMap hashMap, let key);
typedef struct hashMap {
int size; // 当前大小
int listSize; // 有效空间大小
HashCode hashCode; // 哈希函数
Equal equal; // 判等函数
Entry list; // 存储区域
Put put; // 添加键的函数
Get get; // 获取键对应值的函数
Remove remove; // 删除键
Clear clear; // 清空Map
Exists exists; // 判断键是否存在
Boolean autoAssign; // 设定是否根据当前数据量动态调整内存大小,默认开启
}*HashMap;
// 迭代器结构
typedef struct hashMapIterator {
Entry entry; // 迭代器当前指向
int count; // 迭代次数
int hashCode; // 键值对的哈希值
HashMap hashMap;
}*HashMapIterator;
#define newHashMapIterator() NEW(struct hashMapIterator)
// 默认哈希函数
static int defaultHashCode(HashMap hashMap, let key);
// 默认判断键值是否相等
static Boolean defaultEqual(let key1, let key2);
// 默认添加键值对
static void defaultPut(HashMap hashMap, let key, let value);
// 默认获取键对应值
static let defaultGet(HashMap hashMap, let key);
// 默认删除键
static let defaultRemove(HashMap hashMap, let key);
// 默认判断键是否存在
static Boolean defaultExists(HashMap hashMap, let key);
// 默认清空Map
static void defaultClear(HashMap hashMap);
// 重新构建
static void resetHashMap(HashMap hashMap, int listSize);
// 创建一个哈希结构
HashMap createHashMap(HashCode hashCode, Equal equal);
// 创建哈希结构迭代器
HashMapIterator createHashMapIterator(HashMap hashMap);
// 迭代器是否有下一个
Boolean hasNextHashMapIterator(HashMapIterator iterator);
// 迭代到下一次
HashMapIterator nextHashMapIterator(HashMapIterator iterator);
// 释放迭代器内存
void freeHashMapIterator(HashMapIterator * iterator);
#endif // !__HASHMAP_H__
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/lyxfj/async.git
git@gitee.com:lyxfj/async.git
lyxfj
async
async
master

搜索帮助