add em scheme & exc sys
[sddekit.git] / sk_util.c
blob02756147e1114954ce766534ece01577ec209918
1 /* Apache 2.0 INS-AMU 2015 */
3 #include "stdlib.h"
4 #include "string.h"
5 #include "limits.h"
7 #include "sk_util.h"
9 static int compare_int(const void *a, const void *b)
11 if ( *(int*) a < *(int*) b ) return -1;
12 if ( *(int*) a == *(int*) b ) return 0;
13 else /* a > b */ return 1;
16 int sk_util_fill_gauss(rk_state *rng, int nx, double *x)
18 int i;
20 for (i=0; i<nx; i++)
21 x[i] = rk_gauss(rng);
23 return 0;
26 int sk_util_uniqi(const int n, const int *ints, int *nuniq, int **uints)
28 int i, j, *ints_copy;
30 if (n==0) return 0;
32 /* sort copy of input vector */
33 ints_copy = (int*) malloc(sizeof(int) * n);
34 memcpy(ints_copy, ints, n*sizeof(int));
36 if (n==1) return 0;
38 qsort(ints_copy, n, sizeof(int), compare_int);
40 /* count uniq */
41 *nuniq = 1;
42 for (i=0; i<(n-1); i++)
43 if (ints_copy[i] != ints_copy[i+1])
44 (*nuniq)++;
46 *uints = (int*) malloc (sizeof(int) * *nuniq);
48 /* copy unique into output array */
49 j = 0;
50 (*uints)[j++] = ints_copy[0];
51 for (i=0; i<(n-1); i++)
52 if (ints_copy[i] != ints_copy[i+1])
53 (*uints)[j++] = ints_copy[i+1];
55 free(ints_copy);
57 return 0;