C.Interface.And.Implementations—table(key-value系统)的实现

2024-08-24 18:18

本文主要是介绍C.Interface.And.Implementations—table(key-value系统)的实现,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

1、An  associative table is a set of key-value pairs. It’s like an array except that the indices can be values of any type.


C语言中宏的作用域是文件或者遇到undef为止。


table的实现是以哈希表和链表实现。内存形式如下:



===========================table.h===============================

#ifndef TABLE_INCLUDED
#define TABLE_INCLUDED#define T Table_T
typedef struct T *T;//exported functions
extern T    Table_new(int hint, int cmp(const void *x, const void *y),unsigned hash(const void *key));extern void   Table_free(T *table);extern int    Table_length(T table);
extern void  *Table_put   (T table, const void *key,void *value);
extern void  *Table_get   (T table, const void *key);
extern void  *Table_remove(T table, const void *key);
extern void   Table_map   (T table, void apply(const void *key, void **value, void *cl),void *cl);
extern void **Table_toArray(T table, void *end);#undef T
#endif

=========================table.c===============================

#include <limits.h>
#include <stddef.h>
#include "mem.h"
#include "assert.h"
#include "table.h"#define T Table_T//types
struct T{//fieldsint size;int (*cmp)(const void *x, const void *y);unsigned (*hash)(const void *key);int length;unsigned timestamp;struct binding{struct binding *link;const void *key;void *value;} **buckets;
};//static functions
static int cmpatom(const void *x, const void *y){return x != y;
}static unsigned hashatom(const void *key){return (unsigned long)key>>2;
}//functions
T Table_new(int hint,int cmp(const void *x, const void *y),unsigned hash(const void *key)){T table;int i;static int primes[] = {509, 509, 1021, 2053, 4093,8191, 16383, 32771, 65521, INT_MAX };assert(hint >= 0);for(i = 1; primes[i] < hint; ++i);table = ALLOC(sizeof(*table) + primes[i-1]*sizeof(table->buckets[0]));table->size = primes[i-1];table->cmp = cmp ? cmp : cmpatom;table->hash = hash ? hash : hashatom;table->buckets = (struct binding **)(table + 1);for(i = 0; i < table->size; ++i)table->buckets[i] = NULL;table->length = 0;table->timestamp = 0;return table;
}void *Table_get(T table, const void *key){int i;struct binding *p;assert(table);assert(key);i = (*table->hash)(key)%table->size;for(p = table->buckets[i]; p; p = p->link){if((*table->cmp)(key,p->key) == 0 )break;}return p ? p->value : NULL;
}void *Table_put(T table, const void *key, void *value){int i;struct binding *p;void *prev;assert(table);assert(key);//search table for keyi = (*table->hash)(key)%table->size;for(p = table->buckets[i]; p; p = p->link){if((*table->cmp)(key,p->key) == 0)break;}if(p == NULL){NEW(p);p->key = key;p->link = table->buckets[i];table->buckets[i] = p;table->length++;prev = NULL;}else{prev = p->value;}p->value = value;table->timestamp++;return prev;
}int Table_length(T table){assert(table);return table->length;
}void Table_map(T table,void apply(const void *key, void **value, void *cl),void *cl){int i;unsigned stamp;struct binding *p;assert(table);assert(apply);stamp = table->timestamp;for(i = 0; i < table->size; ++i){for(p = table->buckets[i]; p; p = p->link){apply(p->key, &p->value, cl);assert(table->timestamp == stamp);}}
}void *Table_remove(T table, const void *key){int i;struct binding **pp;assert(table);assert(key);table->timestamp++;i = (*table->hash)(key)%table->size;for(pp = &table->buckets[i]; *pp; pp = &(*pp)->link){if((*table->cmp)(key, (*pp)->key) == 0){struct binding *p = *pp;void *value = p->value;*pp = p->link;FREE(p);table->length--;return value;}}return NULL;
}void **Table_toArray(T table, void *end){int i, j = 0;void **array;struct binding *p;assert(table);array = ALLOC((2*table->length + 1)*sizeof(*array));for(i = 0; i < table->size; ++i){for(p = table->buckets[i]; p; p = p->link){array[j++] = (void *)p->key;array[j++] = p->value;}}array[j] = end;return array;
}void Table_free(T *table){assert(table && *table);if((*table)->length > 0){int i;struct binding *p, *q;for(i = 0; i < (*table)->size; ++i){for(p = (*table)->buckets[i]; p; p = q){q = p->link;FREE(p);}}}FREE(*table);
}


