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 ...
随机推荐
- Keepalived 配置高可用
VRRP协议及Keepalived原理使用 VRRP 协议即 Virtual Router Redundancy Protocol,虚拟路由器冗余协议, 为了解决局域网内默认网关单点失效的问题. ...
- MySQL备份恢复之mysqldump
Preface The day before yesterday,there's a motif about the lock procedure when backing up My ...
- mysql 排名
一.sql1{不管数据相同与否,排名依次排序(1,2,3,4,5,6,7.....)} SELECT obj. AS rownum FROM ( SELECT user_id, score FROM ...
- Ajax知识总结
一 AJAX = Asynchronous JavaScript and XML(异步的 JavaScript 和 XML). AJAX 不是新的编程语言,而是一种使用现有标准的新方法.AJAX 最大 ...
- oracle监听配置
在listener.ora文件中指定监听的实例名和修改ip地址: 查看实例名:[localhost$] echo $ORACLE_SID LISTENER = (DESCRIPTION_LIST = ...
- python学习之函数基础
第一个python函数: >>> def func_1(): ... print 'hello python' ... >>> func_1() hello pyt ...
- python2.7练习小例子(二十七)
27):题目:一个5位数,判断它是不是回文数.即12321是回文数,个位与万位相同,十位与千位相同. #!/usr/bin/python # -*- coding: UTF-8 -* ...
- 再谈js传值和传址
js的传值和传址还是真绕,前回文说道 1.值类型是传值的 2.对象和数组是传址的 这两点通过例子的到了证实 然而还有一种情况没有讨论 即 函数的参数的传值和传址 通过实验,在函数中用一个新对象去覆盖传 ...
- 初步学习pg_control文件之六
接前文:初步学习pg_control文件之五 ,DB_IN_ARCHIVE_RECOVERY何时出现? 看代码:如果recovery.conf文件存在,则返回 InArchiveRecovery = ...
- 从C到C++ (3)
从C到C++ (3) 一. C++中增加了引用 1.引用是给某一个变量起别名.引用的一般格式: 类型 &引用名 = 变量名 定义引用时一定要初始化.在实际应用中,引用一般用作参数传递与返 ...