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 ...
随机推荐
- order by 排序
[order by] 排序 asc 升序(从小到大),desc降序(从打到小) 语法: select 列名 from 表 where 条件 order by 列1,列2 asc或d ...
- navicat 链接oracle时出现的各种问题
1.出现12514错误: 方法:在oracle的安装路径下找到tnsnames.ora文件(我的安装路径为E:\app\sa\product\12.2.0\dbhome_1\network\admin ...
- cornerstone提示“SQLite-database disk image is malformed”
当点击workingCopy时错误如下 google了一下,有是有解决的办法,可是这些都是直接使用sqlite时产生的问题. sqlite错误 The database disk image is m ...
- CommonJs模块规范
1.什么是模块化 文件作用域 通信规则 加载 require 导出 exports 2.CommonJs模块规范 在Node中的Javascript还有一个很重要的概念:模块概念 模块作用域 使用re ...
- Percona-Tookit工具包之pt-ioprofile
Preface As a matter of fact,disk IO is the most important factor which tremendously influenc ...
- 剑指offer—从头到尾打印链表
输入一个链表,按链表值从尾到头的顺序返回一个ArrayList. 递归添加...不为空就加 import java.util.ArrayList; public class Solution { pu ...
- YII2 多MongoDB配置和使用
1:在config/web.php 文件下配置多个连接即可: 注意在componets 下 'mongodb' => [ 'class' => '\yii\mongodb\Connecti ...
- Django的aggregate()和annotate()函数的区别
aggregate() aggregate()为所有的QuerySet生成一个汇总值,相当于Count().返回结果类型为Dict. annotate() annotate()为每一个QuerySet ...
- Horner规则求多项式
/* Horner */ /*多项式:A(x)=a[n]X^n+a[n-1]x^n-1+...+a[1]X^1+a[0]X^0*/ #include <stdio.h> long int ...
- uva 540 - Team Queue(插队队列)
首发:https://mp.csdn.net/mdeditor/80294426 例题5-6 团体队列(Team Queue,UVa540) 有t个团队的人正在排一个长队.每次新来一个人时,如果他有队 ...