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 as published
13 * by the Free Software Foundation, either version 3 of the License,
14 * or (at your option) any later version.
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.
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/>.
32 char *basename(char *name) {
35 if(*name == '/') result = name + 1;
41 char *next_word(char *buffer, char *r, int buffer_size) {
46 while((*r == ' ') || (*r == '\t') || (*r == ',')) r++;
49 while((*r != '"') && (*r != '\0') &&
50 (s<buffer+buffer_size-1))
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;
64 while((*r == ' ') || (*r == '\t') || (*r == ',')) r++;
65 if((*r == '\0') || (*r=='\r') || (*r=='\n')) r = 0;
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]));
78 return (log(t) - s/scalar_t(t))/log(2.0);
81 void random_permutation(int *val, int nb) {
82 for(int k = 0; k < nb; k++) val[k] = k;
84 for(int k = 0; k < nb - 1; k++) {
85 i = int(drand48() * (nb - k)) + k;
92 void tag_subset(bool *val, int nb_total, int nb_to_tag) {
93 ASSERT(nb_to_tag <= 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;
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;
106 void used_memory(size_t &size, size_t &resident,
107 size_t &share, size_t &text, size_t &lib, size_t &data,
109 char buffer[buffer_size];
110 sprintf(buffer, "/proc/%d/statm", getpid());
113 in >> size >> resident >> share >> text >> lib >> data >> dt;
114 size_t ps = getpagesize();
130 cerr << "Can not open " << buffer << " for reading the memory usage." << endl;