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