用C语言实现一个简单实用的hashmap,具有一定的实际意义。尤其我们不想使用STL里面的map<...>类的时候。我实现的这个hashmap,用来做key---value的映射,key必须是有效的字符串,value是调用者分配的任意类型的数据。这个hashmap适合在一些简单的场合下,消耗极少的资源。
首先定义头文件如下:
/*
* hashmap.h
* Generic hash map: key(string)-value(any type).
* cheungmine
* Sep. 22, 2007. All rights reserved.
*/
#ifndef HASHMAP_H_INCLUDED
#define HASHMAP_H_INCLUDED
#include "unistd.h"
/* You should always use 1024 */
#define HASHMAP_SIZE 1024
/* Opaque struct pointer to _hash_map_t */
typedef struct _hash_map_t* hash_map;
typedef void(*pfcb_hmap_value_free)(void* value);
/* An example of free value function implemented by caller:
void my_hmap_free_value(void* pv)
{
free(pv);
}
*/
/* Create before use. eg:
* hash_map hm;
* hmap_create (&hm, HASHMAP_SIZE);
* assert (hm); // out of memory if hm==NULL
* void* mydata=malloc(n);
* hmap_insert(hm, "shanghai", -1, mydata);
...
* hmap_destroy(hm, my_hmap_free_value);
*/
extern void
hmap_create(hash_map *hmap, int size);
/* Destroy after use */
extern void
hmap_destroy(hash_map hmap, pfcb_hmap_value_free);
/* Insert a key-value into hash map. value is a pointer to callee-allocated memory */
extern void
hmap_insert(hash_map hmap, const char* key, int key_len/* -1 for strlen to be called */, void* value);
/* Search a hash map for value of given key string */
extern void*
hmap_search(hash_map hmap, const char *key);
#endif /* HASHMAP_H_INCLUDED */
实现文件如下:
/*
* hashmap.c
* Generic hashmap implementation.
* a map for pair of key-value. key must be a null-end string, value is any type of data.
* cheungmine
* Sep. 22, 2007. All rights reserved.
*/
#include "hashmap.h"
#include "list.h"
typedef struct _hash_map_t
{
size_t size;
listnode_t** key;
listnode_t** value;
}hash_map_t;
/* Hash a string, return a hash key */
static ulong hash_string(const char *s, int len)
{
ulong h = 0;
int i = 0;
assert (s);
if (len < 0)
len = (s? (int)strlen(s): 0);
while(i++ < len) { h = 17*h + *s++; }
return h;
}
static void _free_map_key(listnode_t* node)
{
listnode_t *old;
while(node)
{
old = node;
node = node->next;
free(old->data);
free (old);
}
}
static void _free_map_value(listnode_t* node, pfcb_hmap_value_free pfunc)
{
listnode_t *old;
while(node)
{
old = node;
node = node->next;
if (pfunc)
(*pfunc)(old->data);
free (old);
}
}
/*=============================================================================
Public Functions
=============================================================================*/
/* Create before use */
void
hmap_create(hash_map *hmap, int size)
{
(*hmap) = (hash_map_t*) malloc(sizeof(hash_map_t));
(*hmap)->size = size;
(*hmap)->key = (listnode_t**) calloc(size, sizeof(listnode_t*));
(*hmap)->value = (listnode_t**) calloc(size, sizeof(listnode_t*));
}
/* Destroy after use */
extern void
hmap_destroy(hash_map hmap, pfcb_hmap_value_free pfunc)
{
size_t i;
for(i=0; i<hmap->size; i++){
_free_map_key(hmap->key[i]);
_free_map_value(hmap->value[i], pfunc);
}
free(hmap->key);
free(hmap->value);
free(hmap);
}
/* Insert a key-value into hash map. value is a pointer to callee-allocated memory */
void
hmap_insert(hash_map hmap, const char* key, int key_len, void* value)
{
listnode_t *node_key, *node_val;
ulong h;
char *s;
assert (key);
if (key_len<0) key_len = (int) strlen (key);
s = (char*) malloc (key_len+1);
assert(s);
#pragma warning(push) /* C4996 */
#pragma warning( disable : 4996 )
strncpy (s, key, key_len);
#pragma warning(pop) /* C4996 */
s[key_len] = 0;
node_key = list_node_create ( (void*)s );
node_val = list_node_create ( value );
assert(node_key && node_val);
h = hash_string (s, key_len) % hmap->size;
node_key->next = hmap->key[h];
hmap->key[h] = node_key;
node_val->next = hmap->value[h];
hmap->value[h] = node_val;
}
/* Search a hash map for value of given key string */
void*
hmap_search(hash_map hmap, const char *key)
{
ulong h = hash_string (key, -1) % hmap->size;
listnode_t *pk = hmap->key[h];
listnode_t *pv = hmap->value[h];
while (pk)
{
if (strcmp(key, pk->str) == 0)
return pv->data;
pk = pk->next;
pv = pv->next;
}
return NULL;
}
好了,其中用到的其他文件(unistd.h,list.h,list.c)
用C语言实现一个简单实用的hashmap,具有一定的实际意义。尤其我们不想使用STL里面的map<...>类的时候。我实现的这个hashmap,用来做key---value的映射,key必须是有效的字符串,value是调用者分配的任意类型的数据。这个hashmap适合在一些简单的场合下,消耗极少的资源。
首先定义头文件如下:
/*
* hashmap.h
* Generic hash map: key(string)-value(any type).
* cheungmine
* Sep. 22, 2007. All rights reserved.
*/
#ifndef HASHMAP_H_INCLUDED
#define HASHMAP_H_INCLUDED
#include "unistd.h"
/* You should always use 1024 */
#define HASHMAP_SIZE 1024
/* Opaque struct pointer to _hash_map_t */
typedef struct _hash_map_t* hash_map;
typedef void(*pfcb_hmap_value_free)(void* value);
/* An example of free value function implemented by caller:
void my_hmap_free_value(void* pv)
{
free(pv);
}
*/
/* Create before use. eg:
* hash_map hm;
* hmap_create (&hm, HASHMAP_SIZE);
* assert (hm); // out of memory if hm==NULL
* void* mydata=malloc(n);
* hmap_insert(hm, "shanghai", -1, mydata);
...
* hmap_destroy(hm, my_hmap_free_value);
*/
extern void
hmap_create(hash_map *hmap, int size);
/* Destroy after use */
extern void
hmap_destroy(hash_map hmap, pfcb_hmap_value_free);
/* Insert a key-value into hash map. value is a pointer to callee-allocated memory */
extern void
hmap_insert(hash_map hmap, const char* key, int key_len/* -1 for strlen to be called */, void* value);
/* Search a hash map for value of given key string */
extern void*
hmap_search(hash_map hmap, const char *key);
#endif /* HASHMAP_H_INCLUDED */
实现文件如下:
/*
* hashmap.c
* Generic hashmap implementation.
* a map for pair of key-value. key must be a null-end string, value is any type of data.
* cheungmine
* Sep. 22, 2007. All rights reserved.
*/
#include "hashmap.h"
#include "list.h"
typedef struct _hash_map_t
{
size_t size;
listnode_t** key;
listnode_t** value;
}hash_map_t;
/* Hash a string, return a hash key */
static ulong hash_string(const char *s, int len)
{
ulong h = 0;
int i = 0;
assert (s);
if (len < 0)
len = (s? (int)strlen(s): 0);
while(i++ < len) { h = 17*h + *s++; }
return h;
}
static void _free_map_key(listnode_t* node)
{
listnode_t *old;
while(node)
{
old = node;
node = node->next;
free(old->data);
free (old);
}
}
static void _free_map_value(listnode_t* node, pfcb_hmap_value_free pfunc)
{
listnode_t *old;
while(node)
{
old = node;
node = node->next;
if (pfunc)
(*pfunc)(old->data);
free (old);
}
}
/*=============================================================================
Public Functions
=============================================================================*/
/* Create before use */
void
hmap_create(hash_map *hmap, int size)
{
(*hmap) = (hash_map_t*) malloc(sizeof(hash_map_t));
(*hmap)->size = size;
(*hmap)->key = (listnode_t**) calloc(size, sizeof(listnode_t*));
(*hmap)->value = (listnode_t**) calloc(size, sizeof(listnode_t*));
}
/* Destroy after use */
extern void
hmap_destroy(hash_map hmap, pfcb_hmap_value_free pfunc)
{
size_t i;
for(i=0; i<hmap->size; i++){
_free_map_key(hmap->key[i]);
_free_map_value(hmap->value[i], pfunc);
}
free(hmap->key);
free(hmap->value);
free(hmap);
}
/* Insert a key-value into hash map. value is a pointer to callee-allocated memory */
void
hmap_insert(hash_map hmap, const char* key, int key_len, void* value)
{
listnode_t *node_key, *node_val;
ulong h;
char *s;
assert (key);
if (key_len<0) key_len = (int) strlen (key);
s = (char*) malloc (key_len+1);
assert(s);
#pragma warning(push) /* C4996 */
#pragma warning( disable : 4996 )
strncpy (s, key, key_len);
#pragma warning(pop) /* C4996 */
s[key_len] = 0;
node_key = list_node_create ( (void*)s );
node_val = list_node_create ( value );
assert(node_key && node_val);
h = hash_string (s, key_len) % hmap->size;
node_key->next = hmap->key[h];
hmap->key[h] = node_key;
node_val->next = hmap->value[h];
hmap->value[h] = node_val;
}
/* Search a hash map for value of given key string */
void*
hmap_search(hash_map hmap, const char *key)
{
ulong h = hash_string (key, -1) % hmap->size;
listnode_t *pk = hmap->key[h];
listnode_t *pv = hmap->value[h];
while (pk)
{
if (strcmp(key, pk->str) == 0)
return pv->data;
pk = pk->next;
pv = pv->next;
}
return NULL;
}
好了,其中用到的其他文件(unistd.h,list.h,list.c)看我下一篇文章!
C语言实现一个简单的单向链表list
首先定义头文件如下:
/*
* hashmap.h
* Generic hash map: key(string)-value(any type).
* cheungmine
* Sep. 22, 2007. All rights reserved.
*/
#ifndef HASHMAP_H_INCLUDED
#define HASHMAP_H_INCLUDED
#include "unistd.h"
/* You should always use 1024 */
#define HASHMAP_SIZE 1024
/* Opaque struct pointer to _hash_map_t */
typedef struct _hash_map_t* hash_map;
typedef void(*pfcb_hmap_value_free)(void* value);
/* An example of free value function implemented by caller:
void my_hmap_free_value(void* pv)
{
free(pv);
}
*/
/* Create before use. eg:
* hash_map hm;
* hmap_create (&hm, HASHMAP_SIZE);
* assert (hm); // out of memory if hm==NULL
* void* mydata=malloc(n);
* hmap_insert(hm, "shanghai", -1, mydata);
...
* hmap_destroy(hm, my_hmap_free_value);
*/
extern void
hmap_create(hash_map *hmap, int size);
/* Destroy after use */
extern void
hmap_destroy(hash_map hmap, pfcb_hmap_value_free);
/* Insert a key-value into hash map. value is a pointer to callee-allocated memory */
extern void
hmap_insert(hash_map hmap, const char* key, int key_len/* -1 for strlen to be called */, void* value);
/* Search a hash map for value of given key string */
extern void*
hmap_search(hash_map hmap, const char *key);
#endif /* HASHMAP_H_INCLUDED */
实现文件如下:
/*
* hashmap.c
* Generic hashmap implementation.
* a map for pair of key-value. key must be a null-end string, value is any type of data.
* cheungmine
* Sep. 22, 2007. All rights reserved.
*/
#include "hashmap.h"
#include "list.h"
typedef struct _hash_map_t
{
size_t size;
listnode_t** key;
listnode_t** value;
}hash_map_t;
/* Hash a string, return a hash key */
static ulong hash_string(const char *s, int len)
{
ulong h = 0;
int i = 0;
assert (s);
if (len < 0)
len = (s? (int)strlen(s): 0);
while(i++ < len) { h = 17*h + *s++; }
return h;
}
static void _free_map_key(listnode_t* node)
{
listnode_t *old;
while(node)
{
old = node;
node = node->next;
free(old->data);
free (old);
}
}
static void _free_map_value(listnode_t* node, pfcb_hmap_value_free pfunc)
{
listnode_t *old;
while(node)
{
old = node;
node = node->next;
if (pfunc)
(*pfunc)(old->data);
free (old);
}
}
/*=============================================================================
Public Functions
=============================================================================*/
/* Create before use */
void
hmap_create(hash_map *hmap, int size)
{
(*hmap) = (hash_map_t*) malloc(sizeof(hash_map_t));
(*hmap)->size = size;
(*hmap)->key = (listnode_t**) calloc(size, sizeof(listnode_t*));
(*hmap)->value = (listnode_t**) calloc(size, sizeof(listnode_t*));
}
/* Destroy after use */
extern void
hmap_destroy(hash_map hmap, pfcb_hmap_value_free pfunc)
{
size_t i;
for(i=0; i<hmap->size; i++){
_free_map_key(hmap->key[i]);
_free_map_value(hmap->value[i], pfunc);
}
free(hmap->key);
free(hmap->value);
free(hmap);
}
/* Insert a key-value into hash map. value is a pointer to callee-allocated memory */
void
hmap_insert(hash_map hmap, const char* key, int key_len, void* value)
{
listnode_t *node_key, *node_val;
ulong h;
char *s;
assert (key);
if (key_len<0) key_len = (int) strlen (key);
s = (char*) malloc (key_len+1);
assert(s);
#pragma warning(push) /* C4996 */
#pragma warning( disable : 4996 )
strncpy (s, key, key_len);
#pragma warning(pop) /* C4996 */
s[key_len] = 0;
node_key = list_node_create ( (void*)s );
node_val = list_node_create ( value );
assert(node_key && node_val);
h = hash_string (s, key_len) % hmap->size;
node_key->next = hmap->key[h];
hmap->key[h] = node_key;
node_val->next = hmap->value[h];
hmap->value[h] = node_val;
}
/* Search a hash map for value of given key string */
void*
hmap_search(hash_map hmap, const char *key)
{
ulong h = hash_string (key, -1) % hmap->size;
listnode_t *pk = hmap->key[h];
listnode_t *pv = hmap->value[h];
while (pk)
{
if (strcmp(key, pk->str) == 0)
return pv->data;
pk = pk->next;
pv = pv->next;
}
return NULL;
}
好了,其中用到的其他文件(unistd.h,list.h,list.c)
用C语言实现一个简单实用的hashmap,具有一定的实际意义。尤其我们不想使用STL里面的map<...>类的时候。我实现的这个hashmap,用来做key---value的映射,key必须是有效的字符串,value是调用者分配的任意类型的数据。这个hashmap适合在一些简单的场合下,消耗极少的资源。
首先定义头文件如下:
/*
* hashmap.h
* Generic hash map: key(string)-value(any type).
* cheungmine
* Sep. 22, 2007. All rights reserved.
*/
#ifndef HASHMAP_H_INCLUDED
#define HASHMAP_H_INCLUDED
#include "unistd.h"
/* You should always use 1024 */
#define HASHMAP_SIZE 1024
/* Opaque struct pointer to _hash_map_t */
typedef struct _hash_map_t* hash_map;
typedef void(*pfcb_hmap_value_free)(void* value);
/* An example of free value function implemented by caller:
void my_hmap_free_value(void* pv)
{
free(pv);
}
*/
/* Create before use. eg:
* hash_map hm;
* hmap_create (&hm, HASHMAP_SIZE);
* assert (hm); // out of memory if hm==NULL
* void* mydata=malloc(n);
* hmap_insert(hm, "shanghai", -1, mydata);
...
* hmap_destroy(hm, my_hmap_free_value);
*/
extern void
hmap_create(hash_map *hmap, int size);
/* Destroy after use */
extern void
hmap_destroy(hash_map hmap, pfcb_hmap_value_free);
/* Insert a key-value into hash map. value is a pointer to callee-allocated memory */
extern void
hmap_insert(hash_map hmap, const char* key, int key_len/* -1 for strlen to be called */, void* value);
/* Search a hash map for value of given key string */
extern void*
hmap_search(hash_map hmap, const char *key);
#endif /* HASHMAP_H_INCLUDED */
实现文件如下:
/*
* hashmap.c
* Generic hashmap implementation.
* a map for pair of key-value. key must be a null-end string, value is any type of data.
* cheungmine
* Sep. 22, 2007. All rights reserved.
*/
#include "hashmap.h"
#include "list.h"
typedef struct _hash_map_t
{
size_t size;
listnode_t** key;
listnode_t** value;
}hash_map_t;
/* Hash a string, return a hash key */
static ulong hash_string(const char *s, int len)
{
ulong h = 0;
int i = 0;
assert (s);
if (len < 0)
len = (s? (int)strlen(s): 0);
while(i++ < len) { h = 17*h + *s++; }
return h;
}
static void _free_map_key(listnode_t* node)
{
listnode_t *old;
while(node)
{
old = node;
node = node->next;
free(old->data);
free (old);
}
}
static void _free_map_value(listnode_t* node, pfcb_hmap_value_free pfunc)
{
listnode_t *old;
while(node)
{
old = node;
node = node->next;
if (pfunc)
(*pfunc)(old->data);
free (old);
}
}
/*=============================================================================
Public Functions
=============================================================================*/
/* Create before use */
void
hmap_create(hash_map *hmap, int size)
{
(*hmap) = (hash_map_t*) malloc(sizeof(hash_map_t));
(*hmap)->size = size;
(*hmap)->key = (listnode_t**) calloc(size, sizeof(listnode_t*));
(*hmap)->value = (listnode_t**) calloc(size, sizeof(listnode_t*));
}
/* Destroy after use */
extern void
hmap_destroy(hash_map hmap, pfcb_hmap_value_free pfunc)
{
size_t i;
for(i=0; i<hmap->size; i++){
_free_map_key(hmap->key[i]);
_free_map_value(hmap->value[i], pfunc);
}
free(hmap->key);
free(hmap->value);
free(hmap);
}
/* Insert a key-value into hash map. value is a pointer to callee-allocated memory */
void
hmap_insert(hash_map hmap, const char* key, int key_len, void* value)
{
listnode_t *node_key, *node_val;
ulong h;
char *s;
assert (key);
if (key_len<0) key_len = (int) strlen (key);
s = (char*) malloc (key_len+1);
assert(s);
#pragma warning(push) /* C4996 */
#pragma warning( disable : 4996 )
strncpy (s, key, key_len);
#pragma warning(pop) /* C4996 */
s[key_len] = 0;
node_key = list_node_create ( (void*)s );
node_val = list_node_create ( value );
assert(node_key && node_val);
h = hash_string (s, key_len) % hmap->size;
node_key->next = hmap->key[h];
hmap->key[h] = node_key;
node_val->next = hmap->value[h];
hmap->value[h] = node_val;
}
/* Search a hash map for value of given key string */
void*
hmap_search(hash_map hmap, const char *key)
{
ulong h = hash_string (key, -1) % hmap->size;
listnode_t *pk = hmap->key[h];
listnode_t *pv = hmap->value[h];
while (pk)
{
if (strcmp(key, pk->str) == 0)
return pv->data;
pk = pk->next;
pv = pv->next;
}
return NULL;
}
好了,其中用到的其他文件(unistd.h,list.h,list.c)看我下一篇文章!
C语言实现一个简单的单向链表list
发表评论
-
STL 中 map 用法详解
2009-06-18 09:28 1414STL中map用法详解 说明:如果你具备一定的C++ tem ... -
c++ hash_map 详细介绍
2009-06-17 10:21 34296为什么需要hash_map 用过m ... -
学习CRYPTO第三天
2009-06-17 09:42 18541,CertOpenSystemStore打开系统最常用的证书 ... -
学习CRYPTO第二天
2009-06-17 09:41 1315因为是.net安全,所以必须在VC7上运行下面面的一些例子(今 ... -
学习CRYPTOAPI第一天
2009-06-17 09:41 2335一:准备工作 一般必须 ... -
STL中map用法详解
2009-06-17 09:39 1897STL中map用法详解 说明:如果你具备一定的C++ tem ...
相关推荐
C语言实现hashMap,包含创建hashMap、插入hashMap、查找hashMap、删除hashMap,已经若干经典的hash函数。文章链接:https://blog.csdn.net/sxf1061700625/article/details/109594495
做项目写业务逻辑的时候经常用到的数据...但是c语言是没有提供这类标准库的,本资源提供的是一个可以在正式项目中使用的纯c语言实现的哈希字典。原文链接:https://blog.csdn.net/u013113678/article/details/113765937
18. C语言实现动态数组 100 19. C语言笔试-运算符和表达式 104 20. C语言编程准则之稳定篇 107 21. C语言编程常见问题分析 108 22. C语言编程易犯毛病集合 112 23. C语言缺陷与陷阱(笔记) 119 24. C语言防止缓冲区...
18. C语言实现动态数组 100 19. C语言笔试-运算符和表达式 104 20. C语言编程准则之稳定篇 107 21. C语言编程常见问题分析 108 22. C语言编程易犯毛病集合 112 23. C语言缺陷与陷阱(笔记) 119 24. C语言防止缓冲区...
18. C语言实现动态数组 100 19. C语言笔试-运算符和表达式 104 20. C语言编程准则之稳定篇 107 21. C语言编程常见问题分析 108 22. C语言编程易犯毛病集合 112 23. C语言缺陷与陷阱(笔记) 119 24. C语言防止缓冲区...
18. C语言实现动态数组 100 19. C语言笔试-运算符和表达式 104 20. C语言编程准则之稳定篇 107 21. C语言编程常见问题分析 108 22. C语言编程易犯毛病集合 112 23. C语言缺陷与陷阱(笔记) 119 24. C语言防止缓冲区...
18. C语言实现动态数组 100 19. C语言笔试-运算符和表达式 104 20. C语言编程准则之稳定篇 107 21. C语言编程常见问题分析 108 22. C语言编程易犯毛病集合 112 23. C语言缺陷与陷阱(笔记) 119 24. C语言防止缓冲区...
18. C语言实现动态数组 100 19. C语言笔试-运算符和表达式 104 20. C语言编程准则之稳定篇 107 21. C语言编程常见问题分析 108 22. C语言编程易犯毛病集合 112 23. C语言缺陷与陷阱(笔记) 119 24. C语言防止缓冲区...
C语言实现动态数组 .................................................................................................................... 89 19. C语言笔试-运算符和表达式 ...................................
18. C语言实现动态数组 100 19. C语言笔试-运算符和表达式 104 20. C语言编程准则之稳定篇 107 21. C语言编程常见问题分析 108 22. C语言编程易犯毛病集合 112 23. C语言缺陷与陷阱(笔记) 119 24. C语言防止缓冲区...
用C语言实现的HashMap简单编程,希望同大家共享
18. C语言实现动态数组 100 19. C语言笔试-运算符和表达式 104 20. C语言编程准则之稳定篇 107 21. C语言编程常见问题分析 108 22. C语言编程易犯毛病集合 112 23. C语言缺陷与陷阱(笔记) 119 24. C语言防止...
树:采用C语言实现 动态数组dyArray:采用C语言实现 hashMap,采用链表实现hash 拼图算法:采用二叉树结果拼图算法
linux 下 c语言实现的 哈希表,稳定速度快效率
memcached是一个用C语言开发的分布式的缓存,内部基于类似hashMap的结构。它的优点是协议简单,内置内存存储,并且他的分布式算法是在客户端完成的,不需要服务器端进行通信,我们当时在做项目的时候因为考虑到项目...
主要采用C语言实现 leetcode leetcode是题目解法,按照题目的编号进行命名,文件开头注明了题目名称和链接。 algorthms algorthms是一些算法和数据结构的练习 interview interview是我在面试中遇到的一些算法题 ...
给定一个C语言函数,要求实现在java类中进行调用。 45.如何获得数组的长度? 46.访问修饰符“public/private/protected/缺省的修饰符”的使用 47.用关键字final修饰一个类或者方法时,有何意义? 48.掌握类和...
聊天程序可实现用户上线自动添加功能,客户端可以互发信息
HashMap是Map的实现,其中键被映射到值。 HashSet是Set的实现,其中每个元素都是唯一值。 测验 要运行RSpecs : 叉子/这个仓库 在项目目录中运行bundle install 在目录中(在lib和spec ),运行rspec spec/{file...
CSON 请参阅lite分支以获取简单的hashmap实现