# core.c Core functions of the map feature. Handles initialization, addition, freeing, and growing of the map structure. --- ## Includes ```c #include #include "map.h" #include "internal/map_internal.h" ``` --- ## Function Descriptions ### `map_init` Initializes the map structure. Every map structure must be initialized before use. They also must be freed with `map_free` when no longer needed. #### Parameters - `map`: Pointer to the map structure to initialize. ##### Implementation ```c void map_init(t_map *map) { map->ranges = NULL; map->count = 0; map->capacity = 0; } ``` ### `map_add` Adds a new range to the map structure. #### Parameters - `map`: Pointer to the map structure. - `src_start`: Start of the source range. - `src_end`: End of the source range. - `dst_start`: Start of the destination range. - `dst_end`: End of the destination range. #### Implementation ```c void map_add(t_map *map, uint32_t src_start, uint32_t src_end, uint32_t dst_start, uint32_t dst_end) { t_range *range; if (map->count >= map->capacity) { if (0 != map_grow(map)) return ; } range = &map->ranges[map->count]; range->src_start = src_start; range->src_end = src_end; range->dst_start = dst_start; range->dst_end = dst_end; map->count++; } ``` ### `map_free` Frees the resources allocated with the map structure. #### Parameters - `map`: Pointer to the map structure to free. #### Implementation ```c void map_free(t_map *map) { free(map->ranges); map->ranges = NULL; map->count = 0; map->capacity = 0; } ``` ### `map_grow` Grows the capacity of the map structure. Multiplies the current capacity by 2, or sets it to an initial value if it's zero. #### Parameters - `map`: Pointer to the map structure to grow. #### Return Value Returns 0 on success, or 1 on memory allocation failure. #### Implementation ```c int8_t map_grow(t_map *map) { size_t new_cap; t_range *new_ranges; new_cap = (0 == map->capacity) ? MAP_INIT_CAP : map->capacity * 2; new_ranges = realloc(map->ranges, new_cap * sizeof(t_range)); if (NULL == new_ranges) return (1); map->ranges = new_ranges; map->capacity = new_cap; return (0); } ```