Polaris: SimBiGraphIterator.cc Source File

SimBiGraphIterator.cc

Go to the documentation of this file.
00001 ///
00002 /// \file SimBiGraphIterator.cc
00003 ///
00004 ///
00005 #ifdef POLARIS_GNU_PRAGMAS
00006 #pragma implementation
00007 #endif
00008 ///
00009 #include "utilities/access_util.h"
00010 #include "SimBiGraphIterator.h"
00011 
00012 /// Create an empty iterator
00013 SimBiGraphIterator::SimBiGraphIterator( ) {
00014     #ifdef CLASS_INSTANCE_REGISTRY
00015     register_instance(SIM_BIGRAPH_ITERATOR, sizeof(SimBiGraphIterator), this);
00016     #endif
00017 
00018     _graph = NULL;
00019     _descr1_exists = NULL;
00020     _descr2_exists = NULL;
00021     _check_edge = NULL;
00022     _list1_valid = false;
00023     _list2_valid = false;
00024     _node1_pos = -1;
00025     _node2_pos = -1;
00026 }
00027 
00028 /// Reset the iteration
00029 
00030 void
00031 SimBiGraphIterator::reset( ) 
00032 {
00033     /// ...  For the descrs that exist, set all _check_edge entries true.
00034 
00035     for (int i=0; i < _graph->_nodes1; ++i) {
00036     if ( (*_descr1_exists)[i] ) {
00037 
00038         for (int j=0; j < _graph->_nodes2; ++j ) {
00039         if ( (*_descr2_exists)[j] ) {
00040             
00041             ((*_check_edge)[i])[j] = true;
00042         }
00043         }
00044     }
00045     }
00046 
00047     /// ...  Search for the first valid pair of descriptors
00048 
00049     for (int i=0; i < _graph->_nodes1; ++i) {
00050     if ( (*_descr1_exists)[i] ) {
00051 
00052         for (int j=0; j < _graph->_nodes2; ++j ) {
00053         if ( (*_descr2_exists)[j] ) {
00054             
00055             _list1_valid = true;
00056             _list2_valid = true;
00057             _node1_pos = i;
00058             _node2_pos = j;
00059             return;
00060         }
00061         }
00062     }
00063     }
00064 
00065     _list1_valid = false;
00066     _list2_valid = false;
00067     _node1_pos = -1;
00068     _node2_pos = -1;
00069     return;
00070 }
00071 
00072 /// Make an iterator for an explicit graph
00073 
00074 SimBiGraphIterator::SimBiGraphIterator( SimBiGraph & graph )
00075 : _graph( &graph )
00076 {
00077     #ifdef CLASS_INSTANCE_REGISTRY
00078     register_instance(SIM_BIGRAPH_ITERATOR, sizeof(SimBiGraphIterator), this);
00079     #endif
00080 
00081     /// ...  Mark that all nodes are valid to start with
00082 
00083     _descr1_exists = new Array< bool >;
00084     _descr1_exists->resize( _graph->_nodes1 );
00085 
00086     for (int i=0; i < _graph->_nodes1; ++i) {
00087     (*_descr1_exists)[i] = true;
00088     }
00089 
00090     _descr2_exists = new Array< bool >;
00091     _descr2_exists->resize( _graph->_nodes2 );
00092 
00093     for (int i=0; i < _graph->_nodes2; ++i) {
00094     (*_descr2_exists)[i] = true;
00095     }
00096 
00097     /// ...  Initialize to check every edge
00098 
00099     _check_edge = new Array< Array< bool > >;
00100     _check_edge->resize( _graph->_nodes1 );
00101 
00102     for (int i=0; i < _graph->_nodes1; ++i) {
00103     (*_check_edge)[i].resize(_graph->_nodes2);
00104     }
00105 
00106     reset( );
00107 }
00108 
00109 /// Update the Similarity graph due to the change in a single node
00110 
00111 void
00112 SimBiGraphIterator::record_change( SimTypeOfChange change, SimEdge & edge )
00113 {
00114     int index1;
00115     int index2;
00116 
00117     switch (change) {
00118     case SIM_COALESCE_NODE1:
00119     index1 = edge.index1();
00120     for (int i=0; i<_graph->_nodes1; ++i) {
00121         ((*(_graph->_graph))[index1])[i].type( SIM_UNKNOWN );
00122         ((*_check_edge)[index1])[i] = true;
00123 
00124         ((*(_graph->_graph))[i])[index1].type( SIM_UNKNOWN );
00125         ((*_check_edge)[i])[index1] = true;
00126     }
00127     break;
00128 
00129     case SIM_COALESCE_NODE2:
00130     index2 = edge.index2();
00131     for (int i=0; i<_graph->_nodes1; ++i) {
00132         ((*(_graph->_graph))[index2])[i].type( SIM_UNKNOWN );
00133         ((*_check_edge)[index2])[i] = true;
00134 
00135         ((*(_graph->_graph))[i])[index2].type( SIM_UNKNOWN );
00136         ((*_check_edge)[i])[index2] = true;
00137     }
00138     break;
00139 
00140     case SIM_CONTIG_NODE1:
00141     index1 = edge.index1();
00142     index2 = edge.index2();
00143     (*_descr2_exists)[index2] = false;   /// ...  delete node index2
00144     for (int i=0; i<_graph->_nodes1; ++i) {
00145         SimEdgeKernel & kernel = ((*(_graph->_graph))[index1])[i];
00146         kernel.type( contig_type( kernel.type( ) ) );
00147         ((*_check_edge)[index1])[i] = true;
00148 
00149         SimEdgeKernel & kernel2 = ((*(_graph->_graph))[i])[index1];
00150         kernel2.type( contig_type( kernel2.type( ) ) );
00151         ((*_check_edge)[i])[index1] = true;
00152     }
00153     break;
00154 
00155     case SIM_CONTIG_NODE2:
00156     index1 = edge.index1();
00157     index2 = edge.index2();
00158     (*_descr1_exists)[index1] = false;   /// ...  delete node index1
00159     for (int i=0; i<_graph->_nodes1; ++i) {
00160         SimEdgeKernel & kernel = ((*(_graph->_graph))[index2])[i];
00161         kernel.type( contig_type( kernel.type( ) ) );
00162         ((*_check_edge)[index2])[i] = true;
00163 
00164         SimEdgeKernel & kernel2 = ((*(_graph->_graph))[i])[index2];
00165         kernel2.type( contig_type ( kernel2.type( ) ) );
00166         ((*_check_edge)[i])[index2] = true;
00167     }
00168     break;
00169 
00170     case SIM_EQUIVALENCE:
00171     index2 = edge.index2();
00172     (*_descr2_exists)[index2] = false;   /// ...  delete node index2
00173     break;
00174     case SIM_NOCHANGE:
00175     break;
00176     default:
00177     break;
00178     }
00179 
00180     return;
00181 }
00182 
00183 SimBiGraphIterator::~SimBiGraphIterator( )
00184 {
00185     #ifdef CLASS_INSTANCE_REGISTRY
00186     unregister_instance(SIM_BIGRAPH_ITERATOR, this);
00187     #endif
00188 
00189     if (_descr1_exists) {
00190     delete _descr1_exists;
00191     _descr1_exists = 0;
00192     }
00193 
00194     if (_descr2_exists) {
00195     delete _descr2_exists;
00196     _descr2_exists = 0;
00197     }
00198 
00199     if (_check_edge) {
00200     delete _check_edge;
00201     _check_edge = 0;
00202     }
00203 }
00204 
00205 int 
00206 SimBiGraphIterator::current_list1( )
00207 {
00208     return _node1_pos;
00209 }
00210 
00211 int 
00212 SimBiGraphIterator::current_list2( )
00213 {
00214     return _node2_pos;
00215 }
00216 
00217 SimEdge & 
00218 SimBiGraphIterator::current( )
00219 {
00220     p_assert( _list1_valid && _list2_valid, 
00221          "SimBiGraphIterator not valid on call to current()");
00222 
00223     SimEdgeKernel & kernel = ((*(_graph->_graph))[_node1_pos])[_node2_pos];
00224     SimilarityType type = kernel.type();
00225 
00226     if ((type == SIM_UNKNOWN) ||
00227     (type == SIM_DIM_EQUIV_UP) ||
00228     (type == SIM_STRIDE_EQUIV_UP)) {
00229 
00230     _graph->calc_similarity( _node1_pos, _node2_pos );
00231     }
00232 
00233     SimEdge * edge = new SimEdge( _node1_pos, _node2_pos,
00234                  &(*(*(_graph->_ptrs1))[_node1_pos]), 
00235                  &(*(*(_graph->_ptrs2))[_node2_pos]), 
00236                  &kernel);
00237     _created_edges.ins_last(edge);
00238 
00239     /// ...  Mark that this edge has been checked
00240 
00241     ((*_check_edge)[_node1_pos])[_node2_pos] = false;
00242 
00243     return *edge;
00244 }
00245 
00246 bool
00247 SimBiGraphIterator::list1_valid( )
00248 {
00249     return _list1_valid;
00250 }
00251 
00252 bool
00253 SimBiGraphIterator::list2_valid( )
00254 {
00255     return _list2_valid;
00256 }
00257 
00258 void
00259 SimBiGraphIterator::next_list1_node( )
00260 {
00261     int nodes1 = _graph->_nodes1;
00262     int nodes2 = _graph->_nodes2;
00263 
00264     /// ...  Find the next edge to visit
00265 
00266     int start_i = _node1_pos+1;
00267 
00268     /// ...  Reset list2
00269     _node2_pos = 0;
00270     int start_j = _node2_pos;
00271 
00272     _list2_valid = false;
00273 
00274     for (int j=start_j; j < nodes2; ++j) {
00275     if ( (*_descr2_exists)[j]) {
00276 
00277         _list2_valid = true;
00278         _node2_pos = j;
00279         break;
00280     }
00281     }
00282     if (_list2_valid) {
00283     for (int i=start_i; i < nodes1; ++i) {
00284         if ( (*_descr1_exists)[i] &&
00285         ((*_check_edge)[i])[start_j] ) {
00286 
00287         _list1_valid = true;
00288         _node1_pos = i;
00289         return;
00290         }
00291     }
00292     }
00293     _list1_valid = false;
00294     _node1_pos = 0;
00295  
00296     return;
00297 }
00298 
00299 void
00300 SimBiGraphIterator::next_list2_node( )
00301 {
00302     int nodes2 = _graph->_nodes2;
00303 
00304     /// ...  Find the next edge to visit
00305 
00306     int start_i = _node1_pos;
00307     int start_j = _node2_pos+1;
00308 
00309     for (int j=start_j;  j < nodes2; ++j) {
00310     if ((*_descr2_exists)[j] &&
00311         ((*_check_edge)[start_i])[j] ) {
00312 
00313         _list2_valid = true;
00314         _node2_pos = j;
00315         return;
00316     }
00317     }
00318     _list2_valid = false;
00319     _node2_pos = 0;
00320  
00321     return;
00322 }
00323 
00324 void
00325 SimBiGraphIterator::delete_node1( int node )
00326 {
00327     (*_descr1_exists)[node] = false;
00328 }
00329 
00330 void
00331 SimBiGraphIterator::delete_node2( int node )
00332 {
00333     (*_descr2_exists)[node] = false;
00334 }
00335 
00336 void
00337 SimBiGraphIterator::print(ostream & o) const 
00338 {
00339     o << "{ ";
00340     if (_list1_valid) {
00341     o << "LIST1 VALID;";
00342     }
00343     if (_list2_valid) {
00344     o << "LIST2 VALID;";
00345     }
00346 
00347     if (!_list1_valid && !_list2_valid) {
00348     o << "NOT VALID;";
00349     }
00350 
00351     o << "(" << _node1_pos << "," << _node2_pos << ") }";
00352 }
00353 
00354 SimBiGraphIterator *
00355 SimBiGraphIterator::clone() const
00356 {
00357     return new SimBiGraphIterator( (SimBiGraphIterator &) *this );
00358 }
00359 
00360 Listable *
00361 SimBiGraphIterator::listable_clone() const
00362 {
00363     return (Listable *) clone();
00364 }
00365 
00366 ostream & 
00367 operator << (ostream & o, const SimBiGraphIterator & iter) 
00368 {
00369     iter.print(o);
00370     return o;
00371 }
 © 1995-2005 University of Illinois, Urbana-Champaign. All rights reserved.  Fri Mar 25 23:06:07 2005