1 #include "cado.h" // IWYU pragma: keep
2 #include "tab_point.h"
3 #include "macros.h"
4 
5 #include <stdlib.h>
6 
tabular_point_create(void)7 tabular_point_t *tabular_point_create(void)
8 {
9     tabular_point_t *t = malloc(sizeof(*t));
10     ASSERT(t != NULL);
11 
12     t->index = 0;
13     t->size = 2;
14 
15     t->tab = malloc(t->size * sizeof(point_t *));
16     ASSERT(t->tab != NULL);
17 
18     return t;
19 }
20 
tabular_point_free(tabular_point_t * t)21 void tabular_point_free(tabular_point_t * t)
22 {
23     for (int i = 0; i < t->index; i++)	//size
24 	point_free(t->tab[i]);
25     free(t->tab);
26     free(t);
27 }
28 
tabular_point_realloc(tabular_point_t * t)29 void tabular_point_realloc(tabular_point_t * t)
30 {
31     t->tab = realloc(t->tab, t->size * 2 * (sizeof(point_t *)));
32     ASSERT(t->tab != NULL);
33     t->size *= 2;
34 }
35 
tabular_point_get_index(tabular_point_t * t)36 int tabular_point_get_index(tabular_point_t * t)
37 {
38     return t->index;
39 }
40 
tabular_point_get_point(tabular_point_t * t,int index)41 point_t *tabular_point_get_point(tabular_point_t * t, int index)
42 {
43     ASSERT(index < t->index);
44     return t->tab[index];
45 }
46 
tabular_point_add_point(tabular_point_t * t,point_t * pt)47 void tabular_point_add_point(tabular_point_t * t, point_t * pt)
48 {
49     tabular_point_add(t, pt->number, pt->x, pt->y);
50 }
51 
tabular_point_add(tabular_point_t * t,int numero,double x,double y)52 void tabular_point_add(tabular_point_t * t, int numero, double x, double y)
53 {
54     if (t->index >= t->size)
55 	tabular_point_realloc(t);
56     t->tab[t->index] = point_create(numero, x, y);
57     ASSERT(t->tab[t->index] != NULL);
58     t->index++;
59 }
60