-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathutil.h
69 lines (51 loc) · 1.42 KB
/
util.h
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
#ifndef UTIL_H_
#define UTIL_H_
#include <cstdlib> // exit
#define RAND_FLOAT() (static_cast <float> (rand()) / static_cast <float> (RAND_MAX))
/*#define STATCODE(a) a*/
#define STATCODE(a)
void __terminal_error(const char *file, const unsigned line, const char *func, const char *msg) {
printf("[%s:%u:%s] %s\n",file,line,func,msg);
fflush(stdout);
/*std::raise(SIGINT);*/
exit(1);
}
#define terminal_error(errstr) __terminal_error(__FILE__, __LINE__, __func__,errstr)
inline int rand_int (int max_int) { return std::rand()%(max_int);}
inline int rand_int_range (int min_int,int max_int) {
return min_int+(std::rand()%(max_int-min_int+1));}
static void* safe_malloc(size_t n, unsigned int line)
{
void* p = malloc(n);
if (!p)
{
fprintf(stderr, "[%s:%u]Out of memory(%u bytes)\n",
__FILE__, line, (unsigned int)n);
exit(EXIT_FAILURE);
}
return p;
}
#define safemalloc(n) safe_malloc(n, __LINE__)
inline int* rand_int_vector(int max_int,int n) {
int* int_v = (int*) safemalloc(sizeof(int)*n);
for(int i=0;i<n;i++) {
int_v[i] = rand_int(max_int+1);
}
return int_v;
}
int count_lines(FILE *fp) {
int num_lines = 0;
char ch;
fseek(fp, 0L, SEEK_SET);
while(!feof(fp))
{
ch = fgetc(fp);
if(ch == '\n')
{
num_lines++;
}
}
fseek(fp, 0L, SEEK_SET);
return num_lines;
}
#endif