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 <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

2 源碼

cmap.zip
上面無法下載可以去下面
鏈接:https://pan.baidu.com/s/1G5wsXUPyTZHwy_dLUS9F1g 提取碼: zigy

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章