Inferno
0.2
|
00001 #include "coupling.hpp" 00002 #include "helpers/walk.hpp" 00003 #include "search_replace.hpp" 00004 00005 CouplingKeys::CouplingKeys() : 00006 master(NULL) 00007 { 00008 } 00009 00010 void CouplingKeys::DoKey( TreePtr<Node> x, 00011 TreePtr<Node> pattern, 00012 Conjecture::Choice *gc, 00013 int go ) 00014 { 00015 // INDENT; 00016 shared_ptr<Key> key( new Key ); 00017 if( x ) 00018 key->root = x; 00019 else 00020 key = shared_ptr<Key>(); 00021 return DoKey( key, pattern, gc, go ); 00022 } 00023 00024 00025 void CouplingKeys::DoKey( shared_ptr<Key> key, 00026 TreePtr<Node> pattern, 00027 Conjecture::Choice *gc, 00028 int go ) 00029 { 00030 //INDENT; 00031 ASSERT( this ); 00032 ASSERT( pattern ); 00033 00034 // TRACE("CouplingKeys@%p: ", this)(master?"has ":"does not have ")("master\n"); 00035 00036 if( key ) 00037 { 00038 ASSERT( key->root ); 00039 } 00040 00041 #if 0 00042 TRACE("coupling={"); 00043 bool first=true; 00044 FOREACH( TreePtr<Node> n, coupling ) 00045 { 00046 if( !first ) 00047 TRACE(", "); 00048 if( pattern == n ) 00049 TRACE("-->"); 00050 TRACE(*n); 00051 first=false; 00052 } 00053 TRACE("} key ptr=%p\n", keys_map[coupling].get()); // TODO put this in as a common utility somewhere 00054 #endif 00055 00056 // If we're keying and we haven't keyed this node so far, key it now 00057 if( key && !GetKey( pattern ) ) 00058 { 00059 key->replace_pattern = pattern; 00060 key->governing_choice = gc; 00061 key->governing_offset = go; 00062 keys_map[pattern] = key; 00063 // TRACE("Keyed root=")(*key->root)(" pattern=")(*key->replace_pattern)(" with governing_choice=%p\n", gc); 00064 } 00065 00066 // TRACE("@%p Keyed ", this)(*(key->root))(" size %d\n", keys_map.size()); 00067 } 00068 00069 00070 TreePtr<Node> CouplingKeys::GetCoupled( TreePtr<Node> pattern ) 00071 { 00072 // INDENT; 00073 shared_ptr<Key> k = GetKey( pattern ); 00074 if( k ) 00075 return k->root; 00076 else 00077 return TreePtr<Node>(); // TODO may never happen now, try assert 00078 } 00079 00080 00081 shared_ptr<Key> CouplingKeys::GetKey( TreePtr<Node> pattern ) 00082 { 00083 // INDENT; 00084 //TRACE("@%p Getting key for ", this)(*pattern)(" master is %p size %d\n", master, keys_map.size()); 00085 if( keys_map.IsExist(pattern) ) 00086 { 00087 return keys_map[pattern]; 00088 } 00089 else if( master ) 00090 { 00091 // TRACE("Going to master to get key for ")(*pattern)("\n"); 00092 shared_ptr<Key> k = master->GetKey(pattern); 00093 // if( k ) 00094 // TRACE("Got root ")(*(k->root))("\n"); 00095 // else 00096 // TRACE("Didn't get key\n"); 00097 return k; 00098 } 00099 else 00100 return shared_ptr<Key>(); // TODO may never happen now, try assert 00101 } 00102 00103 00104 Set< TreePtr<Node> > CouplingKeys::GetAllKeys() 00105 { 00106 // INDENT; 00107 Set< TreePtr<Node> > s; 00108 UniqueFilter uf; 00109 // TRACE("Key nodes:\n"); 00110 typedef pair< TreePtr<Node>, shared_ptr<Key> > Pc; 00111 // iterate over out couplings 00112 FOREACH( Pc p, keys_map ) 00113 { 00114 ASSERT( p.first ); 00115 if( p.second ) // TODO make this always be non-NULL 00116 { 00117 // TRACE("Coupling of ")(*(p.first))(": "); 00118 Walk e(p.second->root, &uf); 00119 // Iterate over every node in the subtree under the key 00120 FOREACH( TreePtr<Node> n, e ) 00121 { 00122 s.insert( n ); 00123 TRACE(*n)(" "); 00124 } 00125 TRACE("\n"); 00126 } 00127 } 00128 00129 if( master ) 00130 { 00131 // Fold in the results of calling the master 00132 Set< TreePtr<Node> > ms = master->GetAllKeys(); 00133 FOREACH( TreePtr<Node> n, ms ) 00134 { 00135 s.insert( n ); 00136 TRACE(*n)(" "); 00137 } 00138 } 00139 00140 return s; 00141 } 00142 00143 00144 void CouplingKeys::SetMaster( CouplingKeys *m ) 00145 { 00146 // INDENT; 00147 // TRACE("@%p Setting master to %p\n", this, m); 00148 master = m; 00149 } 00150 00151 00152 void CouplingKeys::Clear() 00153 { 00154 // INDENT; 00155 // TRACE("@%p Clearing keys\n", this); 00156 keys_map.clear(); 00157 } 00158