1 #include "cado.h" // IWYU pragma: keep
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include "macros.h"
5 #include "decomp.h"
6
decomp_create(int len,int * tab,double nb_elem)7 decomp_t *decomp_create(int len, int *tab, double nb_elem)
8 {
9 decomp_t *t = malloc(sizeof(*t));
10 ASSERT_ALWAYS(t != NULL);
11 t->len = len;
12 t->tab = malloc(t->len * sizeof(int));
13 ASSERT_ALWAYS(t->tab != NULL);
14 for (int i = 0; i < t->len; i++)
15 t->tab[i] = tab[i];
16 t->nb_elem = nb_elem;
17 return t;
18 }
19
decomp_free(decomp_t * t)20 void decomp_free(decomp_t * t)
21 {
22 if (t != NULL)
23 {
24 free(t->tab);
25 free(t);
26 }
27 }
28
decomp_get_nb_elem(decomp_t * t)29 double decomp_get_nb_elem(decomp_t * t)
30 {
31 return t->nb_elem;
32 }
33
decomp_get_tab(decomp_t * t)34 int *decomp_get_tab(decomp_t * t)
35 {
36 return t->tab;
37 }
38
decomp_get_len(decomp_t * t)39 int decomp_get_len(decomp_t * t)
40 {
41 return t->len;
42 }
43
decomp_set_decomp(decomp_t * t,int * tab,int len)44 void decomp_set_decomp(decomp_t * t, int *tab, int len)
45 {
46 t->len = len;
47 t->tab = realloc (t->tab, t->len * sizeof(int));
48 for (int i = 0; i < t->len; i++)
49 t->tab[i] = tab[i];
50 }
51
decomp_set_nb_elem(decomp_t * t,double nb_elem)52 void decomp_set_nb_elem(decomp_t * t, double nb_elem)
53 {
54 t->nb_elem = nb_elem;
55 }
56
decomp_fprint(FILE * output_file,decomp_t * t)57 int decomp_fprint(FILE * output_file, decomp_t * t)
58 {
59 if (output_file == NULL)
60 return -1;
61 if (t == NULL)
62 return -2;
63 fprintf(output_file, "[ ");
64 for (int l = 0; l < t->len - 1; l++)
65 fprintf(output_file, "%d, ", t->tab[l]);
66 fprintf(output_file, "%d ] : %f\n", t->tab[t->len - 1], t->nb_elem);
67 return 0;
68 }
69
decomp_print(decomp_t * t)70 int decomp_print(decomp_t * t)
71 {
72 return decomp_fprint(stdout, t);
73 }
74