2 * folded-ctf is an implementation of the folded hierarchy of
3 * classifiers for object detection, developed by Francois Fleuret
6 * Copyright (c) 2008 Idiap Research Institute, http://www.idiap.ch/
7 * Written by Francois Fleuret <francois.fleuret@idiap.ch>
9 * This file is part of folded-ctf.
11 * folded-ctf is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License version 3 as
13 * published by the Free Software Foundation.
15 * folded-ctf is distributed in the hope that it will be useful, but
16 * WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with folded-ctf. If not, see <http://www.gnu.org/licenses/>.
31 // char *basename(char *name) {
32 // char *result = name;
34 // if(*name == '/') result = name + 1;
40 char *next_word(char *buffer, char *r, int buffer_size) {
45 while((*r == ' ') || (*r == '\t') || (*r == ',')) r++;
48 while((*r != '"') && (*r != '\0') &&
49 (s<buffer+buffer_size-1))
53 while((*r != '\r') && (*r != '\n') && (*r != '\0') &&
54 (*r != '\t') && (*r != ' ') && (*r != ',')) {
55 if(s == buffer + buffer_size) {
56 cerr << "Buffer overflow in next_word." << endl;
63 while((*r == ' ') || (*r == '\t') || (*r == ',')) r++;
64 if((*r == '\0') || (*r=='\r') || (*r=='\n')) r = 0;
71 scalar_t discrete_entropy(int *n, int nb) {
72 scalar_t s = 0, t = 0;
73 for(int k = 0; k < nb; k++) if(n[k] > 0) {
74 s += n[k] * log(scalar_t(n[k]));
77 return (log(t) - s/scalar_t(t))/log(2.0);
80 void random_permutation(int *val, int nb) {
81 for(int k = 0; k < nb; k++) val[k] = k;
83 for(int k = 0; k < nb - 1; k++) {
84 i = int(drand48() * (nb - k)) + k;
91 void tag_subset(bool *val, int nb_total, int nb_to_tag) {
92 ASSERT(nb_to_tag <= nb_total);
94 random_permutation(index, nb_total);
95 for(int n = 0; n < nb_total; n++) val[n] = false;
96 for(int n = 0; n < nb_to_tag; n++) val[index[n]] = true;
99 int compare_couple(const void *a, const void *b) {
100 if(((Couple *) a)->value < ((Couple *) b)->value) return -1;
101 else if(((Couple *) a)->value > ((Couple *) b)->value) return 1;
105 void used_memory(size_t &size, size_t &resident,
106 size_t &share, size_t &text, size_t &lib, size_t &data,
108 char buffer[buffer_size];
109 sprintf(buffer, "/proc/%d/statm", getpid());
112 in >> size >> resident >> share >> text >> lib >> data >> dt;
113 size_t ps = getpagesize();
129 cerr << "Can not open " << buffer << " for reading the memory usage." << endl;