Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Namespace Members | Class Members | File Members

SortRowsVMatrix.cc

Go to the documentation of this file.
00001 // -*- C++ -*- 00002 00003 // PLearn (A C++ Machine Learning Library) 00004 // Copyright (C) 1998 Pascal Vincent 00005 // Copyright (C) 1999-2001 Pascal Vincent, Yoshua Bengio, Rejean Ducharme and University of Montreal 00006 // Copyright (C) 2002 Pascal Vincent, Julien Keable, Xavier Saint-Mleux 00007 // Copyright (C) 2003 Olivier Delalleau 00008 // 00009 // Redistribution and use in source and binary forms, with or without 00010 // modification, are permitted provided that the following conditions are met: 00011 // 00012 // 1. Redistributions of source code must retain the above copyright 00013 // notice, this list of conditions and the following disclaimer. 00014 // 00015 // 2. Redistributions in binary form must reproduce the above copyright 00016 // notice, this list of conditions and the following disclaimer in the 00017 // documentation and/or other materials provided with the distribution. 00018 // 00019 // 3. The name of the authors may not be used to endorse or promote 00020 // products derived from this software without specific prior written 00021 // permission. 00022 // 00023 // THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR 00024 // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 00025 // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN 00026 // NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 00027 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 00028 // TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 00029 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 00030 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 00031 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 00032 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00033 // 00034 // This file is part of the PLearn library. For more information on the PLearn 00035 // library, go to the PLearn Web site at www.plearn.org 00036 00037 00038 /* ******************************************************* 00039 * $Id: SortRowsVMatrix.cc,v 1.6 2004/07/14 20:53:38 tihocan Exp $ 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 // Default = no sorting. 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 // build // 00082 void SortRowsVMatrix::build() 00083 { 00084 inherited::build(); 00085 build_(); 00086 } 00087 00089 // build_ // 00091 void SortRowsVMatrix::build_() 00092 { 00093 // Check we don't try to sort twice by the same column (this can be confusing). 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 // Construct the indices vector. 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(); // Since we have just changed the indices. 00108 } 00109 } 00110 00112 // sortRows // 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]; // The current column used to perform sorting. 00121 for (int i = istart; i <= iend-1; i++) { 00122 // Let's look for the i-th element of our result. 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 // Swap i and j. 00134 tmp = indices[j]; 00135 indices[j] = indices[i]; 00136 indices[i] = tmp; 00137 best = jval; 00138 } 00139 } 00140 } 00141 // At this point, we have only sorted according to one column. 00142 if (sort_columns.length() > colstart + 1) { 00143 // There are other sorting criterions. 00144 // Let's find where we need to apply them. 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 // There are consecutive elements with the same value for the sorting 00155 // criterion, thus we must use the other criteria to sort them correctly. 00156 sortRows(m, indices, sort_columns, i, j, colstart + 1, increasing_order); 00157 } 00158 i = j+1; 00159 } 00160 } 00161 } 00162 } 00163 00164 } // end of namespcae PLearn

Generated on Tue Aug 17 16:06:06 2004 for PLearn by doxygen 1.3.7