cn_map / cn_map_init

Syntax

CN_MAP cn_map_init(datatype key_type, datatype value_type, CNC_COMP (*compare_func)(void *, void *))

Description

Initialises the CN_Map and returns a pointer to it. This instance is created via malloc and must be freed by using cn_map_free.

To create a CN_Map, you must specify the datatypes for the keys (key_type) and values (value_type). You must also specify compare_func, a comparison function, so the CN_Map will know how to sort and adjust its internal structure accordingly upon inserting new nodes. This sorting function should return -1 or lower if a value is "less than" another, 0 if equal, and 1 or greater if the item is "greater than" another. For compatibility, we recommend you use the CNC_COMP (int) datatype when making custom comparison functions.

This is the macro variant of new_cn_map that takes types as opposed to sizeof arguments. Since it is much more readable, this is the preferred way to initialise a CN_Map object.

Return Value

Will return a pointer to a CN_MAP on success. Returns NULL otherwise.

Examples

Basic Usage

#include <stdio.h>
#include <stdlib.h>

#include "cn_cmp.h"
#include "cn_map.h"

main() {
	//Create a new cn_map
	CN_MAP map = cn_map_init(int, int, cn_cmp_int);

	//Free the CN_Map
	cn_map_free(map);
}