automatic commit
[folded-ctf.git] / misc.cc
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 #include <fstream>
27
28 using namespace std;
29
30 #include "misc.h"
31
32 char *basename(char *name) {
33   char *result = name;
34   while(*name) {
35     if(*name == '/') result = name + 1;
36     name++;
37   }
38   return result;
39 }
40
41 char *next_word(char *buffer, char *r, int buffer_size) {
42   char *s;
43   s = buffer;
44
45   if(r != 0) {
46     while((*r == ' ') || (*r == '\t') || (*r == ',')) r++;
47     if(*r == '"') {
48       r++;
49       while((*r != '"') && (*r != '\0') &&
50             (s<buffer+buffer_size-1))
51         *s++ = *r++;
52       if(*r == '"') r++;
53     } else {
54       while((*r != '\r') && (*r != '\n') && (*r != '\0') &&
55             (*r != '\t') && (*r != ' ') && (*r != ',')) {
56         if(s == buffer + buffer_size) {
57           cerr << "Buffer overflow in next_word." << endl;
58           exit(1);
59         }
60         *s++ = *r++;
61       }
62     }
63
64     while((*r == ' ') || (*r == '\t') || (*r == ',')) r++;
65     if((*r == '\0') || (*r=='\r') || (*r=='\n')) r = 0;
66   }
67   *s = '\0';
68
69   return r;
70 }
71
72 scalar_t discrete_entropy(int *n, int nb) {
73   scalar_t s = 0, t = 0;
74   for(int k = 0; k < nb; k++) if(n[k] > 0) {
75     s += n[k] * log(scalar_t(n[k]));
76     t += n[k];
77   }
78   return (log(t) - s/scalar_t(t))/log(2.0);
79 }
80
81 void random_permutation(int *val, int nb) {
82   for(int k = 0; k < nb; k++) val[k] = k;
83   int i, t;
84   for(int k = 0; k < nb - 1; k++) {
85     i = int(drand48() * (nb - k)) + k;
86     t = val[i];
87     val[i] = val[k];
88     val[k] = t;
89   }
90 }
91
92 void tag_subset(bool *val, int nb_total, int nb_to_tag) {
93   ASSERT(nb_to_tag <= nb_total);
94   int index[nb_total];
95   random_permutation(index, nb_total);
96   for(int n = 0; n < nb_total; n++) val[n] = false;
97   for(int n = 0; n < nb_to_tag; n++) val[index[n]] = true;
98 }
99
100 int compare_couple(const void *a, const void *b) {
101   if(((Couple *) a)->value < ((Couple *) b)->value) return -1;
102   else if(((Couple *) a)->value > ((Couple *) b)->value) return 1;
103   else return 0;
104 }
105
106 void used_memory(size_t &size, size_t &resident,
107                  size_t &share, size_t &text, size_t &lib, size_t &data,
108                  size_t &dt) {
109   char buffer[buffer_size];
110   sprintf(buffer,  "/proc/%d/statm", getpid());
111   ifstream in(buffer);
112   if(in.good()) {
113     in >> size >> resident >> share >> text >> lib >> data >> dt;
114     size_t ps = getpagesize();
115     size *= ps;
116     resident *= ps;
117     share *= ps;
118     text *= ps;
119     lib *= ps;
120     data *= ps;
121     dt *= ps;
122   } else {
123     size = 0;
124     resident = 0;
125     share = 0;
126     text = 0;
127     lib = 0;
128     data = 0;
129     dt = 0;
130     cerr << "Can not open " << buffer << " for reading the memory usage." << endl;
131   }
132 }