C语言 MAP
最近需要在AWSIOT shadow添加设备状态,很明显JSON这种数据状态很明显每个状态都是Key-Value这种数据类型,很自然的想到使用MAP去实现这种状态。而代码又是跑在嵌入式设备中很明显是C语言,这里就带来一个问题,C语言原生是没有MAP实现的。作为生产环境使用,自己手搓轮子难免有考虑不周情况出现,这里就去github 摸代码,找个相对start高点,先测试一波。
github 地址:https://github.com/petewarden/c_hashmap
源码就两个文件 一个c 一个h,与其他lib相比这个lib, value值可以是动态类型。
1 sample
#include
#include
#include
#include
#include "hashmap.h"
#define KEY_MAX_LENGTH (256)
#define KEY_COUNT (1024*1024)
typedef struct data_struct_s
{
char key_string[KEY_MAX_LENGTH];
int type;
int number;
} data_struct_t;
typedef struct DATASTRUCTSTR
{
char key_string[KEY_MAX_LENGTH];
int type;
char* str;
} data_struct_str;
typedef struct DATASTRUCTANY{
char key_string[KEY_MAX_LENGTH];
int type;
any_t data;
} data_struct_any_t;
int iterate(any_t item, any_t data){
printf("map key:%s\n", ((data_struct_any_t*)data)->key_string);
printf("map type:%d\n", ((data_struct_any_t*)data)->type);
if( ((data_struct_any_t*)data)->type == 0){
printf("map data:%d\n", ((data_struct_t *)data)->number);
}else if(((data_struct_any_t*)data)->type== 1){
printf("map data:%s\n", ((data_struct_str *)data)->str);
}
return MAP_OK;
}
int main(char* argv, int argc)
{
int index;
int error;
map_t mymap;
char key_string[KEY_MAX_LENGTH];
data_struct_any_t* anyt = malloc(sizeof(data_struct_any_t));
data_struct_any_t* readitem = NULL;
mymap = hashmap_new();
printf("\n--------put data start-------\n");
data_struct_t* value;
value = malloc(sizeof(data_struct_t));
snprintf(value->key_string, KEY_MAX_LENGTH, "%s", "number");
value->number = 123;
value->type = 0;//自行定义 0为number
error = hashmap_put(mymap, value->key_string, value);
assert(error==MAP_OK);
printf("key:number, value:%d\n", value->number);
data_struct_str* str;
str = malloc(sizeof(data_struct_str));
snprintf(str->key_string, KEY_MAX_LENGTH, "%s", "str");
str->str = (char*)malloc(sizeof(char)*100);
strcpy(str->str,"helloworld");
str->type = 1;//自行定义 1 str
error = hashmap_put(mymap, str->key_string, str);
assert(error==MAP_OK);
printf("key:str, value:%s\n", str->str);
printf("\n---------get data start--------\n");
error = hashmap_get(mymap, "number", (void**)(&readitem));
assert(error==MAP_OK);
printf("number data:%d\n", ((data_struct_t *)readitem)->number);
error = hashmap_get(mymap, "str", (void**)(&readitem));
assert(error==MAP_OK);
printf("str data:%s\n", ((data_struct_str *)readitem)->str);
printf("\n---------iterate start--------\n");
PFany IterateFunc = iterate;
hashmap_iterate(mymap, IterateFunc, anyt);//这里可以第三个参数 传入指针从而 读到遍历时需要的某个值
printf("\n----------remove start----------\n");
error = hashmap_get(mymap, "number", (void**)(&readitem));
assert(error==MAP_OK);
error = hashmap_remove(mymap, readitem->key_string);
assert(error==MAP_OK);
free(readitem);
printf("---------check remove result--------\n");
anyt = NULL;
hashmap_iterate(mymap, IterateFunc, anyt);
hashmap_free(mymap);
return 1;
}
输出定义了两个map值:
一个key为number value为123
一个key为str key为helloworld
编译:
gcc main.c hashmap.c -o test
测试
--------put data start-------
key:number, value:123
key:str, value:helloworld
---------get data start--------
number data:123
str data:helloworld
---------iterate start--------
map key:number
map type:0
map data:123
map key:str
map type:1
map data:helloworld
----------remove start----------
---------check remove result--------
map key:str
map type:1
map data:helloworld