2 ///////////////////////////////////////////////////////////////////////////
3 // This program is free software: you can redistribute it and/or modify //
4 // it under the terms of the version 3 of the GNU General Public License //
5 // as published by the Free Software Foundation. //
7 // This program is distributed in the hope that it will be useful, but //
8 // WITHOUT ANY WARRANTY; without even the implied warranty of //
9 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU //
10 // General Public License for more details. //
12 // You should have received a copy of the GNU General Public License //
13 // along with this program. If not, see <http://www.gnu.org/licenses/>. //
15 // Written by Francois Fleuret //
16 // (C) Idiap Research Institute //
18 // Contact <francois.fleuret@idiap.ch> for comments & bug reports //
19 ///////////////////////////////////////////////////////////////////////////
23 A bunch of useful little functions.
39 //typedef double scalar_t;
40 typedef float scalar_t;
41 const scalar_t SCALAR_MAX = FLT_MAX;
42 const scalar_t SCALAR_MIN = FLT_MIN;
44 const int buffer_size = 1024;
45 const int large_buffer_size = 65536;
50 #define ASSERT(x) if(!(x)) { \
51 std::cerr << "ASSERT FAILED IN " << __FILE__ << ":" << __LINE__ << endl; \
59 T smooth_min(T x, T y) {
61 return 0.5 * (x + y - (x - y)/(1 + 1/z) - (y - x)/(1 + z));
65 void write_var(ostream *os, const T *x) { os->write((char *) x, sizeof(T)); }
68 void read_var(istream *is, T *x) { is->read((char *) x, sizeof(T)); }
71 void grow(int *nb_max, int nb, T** current, int factor) {
74 T *tmp = new T[*nb_max * factor];
75 memcpy(tmp, *current, *nb_max * sizeof(T));
87 inline scalar_t log2(scalar_t x) {
88 return log(x)/log(2.0);
91 inline scalar_t xi(scalar_t x) {
92 if(x <= 0.0) return 0.0;
93 else return - x * log(x)/log(2.0);
96 scalar_t discrete_entropy(int *n, int nb);
98 char *basename(char *name);
100 char *next_word(char *buffer, char *r, int buffer_size);
102 void random_permutation(int *val, int nb);
103 void tag_subset(bool *val, int nb_total, int nb_to_tag);
110 int compare_couple(const void *a, const void *b);
112 // size total program size
113 // resident resident set size
114 // share shared pages
118 // dt dirty pages (unused in Linux 2.6)
120 void used_memory(size_t &size, size_t &resident,
121 size_t &share, size_t &text, size_t &lib, size_t &data,