Removed the definition of basename, which confuses an existing system one.
[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 version 3 as
13  *  published by the Free Software Foundation.
14  *
15  *  folded-ctf 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 folded-ctf.  If not, see <http://www.gnu.org/licenses/>.
22  *
23  */
24
25 /*
26
27   A bunch of useful little functions.
28
29  */
30
31 #ifndef MISC_H
32 #define MISC_H
33
34 #include <iostream>
35 #include <cmath>
36 #include <fstream>
37 #include <cfloat>
38 #include <stdlib.h>
39 #include <string.h>
40
41 using namespace std;
42
43 //typedef double scalar_t;
44 typedef float scalar_t;
45 const scalar_t SCALAR_MAX = FLT_MAX;
46 const scalar_t SCALAR_MIN = FLT_MIN;
47
48 const int buffer_size = 1024;
49 const int large_buffer_size = 65536;
50
51 using namespace std;
52
53 #ifdef DEBUG
54 #define ASSERT(x) if(!(x)) { \
55   std::cerr << "ASSERT FAILED IN " << __FILE__ << ":" << __LINE__ << endl; \
56   abort(); \
57 }
58 #else
59 #define ASSERT(x)
60 #endif
61
62 template<class T>
63 T smooth_min(T x, T y) {
64   T z = exp(x - y);
65   return 0.5 * (x + y - (x - y)/(1 + 1/z) - (y - x)/(1 + z));
66 }
67
68 template <class T>
69 void write_var(ostream *os, const T *x) { os->write((char *) x, sizeof(T)); }
70
71 template <class T>
72 void read_var(istream *is, T *x) { is->read((char *) x, sizeof(T)); }
73
74 template <class T>
75 void grow(int *nb_max, int nb, T** current, int factor) {
76   ASSERT(*nb_max > 0);
77   if(nb == *nb_max) {
78     T *tmp = new T[*nb_max * factor];
79     memcpy(tmp, *current, *nb_max * sizeof(T));
80     delete[] *current;
81     *current = tmp;
82     *nb_max *= factor;
83   }
84 }
85
86 template <class T>
87 inline T sq(T x) {
88   return x * x;
89 }
90
91 inline scalar_t log2(scalar_t x) {
92   return log(x)/log(2.0);
93 }
94
95 inline scalar_t xi(scalar_t x) {
96   if(x <= 0.0) return 0.0;
97   else         return - x * log(x)/log(2.0);
98 }
99
100 scalar_t discrete_entropy(int *n, int nb);
101
102 // char *basename(char *name);
103
104 char *next_word(char *buffer, char *r, int buffer_size);
105
106 void random_permutation(int *val, int nb);
107 void tag_subset(bool *val, int nb_total, int nb_to_tag);
108
109 struct Couple {
110   int index;
111   scalar_t value;
112 };
113
114 int compare_couple(const void *a, const void *b);
115
116 //  size       total program size
117 //  resident   resident set size
118 //  share      shared pages
119 //  text       text (code)
120 //  lib        library
121 //  data       data/stack
122 //  dt         dirty pages (unused in Linux 2.6)
123
124 void used_memory(size_t &size, size_t &resident,
125                  size_t &share, size_t &text, size_t &lib, size_t &data,
126                  size_t &dt);
127
128 #endif