automatic commit
[folded-ctf.git] / misc.h
1 /*
2  *  folded-ctf is an implementation of the folded hierarchy of
3  *  classifiers for object detection, developed by Francois Fleuret
4  *  and Donald Geman.
5  *
6  *  Copyright (c) 2008 Idiap Research Institute, http://www.idiap.ch/
7  *  Written by Francois Fleuret <francois.fleuret@idiap.ch>
8  *
9  *  This file is part of folded-ctf.
10  *
11  *  folded-ctf is free software: you can redistribute it and/or modify
12  *  it under the terms of the GNU General Public License as published
13  *  by the Free Software Foundation, either version 3 of the License,
14  *  or (at your option) any later version.
15  *
16  *  folded-ctf is distributed in the hope that it will be useful, but
17  *  WITHOUT ANY WARRANTY; without even the implied warranty of
18  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19  *  General Public License for more details.
20  *
21  *  You should have received a copy of the GNU General Public License
22  *  along with folded-ctf.  If not, see <http://www.gnu.org/licenses/>.
23  *
24  */
25
26 /*
27
28   A bunch of useful little functions.
29
30  */
31
32 #ifndef MISC_H
33 #define MISC_H
34
35 #include <iostream>
36 #include <cmath>
37 #include <fstream>
38 #include <cfloat>
39 #include <stdlib.h>
40 #include <string.h>
41
42 using namespace std;
43
44 //typedef double scalar_t;
45 typedef float scalar_t;
46 const scalar_t SCALAR_MAX = FLT_MAX;
47 const scalar_t SCALAR_MIN = FLT_MIN;
48
49 const int buffer_size = 1024;
50 const int large_buffer_size = 65536;
51
52 using namespace std;
53
54 #ifdef DEBUG
55 #define ASSERT(x) if(!(x)) { \
56   std::cerr << "ASSERT FAILED IN " << __FILE__ << ":" << __LINE__ << endl; \
57   abort(); \
58 }
59 #else
60 #define ASSERT(x)
61 #endif
62
63 template<class T>
64 T smooth_min(T x, T y) {
65   T z = exp(x - y);
66   return 0.5 * (x + y - (x - y)/(1 + 1/z) - (y - x)/(1 + z));
67 }
68
69 template <class T>
70 void write_var(ostream *os, const T *x) { os->write((char *) x, sizeof(T)); }
71
72 template <class T>
73 void read_var(istream *is, T *x) { is->read((char *) x, sizeof(T)); }
74
75 template <class T>
76 void grow(int *nb_max, int nb, T** current, int factor) {
77   ASSERT(*nb_max > 0);
78   if(nb == *nb_max) {
79     T *tmp = new T[*nb_max * factor];
80     memcpy(tmp, *current, *nb_max * sizeof(T));
81     delete[] *current;
82     *current = tmp;
83     *nb_max *= factor;
84   }
85 }
86
87 template <class T>
88 inline T sq(T x) {
89   return x * x;
90 }
91
92 inline scalar_t log2(scalar_t x) {
93   return log(x)/log(2.0);
94 }
95
96 inline scalar_t xi(scalar_t x) {
97   if(x <= 0.0) return 0.0;
98   else         return - x * log(x)/log(2.0);
99 }
100
101 scalar_t discrete_entropy(int *n, int nb);
102
103 char *basename(char *name);
104
105 char *next_word(char *buffer, char *r, int buffer_size);
106
107 void random_permutation(int *val, int nb);
108 void tag_subset(bool *val, int nb_total, int nb_to_tag);
109
110 struct Couple {
111   int index;
112   scalar_t value;
113 };
114
115 int compare_couple(const void *a, const void *b);
116
117 //  size       total program size
118 //  resident   resident set size
119 //  share      shared pages
120 //  text       text (code)
121 //  lib        library
122 //  data       data/stack
123 //  dt         dirty pages (unused in Linux 2.6)
124
125 void used_memory(size_t &size, size_t &resident,
126                  size_t &share, size_t &text, size_t &lib, size_t &data,
127                  size_t &dt);
128
129 #endif