cn_map / cn_map_at_begin

Syntax

CNM_BYTE cn_map_at_begin(CN_MAP map, CNM_ITERATOR * iterator)

Description

Checks if iterator is at the first node (lowest value) in the CN_Map map. Returns 1 if true. Returns 0 otherwise.

Return Value

Returns a unsigned char (typedef'd to cnm_byte).

Examples

Basic Usage

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

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

void print_first_or_last(CN_MAP map, CNM_ITERATOR it) {
	/*
	 * Print if we are at first or last node:
	 * 00 - Neither
	 * 01 - Last
	 * 10 - First
	 */
	printf(
		"POSITION: %d%d\n",
		cn_map_at_begin(map, &it),
		cn_map_at_rbegin(map, &it)
	);

	//Print whatever is at the iterator as well
	printf(
		"%c -> %d\n",
		cn_map_iterator_key  (&it, char),
		cn_map_iterator_value(&it, int)
	);
}

main() {
	CN_MAP map = cn_map_init(char, int, cn_cmp_char);

	char key;
	int  value;

	//Put in a-z with 0-25 in as key/value pairs.
	for (key = 'a', value = 0; key <= 'z'; key++, value++) {
		cn_map_insert(map, &key, &value);
	}

	CNM_ITERATOR it;

	//Set it to the first key in the CN_Map
	cn_map_begin(map, &it);

	//Are we at the first or last?
	print_first_or_last(map, it);

	//Set it to the last key in the CN_Map
	cn_map_rbegin(map, &it);

	//Check again.
	print_first_or_last(map, it);

	cn_map_free(map);
}