json-gen-c  0.1.5
json-gen-c generate C code for json manipulation
hash_map.h File Reference

A simple hash_map implementation. More...

#include <stddef.h>
#include "utils/sstr.h"
Include dependency graph for hash_map.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

struct  hash_map_entry
 
struct  hash_map
 

Macros

#define HASH_MAP_OK   0
 return code of hash_map functions.
 
#define HASH_MAP_DUPLICATE_KEY   1
 
#define HASH_MAP_ERROR   -1
 

Functions

struct hash_map_entryhash_map_entry_new (void *key, void *value)
 create a new hash_map_entry with key and value. More...
 
void hash_map_entry_free (struct hash_map *map, struct hash_map_entry *entry)
 free a hash_map_entry. the key and value will be freed by key_free_func and value_free_func of map. More...
 
struct hash_maphash_map_new (int bucket_count, unsigned int(*hash_func)(void *), int(*key_cmp_func)(void *, void *), void(*key_free_func)(void *), void(*value_free_func)(void *))
 create a new hash_map. More...
 
void hash_map_free (struct hash_map *map)
 free a hash_map. More...
 
int hash_map_insert (struct hash_map *map, void *key, void *value)
 insert a new entry into hash_map. More...
 
int hash_map_find (struct hash_map *map, void *key, void **value)
 find a entry in hash_map. More...
 
int hash_map_delete (struct hash_map *map, void *key)
 remove a entry from hash_map. More...
 
void hash_map_for_each (struct hash_map *map, void(*fn)(void *key, void *value, void *ptr), void *ptr)
 for each key-value pair in hash_map, call fn(key, value). More...
 
unsigned int hash (const char *data, size_t n)
 calculate the hash value of a key. More...
 
unsigned int sstr_key_hash (void *key)
 
int sstr_key_cmp (void *a, void *b)
 
void sstr_key_free (void *key)
 

Detailed Description

A simple hash_map implementation.

Function Documentation

◆ hash()

unsigned int hash ( const char *  data,
size_t  n 
)

calculate the hash value of a key.

Parameters
datapointer to key buffer.
nthe length of key buffer.
Returns
unsigned int the hash value.

◆ hash_map_delete()

int hash_map_delete ( struct hash_map map,
void *  key 
)

remove a entry from hash_map.

Parameters
mapthe hash_map.
keythe key of the entry.
Returns
int HASH_MAP_OK if found, HASH_MAP_ERROR if not found.
Note
the entry will be free.

◆ hash_map_entry_free()

void hash_map_entry_free ( struct hash_map map,
struct hash_map_entry entry 
)

free a hash_map_entry. the key and value will be freed by key_free_func and value_free_func of map.

Parameters
mapthe hash_map, container map->key_free_func and map->value_free_func.
entrythe entry to be freed.

◆ hash_map_entry_new()

struct hash_map_entry* hash_map_entry_new ( void *  key,
void *  value 
)

create a new hash_map_entry with key and value.

Parameters
keythe key of the entry.
valuethe value of the entry.
Returns
struct hash_map_entry* the new entry.
Return values
NULLmalloc failed.
!NULLthe new entry.

◆ hash_map_find()

int hash_map_find ( struct hash_map map,
void *  key,
void **  value 
)

find a entry in hash_map.

Parameters
mapthe hash_map.
keythe key of the entry.
valuethe value of the entry if found.
Returns
int HASH_MAP_OK if found, HASH_MAP_ERROR if not found.

◆ hash_map_for_each()

void hash_map_for_each ( struct hash_map map,
void(*)(void *key, void *value, void *ptr)  fn,
void *  ptr 
)

for each key-value pair in hash_map, call fn(key, value).

Parameters
mapthe hash_map.
fnthe function to be called.
ptruser data.

◆ hash_map_free()

void hash_map_free ( struct hash_map map)

free a hash_map.

Parameters
mapthe hash_map to be freed.

◆ hash_map_insert()

int hash_map_insert ( struct hash_map map,
void *  key,
void *  value 
)

insert a new entry into hash_map.

Parameters
mapthe hash_map.
keythe key of the entry.
valuethe value of the entry.
Returns
int HASH_MAP_OK if success, HASH_MAP_DUPLICATE_KEY will ignore the entry, HASH_MAP_ERROR if malloc failed.

◆ hash_map_new()

struct hash_map* hash_map_new ( int  bucket_count,
unsigned int(*)(void *)  hash_func,
int(*)(void *, void *)  key_cmp_func,
void(*)(void *)  key_free_func,
void(*)(void *)  value_free_func 
)

create a new hash_map.

Parameters
bucket_countthe number of buckets.
hash_functhe hash function.
key_cmp_functhe key compare function, return 0 if equal, else not 0.
key_free_functhe key free function.
value_free_functhe value free function.
Returns
struct hash_map* the new hash_map.