PLearn 0.1
PLearnDiff.cc
Go to the documentation of this file.
00001 // -*- C++ -*-
00002 
00003 // PLearnDiff.cc
00004 //
00005 // Copyright (C) 2005 Olivier Delalleau 
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: .pyskeleton_header 544 2003-09-01 00:05:31Z plearner $ 
00037  ******************************************************* */
00038 
00039 // Authors: Olivier Delalleau
00040 
00044 #include "PLearnDiff.h"
00045 #include <plearn/math/TMat.h>
00046 
00047 namespace PLearn {
00048 using namespace std;
00049 
00050 PLearnDiff::PLearnDiff():
00051     absolute_tolerance(ABSOLUTE_TOLERANCE),
00052     relative_tolerance(RELATIVE_TOLERANCE),
00053     save_diffs(true)
00054 {
00055     build_();
00056     forget();
00057 }
00058 
00059 PLEARN_IMPLEMENT_OBJECT(PLearnDiff,
00060                         "Object that represents settings and results of a diff between two PLearn objects",
00061                         ""
00062     );
00063 
00064 void PLearnDiff::declareOptions(OptionList& ol)
00065 {
00066     declareOption(ol, "absolute_tolerance", &PLearnDiff::absolute_tolerance, OptionBase::buildoption,
00067             "The absolute tolerance used when comparing real numbers less than 1.");
00068 
00069     declareOption(ol, "relative_tolerance", &PLearnDiff::relative_tolerance, OptionBase::buildoption,
00070             "The relative tolerance used when comparing real numbers and one is more than 1.");
00071 
00072     declareOption(ol, "save_diffs", &PLearnDiff::save_diffs, OptionBase::buildoption,
00073             "If set to 1, each call to 'diff' will save any difference found.\n"
00074             "Otherwise, differences will not be saved.");
00075 
00076     // Now call the parent class' declareOptions
00077     inherited::declareOptions(ol);
00078 }
00079 
00081 // build_ //
00083 void PLearnDiff::build_()
00084 {
00085     diffs.resize(diffs.length(), 3);
00086 }
00087 
00089 // build //
00091 void PLearnDiff::build()
00092 {
00093     inherited::build();
00094     build_();
00095 }
00096 
00098 // addDiffPrefix //
00100 void PLearnDiff::addDiffPrefix(const string& prefix, int n)
00101 {
00102     PLASSERT( n >= 0 && n <= diffs.length() );
00103     int l = diffs.length() - 1;
00104     for (int j = 0; j < n; j++)
00105         diffs(l - j, 0) = prefix + diffs(l - j, 0);
00106 }
00107 
00108 void addDiffPrefix(PLearnDiff* diffs, const string& prefix, int n)
00109 {
00110     PLASSERT( diffs );
00111     diffs->addDiffPrefix(prefix, n);
00112 }
00113 
00115 // diff //
00117 int PLearnDiff::diff(const string& refer, const string& other, const string& name)
00118 {
00119     static TVec<string> diff_row;
00120     diff_row.resize(3);
00121     if (refer != other) {
00122         if (save_diffs) {
00123             diff_row[0] = name;
00124             diff_row[1] = refer;
00125             diff_row[2] = other;
00126             diffs.appendRow(diff_row);
00127         }
00128         return 1;
00129     } else
00130         return 0;
00131 }
00132 
00133 int diff(PLearnDiff* diffs, const string& refer, const string& other, const string& name)
00134 {
00135     PLASSERT( diffs );
00136     return diffs->diff(refer, other, name);
00137 }
00138 
00140 // forget //
00142 void PLearnDiff::forget()
00143 {
00144     diffs.resize(0, diffs.width());
00145 }
00146 
00148 // get_absolute_tolerance //
00150 real get_absolute_tolerance(PLearnDiff* diffs)
00151 {
00152     PLASSERT( diffs );
00153     return diffs->absolute_tolerance;
00154 }
00155 
00157 // get_relative_tolerance //
00159 real get_relative_tolerance(PLearnDiff* diffs)
00160 {
00161     PLASSERT( diffs );
00162     return diffs->relative_tolerance;
00163 }
00164 
00166 // makeDeepCopyFromShallowCopy //
00168 void PLearnDiff::makeDeepCopyFromShallowCopy(CopiesMap& copies)
00169 {
00170     inherited::makeDeepCopyFromShallowCopy(copies);
00171 
00172     // ### Call deepCopyField on all "pointer-like" fields 
00173     // ### that you wish to be deepCopied rather than 
00174     // ### shallow-copied.
00175     // ### ex:
00176     // deepCopyField(trainvec, copies);
00177 
00178     // ### Remove this line when you have fully implemented this method.
00179     PLERROR("PLearnDiff::makeDeepCopyFromShallowCopy not fully (correctly) implemented yet!");
00180 }
00181 
00183 // printDiffs //
00185 void PLearnDiff::printDiffs(PStream& out, unsigned int indent,
00186                             unsigned int tab_step, unsigned int max_width)
00187 {
00188     int n = nDiffs();
00189     for (int i = 0; i < n; i++) {
00190         const string& diff_name = diffs(i, 0);
00191         const string& diff_val1 = diffs(i, 1);
00192         const string& diff_val2 = diffs(i, 2);
00193         unsigned int n_blanks_after_name =
00194             tab_step - (unsigned int)(diff_name.size()) % tab_step;
00195         unsigned int n_blanks_after_val1 =
00196             tab_step - (unsigned int)(diff_val1.size()) % tab_step;
00197         if (indent + diff_name.size() + n_blanks_after_name + 3 
00198             + diff_val1.size() + n_blanks_after_val1 + 7 + diff_val2.size()
00199             < max_width) {
00200             out << string(indent, ' ')
00201                 << diff_name << string(n_blanks_after_name, ' ')
00202                 << ":  "
00203                 << diff_val1 << string(n_blanks_after_val1, ' ')
00204                 << "  -->  "
00205                 << diff_val2 << endl;
00206         } else {
00207             out << string(indent, ' ')
00208                 << diff_name << ":" << endl
00209                 << diff_val1 << endl
00210                 << "  -->  " << endl
00211                 << diff_val2 << endl;
00212         }
00213     }
00214 }
00215 
00216 
00218 // setSaveDiffs //
00220 void PLearnDiff::setSaveDiffs(bool save_diffs, bool* save_diffs_backup)
00221 {
00222     if (save_diffs_backup)
00223         *save_diffs_backup = this->save_diffs;
00224     this->save_diffs = save_diffs;
00225 }
00226 
00227 void setSaveDiffs(PLearnDiff* diffs, bool save_diffs, bool* save_diffs_backup)
00228 {
00229     diffs->setSaveDiffs(save_diffs, save_diffs_backup);
00230 }
00231 
00232 
00233 } // end of namespace PLearn
00234 
00235 
00236 /*
00237   Local Variables:
00238   mode:c++
00239   c-basic-offset:4
00240   c-file-style:"stroustrup"
00241   c-file-offsets:((innamespace . 0)(inline-open . 0))
00242   indent-tabs-mode:nil
00243   fill-column:79
00244   End:
00245 */
00246 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=79 :
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines