/** * search for the pattern in the given list, if found return a pointer to its similar pattern */ Pattern* CLMap::exists(Pattern* pattern, list<Pattern*>* v) { if(Settings::debugMSG) { cout<<"CLMap::exists starts..."<<endl; } for(list<Pattern*>::iterator iter = v->begin();iter!=v->end();iter++) { Pattern* temp = *iter; if(theSame(pattern, temp)) { if(Settings::debugMSG) { cout<<"CLMap::exists finished, pattern: "<<temp->getID()<<" found."<<endl; } return temp; } } if(Settings::debugMSG) { cout<<"CLMap::exists finished, no pattern found."<<endl; } return 0; }
CLiveInOutCalculator::CLiveInOutCalculator( const std::list<const CBaseInstruction*>& asmFunction ) : workflow( asmFunction ), liveIn( workflow.Size() ), liveOut( workflow.Size() ) { bool setsChanged = true; int mainFuncIndex = 0; std::vector<int> revTopsort = CTopSort::topSort( workflow, mainFuncIndex ); buildCommands( asmFunction ); buildDefines( asmFunction ); buildUses( asmFunction ); std::reverse( revTopsort.begin(), revTopsort.end() ); while( setsChanged ) { setsChanged = false; for( auto nodeIndex : revTopsort ) { std::set<std::string> newLiveIn = liveOut[nodeIndex]; auto currList = commands[nodeIndex]->DefinedVars(); while( currList != nullptr && currList->Head() != nullptr ) { auto inSet = newLiveIn.find( currList->Head()->GetName()->GetString() ); if( inSet != newLiveIn.end() ) { newLiveIn.erase( inSet ); } currList = currList->Tail(); } currList = commands[nodeIndex]->UsedVars(); while( currList != nullptr && currList->Head() != nullptr ) { newLiveIn.insert( currList->Head()->GetName()->GetString() ); currList = currList->Tail(); } std::set<std::string> newLiveOut; for( auto succ : workflow.GetNode( nodeIndex ).out ) { for( auto var : liveIn[succ] ) { newLiveOut.insert( var ); } } if( !theSame( newLiveIn, liveIn[nodeIndex] ) || !theSame( newLiveOut, liveOut[nodeIndex] ) ) { setsChanged = true; liveIn[nodeIndex] = newLiveIn; liveOut[nodeIndex] = newLiveOut; } } } }
/* * remove a pattern if found, otherwise no effect and return false * return true only if the pattern is removed */ bool CLMap::remove(Pattern* pattern, list<Pattern*>* v) { for(list<Pattern*>::iterator iter = v->begin();iter!=v->end();iter++) { Pattern* temp = *iter; if(theSame(pattern, temp)) { v->erase(iter); return true; } } return false; }
bool isSymmetric(TreeNode *root) { // Start typing your C/C++ solution below // DO NOT write int main() function if(!root) return true; return theSame(root->left,root->right); }
bool theSame(TreeNode *a,TreeNode* b){ if(!a||!b) return a==b; else return a->val==b->val && theSame(a->left,b->right) && theSame(a->right,b->left); }