ARB
spreadin.c
Go to the documentation of this file.
1 #define __MODUL__
2 #include "spreadin.h"
3 #include <stdlib.h>
4 
5 Spreading newSpreading(double *value, int values) {
6  Spreading s;
7  int v;
8  double val = 0.0,
9  sum = 0.0;
10 
11  s = malloc(sizeof(struct S_Spreading));
12  if (!s) outOfMemory();
13 
14  assert(values>0);
15 
16  s->values = values;
17  s->border = malloc(sizeof(*(s->border))*values);
18  if (!s->border) outOfMemory();
19 
20  for (v = 0; v<values; v++) sum += value[v]; // Summe bilden
21 
22  for (v = 0; v<values; v++) {
23  val += value[v];
24  s->border[v] = (val/sum)*RAND_MAX;
25  }
26 
27  s->border[values-1] = RAND_MAX;
28 
29  return s;
30 }
32  free(s->border);
33  free(s);
34 }
36  int val = rand(),
37  l = 0,
38  h = s->values-1,
39  m;
40 
41  while (1) {
42  m = (l+h)/2;
43 
44  if (val<=s->border[m]) {
45  if (m==0 || val>s->border[m-1]) break;
46  h = m;
47  }
48  else {
49  if (m==(h-1)) {
50  if (val<=s->border[h]) {
51  m = h;
52  break;
53  }
54  }
55 
56  l = m;
57  }
58  }
59 
60  assert(m>=0 && m<s->values);
61  assert(val<=s->border[m]);
62  assert(m==0 || val>=s->border[m-1]);
63 
64  return m;
65 }
66 
67 
void freeSpreading(Spreading s)
Definition: spreadin.c:31
int * border
Definition: spreadin.h:20
Spreading newSpreading(double *value, int values)
Definition: spreadin.c:5
int spreadRand(Spreading s)
Definition: spreadin.c:35
#define assert(bed)
#define outOfMemory()
Definition: defines.h:14
int values
Definition: spreadin.h:20
GB_write_int const char s
Definition: AW_awar.cxx:156