-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathutils.c
More file actions
172 lines (147 loc) · 3.04 KB
/
utils.c
File metadata and controls
172 lines (147 loc) · 3.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/********************************************************************
$RCSfile: utils.c,v $
$Author: alexvk $
$Revision: 1.1 $
$Date: 1997/10/15 02:52:53 $
********************************************************************/
static char rcsid[] = "$Id: utils.c,v 1.1 1997/10/15 02:52:53 alexvk Exp alexvk $";
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <ctype.h>
#include <stdarg.h>
#include "utils.h"
int DbgFlag = true;
size_t TotalMem = 0;
size_t TotalMemGet() { return TotalMem; }
void ErrorFatal(const char* name, const char* fmt, ...)
{
va_list args;
fprintf(stderr, "ERROR in %s: ", name);
va_start(args, fmt);
vfprintf(stderr, fmt, args);
va_end(args);
exit(-1);
}
void ErrorSumOne(str, sum)
char *str;
VECTOR sum;
{
if((ABS(sum - 1)) > 0.0001) {
fprintf(stderr, "%s: WARNING probabilities sum to %8.6f instead of 1\n", str, sum);
}
}
void *a_calloc(n, size)
size_t n;
size_t size;
{
void *temp = calloc(n,size);
if (temp == NULL && n != 0) {
ErrorFatal("a_calloc",
"Not enough memory (wanted %ld x %ld).\n", n, size);
}
TotalMem += (n * size);
return temp;
}
char *strlwr(s)
char *s;
{
while (*s) {
*s++ = tolower(*s);
}
}
void SetDump(set, len)
int *set;
int len;
{
int i;
printf("[ ");
for (i = 0; i < len; i++)
printf("%d ",set[i]);
printf("]\n");
}
void VectorDump(set, len)
VECTOR *set;
size_t len;
{
int i;
printf("[ ");
for (i = 0; i < len; i++)
printf("%6.4lf ", set[i]);
printf("]\n");
}
void VectorSum(set, len, result)
VECTOR *set;
size_t len;
VECTOR *result;
{
size_t i;
VECTOR sum = 0;
for(i = 0; i < len; i++) {
sum += set[i];
}
*result = sum;
}
void VectorNormalize(set, len)
VECTOR *set;
size_t len;
{
int i;
VECTOR sum = 0;
for(i = 0; i < len; sum += set[i++]);
for(i = 0; i < len; set[i++] /= sum);
}
void OdometerInitialize(odometer, nodeSizes, length)
long *odometer;
int *nodeSizes;
int length;
{
int i;
odometer[length - 1] = 1;
for (i = length - 2; i >= 0; i--)
odometer[i] = odometer[i+1] * nodeSizes[i+1];
}
#define A 16807
#define M 2147483647
#define Q 127773
#define R 2836
static long _randseed;
void RandomZeroOne(prob)
double *prob;
{
long test;
test = A * (_randseed % Q) - R * (_randseed / Q);
_randseed = (test > 0 ? test : test + M);
*prob = (double) ((double) _randseed / (double) M);
}
/* Get a seed number */
void Randomize()
{
time(&_randseed);
/* printf("randseed: %ld\n", _randseed); */
}
/* Pick a random integer between low and high */
int RandomLowHigh(low, high)
int low;
int high;
{
register int i;
if (low >= high)
i = low;
else {
double temp;
RandomZeroOne(&temp);
i = (int) (temp * (high - low + 1) + low);
if (i > high)
i = high;
}
return(i);
}
/* Flip a biased coin -- return 0 or 1 */
int CoinFlip(prob)
double prob;
{
double temp;
RandomZeroOne(&temp);
return((prob == 1.0 || temp <= prob));
}