c-md/srcs/map/core.c
2026-01-12 00:48:09 +01:00

55 lines
951 B
C

#include <stdlib.h>
#include "map.h"
#include "internal/map_internal.h"
void
map_init(t_map *map)
{
map->ranges = NULL;
map->count = 0;
map->capacity = 0;
}
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++;
}
void
map_free(t_map *map)
{
free(map->ranges);
map->ranges = NULL;
map->count = 0;
map->capacity = 0;
}
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);
}