Main Page Namespace List Class Hierarchy Compound List File List Namespace Members Compound Members File Members Related Pages

# matchingdatabase.cpp

Go to the documentation of this file.

    00001
    00043 #include "matchingdatabase.hpp"
    00044
    00045 ostream& DAGPrototype::write(ostream& os)
    00046 {
    00047         os.write(&count,sizeof(count));
    00048         os.write(&view,sizeof(view));
    00049
    00050         return os;
    00051 }
    00052
    00053 istream& DAGPrototype::read(istream& is)
    00054 {
    00055         is.read(&count,sizeof(count));
    00056         is.read(&view, sizeof(view));
    00057
    00058         return is;
    00059 }
    00060
    00061 ostream& DAGType::write(ostream& os)
    00062 {
    00063         size_t t;
    00064         os.write(&count,sizeof(count));
    00065
    00066         t = name.length() + 1;
    00067         os.write(&t,sizeof(t));
    00068         os.write(name.c_str(),t);
    00069
    00070         return os;
    00071 }
    00072
    00073 istream& DAGType::read(istream& is)
    00074 {
    00075         size_t t;
    00076         char* namestr;
    00077
    00078         is.read(&count, sizeof(count));
    00079         is.read(&t, sizeof(t));
    00080
    00081         namestr = new char[t];
    00082
    00083         is.read(namestr, t);
    00084         name = string(namestr);
    00085
    00086         delete [] namestr;
    00087
    00088         return is;
    00089 }
    00090
    00091 ostream& DAGView::write(ostream& os)
    00092 {
    00093         os.write(&graph,sizeof(graph));
    00094         os.write(&type,sizeof(type));
    00095         os.write(&region,sizeof(region));
    00096
    00097         return os;
    00098 }
    00099
    00100 istream& DAGView::read(istream& is)
    00101 {
    00102         is.read(&graph, sizeof(graph));
    00103         is.read(&type, sizeof(type));
    00104         is.read(&region, sizeof(region));
    00105
    00106         return is;
    00107 }
    00108
    00109 DAGView DAGRegion::GetPrototype()
    00110 {
    00111 }
    00112
    00113 ostream& DAGRegion::write(ostream& os)
    00114 {
    00115   size_t size;
    00116   ID id;
    00117
    00118   size = views.size();
    00119
    00120   os.write(&size, sizeof(size));
    00121
    00122   for(; size > 0; size--)
    00123     {
    00124           id = views[size];
    00125       os.write(&id, sizeof(id));
    00126     }
    00127
    00128   return os;
    00129 }
    00130
    00131 istream& DAGRegion::read(istream& is)
    00132 {
    00133         size_t size;
    00134         ID id;
    00135
    00136         //remove all existing records
    00137         views.clear();
    00138
    00139         //read in the size of the database
    00140         is.read(&size, sizeof(size));
    00141         views.resize(size);
    00142
    00143         for(; size > 0; size--)
    00144         {
    00145                 is.read(&id, sizeof(id));
    00146                 views[size] = id;
    00147         }
    00148
    00149         return is;
    00150 }
    00151
    00152 DAGSearchDatabase::DAGSearchDatabase()
    00153 {
    00154         nnd.name = "";
    00155         nnd.data = HnSRTreeFile::null;
    00156         nnd.dimension = 0;
    00157 }
    00158
    00159 DAGSearchDatabase::~DAGSearchDatabase()
    00160 {
    00161   if(nnd.data!=HnSRTreeFile::null)
    00162     {
    00163       nnd.data.close();
    00164     }
    00165 }
    00166
    00167 void DAGSearchDatabase::Add(ID prototype, const DAG& dag)
    00168 {
    00169         leda_node v;
    00170         DAGSearchRecord record;
    00171         TSV h;
    00172         HnPoint pn;
    00173
    00174         record.prototype = prototype;
    00175
    00176         //Check if database has been activated
    00177         if(nnd.data!=HnSRTreeFile::null)
    00178         {
    00179                 //for each node in the dag class record its TSV to the NND
    00180                 forall_nodes(v, dag)
    00181             {
    00182                         record.node = dag.GetNodeIndex(v);
    00183                         h = dag.GetNodeTSV(v, nnd.dimension);
    00184                         pn = TSVtoHnPoint(h);
    00185                         nnd.data.store(pn, new_HnDataItem((void*)(&record),sizeof(DAGSearchRecord)));
    00186             }
    00187         }
    00188 }
    00189
    00190 void  DAGSearchDatabase::Remove(ID prototype, DAG& dag)
    00191 {
    00192         leda_node v;
    00193
    00194         DAGSearchRecord record;
    00195         record.prototype = prototype;
    00196
    00197         //Check if database has been activated
    00198         if(nnd.data!=HnSRTreeFile::null)
    00199         {
    00200           forall_nodes(v, dag)
    00201           {
    00202                   record.node = dag.GetNodeIndex(v);
    00203                   // I am not sure if I should allocate mdemory for each search record
    00204                   nnd.data.remove(TSVtoHnPoint(dag.GetNodeTSV(v,nnd.dimension)), new_HnDataItem((void*)(&record),sizeof(DAGSearchRecord)));
    00205           }
    00206         }
    00207 }
    00208
    00216 SmartArray<DAGSearchRecordEx> DAGSearchDatabase::GetClosest(const DAG& dag, const leda_node& querynode, int n)
    00217 {
    00218         SmartArray<DAGSearchRecordEx> closestDAGs;
    00219
    00220         //Check if database has been activated
    00221         if(nnd.data != HnSRTreeFile::null && n >= 1)
    00222         {
    00223                 int i, size;
    00224                 HnPoint pp;
    00225                 TSV tsv;
    00226                 DAGSearchRecordEx sr;
    00227
    00228                 HnPointVector points;
    00229                 HnDataItemVector dataItems;
    00230
    00231                 tsv = dag.GetNodeTSV(querynode, nnd.dimension);
    00232
    00233                 pp = TSVtoHnPoint(tsv);
    00234
    00235                 nnd.data.getNeighbors(pp, n, &points, &dataItems);
    00236
    00237                 closestDAGs.ReSize(size = dataItems.size());
    00238
    00239                 for(i = 0; i < size; i++)
    00240                 {
    00241                         sr = *(DAGSearchRecord*)(dataItems.elementAt(i).toCharArray());
    00242                         sr.nodeTSV = HnPointToTSV(points.elementAt(i));
    00243                         closestDAGs.Add(sr);
    00244                 }
    00245         }
    00246
    00247         return closestDAGs;
    00248 }
    00249
    00250 bool DAGSearchDatabase::Create(string& name, int dim)
    00251 {
    00252     //check if the dimensions are larger or equal to the minimal dimensions allowed
    00253     if(dim < Min_NND_Dimension) return false;
    00254
    00255     //Check if database has been activated
    00256         if(nnd.data==HnSRTreeFile::null)
    00257         {
    00258           //create the near neighbor database
    00259           nnd.data = new_HnSRTreeFile(name.c_str(), dim, sizeof(DAGSearchRecord), HnProperties::null);
    00260
    00261           //check if the database was created successful
    00262           if(nnd.data == HnSRTreeFile::null)
    00263             {
    00264               //return false if an error occured
    00265               return false;
    00266             }
    00267           else
    00268             {
    00269               nnd.name = name;
    00270               nnd.dimension = dim;
    00271             }
    00272         }
    00273         else
    00274         {
    00275           return false;
    00276         }
    00277         return true;
    00278 }
    00279
    00280 bool DAGSearchDatabase::Open(string& name)
    00281 {
    00282   //close the indexing database
    00283   if(nnd.data!=HnSRTreeFile::null)
    00284   {
    00285     nnd.data.close();
    00286   }
    00287
    00288   nnd.data = new_HnSRTreeFile(name.c_str(), "rw");
    00289   if(nnd.data == HnSRTreeFile::null) return false;
    00290
    00291   nnd.name = name;
    00292   nnd.dimension = nnd.data.getDimension();
    00293   return true;
    00294 }
    00295
    00296 istream&  DAGSearchDatabase::Read(istream& is)
    00297 {
    00298   char* namestr;
    00299   size_t t;
    00300   string name;
    00301   int dim;
    00302
    00303   //close the indexing database
    00304   if(nnd.data!=HnSRTreeFile::null)
    00305   {
    00306     nnd.data.close();
    00307   }
    00308
    00309   is.read(&t,sizeof(t));
    00310   namestr = new char[t];
    00311
    00312   is.read(namestr, t);
    00313   name = string(namestr);
    00314   delete [] namestr;
    00315
    00316   is.read(&dim,sizeof(dim));
    00317
    00318   //load the near neighbor database
    00319   nnd.data = new_HnSRTreeFile(name.c_str(), "rw");
    00320
    00321   //check if the database was created successfuly
    00322   if(nnd.data == HnSRTreeFile::null)
    00323     {
    00324       //if error occured set stream state to fail and return it
    00325       is.setstate(ios::failbit);
    00326       return is;
    00327     }
    00328
    00329      nnd.name = name;
    00330      nnd.dimension = dim;
    00331
    00332      return is;
    00333 }
    00334
    00335 ostream&  DAGSearchDatabase::Write(ostream& os)
    00336 {
    00337   size_t t;
    00338
    00339   t = nnd.name.length() + 1;
    00340
    00341   os.write(&t,sizeof(t));
    00342   os.write(nnd.name.c_str(), t);
    00343   os.write(&nnd.dimension,sizeof(nnd.dimension));
    00344
    00345   return os;
    00346 }
    00347
    00348 HnPoint DAGSearchDatabase::TSVtoHnPoint(const TSV& tsv)
    00349 {
    00350         int d = tsv.GetSize();
    00351         HnPoint hp = new_HnPoint(d);
    00352
    00353         for(int i = 0; i < d; i++)
    00354                 hp.setCoordAt(tsv[i], i);
    00355
    00356         return hp;
    00357 }
    00358
    00359 TSV DAGSearchDatabase::HnPointToTSV(const HnPoint& pt)
    00360 {
    00361         int d = pt.getDimension();
    00362         TSV tsv(d);
    00363
    00364         for(int i = 0; i < d; i++)
    00365                 tsv[i] = pt.getCoordAt(i);
    00366
    00367         return tsv;
    00368 }