Fixed a stupid typo in the headers.
[svrt.git] / misc.h
1 /*
2  *  svrt is the ``Synthetic Visual Reasoning Test'', an image
3  *  generator for evaluating classification performance of machine
4  *  learning systems, humans and primates.
5  *
6  *  Copyright (c) 2009 Idiap Research Institute, http://www.idiap.ch/
7  *  Written by Francois Fleuret <francois.fleuret@idiap.ch>
8  *
9  *  This file is part of svrt.
10  *
11  *  svrt is free software: you can redistribute it and/or modify it
12  *  under the terms of the GNU General Public License version 3 as
13  *  published by the Free Software Foundation.
14  *
15  *  svrt is distributed in the hope that it will be useful, but
16  *  WITHOUT ANY WARRANTY; without even the implied warranty of
17  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18  *  General Public License for more details.
19  *
20  *  You should have received a copy of the GNU General Public License
21  *  along with svrt.  If not, see <http://www.gnu.org/licenses/>.
22  *
23  */
24
25 #ifndef MISC_H
26 #define MISC_H
27
28 #include <iostream>
29 #include <cmath>
30 #include <fstream>
31 #include <cfloat>
32 #include <stdlib.h>
33 #include <string.h>
34
35 using namespace std;
36
37 typedef double scalar_t;
38 // typedef float scalar_t;
39
40 const int buffer_size = 1024;
41
42 using namespace std;
43
44 #ifdef DEBUG
45 #define ASSERT(x) if(!(x)) { \
46   std::cerr << "ASSERT FAILED IN " << __FILE__ << ":" << __LINE__ << endl; \
47   abort(); \
48 }
49 #else
50 #define ASSERT(x)
51 #endif
52
53 template<class T>
54 T **allocate_array(int a, int b) {
55   T *tmp = new T[a * b];
56   T **array = new T *[a];
57   for(int k = 0; k < a; k++) {
58     array[k] = tmp;
59     tmp += b;
60   }
61   return array;
62 }
63
64 template<class T>
65 void deallocate_array(T **array) {
66   delete[] array[0];
67   delete[] array;
68 }
69
70 template <class T>
71 void write_var(ostream *os, const T *x) { os->write((char *) x, sizeof(T)); }
72
73 template <class T>
74 void read_var(istream *is, T *x) { is->read((char *) x, sizeof(T)); }
75
76 template <class T>
77 inline T sq(T x) {
78   return x * x;
79 }
80
81 inline scalar_t log2(scalar_t x) {
82   return log(x)/log(2.0);
83 }
84
85 template <class T>
86 void grow(int *nb_max, int nb, T** current, int factor) {
87   ASSERT(*nb_max > 0);
88   if(nb == *nb_max) {
89     T *tmp = new T[*nb_max * factor];
90     memcpy(tmp, *current, *nb_max * sizeof(T));
91     delete[] *current;
92     *current = tmp;
93     *nb_max *= factor;
94   }
95 }
96
97 #endif