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, (C) IDIAP //
16 // Contact <francois.fleuret@idiap.ch> for comments & bug reports //
17 ///////////////////////////////////////////////////////////////////////////
25 char *basename(char *name) {
28 if(*name == '/') result = name + 1;
34 char *next_word(char *buffer, char *r, int buffer_size) {
39 while((*r == ' ') || (*r == '\t') || (*r == ',')) r++;
42 while((*r != '"') && (*r != '\0') &&
43 (s<buffer+buffer_size-1))
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;
57 while((*r == ' ') || (*r == '\t') || (*r == ',')) r++;
58 if((*r == '\0') || (*r=='\r') || (*r=='\n')) r = 0;
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]));
71 return (log(t) - s/scalar_t(t))/log(2.0);
74 void random_permutation(int *val, int nb) {
75 for(int k = 0; k < nb; k++) val[k] = k;
77 for(int k = 0; k < nb - 1; k++) {
78 i = int(drand48() * (nb - k)) + k;
85 void tag_subset(bool *val, int nb_total, int nb_to_tag) {
86 ASSERT(nb_to_tag <= 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;
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;
99 void used_memory(size_t &size, size_t &resident,
100 size_t &share, size_t &text, size_t &lib, size_t &data,
102 char buffer[buffer_size];
103 sprintf(buffer, "/proc/%d/statm", getpid());
106 in >> size >> resident >> share >> text >> lib >> data >> dt;
107 size_t ps = getpagesize();
123 cerr << "Can not open " << buffer << " for reading the memory usage." << endl;