Skip to content

Code_c_map

lemmiix edited this page Mar 5, 2026 · 5 revisions

c_map documentation

c_map

struct struct_uint_bool {
	unsigned int key;
	bool active;
};

This struct provides the basic members to create a key-value map.
@member key: the identifier of the map
@member active: either TRUE or FALSE value

Usage:

/**
 * Do NOT use this directly
 * This is used inside `map_uint_bool`
*/


---

struct map_uint_bool {
	int size;
	struct struct_uint_bool struct_uint_bool[];
};

This struct represents a map for <uint,bool> values.
@member size: current size of the map
@member struct_uint_bool[]: array of <uint,bool> values

Usage:

/**
 * Do NOT use this directly
 * Use get and set functions instead
*/


---

int map_uint_bool_add(struct map_uint_bool** map, unsigned int key) {
	bool key_contained = false;

	for (int i = 0; i < (*map)->size && !key_contained; ++i) {
		key_contained = (*map)->struct_uint_bool[i].key == key;
	}

	if (!key_contained) {
		struct map_uint_bool* tmp = *map;
		int new_size = (*map)->size +1;
		*map = realloc(*map,
				sizeof(struct map_uint_bool) + new_size * sizeof(struct struct_uint_bool));
		if (*map == NULL) {
			*map = tmp;
			return 1;
		} else {
			(*map)->size = new_size;
			(*map)->struct_uint_bool[new_size - 1].key = key;
			(*map)->struct_uint_bool[new_size - 1].active = false;
		}
	}

	return 0;
}

This adds a new key to the <uint,bool> map and sets bool to FALSE.
@param map: map to which the new key should be added
@param key: SDLK keyboard scan code
@return: '0' if succesful

Usage:

map_uint_bool_add(&map, SDLK_W);


---

int map_uint_bool_remove(struct map_uint_bool** map, unsigned int key) {
	bool key_contained = false;
	int key_idx = 0;

	while (key_idx < (*map)->size && !key_contained) {
		key_contained = (*map)->struct_uint_bool[key_idx].key == key;
		++key_idx;
	}

	if (key_contained) {
		for (int i = key_idx; i < (*map)->size; ++i) {
			(*map)->struct_uint_bool[i - 1].key = (*map)->struct_uint_bool[i].key;
			(*map)->struct_uint_bool[i - 1].active = (*map)->struct_uint_bool[i].active;
			*map = realloc(*map,
					sizeof(struct map_uint_bool) + ((*map)->size - 1) * sizeof(struct struct_uint_bool));
		}
	}

	return 0;
}

This removes a new key from the <uint,bool> map.
@param map: map from which the key should be removed
@param key: SDLK keyboard scan code
@return: '0' if succesful

Usage:

map_uint_bool_remove(&map, SDLK_W);


---

bool map_uint_bool_get_value(struct map_uint_bool** map, unsigned int key) {
	bool key_contained = false;
	bool value = false;

	for (int i = 0; i < (*map)->size && !key_contained; ++i) {
		key_contained = (*map)->struct_uint_bool[i].key == key;

		if (key_contained) {
			value = (*map)->struct_uint_bool[i].active;
		}
	}

	return value;
}

This gets the bool value of the passed key.
@param map: map from which the vallue should be returned
@param key: SDLK keyboard scan code
@return: bool value of the key

Usage:

map_uint_bool_get_value(&map, SDLK_W);


---

int map_uint_bool_set_value(struct map_uint_bool** map, unsigned int key, bool active) {
	bool key_contained = false;

	for (int i = 0; i < (*map)->size && !key_contained; ++i) {
		key_contained = (*map)->struct_uint_bool[i].key == key;

		if (key_contained) {
			(*map)->struct_uint_bool[i].active = active;
		}
	}

	return 0;
}

This sets the active value for a specified key.
@param map: map that should be changed
@param key: SDLK keyboard scan code
@param active: TRUE or FALSE if the key is active or not

Usage:

map_uint_bool_set_value(&map, SDLK_W, TRUE);


---

void free_map_uint_bool(struct map_uint_bool** map) {
	free(*map);
	*map = NULL;
}

This removes all values from the map.
@param map: map whose key/values should be removed

Usage:

free_map_uint_bool(&map);

Quicknav

Go back to wiki home

Code documentation:
doc_code

Architecture documentation:
doc_arch

Project license

Clone this wiki locally