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

FilteredVMatrix.cc

Go to the documentation of this file.
00001 // -*- C++ -*- 00002 00003 // FilteredVMatrix.cc 00004 // 00005 // Copyright (C) 2003 Pascal Vincent 00006 // 00007 // Redistribution and use in source and binary forms, with or without 00008 // modification, are permitted provided that the following conditions are met: 00009 // 00010 // 1. Redistributions of source code must retain the above copyright 00011 // notice, this list of conditions and the following disclaimer. 00012 // 00013 // 2. Redistributions in binary form must reproduce the above copyright 00014 // notice, this list of conditions and the following disclaimer in the 00015 // documentation and/or other materials provided with the distribution. 00016 // 00017 // 3. The name of the authors may not be used to endorse or promote 00018 // products derived from this software without specific prior written 00019 // permission. 00020 // 00021 // THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR 00022 // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 00023 // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN 00024 // NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 00025 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 00026 // TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 00027 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 00028 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 00029 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 00030 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00031 // 00032 // This file is part of the PLearn library. For more information on the PLearn 00033 // library, go to the PLearn Web site at www.plearn.org 00034 00035 /* ******************************************************* 00036 * $Id: FilteredVMatrix.cc,v 1.11 2004/08/05 13:51:09 tihocan Exp $ 00037 ******************************************************* */ 00038 00039 // Authors: Pascal Vincent 00040 00043 #include <plearn/base/ProgressBar.h> 00044 #include "FilteredVMatrix.h" 00045 00046 namespace PLearn { 00047 using namespace std; 00048 00049 00050 FilteredVMatrix::FilteredVMatrix() 00051 : inherited(), 00052 build_complete(false) 00053 { 00054 } 00055 00056 FilteredVMatrix::FilteredVMatrix(VMat the_source, const string& program_string, const string& the_metadatadir) 00057 : SourceVMatrix(the_source), 00058 prg(program_string) 00059 { 00060 metadatadir = the_metadatadir; 00061 build_(); 00062 } 00063 00064 PLEARN_IMPLEMENT_OBJECT(FilteredVMatrix, "A filtered view of its source vmatrix", 00065 "The filter is an exression in VPL language.\n" 00066 "The filtered indexes are saved in the metadata directory, that NEEDS to\n" 00067 "be provided.\n" ); 00068 00069 00070 00071 void FilteredVMatrix::openIndex() 00072 { 00073 string idxfname = getMetaDataDir()+"filtered.idx"; 00074 00075 if(file_exists(idxfname) && mtime(idxfname)>=getMtime()) 00076 indexes.open(idxfname); 00077 else // let's (re)create the index 00078 { 00079 rm(idxfname); // force remove it 00080 int l = source.length(); 00081 Vec result(1); 00082 indexes.open(idxfname,true); 00083 ProgressBar pb("Filtering source vmat", l); 00084 for(int i=0; i<l; i++) 00085 { 00086 pb.update(i); 00087 program.run(i,result); 00088 if(result[0]!=0) 00089 indexes.append(i); 00090 } 00091 indexes.close(); 00092 indexes.open(idxfname); 00093 } 00094 00095 length_ = indexes.length(); 00096 } 00097 00098 void FilteredVMatrix::setMetaDataDir(const string& the_metadatadir) 00099 { 00100 inherited::setMetaDataDir(the_metadatadir); 00101 if (build_complete) { 00102 // Only call openIndex() if the build has been completed, 00103 // otherwise the filtering program won't be ready yet. 00104 openIndex(); 00105 } 00106 } 00107 00108 void FilteredVMatrix::getNewRow(int i, const Vec& v) const 00109 { 00110 if (indexes.length() == -1) 00111 PLERROR("In FilteredVMatrix::getNewRow - The filtered indexes are not set, make sure you provided a metadatadir"); 00112 source->getRow(indexes[i],v); 00113 } 00114 00115 void FilteredVMatrix::declareOptions(OptionList& ol) 00116 { 00117 declareOption(ol, "prg", &FilteredVMatrix::prg, OptionBase::buildoption, 00118 "The VPL code that should produce a single scalar, indicating whether \n" 00119 "we should keep the line (if the produced scalar is non zero) or throw it away (if it's zero)"); 00120 00121 // Now call the parent class' declareOptions 00122 inherited::declareOptions(ol); 00123 } 00124 00125 void FilteredVMatrix::build_() 00126 { 00127 vector<string> fieldnames; 00128 program.setSource(source); 00129 // TODO: What happens when ptr is null 00130 program.compileString(prg,fieldnames); 00131 if(metadatadir != "") { 00132 openIndex(); 00133 } 00134 setMetaInfoFromSource(); 00135 build_complete = true; 00136 } 00137 00138 // ### Nothing to add here, simply calls build_ 00139 void FilteredVMatrix::build() 00140 { 00141 build_complete = false; 00142 inherited::build(); 00143 build_(); 00144 } 00145 00146 void FilteredVMatrix::makeDeepCopyFromShallowCopy(map<const void*, void*>& copies) 00147 { 00148 inherited::makeDeepCopyFromShallowCopy(copies); 00149 } 00150 00151 } // end of namespace PLearn 00152

Generated on Tue Aug 17 15:53:07 2004 for PLearn by doxygen 1.3.7