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