一个简单的 HashMap C 语言实现
/*
* 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);