ECOCPAK v0.9
fn_group_classes.hpp
Go to the documentation of this file.
00001 // Copyright (C) 2011 the authors listed below
00002 // http://ecocpak.sourceforge.net
00003 //
00004 // Authors:
00005 // - Dimitrios Bouzas (bouzas at ieee dot org)
00006 // - Nikolaos Arvanitopoulos (niarvani at ieee dot org)
00007 // - Anastasios Tefas (tefas at aiia dot csd dot auth dot gr)
00008 //
00009 // This file is part of the ECOC PAK C++ library. It is
00010 // provided without any warranty of fitness for any purpose.
00011 //
00012 // You can redistribute this file and/or modify it under
00013 // the terms of the GNU Lesser General Public License (LGPL)
00014 // as published by the Free Software Foundation, either
00015 // version 3 of the License or (at your option) any later
00016 // version.
00017 // (see http://www.opensource.org/licenses for more info)
00018 
00019 
00022 
00023 
00024 
00042 void
00043 group_classes
00044   (
00045   const ivec& labels,
00046   int* nr_class_ret,
00047   int** label_ret,
00048   int** start_ret,
00049   int** count_ret,
00050   int* perm
00051   )
00052   {
00053   int l = labels.n_rows;
00054   int max_nr_class = 16;
00055   int nr_class = 0;
00056   int* label = Malloc(int, max_nr_class);
00057   int* count = Malloc(int, max_nr_class);
00058   int* data_label = Malloc(int, l);
00059   int i;
00060 
00061   for(i = 0; i < l; i++)
00062     {
00063     int this_label = (int) labels[i];
00064 
00065     int j;
00066     for(j = 0; j < nr_class; j++)
00067       {
00068       if(this_label == label[j])
00069         {
00070         ++count[j];
00071         break;
00072         }
00073       }
00074 
00075     data_label[i] = j;
00076     if(j == nr_class)
00077       {
00078       if(nr_class == max_nr_class)
00079         {
00080         max_nr_class *= 2;
00081         label = (int*) realloc(label, max_nr_class * sizeof(int));
00082         count = (int*) realloc(count, max_nr_class * sizeof(int));
00083         }
00084 
00085       label[nr_class] = this_label;
00086       count[nr_class] = 1;
00087       ++nr_class;
00088       }
00089     }
00090 
00091   int* start = Malloc(int, nr_class);
00092 
00093   start[0] = 0;
00094   for(i = 1; i < nr_class; i++)
00095     {
00096     start[i] = start[i - 1] + count[i - 1];
00097     }
00098 
00099   for(i = 0; i < l; i++)
00100     {
00101     perm[start[data_label[i]]] = i;
00102     ++start[data_label[i]];
00103     }
00104 
00105   start[0] = 0;
00106   for(i = 1; i < nr_class; i++)
00107     {
00108     start[i] = start[i - 1] + count[i - 1];
00109     }
00110 
00111   *nr_class_ret = nr_class;
00112   *label_ret = label;
00113   *start_ret = start;
00114   *count_ret = count;
00115   free(data_label);
00116   }
00117 
00118 
00119 
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerator Defines