DoubleLi

qq: 517712484 wx: ldbgliet

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::
  4737 随笔 :: 2 文章 :: 542 评论 :: 1615万 阅读
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

最近需要在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 <stdlib.h>
#include <stdio.h>
#include <assert.h>
#include <string.h>
#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

posted on   DoubleLi  阅读(1006)  评论(0编辑  收藏  举报
编辑推荐:
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!
历史上的今天:
2017-11-17 C++11并发之std::thread
2014-11-17 H.264 基础及 RTP 封包详解
2014-11-17 live555学习之基本类介绍及计划任务深度探讨
2014-11-17 live555学习之RTSP连接建立以及请求消息处理过程
2014-11-17 Live555类结构
2014-11-17 Live555中RTP包的打包与发送过程分析
2014-11-17 live555 RTSP服务器建立及消息处理流程
点击右上角即可分享
微信分享提示