这篇关于C.Interface.And.Implementations—table(key-value系统)的实现的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1103234

相关文章

HTML5实现的移动端购物车自动结算功能示例代码

《HTML5实现的移动端购物车自动结算功能示例代码》本文介绍HTML5实现移动端购物车自动结算,通过WebStorage、事件监听、DOM操作等技术,确保实时更新与数据同步,优化性能及无障碍性,提升用... 目录1. 移动端购物车自动结算概述2. 数据存储与状态保存机制2.1 浏览器端的数据存储方式2.1.

基于 HTML5 Canvas 实现图片旋转与下载功能(完整代码展示)

《基于HTML5Canvas实现图片旋转与下载功能(完整代码展示)》本文将深入剖析一段基于HTML5Canvas的代码,该代码实现了图片的旋转(90度和180度)以及旋转后图片的下载... 目录一、引言二、html 结构分析三、css 样式分析四、JavaScript 功能实现一、引言在 Web 开发中,

SpringBoot中使用Flux实现流式返回的方法小结

《SpringBoot中使用Flux实现流式返回的方法小结》文章介绍流式返回(StreamingResponse)在SpringBoot中通过Flux实现,优势包括提升用户体验、降低内存消耗、支持长连... 目录背景流式返回的核心概念与优势1. 提升用户体验2. 降低内存消耗3. 支持长连接与实时通信在Sp

Conda虚拟环境的复制和迁移的四种方法实现

《Conda虚拟环境的复制和迁移的四种方法实现》本文主要介绍了Conda虚拟环境的复制和迁移的四种方法实现,包括requirements.txt,environment.yml,conda-pack,... 目录在本机复制Conda虚拟环境相同操作系统之间复制环境方法一:requirements.txt方法

Spring Boot 实现 IP 限流的原理、实践与利弊解析

《SpringBoot实现IP限流的原理、实践与利弊解析》在SpringBoot中实现IP限流是一种简单而有效的方式来保障系统的稳定性和可用性,本文给大家介绍SpringBoot实现IP限... 目录一、引言二、IP 限流原理2.1 令牌桶算法2.2 漏桶算法三、使用场景3.1 防止恶意攻击3.2 控制资源

Mac系统下卸载JAVA和JDK的步骤

《Mac系统下卸载JAVA和JDK的步骤》JDK是Java语言的软件开发工具包,它提供了开发和运行Java应用程序所需的工具、库和资源,:本文主要介绍Mac系统下卸载JAVA和JDK的相关资料,需... 目录1. 卸载系统自带的 Java 版本检查当前 Java 版本通过命令卸载系统 Java2. 卸载自定

springboot下载接口限速功能实现

《springboot下载接口限速功能实现》通过Redis统计并发数动态调整每个用户带宽,核心逻辑为每秒读取并发送限定数据量,防止单用户占用过多资源,确保整体下载均衡且高效,本文给大家介绍spring... 目录 一、整体目标 二、涉及的主要类/方法✅ 三、核心流程图解(简化) 四、关键代码详解1️⃣ 设置

Nginx 配置跨域的实现及常见问题解决

《Nginx配置跨域的实现及常见问题解决》本文主要介绍了Nginx配置跨域的实现及常见问题解决,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来... 目录1. 跨域1.1 同源策略1.2 跨域资源共享(CORS)2. Nginx 配置跨域的场景2.1

Python中提取文件名扩展名的多种方法实现

《Python中提取文件名扩展名的多种方法实现》在Python编程中,经常会遇到需要从文件名中提取扩展名的场景,Python提供了多种方法来实现这一功能,不同方法适用于不同的场景和需求,包括os.pa... 目录技术背景实现步骤方法一:使用os.path.splitext方法二:使用pathlib模块方法三

CSS实现元素撑满剩余空间的五种方法

《CSS实现元素撑满剩余空间的五种方法》在日常开发中,我们经常需要让某个元素占据容器的剩余空间,本文将介绍5种不同的方法来实现这个需求,并分析各种方法的优缺点,感兴趣的朋友一起看看吧... css实现元素撑满剩余空间的5种方法 在日常开发中,我们经常需要让某个元素占据容器的剩余空间。这是一个常见的布局需求