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

gmvl_node_cache.cxx

Go to the documentation of this file.
00001 // This is gel/gmvl/gmvl_node_cache.cxx
00002 #ifdef VCL_NEEDS_PRAGMA_INTERFACE
00003 #pragma implementation
00004 #endif
00005 //:
00006 // \file
00007 // \author crossge@crd.ge.com
00008 
00009 #include "gmvl_node_cache.h"
00010 #include <vcl_iostream.h>
00011 
00012 // constructors and destructors
00013 gmvl_node_cache::gmvl_node_cache()
00014 {
00015 }
00016 
00017 gmvl_node_cache::~gmvl_node_cache()
00018 {
00019 }
00020 
00021 // trivial accessors
00022 void gmvl_node_cache::add( const gmvl_node_sptr node)
00023 {
00024   node->ref_= nodes_.size();
00025   nodes_.push_back( node);
00026 
00027   // add to cache
00028   bool found= false;
00029 
00030   for (unsigned int j=0; j< typecache_.size() && !found; ++j)
00031   {
00032     if (typecache_[j].first== node->type_)
00033     {
00034       typecache_[j].second.push_back( node);
00035       found= true;
00036     }
00037   }
00038 
00039   if (!found)
00040   {
00041     vcl_pair<vcl_string,vcl_vector<gmvl_node_sptr> > pair;
00042 
00043     pair.first= node->type_;
00044     pair.second.push_back( node);
00045 
00046     typecache_.push_back( pair);
00047   }
00048 }
00049 
00050 void gmvl_node_cache::remove( const gmvl_node_sptr node)
00051 {
00052   vcl_vector<gmvl_node_sptr> newnodes;
00053 
00054   for (unsigned int i=0; i< nodes_.size(); ++i)
00055   {
00056     if (nodes_[i].ptr()!= node.ptr())
00057     {
00058       nodes_[i]->ref_= newnodes.size();
00059       newnodes.push_back( nodes_[i]);
00060     }
00061   }
00062 
00063   nodes_= newnodes;
00064 
00065   rebuild();
00066 }
00067 
00068 bool gmvl_node_cache::cached( const gmvl_node_sptr node) const
00069 {
00070   if (node->ref_== -1)
00071     return false;
00072 
00073   return true;
00074 }
00075 
00076 // clever accessors
00077 
00078 vcl_vector<gmvl_node_sptr> gmvl_node_cache::get( const vcl_string type) const
00079 {
00080   vcl_vector<gmvl_node_sptr> empty;
00081 
00082   for (unsigned int i=0; i< typecache_.size(); ++i)
00083   {
00084     if (typecache_[i].first== type)
00085     {
00086       return typecache_[i].second;
00087     }
00088   }
00089 
00090   return empty;
00091 }
00092 
00093 void gmvl_node_cache::rebuild()
00094 {
00095   typecache_.clear();
00096 
00097   for (unsigned int i=0; i< nodes_.size(); ++i)
00098   {
00099     bool found= false;
00100 
00101     for (unsigned int j=0; j< typecache_.size() && !found; ++j)
00102     {
00103       if (typecache_[j].first== nodes_[i]->type_)
00104       {
00105         typecache_[j].second.push_back( nodes_[i]);
00106         found= true;
00107       }
00108     }
00109 
00110     if (!found)
00111     {
00112       vcl_pair<vcl_string,vcl_vector<gmvl_node_sptr> > pair;
00113 
00114       pair.first= nodes_[i]->type_;
00115       pair.second.push_back( nodes_[i]);
00116 
00117       typecache_.push_back( pair);
00118     }
00119   }
00120 }
00121 
00122 // input and output
00123 vcl_ostream &operator<<( vcl_ostream &os, const gmvl_node_cache &c)
00124 {
00125   for (unsigned int i=0; i< c.nodes_.size(); ++i)
00126     os << *c.nodes_[i] << ' ';
00127 
00128   return os;
00129 }

Generated on Thu Jan 10 14:47:45 2008 for contrib/gel/gmvl by  doxygen 1.4.4