00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 
00018 
00019 
00020 
00021 
00022 
00023 
00024 
00025 
00026 
00027 
00028 
00029 
00030 
00031 
00032 
00033 
00034 
00035 
00036 
00037 
00038 
00039 
00040 
00041 
00042 
#include "SubVMatrix.h"
00043 
#include "SortRowsVMatrix.h"
00044 
00045 
namespace PLearn {
00046 
using namespace std;
00047 
00050 
PLEARN_IMPLEMENT_OBJECT(SortRowsVMatrix, 
00051     
"Sort the samples of a VMatrix according to one (or more) given columns.",
00052     
"The implementation is not efficient at all, feel free to improve it !"
00053 );
00054 
00055 SortRowsVMatrix::SortRowsVMatrix() 
00056   : increasing_order(1)
00057 {
00058   
00059   
sort_columns.
resize(0);
00060 }
00061 
00062 void SortRowsVMatrix::declareOptions(
OptionList &ol)
00063 {
00064     
declareOption(ol, 
"sort_columns", &SortRowsVMatrix::sort_columns, OptionBase::buildoption,
00065         
"    the column(s) that must be sorted (the first one is the first criterion)");
00066     
00067     
declareOption(ol, 
"increasing_order", &SortRowsVMatrix::increasing_order, OptionBase::buildoption,
00068         
"    if set to 1, the data will be sorted in increasing order");
00069     
00070     inherited::declareOptions(ol);
00071 }
00072 
00073 void SortRowsVMatrix::makeDeepCopyFromShallowCopy(map<const void*, void*>& copies)
00074 {
00075   
deepCopyField(
sort_columns, copies);
00076   inherited::makeDeepCopyFromShallowCopy(copies);
00077 }
00078 
00080 
00082 void SortRowsVMatrix::build()
00083 {
00084   inherited::build();
00085   
build_();
00086 }
00087 
00089 
00091 void SortRowsVMatrix::build_()
00092 {
00093   
00094   
if (
sort_columns.
isNotEmpty()) {
00095     
for (
int i = 0; i < 
sort_columns.
length(); i++) {
00096       
for (
int j = i + 1; j < 
sort_columns.
length(); j++) {
00097         
if (
sort_columns[j] == 
sort_columns[i]) {
00098           
PLERROR(
"In SortRowsVMatrix::build_ - You have a duplicated index in the 'sort_columns' vector");
00099         }
00100       }
00101     }
00102   }
00103   
00104   
if (source) {
00105     indices = 
TVec<int>(0, source.
length()-1, 1);
00106     
sortRows(source, indices, 
sort_columns, 0, source->
length()-1, 0, 
increasing_order);
00107     inherited::build(); 
00108   }
00109 }
00110 
00112 
00114 void SortRowsVMatrix::sortRows(
VMat& m, 
TVec<int>& indices, 
TVec<int>& sort_columns, 
int istart, 
int iend, 
int colstart, 
bool increasing_order) {
00115   
real best;
00116   
real jval;
00117   
int tmp;
00118   
bool better;
00119   
if (sort_columns.
size() > colstart) {
00120     
int col = sort_columns[colstart]; 
00121     
for (
int i = istart; i <= iend-1; i++) {
00122       
00123       best = m(indices[i],col);
00124       
for (
int j = i+1; j <= iend; j++) {
00125         better = 
false;
00126         jval = m(indices[j],col);
00127         
if (increasing_order && jval < best) {
00128           better = 
true;
00129         } 
else if (!increasing_order && jval > best) {
00130           better = 
true;
00131         }
00132         
if (better) {
00133           
00134           tmp = indices[j];
00135           indices[j] = indices[i];
00136           indices[i] = tmp;
00137           best = jval;
00138         }
00139       }
00140     }
00141     
00142     
if (sort_columns.length() > colstart + 1) {
00143       
00144       
00145       
int i = istart;
00146       
real val;
00147       
while (i <= iend - 1) {
00148         
val = m(indices[i],col);
00149         
int j = i+1;
00150         
while (j <= iend && m(indices[j],col) == 
val)
00151           j++;
00152         j--;
00153         
if (j > i) {
00154           
00155           
00156           
sortRows(m, indices, sort_columns, i, j, colstart + 1, increasing_order);
00157         }
00158         i = j+1;
00159       }
00160     }
00161   }
00162 }
00163 
00164 }