当前位置:网站首页>C language hashtable/hashset library summary
C language hashtable/hashset library summary
2022-07-03 03:39:00 【___ Bozi mi pro】
stay https://stackoverflow.com/questions/1138742/looking-for-a-good-hash-table-implementation-in-c Many related libraries are introduced in . After personal screening , Comprehensively consider the execution efficiency 、 The perfection of the document 、 The supported data types are in three aspects , I chose uthash.
Official documents can be found in :http://troydhanson.github.io/uthash/userguide.html
* If you want to be HashSet use , It only needs value Just assign any value
* If you only need String Type of key and value, You can also consider strmap
* There's no choice python Built in libcfu( Download address , Official documents ), Because it's in Android There are many problems when compiling on the environment of .
Brief example ( See official documents for details )
1. initialization
#include "uthash.h"
struct my_struct {
int id; // key
char name[10]; //value
UT_hash_handle hh; // There has to be , Let this structure hashable
};
struct my_struct *users = NULL; // important , Must be initialized to NULL
2. add to
void add_user(int user_id, char *name) {
struct my_struct *s;
HASH_FIND_INT(users, &user_id, s);
if (s == NULL) {
s = (struct my_struct *)malloc(sizeof *s);
s->id = user_id;
HASH_ADD_INT(users, id, s); // id: key Name
}
strcpy(s->name, name);
}
3. lookup
struct my_struct *find_user(int user_id) {
struct my_struct *s;
HASH_FIND_INT(users, &user_id, s);
return s;
}
4. Delete
void delete_user(struct my_struct *user) {
HASH_DEL(users, user);
free(user); // Optional
}
Refer to the official website demo Source code
#include <stdio.h> /* printf */
#include <stdlib.h> /* atoi, malloc */
#include <string.h> /* strcpy */
#include "uthash.h"
struct my_struct {
int id; /* key */
char name[21];
UT_hash_handle hh; /* makes this structure hashable */
};
struct my_struct *users = NULL;
void add_user(int user_id, const char *name)
{
struct my_struct *s;
HASH_FIND_INT(users, &user_id, s); /* id already in the hash? */
if (s == NULL) {
s = (struct my_struct*)malloc(sizeof *s);
s->id = user_id;
HASH_ADD_INT(users, id, s); /* id is the key field */
}
strcpy(s->name, name);
}
struct my_struct *find_user(int user_id)
{
struct my_struct *s;
HASH_FIND_INT(users, &user_id, s); /* s: output pointer */
return s;
}
void delete_user(struct my_struct *user)
{
HASH_DEL(users, user); /* user: pointer to deletee */
free(user);
}
void delete_all()
{
struct my_struct *current_user;
struct my_struct *tmp;
HASH_ITER(hh, users, current_user, tmp) {
HASH_DEL(users, current_user); /* delete it (users advances to next) */
free(current_user); /* free it */
}
}
void print_users()
{
struct my_struct *s;
for (s = users; s != NULL; s = (struct my_struct*)(s->hh.next)) {
printf("user id %d: name %s\n", s->id, s->name);
}
}
int by_name(const struct my_struct *a, const struct my_struct *b)
{
return strcmp(a->name, b->name);
}
int by_id(const struct my_struct *a, const struct my_struct *b)
{
return (a->id - b->id);
}
const char *getl(const char *prompt)
{
static char buf[21];
char *p;
printf("%s? ", prompt); fflush(stdout);
p = fgets(buf, sizeof(buf), stdin);
if (p == NULL || (p = strchr(buf, '\n')) == NULL) {
puts("Invalid input!");
exit(EXIT_FAILURE);
}
*p = '\0';
return buf;
}
int main()
{
int id = 1;
int running = 1;
struct my_struct *s;
int temp;
while (running) {
printf(" 1. add user\n");
printf(" 2. add or rename user by id\n");
printf(" 3. find user\n");
printf(" 4. delete user\n");
printf(" 5. delete all users\n");
printf(" 6. sort items by name\n");
printf(" 7. sort items by id\n");
printf(" 8. print users\n");
printf(" 9. count users\n");
printf("10. quit\n");
switch (atoi(getl("Command"))) {
case 1:
add_user(id++, getl("Name (20 char max)"));
break;
case 2:
temp = atoi(getl("ID"));
add_user(temp, getl("Name (20 char max)"));
break;
case 3:
s = find_user(atoi(getl("ID to find")));
printf("user: %s\n", s ? s->name : "unknown");
break;
case 4:
s = find_user(atoi(getl("ID to delete")));
if (s) {
delete_user(s);
} else {
printf("id unknown\n");
}
break;
case 5:
delete_all();
break;
case 6:
HASH_SORT(users, by_name);
break;
case 7:
HASH_SORT(users, by_id);
break;
case 8:
print_users();
break;
case 9:
temp = HASH_COUNT(users);
printf("there are %d users\n", temp);
break;
case 10:
running = 0;
break;
}
}
delete_all(); /* free any structures */
return 0;
}
matters needing attention
- If in Android/gcc Compile times
fall-through between switch labels
, You can use pragma The sentence put uthash The relevant code is enclosed
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wimplicit-fallthrough"
// uthash Related codes
#pragma GCC diagnostic pop
Actually, it can be used __attribute__ ((fallthrough));
But the actual measurement did not pragma This method is good .
If compiling times
padding struct 'struct UT_hash_table' with 4 bytes to align 'tail'
, May, in accordance with the https://github.com/troydhanson/uthash/issues/118#issue-223582662 Answer , adjustment uthash.h The order of variables inIf you want to be HashSet use , You may also need to declare an additional variable , Otherwise, there may be padding The problem of , newspaper
padding struct 'struct fd_struct' with 4 bytes to align 'hh'
, as follows :
struct fd_struct {
int fd; // key
int padding; // For filling , Or is it char padding[10] And so on. ;
UT_hash_handle hh;
};
边栏推荐
- 简易版 微信小程序开发之页面跳转、数据绑定、获取用户信息、获取用户位置信息
- Limit of one question per day
- User value is the last word in the competition of mobile phone market
- [mathematical logic] propositional logic (propositional and connective review | propositional formula | connective priority | truth table satisfiable contradiction tautology)
- Download and install node, NPM and yarn
- 动态规划:最长公共子串和最长公共子序列
- Solve high and send system Currenttimemillis Caton
- Limit of one question per day
- Pytorch轻量级可视化工具wandb(local)
- Applet get user avatar and nickname
猜你喜欢
Hi3536c v100r001c02spc040 cross compiler installation
Hutool动态添加定时任务
Pytoch configuration
Pytorch轻量级可视化工具wandb(local)
简易版 微信小程序开发之for指令、上传图片及展示效果优化
Mongodb installation & Deployment
How to move towards IPv6: IPv6 Transition Technology - Shangwen network quigo
navicat 导出数据库的表结构
umi 路由拦截(简单粗暴)
TCP/IP模型中的重磅嘉宾TCP--尚文网络奎哥
随机推荐
Mongodb replication set [master-slave replication]
[algebraic structure] group (definition of group | basic properties of group | proof method of group | commutative group)
Limit of one question per day
Mysql Mac版下载安装教程
Small guide for rapid formation of manipulator (VIII): kinematic modeling (standard DH method)
Vs 2019 installation and configuration opencv
[mathematical logic] propositional logic (propositional and connective review | propositional formula | connective priority | truth table satisfiable contradiction tautology)
Table structure of Navicat export database
@Accessors annotation function specifies that the prefix follows the hump naming
900w+ data, from 17s to 300ms, how to operate
Nce detail of softmax approximation
Introduction to mongodb
【学习笔记】seckill-秒杀项目--(11)项目总结
[mathematical logic] predicate logic (individual word | individual domain | predicate | full name quantifier | existence quantifier | predicate formula | exercise)
Ffmpeg one / more pictures synthetic video
On the adjacency matrix and adjacency table of graph storage
编译文件时报错:错误: 编码GBK的不可映射字符
Simple wechat applet development page Jump, data binding, obtaining user information, obtaining user location information
【DRM】DRM bridge驱动调用流程简单分析
C# WebRequest POST模式 ,基于“Basic Auth”口令认证模式,使用multipart/form-data方式上传文件及提交其他数据