MentOS  0.8.0
The Mentoring Operating System
Classes | Functions
hashmap.c File Reference

Classes

struct  hashmap_entry_t
 Stores information of an entry of the hashmap. More...
 
struct  hashmap_t
 Stores information of a hashmap. More...
 

Functions

static hashmap_t__alloc_hashmap (void)
 Allocates the memory for an hasmap. More...
 
static hashmap_entry_t__alloc_entry (void)
 Allocates the memory for an entry of the hasmap. More...
 
static void __dealloc_entry (hashmap_entry_t *entry)
 Frees the memory of an entry of the hasmap. More...
 
static hashmap_entry_t ** __alloc_entries (unsigned int size)
 Allocates the memory for a number of entries in the hasmap. More...
 
static void __dealloc_entries (hashmap_entry_t **entries)
 Frees the memory of a vector of entries. More...
 
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...
 
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

Function Documentation

◆ __alloc_entries()

static hashmap_entry_t** __alloc_entries ( unsigned int  size)
inlinestatic

Allocates the memory for a number of entries in the hasmap.

Parameters
sizethe number of entries.
Returns
the newly allocated vector of entries in the hashmap.

◆ __alloc_entry()

static hashmap_entry_t* __alloc_entry ( void  )
inlinestatic

Allocates the memory for an entry of the hasmap.

Returns
the newly allocated entry of the hashmap.

◆ __alloc_hashmap()

static hashmap_t* __alloc_hashmap ( void  )
inlinestatic

Allocates the memory for an hasmap.

Returns
the newly allocated hashmap.

◆ __dealloc_entries()

static void __dealloc_entries ( hashmap_entry_t **  entries)
inlinestatic

Frees the memory of a vector of entries.

Parameters
entriesthe vector of entries.

◆ __dealloc_entry()

static void __dealloc_entry ( hashmap_entry_t entry)
inlinestatic

Frees the memory of an entry of the hasmap.

Parameters
entrythe entry we destroy.

◆ 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_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.