c语言 ---- HashMap实现
来源:互联网 发布:java url encoder 编辑:程序博客网 时间:2024/05/02 19:20
c语言中,关于类似HashTable的实现;
优点:检索/插入/删除高效率;
注意:可存储任意类型的指针(非值存储)/*************************************************************************
> File Name: shashmap.h> Author: tangzs
> Mail: 21349338@qq.com
> Created Time: 2017年09月18日 星期一 22时58分01秒
************************************************************************/
#ifndef ____SHASHMAP_H____
#define ____SHASHMAP_H____
/*
* 求 int 型哈希值
*/
extern unsigned int hashcode_int(void *key);
/*
* int 型指针比较
*/
extern int equals_int(void *key1, void *key2);
/*
* 求 string 型哈系值
*/
extern unsigned int hashcode_str(void *key);
/*
* string 型比较
*/
extern int equals_str(void *key1, void *key2);
/*
* 哈希表节点结构定义
*/
typedef struct st_entry {
void *key; // key
void *val; // val
struct st_entry *next; // 解决冲突/列表方式
} Entry;
/*
* 哈希表结构定义
*/
typedef struct {
int size; // 当前有效大小
int table_size; // 哈系表数组大小
Entry *table_array; // 哈系表数组
unsigned int (*hashcode)(void *key); // 键值哈希值函数
int (*equals)(void *key1, void *key2); // 键比较函数
} SHashMap;
/*
* 哈希表创建及初始化
*/
extern SHashMap *shashmap_init(unsigned int (*hashcode)(void *key),
int (*equals)(void *key1, void *key2));
/*
* 向哈希表添加键值对
*/
extern void shashmap_put(SHashMap *map, void *key, void *val);
/*
* 根据键获取值
*/
extern void *shashmap_get(SHashMap *map, void *key);
/*
* 销毁哈希表
*/
extern void shashmap_free(SHashMap *map);
#endif
/*************************************************************************
> File Name: shashmap.c
> Author: tangzs
> Mail: 21349338@qq.com
> Created Time: 2017年09月18日 星期一 23时02分45秒
************************************************************************/
#include "shashmap.h"
#include <stdio.h>
#include <stdlib.h>
extern unsigned int hashcode_int(void *key) {
int k = *(int*)key;
return k < 0? 131 - k: 131 + k;
}
extern int equals_int(void *key1, void *key2) {
int k1 = *(int*)key1;
int k2 = *(int*)key2;
return k1 == k2? 1: 0;
}
extern unsigned int hashcode_str(void *key) {
unsigned int hashcode = 0;
char *str = (char*)key;
while (*str) {
hashcode = (*str++) + (hashcode << 6) + (hashcode << 16) - hashcode;
}
hashcode &= 0x7FFFFFFF;
return hashcode;
}
extern int equals_str(void *key1, void *key2) {
char *k1 = (char*)key1;
char *k2 = (char*)key2;
while (1) {
if (*k1 != *k2) {
return 0;
}
if (*k1) {
break;
}
}
return 1;
}
extern SHashMap *shashmap_init(unsigned int (*hashcode)(void *key),
int (*equals)(void *key1, void *key2)) {
SHashMap *map = malloc(sizeof(SHashMap));
if (map == NULL) {
perror("malloc shashmap error");
exit(0);
}
map->hashcode = hashcode;
map->equals = equals;
map->size = 0;
map->table_size = 131;
map->table_array = calloc(map->table_size, sizeof(Entry));
if (map->table_array == NULL) {
perror("calloc shashmap table array error");
exit(0);
}
return map;
}
extern void shashmap_put(SHashMap *map, void *key, void *val) {
int index = (*(map->hashcode))(key) % map->table_size;
Entry *table = map->table_array + index;
Entry *entry = malloc(sizeof(Entry));
if (entry == NULL) {
perror("malloc entry error");
exit(0);
}
entry->key = key;
entry->val = val;
entry->next = table->next;
table->next = entry;
}
extern void *shashmap_get(SHashMap *map, void *key) {
int index = (*(map->hashcode))(key) % map->table_size;
Entry *entry = (map->table_array + index)->next;
while (entry != NULL) {
int v = ((*map->equals)(key, entry->key));
if ((*map->equals)(key, entry->key)) {
return entry->val;
}
entry = entry->next;
}
return NULL;
}
extern void shashmap_free(SHashMap *map) {
for (int i = 0; i < map->table_size; i++) {
Entry *entry = (map->table_array + i)->next;
while (entry != NULL) {
Entry *temp = entry;
entry = entry->next;
free(temp);
}
}
free(map->table_array);
free(map);
}
- hashmap C语言实现
- C语言实现HashMap
- c语言 ---- HashMap实现
- 一个简单的HashMap C语言实现
- 一个简单的HashMap C语言实现
- c语言下的hashmap实现
- C语言hashmap
- c语言实现通用数据结构:通用映射(HashMap)
- C语言实现hashmap,安卓系统AOSP源码
- [C++]HashMap实现STL map
- c语言实现通用数据结构(五):通用映射(HashMap)
- c语言实现通用数据结构(五):通用映射(HashMap)
- 链栈 C语言实现
- 地球 c语言实现
- C语言实现堆栈
- c语言实现计算器
- TrimLeft C语言实现
- C语言实现有限状态机
- POJ
- 权限管理模型
- 海康威视技术面试总结
- LeetCode41
- 关于Java Web中文乱码的问题
- c语言 ---- HashMap实现
- #leetcode编程日记#94. Binary Tree Inorder Traversal
- 一个屌丝程序猿的人生(七十三)
- SSH更改字符编码格式
- Algorithm1——一组数据求出max1和max2
- 集群运行MapReduce任务报错Error: Java heap space
- SCREEN中 必输字段没写的情况下 强制实现按钮功能(退出等)
- neo4j简单学习
- 数据结构-栈的链式表示(指针双星问题**prt)