///////////////////////////////////////////// // MT1Q5a.cpp - Scope class #include "MT1Q5a.h" #include #include #include #include int main() { std::cout << "\n MT1Q5a - Scope class and sorting by complexity"; std::cout << "\n ================================================\n"; IScope* ps1 = IScope::Create("Scope1", "function", 6); IScope* ps2 = IScope::Create("Scope2", "for", 3); IScope* ps3 = IScope::Create("Scope3", "for", 2); IScope* ps4 = IScope::Create("Scope4", "if", 1); IScope* ps5 = IScope::Create("Scope5", "if", 1); IScope* ps6 = IScope::Create("Scope6", "if", 1); IScope* ps7 = IScope::Create("Scope7", "for", 1); // sorting vector of scopes using Scopes = std::vector; Scopes scopes{ ps6, ps4, ps3, ps1, ps2, ps5 }; std::function less = [](IScope* pS1, IScope* pS2) { return pS1->operator<(pS2); }; std::sort(std::begin(scopes), std::end(scopes), less); for (auto pScope : scopes) { std::cout << "\n " << pScope->name() << " - complexity = " << pScope->complexity(); std::cout << ", type = " << pScope->type(); } // non-stressing test of scope matching std::cout << "\n"; for (auto pScope : scopes) { if (ps2->match(pScope)) std::cout << "\n " << pScope->name() << " matches " << ps2->name(); else std::cout << "\n " << pScope->name() << " does not match " << ps2->name(); } delete ps1; delete ps2; delete ps3; delete ps4; delete ps5; delete ps6; delete ps7; std::function compareComplexity = [](const IScope* pS1, const IScope* pS2) -> bool { IScope* pNCS1 = const_cast(pS1); IScope* pNCS2 = const_cast(pS2); return pNCS1->complexity() > pNCS2->complexity(); }; Scope s1{ "Scope1", "function", 6 }; Scope s2{ "Scope2", "for", 3 }; Scope s3{ "Scope3", "if", 1 }; Scope s4{ "Scope4", "while", 2 }; Scope s5{ "Scope5", "if", 1 }; Scope s6{ "Scope6", "else", 1 }; std::cout << "\n"; std::cout << "\n Using IScopeCollection"; std::cout << "\n ------------------------"; using IScopeColl = IScopeCollection; IScopeColl* pISC = IScopeColl::create(); pISC->add(&s4).add(&s6).add(&s3).add(&s2).add(&s1).add(&s5); pISC->sort(compareComplexity); for (auto pScope : *pISC) std::cout << "\n " << pScope->name() << ", " << pScope->complexity() << ", " << pScope->type(); std::cout << "\n"; std::cout << "\n Using ScopeCollection"; std::cout << "\n -----------------------"; ScopeCollection sc{ &s4, &s6, &s3, &s1, &s2, &s5 }; sc.sort(compareComplexity); for (auto pScope : sc) std::cout << "\n " << pScope->name() << ", " << pScope->complexity() << ", " << pScope->type(); std::cout << "\n\n"; }