当前位置:网站首页>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;
};边栏推荐
- 使用InputFilter限制EditText时踩坑及解决方案
- CEPH Shangwen network xUP Nange that releases the power of data
- umi 路由拦截(简单粗暴)
- Latest version of NPM: the "NPM" item cannot be recognized as the name of a cmdlet, function, script file, or runnable program. Please check
- 动态规划:最长回文子串和子序列
- NPM: the 'NPM' item cannot be recognized as the name of a cmdlet, function, script file, or runnable program. Please check the spelling of the name. If the path is included, make sure the path is corr
- 递归:深度优先搜索
- Small guide for rapid formation of manipulator (VIII): kinematic modeling (standard DH method)
- [mathematical logic] predicate logic (individual word | individual domain | predicate | full name quantifier | existence quantifier | predicate formula | exercise)
- [mathematical logic] propositions and connectives (propositions | propositional symbolization | truth connectives | no | conjunction | disjunction | non truth connectives | implication | equivalence)
猜你喜欢

Vs 2019 configuration tensorrt

Recursion: quick sort, merge sort and heap sort

Download and install node, NPM and yarn

Nce detail of softmax approximation

npm : 无法将“npm”项识别为 cmdlet、函数、脚本文件或可运行程序的名称。请检查名称的拼写,如果包括路径,请确保路径正确,然后再试一次。

CEPH Shangwen network xUP Nange that releases the power of data

FileZilla Client下载安装

Summary of matrix knowledge points in Chapter 2 of Linear Algebra (Jeff's self perception)

Applet get user avatar and nickname

Bid farewell to artificial mental retardation: Mengzi open source project team received RMB 100 million financing to help NLP develop
随机推荐
Elsevier latex 提交文章 pdftex.def Error: File `thumbnails/cas-email.jpeg‘ not found: using draf
Captura下载安装及在Captura配置FFmpeg
Compare float with 0
Separable bonds and convertible bonds
简易版 微信小程序开发之for指令、上传图片及展示效果优化
Tidal characteristics of the Bohai Sea and the Yellow Sea
【全民编程】《软件编程-讲课视频》【零基础入门到实战应用】
Some preliminary preparations for QQ applet development: make an appointment for a development account, download and install developer tools, and create QQ applet
Mongodb installation & Deployment
node 开启服务器
Nanning water leakage detection: warmly congratulate Guangxi Zhongshui on winning the first famous brand in Guangxi
900w+ data, from 17s to 300ms, how to operate
com. fasterxml. jackson. databind. Exc.invalidformatexception problem
Limit of one question per day
The difference between static web pages and dynamic web pages & the difference between Web1.0 and Web2.0 & the difference between get and post
FileZilla Client下載安裝
softmax的近似之NCE详解
Ansible简介【暂未完成(半成品)】
Section 26 detailed explanation and demonstration of IPSec virtual private network configuration experiment - simulation experiment based on packettracer8.0
Message queue addition failure