1 /* copyright 2016 Apache 2 sddekit authors */
5 static int compare_int(const void *a
, const void *b
)
7 if ( *(uint32_t*) a
< *(uint32_t*) b
) return -1;
8 if ( *(uint32_t*) a
== *(uint32_t*) b
) return 0;
9 else /* a > b */ return 1;
12 sd_stat
sd_util_uniqi(
14 uint32_t * restrict ints
,
15 uint32_t * restrict nuniq
,
18 uint32_t i
, j
, *ints_copy
;
20 if (n
==0) return SD_OK
;
24 if ((*uints
= sd_malloc (sizeof(uint32_t)))==NULL
) {
25 sd_err("failed to allocate memory for unique integers");
28 (*uints
)[0] = ints
[0];
32 /* sort copy of input vector */
33 if ((ints_copy
= sd_malloc(sizeof(uint32_t) * n
))==NULL
) {
34 sd_err("failed to allocate memory for ints copy.");
37 memcpy(ints_copy
, ints
, n
*sizeof(uint32_t));
39 qsort(ints_copy
, n
, sizeof(uint32_t), compare_int
);
43 for (i
=0; i
<(n
-1); i
++)
44 if (ints_copy
[i
] != ints_copy
[i
+1])
47 if ((*uints
= sd_malloc (sizeof(uint32_t) * *nuniq
))==NULL
) {
48 sd_err("failed to allocate memory for unique integers.");
53 /* copy unique into output array */
55 (*uints
)[j
++] = ints_copy
[0];
56 for (i
=0; i
<(n
-1); i
++)
57 if (ints_copy
[i
] != ints_copy
[i
+1])
58 (*uints
)[j
++] = ints_copy
[i
+1];
65 sd_stat
sd_util_read_square_matrix(const char *fname
, uint32_t *n
, double **w
)
71 fd
= fopen(fname
, "r");
73 sd_err("failed to open file");
76 /* count number of readable elements */
81 count
= fscanf(fd
, "%lg", &_
);
89 *n
= (uint32_t) sqrt(nn
);
90 wi
= sd_malloc (sizeof(double)*nn
);
96 /* read data into memory this time */
100 count
= fscanf(fd
, "%lg", wi
++);