c语言实现带LRU机制的哈希表
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h> #define HASH_BUCKET_MAX (1024)
#define HASH_BUCKET_CAPACITY_MAX (256)
#define HASHTABLE_DEBUG
#define TRUE 1
#define FALSE 0 #ifdef HASHTABLE_DEBUG
#define DEBUG(format, ...) printf("[%s] [%d] : "format"\n", __FUNCTION__, __LINE__, __VA_ARGS__)
#else
#define DEBUG(format, ...)
#endif struct hash_bucket {
int capacity; /* 桶的容量 */
void *hkey; /* hashtable的key */
void *hdata; /* hashtable的data */
struct hash_bucket *prev;
struct hash_bucket *next;
struct hash_bucket *tail;
}; struct hash {
uint32_t (*hash_key)(void *);
int (*hash_cmp)(const void *, const void*, int len);
struct hash_bucket* bucket[HASH_BUCKET_MAX];
}; uint32_t string_hash_key(void* str);
int string_hash_cmp(const void* src, const void* dst); static struct hash *g_htable = NULL; void hash_create();
void *hash_lookup(void *key);
int hash_add(void *key, void* data);
int hash_delete(void *key);
void hash_destroy();
void hash_iter_print(); #define bucket_free(bucket) \
free(bucket->hkey); \
free(bucket->hdata); \
free(bucket); \
bucket = NULL; uint32_t string_hash_key(void* str) {
char *tmp = (char *)str;
uint32_t key = ;
while(*tmp)
key = (key * ) ^ (uint32_t)(tmp++); DEBUG("string key : %u, index : %d", key, key%HASH_BUCKET_MAX); return key%HASH_BUCKET_MAX;
} int string_hash_cmp(const void* src, const void* dst, int len) {
if (!src || !dst) {
DEBUG("src addr: %p, dst addr: %p", src, dst);
return -;
}
return strncmp((char *)src, (char *)dst, len);
} void hash_create() {
if (g_htable) {
DEBUG("the default hashtable is already created");
return;
} g_htable = (struct hash *)malloc(sizeof(struct hash));
if (!g_htable) {
DEBUG("memory alloc failed.");
return;
} memset(g_htable, , sizeof(struct hash)); g_htable->hash_key = string_hash_key;
g_htable->hash_cmp = string_hash_cmp; return;
} static void bucket_delete(struct hash_bucket** ptr) {
struct hash_bucket *bucket = *ptr;
struct hash_bucket *tmp; while(bucket) {
tmp = bucket;
bucket = bucket->next;
bucket_free(tmp);
}
} void hash_destroy() {
if (g_htable) {
for(int i=; i<HASH_BUCKET_MAX; i++) {
if (g_htable->bucket[i]) {
bucket_delete(&g_htable->bucket[i]);
}
} free(g_htable);
g_htable = NULL;
}
return;
} #define lru_bucket_move(bucket, head) \
bucket->next = head; \
bucket->prev = NULL; \
bucket->capacity = head->capacity; \
\
head->prev = bucket; \
head->tail = NULL; void *hash_lookup(void *key) {
if (!key) {
DEBUG("input para is NULL\n");
return NULL;
} uint32_t index = g_htable->hash_key(key);
struct hash_bucket* head = g_htable->bucket[index];
struct hash_bucket* bucket = head; while(bucket) {
if ( == g_htable->hash_cmp(key, bucket->hkey, strlen((char*)key))) {
if (head != bucket && bucket != head->tail) {
bucket->prev->next = bucket->next;
bucket->next->prev = bucket->prev;
bucket->tail = head->tail; lru_bucket_move(bucket, head);
} else if (bucket == head->tail && head->capacity>) {
bucket->prev->next = NULL;
bucket->tail = bucket->prev; lru_bucket_move(bucket, head);
}
g_htable->bucket[index] = bucket;
return bucket->hdata;
}
bucket = bucket->next;
}
return NULL;
} int hash_add(void *key, void* data) {
if (!key || !data) {
DEBUG("input para is NULL\n");
return FALSE;
} uint32_t index = g_htable->hash_key(key);
struct hash_bucket* head = g_htable->bucket[index]; if (!head) {
head = (struct hash_bucket*)malloc(sizeof(struct hash_bucket));
if (!head) {
DEBUG("no memory for more hash_bucket\n");
return FALSE;
} memset(head, , sizeof(*head));
head->capacity++; head->hkey = strdup((char *)key);
head->hdata = strdup((char *)data);
head->tail = head;
g_htable->bucket[index] = head;
return TRUE;
} int capacity = head->capacity;
struct hash_bucket *new_bucket =
(struct hash_bucket *)malloc(sizeof(struct hash_bucket)); if (!new_bucket) {
DEBUG("no memory for more hash_bucket\n");
return FALSE;
} if (capacity >= HASH_BUCKET_CAPACITY_MAX) {
struct hash_bucket *tail = head->tail;
head->tail = tail->prev; tail->prev->next = NULL;
bucket_free(tail);
} head->prev = new_bucket;
new_bucket->next = head;
new_bucket->capacity = capacity + ;
new_bucket->tail = head->tail;
head->tail = NULL; head->hkey = strdup((char *)key);
head->hdata = strdup((char *)data); g_htable->bucket[index] = new_bucket; return TRUE;
} int hash_delete(void *key) {
if (!key) {
DEBUG("input para is NULL\n");
return FALSE;
} uint32_t index = g_htable->hash_key(key);
struct hash_bucket* head = g_htable->bucket[index];
struct hash_bucket* bkt = head; while(bkt) {
if ( == g_htable->hash_cmp(key, bkt->hkey, strlen((char*)key))) {
if (head != bkt && bkt != head->tail) {
bkt->prev->next = bkt->next;
bkt->next->prev = bkt->prev; } else if (bkt == head->tail && head->capacity>) {
bkt->prev->next = NULL;
bkt->tail = bkt->prev; } else {
if (bkt->next) {
bkt->next->tail = bkt->tail;
bkt->next->capacity = bkt->capacity;
bkt->next->prev = NULL;
g_htable->bucket[index] = bkt->next;
} else {
g_htable->bucket[index] = NULL;
}
} bucket_free(bkt);
if (g_htable->bucket[index]) {
g_htable->bucket[index]->capacity--;
} return TRUE;
}
bkt = bkt->next;
}
return FALSE;
} static void bucket_print(struct hash_bucket** ptr) {
struct hash_bucket *bkt = *ptr;
struct hash_bucket *tmp; while(bkt) {
printf("key=[%s],data=[%s]\n", (char*)bkt->hkey, (char*)bkt->hdata);
bkt = bkt->next;
}
} void hash_iter_print() {
if (g_htable) {
for(int i=; i<HASH_BUCKET_MAX; i++) {
if (g_htable->bucket[i]) {
bucket_print(&g_htable->bucket[i]);
}
}
}
} int main(int argc, char* argv[]) {
hash_create(); hash_add("first", "danxi");
hash_add("second", "test");
hash_add("three", "sad code");
hash_add("four", "let's go"); hash_iter_print(); char * t1 = (char *)hash_lookup("first");
char * t2 = (char *)hash_lookup("second"); printf("%s %s \n", t1, t2);
printf("%s \n", (char*)hash_lookup("four")); hash_delete("four");
hash_iter_print();
hash_destroy(); return ;
}
c语言实现带LRU机制的哈希表的更多相关文章
- C语言自带的快速排序(qsort)函数使用方法
感觉打快排太慢了,找到了c语言自带的函数.这函数用起来没c++的方便,不过也够了. 函数名称:qsort,在头文件:<stdlib.h>中 不多说,上代码: #include <st ...
- C 语言多线程与锁机制
C 语言多线程与锁机制 多线程 #include <pthread.h> void *TrainModelThread(void *id) { ... pthread_exit(NULL) ...
- django自带权限机制
1. Django权限机制概述 权限机制能够约束用户行为,控制页面的显示内容,也能使API更加安全和灵活:用好权限机制,能让系统更加强大和健壮.因此,基于Django的开发,理清Django权限机制是 ...
- Redis的LRU机制(转)
原文:Redis的LRU机制 在Redis中,如果设置的maxmemory,那就要配置key的回收机制参数maxmemory-policy,默认volatile-lru,参阅Redis作者的原博客:a ...
- 【C++】异常简述(一):C语言中的异常处理机制
人的一生会遇到很多大起大落,尤其是程序员. 程序员写好的程序,论其消亡形式无非三种:无疾而终.自杀.他杀. 当然作为一名程序员,最乐意看到自己写的程序能够无疾而终,因此尽快的学习异常处理机制是非常重要 ...
- Java程序语言的后门-反射机制
在文章JAVA设计模式-动态代理(Proxy)示例及说明和JAVA设计模式-动态代理(Proxy)源码分析都提到了反射这个概念. // 通过反射机制,通知力宏做事情 method.invoke(obj ...
- 146. LRU 缓存机制 + 哈希表 + 自定义双向链表
146. LRU 缓存机制 LeetCode-146 题目描述 题解分析 java代码 package com.walegarrett.interview; /** * @Author WaleGar ...
- 浅谈MatrixOne如何用Go语言设计与实现高性能哈希表
目录 MatrixOne数据库是什么? 哈希表数据结构基础 哈希表基本设计与对性能的影响 碰撞处理 链地址法 开放寻址法 Max load factor Growth factor 空闲桶探测方法 一 ...
- 数据结构算法C语言实现(二)---2.3线性表的链式表示和实现之单链表
一.简述 [暂无] 二.头文件 #ifndef _2_3_part1_H_ #define _2_3_part1_H_ //2_3_part1.h /** author:zhaoyu email:zh ...
随机推荐
- 模块socket使用
什么是socket:socket是应用层与TCP/IP协议族通信的中间软件抽象层,它是一组接口.我们无需再去深入理解tcp/udp协议,按照socket的规定去使用就行了. 首先一个c/s架构:分为两 ...
- 【TOJ 3369】CD(二分)
描述 Jack and Jill have decided to sell some of their Compact Discs, while they still have some value. ...
- ABAP术语-Business Process
Business Process 原文:http://www.cnblogs.com/qiangsheng/archive/2008/01/11/1035316.html A prepared sce ...
- mysql数据库关于事务的问题?求解答
表格代码: CREATE TABLE `t_teacher` ( `id` ) NOT NULL AUTO_INCREMENT, `name` ) NOT NULL, `deposit` ) DEFA ...
- mysql like 变量
Mysql: select * from 表名 where 字段 like concat('%',变量,'%');
- Docker 入坑教程笔记
Docker 入坑教程笔记 视频网址B站:点这里 查询命令 man docker 简单启动和退出 docker run --name [容器名] -i -t ubuntu /bin/bash 交互启动 ...
- This system is registered to Red Hat Subscription Management, but is not receiving updates. You can use subscription-manager to assign subscriptions.
Wrong date and time, reset the date and time in the system properly. It may also happen that system ...
- Python学习 :深浅拷贝
深浅拷贝 一.浅拷贝 只拷贝第一层数据(不可变的数据类型),并创建新的内存空间进行储蓄,例如:字符串.整型.布尔 除了字符串以及整型,复杂的数据类型都使用一个共享的内存空间,例如:列表 列表使用的是同 ...
- Educational Codeforces Round 47 (Rated for Div. 2) :B. Minimum Ternary String
题目链接:http://codeforces.com/contest/1009/problem/B 解题心得: 题意就是给你一个只包含012三个字符的字符串,位置并且逻辑相邻的字符可以相互交换位置,就 ...
- Java——static关键字---18.09.27
static表示“全局”或者“静态”的意思,用来修饰成员变量和成员方法,也可以形成静态static代码块,但在Java语言中没有全局变量的概念. static关键字主要有两种作用: 一.为某特定数据类 ...