C語言哈希表的實現

使用C語言實現HashMap


寫這個HashMap的最初目的是在單片機上使用,後來就着學習的態度自己就把他完善了一下,HashMap的大小、key的最大長度、value的最大長度都是在頭文件中通過宏定義配置。
完整代碼使用到了:

完整代碼:https://github.com/ankun6/HashMap


頭文件如下:

//
// Created by AnKun on 2019/12/3.
//

#ifndef MYHASHMAP_HASHMAP_H
#define MYHASHMAP_HASHMAP_H

#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <stdbool.h>
#include "malloc.h"
#include "List.h"

#define hmalloc mem_malloc
#define hfree   mem_free
#define hmemcpy mem_memcpy
#define hmemset mem_memset
#define hrealloc mem_realloc


#define KEY_MAX_LEN 16      // key的最大長度
#define VAL_MAX_LEN 32      // value的最大長度

#pragma pack(4)

typedef struct list_head HashTable;

typedef struct
{
    HashTable *items;   // 鍵值對條目,如果有衝突鍵值對則掛在鏈表後
    uint32_t used;      // 使用量
    uint32_t size;      // 總大小
} HashMap;

typedef struct
{
    uint8_t key[KEY_MAX_LEN];
    uint8_t value[VAL_MAX_LEN];
    struct list_head node;
} Entry;

HashMap *HashMap_Create(uint32_t size);
void HashMap_Destroy(HashMap *hashMap);
bool HashMap_Put(HashMap *hashMap, const void *key, const void *value);
void *HashMap_Get(const HashMap *const hashMap, const void *key);
void HashMap_Clear(HashMap *hashMap);
void HashMap_Remove(HashMap *hashMap, const void *key);
bool HashMap_Exists(const HashMap *const hashMap, const void *key);
void HashMap_GetKeys(const HashMap *const hashMap, uint8_t **keys, uint32_t *count);

#pragma pack()

#endif //MYHASHMAP_HASHMAP_H


使用例程:

#include <stdio.h>
#include "HashMap.h"

#define Put(key, value)             HashMap_Put(hashMap, key, value)
#define Get(key)                    HashMap_Get(hashMap, key)
#define GetKesy(keys, count)       HashMap_GetKeys(hashMap, keys, count)
#define Remove(key)                 HashMap_Remove(hashMap, key)
#define Clear()                     HashMap_Clear(hashMap)
#define Exists(key)                 HashMap_Exists(hashMap, key)

int main()
{
	char pairs[][2][10] = {{"key1", "value1"},
						   {"key2", "value2"},
						   {"key3", "value3"}};
    const unsigned int npair = sizeof(pairs) / sizeof(pairs[0]);

    // 初始化自建內存池
    mem_init();

    // 創建HashMap
    HashMap *hashMap = HashMap_Create(10);

    // 加入和獲取
    for (int i = 0; i != npair; i++)
    {
        Put(pairs[i][0], pairs[i][1]);
        printf("[%s] ", Get(pairs[i][0]));
    }
    printf("\r\n");

    // 查看內存池信息
	printf("memory: <preused:%.2f%%> <used:%d> <free:%d> \r\n", mem_perused(), mem_getused(), mem_getfree());

    // 獲取所有key
    int count = 0;
    uint8_t *keys[10];
    GetKesy(keys, &count);
    for (int j = 0; j < count; ++j)
    {
        printf("[%s] ", keys[j]);
    }
    printf("\r\n");

    // 刪除
    Remove("key1");

    // 檢測是否存在
    bool isExists = Exists("key1");
    printf("%s\r\n", isExists ? "true" : "false");

    // 清空
    Clear();

    // 銷燬HashMap
    HashMap_Destroy(hashMap);

    // 查看內存池信息
	printf("memory: <preused:%.2f%%> <used:%d> <free:%d> \r\n", mem_perused(), mem_getused(), mem_getfree());

    return 0;
}

運行效果:

在這裏插入圖片描述


ends…

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