MentOS  0.8.0
The Mentoring Operating System
Typedefs | Functions
hashmap.h File Reference

Functions for managing a structure that can map keys to values. More...

Go to the source code of this file.

Typedefs

typedef struct hashmap_entry_t hashmap_entry_t
 Stores information of an entry of the hashmap.
 
typedef struct hashmap_t hashmap_t
 Stores information of a hashmap.
 
typedef unsigned int(* hashmap_hash_t) (const void *key)
 Hashing function, used to generate hash keys.
 
typedef int(* hashmap_comp_t) (const void *a, const void *b)
 Comparison function, used to compare hash keys.
 
typedef void *(* hashmap_dupe_t) (const void *)
 Key duplication function, used to duplicate hash keys.
 
typedef void(* hashmap_free_t) (void *)
 Key deallocation function, used to free the memory occupied by hash keys.
 

Functions

unsigned int hashmap_int_hash (const void *key)
 Transforms an integer key into a hash key. More...
 
int hashmap_int_comp (const void *a, const void *b)
 Compares two integer hash keys. More...
 
unsigned int hashmap_str_hash (const void *key)
 Transforms a string key into a hash key. More...
 
int hashmap_str_comp (const void *a, const void *b)
 Compares two string hash keys. More...
 
void * hashmap_do_not_duplicate (const void *value)
 This function can be passed as hashmap_dupe_t, it does nothing. More...
 
void hashmap_do_not_free (void *value)
 This function can be passed as hashmap_free_t, it does nothing. More...
 
hashmap_thashmap_create (unsigned int size, hashmap_hash_t hash_fun, hashmap_comp_t comp_fun, hashmap_dupe_t dupe_fun, hashmap_free_t key_free_fun)
 User-defined hashmap. More...
 
hashmap_thashmap_create_str (unsigned int size)
 Standard hashmap with keys of type (char *). More...
 
hashmap_thashmap_create_int (unsigned int size)
 Standard hashmap with keys of type (char *). More...
 
void hashmap_free (hashmap_t *map)
 Frees the memory of the hashmap. More...
 
void * hashmap_set (hashmap_t *map, const void *key, void *value)
 Sets the value for the given key in the hashmap map. More...
 
void * hashmap_get (hashmap_t *map, const void *key)
 Access the value for the given key. More...
 
void * hashmap_remove (hashmap_t *map, const void *key)
 Removes the entry with the given key. More...
 
int hashmap_is_empty (hashmap_t *map)
 Checks if the hashmap is empty. More...
 
int hashmap_has (hashmap_t *map, const void *key)
 Checks if the hashmap contains an entry with the given key. More...
 
list_thashmap_keys (hashmap_t *map)
 Provides access to all the keys. More...
 
list_thashmap_values (hashmap_t *map)
 Provides access to all the values. More...
 

Detailed Description

Functions for managing a structure that can map keys to values.

Function Documentation

◆ hashmap_create()

hashmap_t* hashmap_create ( unsigned int  size,
hashmap_hash_t  hash_fun,
hashmap_comp_t  comp_fun,
hashmap_dupe_t  dupe_fun,
hashmap_free_t  key_free_fun 
)

User-defined hashmap.

Parameters
sizeDimension of the hashmap.
hash_funThe hashing function.
comp_funThe hash compare function.
dupe_funThe key duplication function.
key_free_funThe function used to free memory of keys.
Returns
A pointer to the hashmap.

(key_free_fun) : No free function. (val_free_fun) : Standard free function.

◆ hashmap_create_int()

hashmap_t* hashmap_create_int ( unsigned int  size)

Standard hashmap with keys of type (char *).

Parameters
sizeDimension of the hashmap.
Returns
A pointer to the hashmap.

(key_free_fun) : No free function. (val_free_fun) : Standard free function.

◆ hashmap_create_str()

hashmap_t* hashmap_create_str ( unsigned int  size)

Standard hashmap with keys of type (char *).

Parameters
sizeDimension of the hashmap.
Returns
A pointer to the hashmap.

(key_free_fun) : Standard free function. (val_free_fun) : Standard free function.

◆ hashmap_do_not_duplicate()

void* hashmap_do_not_duplicate ( const void *  value)

This function can be passed as hashmap_dupe_t, it does nothing.

Parameters
valueThe value to duplicate.
Returns
The duplicated value.

◆ hashmap_do_not_free()

void hashmap_do_not_free ( void *  value)

This function can be passed as hashmap_free_t, it does nothing.

Parameters
valueThe value to free.

◆ hashmap_free()

void hashmap_free ( hashmap_t map)

Frees the memory of the hashmap.

Parameters
mapA pointer to the hashmap.

◆ hashmap_get()

void* hashmap_get ( hashmap_t map,
const void *  key 
)

Access the value for the given key.

Parameters
mapThe hashmap.
keyThe key of the entry we are searching.
Returns
The value on success, or NULL on failure.

◆ hashmap_has()

int hashmap_has ( hashmap_t map,
const void *  key 
)

Checks if the hashmap contains an entry with the given key.

Parameters
mapThe hashmap.
keyThe key of the entry we are searching.
Returns
1 if the entry is present, 0 otherwise.

◆ hashmap_int_comp()

int hashmap_int_comp ( const void *  a,
const void *  b 
)

Compares two integer hash keys.

Parameters
aThe first hash key.
bThe second hash key.
Returns
Result of the comparison.

◆ hashmap_int_hash()

unsigned int hashmap_int_hash ( const void *  key)

Transforms an integer key into a hash key.

Parameters
keyThe integer key.
Returns
The resulting hash key.

◆ hashmap_is_empty()

int hashmap_is_empty ( hashmap_t map)

Checks if the hashmap is empty.

Parameters
mapThe hashmap.
Returns
1 if empty, 0 otherwise.

◆ hashmap_keys()

list_t* hashmap_keys ( hashmap_t map)

Provides access to all the keys.

Parameters
mapThe hashmap.
Returns
A list with all the keys, remember to destroy the list.

◆ hashmap_remove()

void* hashmap_remove ( hashmap_t map,
const void *  key 
)

Removes the entry with the given key.

Parameters
mapThe hashmap.
keyThe key of the entry we are searching.
Returns
The value on success, or NULL on failure.

◆ hashmap_set()

void* hashmap_set ( hashmap_t map,
const void *  key,
void *  value 
)

Sets the value for the given key in the hashmap map.

Parameters
mapThe hashmap.
keyThe entry key.
valueThe entry value.
Returns
NULL on success, a pointer to an already existing entry if fails.

◆ hashmap_str_comp()

int hashmap_str_comp ( const void *  a,
const void *  b 
)

Compares two string hash keys.

Parameters
aThe first hash key.
bThe second hash key.
Returns
Result of the comparison.

◆ hashmap_str_hash()

unsigned int hashmap_str_hash ( const void *  key)

Transforms a string key into a hash key.

Parameters
keyThe string key.
Returns
The resulting hash key.

◆ hashmap_values()

list_t* hashmap_values ( hashmap_t map)

Provides access to all the values.

Parameters
mapThe hashmap.
Returns
A list with all the values, remember to destroy the list.