libdatatypes 0.3.2
Abstract datatypes for C.
Loading...
Searching...
No Matches
assocarray.c File Reference

Generic associative array. More...

#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
#include <assert.h>
#include <string.h>
#include <limits.h>
#include "assocarray.h"

Go to the source code of this file.

Functions

AssocArrayassoc_array_new (CompareFunc compare_keys, FreeFunc free_key, FreeFunc free_value)
 
void assoc_array_init (AssocArray *array, CompareFunc compare_keys, FreeFunc free_key, FreeFunc free_value)
 
void assoc_array_destroy (AssocArray *array)
 
void assoc_array_free (AssocArray *array)
 
void assoc_array_clear (AssocArray *array)
 
AssocArrayInsertResult assoc_array_set (AssocArray *array, void *key, void *value, bool overwrite_key)
 
void assoc_array_remove (AssocArray *array, const void *key)
 
AssocArrayPairassoc_array_lookup (AssocArray *array, const void *key)
 
void * assoc_array_pair_get_key (const AssocArrayPair *pair)
 
void * assoc_array_pair_get_value (const AssocArrayPair *pair)
 
void assoc_array_pair_set_value (AssocArrayPair *pair, void *value)
 
bool assoc_array_key_exists (const AssocArray *array, const void *key)
 
size_t assoc_array_count (const AssocArray *array)
 
size_t assoc_array_size (const AssocArray *array)
 
void assoc_array_iter_init (const AssocArray *array, AssocArrayIter *iter)
 
bool assoc_array_iter_next (AssocArrayIter *iter)
 
void * assoc_array_iter_get_key (const AssocArrayIter *iter)
 
void * assoc_array_iter_get_value (const AssocArrayIter *iter)
 

Detailed Description

Generic associative array.

Author
Sebastian Fedrau sebas.nosp@m.tian.nosp@m..fedr.nosp@m.au@g.nosp@m.mail..nosp@m.com

Definition in file assocarray.c.

Macro Definition Documentation

◆ _GNU_SOURCE

#define _GNU_SOURCE

Definition at line 22 of file assocarray.c.

Function Documentation

◆ assoc_array_clear()

void assoc_array_clear ( AssocArray array)
Parameters
arrayan AssocArray

Removes all elements from the AssocArray.

Definition at line 170 of file assocarray.c.

◆ assoc_array_count()

size_t assoc_array_count ( const AssocArray array)
Parameters
arrayan AssocArray
Returns
number of stored elements

Gets the number of stored elements.

Definition at line 437 of file assocarray.c.

◆ assoc_array_destroy()

void assoc_array_destroy ( AssocArray array)
Parameters
arrayan AssocArray

Frees all keys, values and the array pointer.

Definition at line 128 of file assocarray.c.

◆ assoc_array_free()

void assoc_array_free ( AssocArray array)
Parameters
arrayan AssocArray

Frees all keys and values without freeing the array pointer.

Definition at line 159 of file assocarray.c.

◆ assoc_array_init()

void assoc_array_init ( AssocArray array,
CompareFunc  compare_keys,
FreeFunc  free_key,
FreeFunc  free_value 
)
Parameters
arrayan AssocArray
compare_keysfunction to compare two keys
free_keyfunction to free keys or NULL
free_valuefunction to free values or NULL

Initializes an AssocArray.

Definition at line 97 of file assocarray.c.

◆ assoc_array_iter_get_key()

void * assoc_array_iter_get_key ( const AssocArrayIter iter)
Parameters
iteran AssocArrayIter
Returns
key of current element

Retrieves the key of the current element.

Definition at line 473 of file assocarray.c.

◆ assoc_array_iter_get_value()

void * assoc_array_iter_get_value ( const AssocArrayIter iter)
Parameters
iteran AssocArrayIter
Returns
value of current element

Retrieves the value of the current element.

Definition at line 481 of file assocarray.c.

◆ assoc_array_iter_init()

void assoc_array_iter_init ( const AssocArray array,
AssocArrayIter iter 
)
Parameters
arrayan AssocArray
iteran uninitialized AssocArrayIter

Initializes a key/value pair iterator and associates it with the array. Modifying the array while using the iterator might lead to undefined behaviour.

Definition at line 453 of file assocarray.c.

◆ assoc_array_iter_next()

bool assoc_array_iter_next ( AssocArrayIter iter)
Parameters
iteran AssocArrayIter
Returns
false if end of the AssocArray has been reached

Goes to next element of an AssocArray.

Definition at line 463 of file assocarray.c.

◆ assoc_array_key_exists()

bool assoc_array_key_exists ( const AssocArray array,
const void *  key 
)
Parameters
arrayan AssocArray
keykey to test
Returns
true if given key does exist

Checks if a key does exist.

Definition at line 421 of file assocarray.c.

◆ assoc_array_lookup()

AssocArrayPair * assoc_array_lookup ( AssocArray array,
const void *  key 
)
Parameters
arrayan AssocArray
keykey to lookup
Returns
the found key-value pair or NULL.

Looks up a key-value pair in the AssocArray.

Definition at line 368 of file assocarray.c.

◆ assoc_array_new()

AssocArray * assoc_array_new ( CompareFunc  compare_keys,
FreeFunc  free_key,
FreeFunc  free_value 
)
Parameters
compare_keysfunction to compare two keys
free_keyfunction to free keys or NULL
free_valuefunction to free values or NULL
Returns
a new AssocArray

Creates a new AssocArray.

Definition at line 79 of file assocarray.c.

◆ assoc_array_pair_get_key()

void * assoc_array_pair_get_key ( const AssocArrayPair pair)
Parameters
paira key-value pair
Returns
key of the pair

Retrieves the key of a key-value pair.

Definition at line 389 of file assocarray.c.

◆ assoc_array_pair_get_value()

void * assoc_array_pair_get_value ( const AssocArrayPair pair)
Parameters
paira key-value pair
Returns
value of the pair

Retrieves the value of a key-value pair.

Definition at line 398 of file assocarray.c.

◆ assoc_array_pair_set_value()

void assoc_array_pair_set_value ( AssocArrayPair pair,
void *  value 
)
Parameters
paira AssocArrayPair
valuenew value to set

Overwrites the value of a key-value pair.

Definition at line 407 of file assocarray.c.

◆ assoc_array_remove()

void assoc_array_remove ( AssocArray array,
const void *  key 
)
Parameters
arrayan AssocArray
keykey of the element to remove

Removes an element from the AssocArray.

Definition at line 335 of file assocarray.c.

◆ assoc_array_set()

AssocArrayInsertResult assoc_array_set ( AssocArray array,
void *  key,
void *  value,
bool  overwrite_key 
)
Parameters
arrayan AssocArray
keykey to insert
valuethe value to associate with the key
overwrite_keytrue to overwrite already exisiting keys
Returns
type of the performed insert operation

Inserts a new key and value in the AssocArray. If overwrite_key is set an existing key is freed using the specified free_key function before it gets replaced.

Definition at line 288 of file assocarray.c.

◆ assoc_array_size()

size_t assoc_array_size ( const AssocArray array)
Parameters
arrayan AssocArray
Returns
size of the array

Gets the size of the array.

Definition at line 445 of file assocarray.c.