coll  0.1.0-alpha.1
A collections library
Data Structures | Macros | Typedefs | Functions
coll_arraylist_map.h File Reference
#include "coll_arraylist.h"
#include <stdbool.h>

Go to the source code of this file.

Data Structures

struct  coll_al_map_t
 

Macros

#define COLL_AL_MAP_DEFAULT_CAPACITY   25
 
#define E_COLL_AL_MAP_ALLOC_FAILED   10
 
#define E_COLL_AL_MAP_NULL   11
 
#define E_COLL_AL_MAP_ALREADY_EXISTS   12
 
#define E_COLL_AL_MAP_KEY_NULL   13
 
#define E_COLL_AL_MAP_COMPARE_FN_NULL   15
 
#define E_COLL_AL_MAP_SUCCESS   0
 
#define coll_al_map_keys(map)   map->keys
 
#define coll_al_map_keys_length(map)   arraylist_length(coll_al_map_keys(map))
 
#define coll_al_map_keys_get_idx(map, i)   arraylist_get(coll_al_map_keys(map), i)
 
#define coll_al_map_values(map)   map->values
 
#define coll_al_map_values_length(map)   arraylist_length(coll_al_map_values(map))
 
#define coll_al_map_values_get_idx(map, i)   arraylist_get(coll_al_map_values(map), i)
 

Typedefs

typedef int() coll_al_map_compare_fn(void *first, void *second)
 
typedef void() coll_al_map_iter_fn(size_t index, void *key, void *value)
 
typedef struct coll_al_map_t coll_al_map
 

Functions

coll_al_mapmake_coll_al_map (coll_al_map_compare_fn *compare)
 
coll_al_mapmake_coll_al_map_w_capacity (coll_al_map_compare_fn *compare, size_t initial_capacity)
 
int coll_al_map_put (coll_al_map *map, void *key, void *val)
 
void * coll_al_map_get (coll_al_map *map, void *key)
 
bool coll_al_map_remove (coll_al_map *map, void *key, coll_al_map_iter_fn *iter_fn)
 
bool coll_al_map_has (coll_al_map *map, void *key)
 
void free_coll_al_map (coll_al_map *map)
 
void coll_al_map_foreach_fn (coll_al_map *map, coll_al_map_iter_fn *iter_fn)
 

Detailed Description

AL_MAP: This is a datastructure which provides a naive implementation for a Map/Dictionary, backed by the ArrayList datastructure implemented in the same library.

Function Documentation

◆ coll_al_map_foreach_fn()

void coll_al_map_foreach_fn ( coll_al_map map,
coll_al_map_iter_fn *  iter_fn 
)

Iterate over the key/value pairs of the map.

Parameters
mapthe map
iter_fnthe function which is called per index, key, value

◆ coll_al_map_get()

void* coll_al_map_get ( coll_al_map map,
void *  key 
)

Get the value associated with the key in the map Returns NULL if value is NULL or not found.

Parameters
mapthe map
keythe key
Returns
the value

◆ coll_al_map_has()

bool coll_al_map_has ( coll_al_map map,
void *  key 
)

Return a flag indicating whether the given key is in the map.

Parameters
mapthe map
keythe key
Returns
flag indicating whether key is found.

◆ coll_al_map_put()

int coll_al_map_put ( coll_al_map map,
void *  key,
void *  val 
)

Put a new key value pair in the map. Returns E_COLL_AL_MAP_ALREADY_EXISTS if the key exists. Returns E_COLL_AL_MAP_KEY_NULL if key is NULL. Returns E_COLL_AL_MAP_ALLOC_FAILED if allocation failed. Returns E_COLL_AL_MAP_SUCCESS (0) on success.

Parameters
mapthe map to insert the value.
keythe key
valuethe value
Returns
error code

◆ coll_al_map_remove()

bool coll_al_map_remove ( coll_al_map map,
void *  key,
coll_al_map_iter_fn *  iter_fn 
)

Remove the key/value pair identified by the key. The iter_fn if provided is called with index, key and value, just before the delete. This allows the caller to perform an operation such as free on the items being deleted.

The items are not freed when they are deleted from the arraylist.

Parameters
mapthe map
keythe key
Returns
flag indicating whether key is found.

◆ free_coll_al_map()

void free_coll_al_map ( coll_al_map map)

Free the map. The keys and values should be freed before freeing the map.

Parameters
mapthe map

◆ make_coll_al_map()

coll_al_map* make_coll_al_map ( coll_al_map_compare_fn *  compare)

Create a new map based on arraylist, with default initial capacity.

Returns NULL if unable to allocate the map.

Parameters
comparefunction to compare two keys
Returns
arraylist map

◆ make_coll_al_map_w_capacity()

coll_al_map* make_coll_al_map_w_capacity ( coll_al_map_compare_fn *  compare,
size_t  initial_capacity 
)

Create a new map based on arraylist, with given initial capacity.

Returns NULL if unable to allocate the map.

Parameters
comparefunction to compare two keys
initial_capacityinitial capacity of the keys and values lists
Returns
arraylist map