COOPY » Guide  version 0.6.5
/home/paulfitz/cvs/coopy_scm/coopy/src/libcoopy_core/include/coopy/RowMan.h
Go to the documentation of this file.
00001 #ifndef COOPY_ROWMAN
00002 #define COOPY_ROWMAN
00003 
00004 #include <coopy/Measure.h>
00005 #include <coopy/CompareFlags.h>
00006 #include <coopy/FMap.h>
00007 #include <coopy/OrderResult.h>
00008 
00009 namespace coopy {
00010   namespace cmp {
00011     template <class map_type> class RowManOf;
00012     typedef RowManOf<FMap> RowMan;
00013     typedef RowManOf<FMultiMap> RowMan2;
00014     class CombinedRowMan;
00015   }
00016 }
00017 
00022 template <class map_type> 
00023 class coopy::cmp::RowManOf : public Measure {
00024 public:
00025   CompareFlags flags;
00026   int vigor;
00027   int bound;
00028   map_type m;
00029   std::vector<int> ref_subset, query_subset;
00030   coopy::store::SparseFloatSheet match;
00031   const OrderResult& comp;
00032 
00033  RowManOf(const CompareFlags& flags,
00034           const OrderResult& comp,
00035           int len) : flags(flags), m(match,len), comp(comp) {
00036     vigor = 0;
00037     bound = -1;
00038   }
00039 
00040   void setVigor(int vigor) {
00041     this->vigor = vigor;
00042   }
00043 
00044   virtual void setup(MeasurePass& pass) {
00045     pass.setSize(pass.a.height(),pass.b.height());
00046     if (flags.trust_ids||flags.bias_ids) {
00047       ref_subset = pass.va.meta.getSubset();
00048       query_subset = pass.vb.meta.getSubset();
00049     }
00050   }
00051 
00052   void apply(coopy::store::DataSheet& a, 
00053              coopy::store::IntSheet& asel, 
00054              int target,
00055              bool query, 
00056              bool alt, int ctrl) {
00057     int w = a.width();
00058     int h = a.height();
00059     m.resetCount();
00060     int top = (bound<0)?h:bound;
00061     int at = 0;
00062     for (int y=0; y<h; y++) {
00063       if (asel.cell(0,y)==-1) {
00064         if (at<top) {
00065           at++;
00066           if (!(flags.trust_ids)) {
00067             if (!flags.bias_ids) {
00068               int first = target;
00069               int last = target;
00070               if (target==-1) {
00071                 first = 0;
00072                 last = w-1;
00073               }
00074               for (int x=first; x<=last; x++) {
00075                 std::string txt = a.cellString(x,y);
00076                 m.setCurr(x,y);
00077                 m.add(txt,query,alt,ctrl);
00078                 //printf("ADD %d %d %s %d\n", x, y, txt.c_str(), query);
00079               }
00080             } else {
00081               const std::vector<int>& subset = query?query_subset:ref_subset;
00082               for (int x=0; x<(int)subset.size(); x++) {
00083                 std::string txt = a.cellString(subset[x],y);
00084                 m.setCurr(subset[x],y);
00085                 m.add(txt,query,alt,ctrl);
00086               }
00087             }
00088           } else {
00089             const std::vector<int>& subset = query?query_subset:ref_subset;
00090             std::string txt = "";
00091             for (int x=0; x<(int)subset.size(); x++) {
00092               txt += a.cellString(subset[x],y) + "__";
00093             }
00094             m.setCurr(0,y);
00095             m.add(txt,query,alt,0);
00096           }
00097         } else {
00098           match.cell(0,y) = -2;
00099         }
00100       }
00101     }
00102     if (vigor==1) {
00103       for (int y=0; y<h; y++) {
00104         if (asel.cell(0,y)>=-1) {
00105           for (int x=0; x<w-1; x++) {
00106             std::string txt = a.cellString(x,y);
00107             txt += a.cellString(x+1,y);
00108             m.setCurr(x,y);
00109             m.add(txt,query,alt,ctrl);
00110           }
00111         }
00112       }
00113     }
00114     m.summarize(true);
00115   }
00116 
00117   void apply(coopy::store::DataSheet& a, 
00118              coopy::store::DataSheet& b, 
00119              coopy::store::IntSheet& asel, 
00120              coopy::store::IntSheet& bsel,
00121              int ctrl) {
00122     match.resize(a.height(),b.height(),0);
00123     if (flags.trust_ids||flags.bias_ids||comp.isBlank()) {
00124       apply(a,asel,-1,false,false,ctrl);
00125       apply(b,bsel,-1,true,false,ctrl);
00126       return;
00127     }
00128     // Have a column mapping
00129 
00130     int w = a.width();
00131     for (int i=0; i<w; i++) {
00132       int j = comp.a2b(i);
00133       if (j!=-1) {
00134         m.resetCache();
00135         apply(a,asel,i,false,false,ctrl);
00136         apply(b,bsel,j,false,true,ctrl);
00137         apply(b,bsel,j,true,false,ctrl);
00138       }
00139     }
00140   }
00141 
00142   virtual void measure(MeasurePass& pass, int ctrl) {
00143     bound = pass.bound;
00144     apply(pass.a, pass.b, pass.asel, pass.bsel, ctrl);
00145     pass.match = match;
00146   }
00147 
00148   virtual int getCtrlMax() {
00149     return map_type::getCtrlMax();
00150   }
00151 };
00152 
00157 class coopy::cmp::CombinedRowMan : public Measure {
00158 public:
00159   RowMan man1;
00160   RowMan2 man2;
00161   int theta;
00162   bool flip;
00163 
00164  CombinedRowMan(const CompareFlags& flags,
00165                 const OrderResult& comp,
00166                 int len) : man1(flags,comp,len), man2(flags,comp,len) {
00167     theta = man1.getCtrlMax()/2;
00168     flip = false;
00169   }
00170 
00171   virtual void setup(MeasurePass& pass) {
00172     man1.setup(pass);
00173   }
00174 
00175   virtual void measure(MeasurePass& pass, int ctrl) {
00176     /*
00177     if (ctrl==theta) {
00178       int h = pass.asel.height();
00179       int hits = 0;
00180       for (int y=0; y<h; y++) {
00181         if (pass.asel.cell(0,y)!=-1) hits++;
00182       }
00183       if (hits<h*0.25) {
00184         flip = true;
00185       }
00186     }
00187     */
00188     if (ctrl>=theta) {
00189       man2.measure(pass,ctrl-theta);
00190     } else {
00191       man1.measure(pass,ctrl);
00192     }
00193       /*
00194     if (!flip) {
00195       man1.measure(pass,ctrl);
00196     } else {
00197       man2.measure(pass,ctrl);
00198     }
00199       */
00200   }
00201 
00202   virtual int getCtrlMax() {
00203     return man1.getCtrlMax();
00204   }
00205 
00206 };
00207 
00208 #endif
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines