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

PrecomputedVMatrix.cc

Go to the documentation of this file.
00001 // -*- C++ -*- 00002 00003 // PrecomputedVMatrix.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: PrecomputedVMatrix.cc,v 1.9 2004/07/07 17:30:48 tihocan Exp $ 00037 ******************************************************* */ 00038 00039 // Authors: Pascal Vincent 00040 00044 #include "PrecomputedVMatrix.h" 00045 #include "DiskVMatrix.h" 00046 #include "FileVMatrix.h" 00047 00048 namespace PLearn { 00049 using namespace std; 00050 00051 00052 PrecomputedVMatrix::PrecomputedVMatrix() 00053 :precomp_type("dmat") 00054 { 00055 } 00056 00057 PLEARN_IMPLEMENT_OBJECT(PrecomputedVMatrix, 00058 "VMatrix that caches (pre-computes on disk) the content of a source vmatrix", 00059 "This sub-class of SourceVMatrix pre-computes the content of a source vmatrix\n" 00060 "in a dmat or pmat file. The name of the disk file is obtained from the metadatadir option\n" 00061 "followed by precomp.dmat or precomp.pmat"); 00062 00063 void PrecomputedVMatrix::getNewRow(int i, const Vec& v) const 00064 { 00065 if(precomp_source.isNull()) 00066 PLERROR("Source has not been precomputed. Did you properly set the MetaDataDir?"); 00067 precomp_source->getRow(i,v); 00068 } 00069 00070 void PrecomputedVMatrix::declareOptions(OptionList& ol) 00071 { 00072 // ### Declare all of this object's options here 00073 // ### For the "flags" of each option, you should typically specify 00074 // ### one of OptionBase::buildoption, OptionBase::learntoption or 00075 // ### OptionBase::tuningoption. Another possible flag to be combined with 00076 // ### is OptionBase::nosave 00077 00078 // ### ex: 00079 declareOption(ol, "precomp_type", &PrecomputedVMatrix::precomp_type, OptionBase::buildoption, 00080 "The type of vmatrix to be used for the cached precomputed version of the source.\n" 00081 "Currently supported are: dmat, pmat"); 00082 // ... 00083 00084 // Now call the parent class' declareOptions 00085 inherited::declareOptions(ol); 00086 } 00087 00088 void PrecomputedVMatrix::setMetaDataDir(const string& the_metadatadir) 00089 { 00090 inherited::setMetaDataDir(the_metadatadir); 00091 if(hasMetaDataDir()) // don't do anything if the meta-data-dir is not yet set. 00092 usePrecomputed(); 00093 } 00094 00095 void PrecomputedVMatrix::usePrecomputed() 00096 { 00097 string mdir = append_slash(getMetaDataDir()); 00098 if(precomp_type=="dmat") 00099 { 00100 string dmatdir = mdir + "precomp.dmat"; 00101 bool recompute = true; 00102 if(isdir(dmatdir)) 00103 { 00104 precomp_source = new DiskVMatrix(dmatdir); 00105 if(precomp_source->getMtime() >= source->getMtime()) 00106 recompute = false; 00107 } 00108 if(recompute) 00109 { 00110 force_rmdir(dmatdir); 00111 source->saveDMAT(dmatdir); 00112 precomp_source = new DiskVMatrix(dmatdir); 00113 } 00114 length_ = precomp_source->length(); 00115 } 00116 else if(precomp_type=="pmat") 00117 { 00118 string pmatfile = mdir + "precomp.pmat"; 00119 bool recompute = true; 00120 if(isfile(pmatfile)) 00121 { 00122 precomp_source = new FileVMatrix(pmatfile); 00123 if(precomp_source->getMtime() >= source->getMtime()) 00124 recompute = false; 00125 } 00126 if(recompute) 00127 { 00128 rm(pmatfile); 00129 source->savePMAT(pmatfile); 00130 precomp_source = new FileVMatrix(pmatfile); 00131 } 00132 length_ = precomp_source->length(); 00133 } 00134 else 00135 PLERROR("Invalid precomp_type=%s. Must be one of: dmat, pmat.",precomp_type.c_str()); 00136 00137 } 00138 00139 void PrecomputedVMatrix::build_() 00140 { 00141 setMetaInfoFromSource(); 00142 } 00143 00144 // ### Nothing to add here, simply calls build_ 00145 void PrecomputedVMatrix::build() 00146 { 00147 inherited::build(); 00148 build_(); 00149 } 00150 00151 void PrecomputedVMatrix::makeDeepCopyFromShallowCopy(map<const void*, void*>& copies) 00152 { 00153 inherited::makeDeepCopyFromShallowCopy(copies); 00154 00155 // ### Call deepCopyField on all "pointer-like" fields 00156 // ### that you wish to be deepCopied rather than 00157 // ### shallow-copied. 00158 // ### ex: 00159 deepCopyField(precomp_source, copies); 00160 } 00161 00162 } // end of namespace PLearn 00163

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