当前位置:网站首页>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;
};边栏推荐
- redis在服务器linux下的启动的相关命令(安装和配置)
- Summary of electromagnetic spectrum
- shardingsphere动态数据源
- 【DRM】DRM bridge驱动调用流程简单分析
- The XML file generated by labelimg is converted to VOC format
- MongoDB复制集【主从复制】
- Mongodb installation & Deployment
- Using jasmine to monitor constructors - spying on a constructor using Jasmine
- Limit of one question per day
- Stepping on pits and solutions when using inputfilter to limit EditText
猜你喜欢

Hi3536c v100r001c02spc040 cross compiler installation

Pytorch轻量级可视化工具wandb(local)

Table structure of Navicat export database

释放数据力量的Ceph-尚文网络xUP楠哥

Why does thread crash not cause JVM crash
![Mongodb replication set [master-slave replication]](/img/2c/8030548455f45fa252062dd90e7b8b.png)
Mongodb replication set [master-slave replication]

Small guide for rapid formation of manipulator (VIII): kinematic modeling (standard DH method)

简易版 微信小程序开发之页面跳转、数据绑定、获取用户信息、获取用户位置信息

Latest version of NPM: the "NPM" item cannot be recognized as the name of a cmdlet, function, script file, or runnable program. Please check

TCP/IP模型中的重磅嘉宾TCP--尚文网络奎哥
随机推荐
403 error displayed when vs cloning
Avec trois. JS fait une scène 3D simple
QQ小程序开发之 一些前期准备:预约开发账号、下载安装开发者工具、创建qq小程序
ffmpeg录制屏幕和截屏
Stepping on pits and solutions when using inputfilter to limit EditText
[combinatorics] basic counting principle (addition principle | multiplication principle)
8.8.2-PointersOnC-20220214
umi 路由拦截(简单粗暴)
机械臂速成小指南(八):运动学建模(标准DH法)
使用InputFilter限制EditText时踩坑及解决方案
Vs 2019 configuration tensorrt
Ansible简介【暂未完成(半成品)】
com. fasterxml. jackson. databind. Exc.invalidformatexception problem
CEPH Shangwen network xUP Nange that releases the power of data
MongoDB主配置文件
UMI route interception (simple and rough)
Numpy warning visibledeprecationwarning: creating an ndarray from ragged needed sequences
Réglez la hauteur et lancez le système. Currenttimemillis catton
[set theory] partial order relation (partial order relation definition | partial order set definition | greater than or equal to relation | less than or equal to relation | integer division relation |
TCP/IP模型中的重磅嘉宾TCP--尚文网络奎哥