From 511b15509f21d5b1a52e7e77a28eee5702df8d83 Mon Sep 17 00:00:00 2001 From: Jan Travnicek <Jan.Travnicek@fit.cvut.cz> Date: Sat, 3 May 2014 09:47:25 +0200 Subject: [PATCH] operator< using linear number of methods --- alib2/src/regexp/Alternation.cpp | 28 ++++++++++++---------------- alib2/src/regexp/Alternation.h | 1 - alib2/src/regexp/Iteration.cpp | 8 -------- alib2/src/regexp/Iteration.h | 2 -- alib2/src/regexp/RegExpElement.cpp | 15 +++++++++------ alib2/src/regexp/RegExpEmpty.cpp | 20 ++------------------ alib2/src/regexp/RegExpEmpty.h | 6 +----- alib2/src/regexp/RegExpEpsilon.cpp | 16 ++-------------- alib2/src/regexp/RegExpEpsilon.h | 5 +---- alib2/src/regexp/RegExpSymbol.cpp | 18 +++++------------- alib2/src/regexp/RegExpSymbol.h | 6 ++---- 11 files changed, 34 insertions(+), 91 deletions(-) diff --git a/alib2/src/regexp/Alternation.cpp b/alib2/src/regexp/Alternation.cpp index 3140e27bc7..b70f17e488 100644 --- a/alib2/src/regexp/Alternation.cpp +++ b/alib2/src/regexp/Alternation.cpp @@ -93,10 +93,6 @@ bool Alternation::operator>(const RegExpElement& other) const { } -bool Alternation::operator<(const Concatenation&) const { - return true; -} - bool Alternation::operator<(const Alternation& other) const { int thisSize = this->elements.size(); int otherSize = other.elements.size(); @@ -113,6 +109,18 @@ bool Alternation::operator<(const Alternation& other) const { return **thisIter < **otherIter; } +bool Alternation::operator==(const Alternation& other) const { + if(this->elements.size() != other.elements.size()) return false; + + auto thisIter = this->elements.begin(); + auto otherIter = other.elements.begin(); + for(; thisIter != this->elements.end(); thisIter++, otherIter++) { + if(**thisIter != **otherIter) return false; + } + + return true; +} + bool Alternation::testSymbol( const alphabet::Symbol & symbol ) const { for(const auto& child : this->elements) if(child->testSymbol(symbol)) return true; @@ -131,18 +139,6 @@ void Alternation::computeMinimalAlphabet( std::set<alphabet::Symbol>& alphabet ) child->computeMinimalAlphabet(alphabet); } -bool Alternation::operator==(const Alternation& other) const { - if(this->elements.size() != other.elements.size()) return false; - - auto thisIter = this->elements.begin(); - auto otherIter = other.elements.begin(); - for(; thisIter != this->elements.end(); thisIter++, otherIter++) { - if(**thisIter != **otherIter) return false; - } - - return true; -} - void Alternation::operator>>(std::ostream& out) const { out << "(Alternation"; for(const auto& e : elements) diff --git a/alib2/src/regexp/Alternation.h b/alib2/src/regexp/Alternation.h index dfece1ff41..99bef9c72b 100644 --- a/alib2/src/regexp/Alternation.h +++ b/alib2/src/regexp/Alternation.h @@ -75,7 +75,6 @@ public: virtual bool operator==(const RegExpElement&) const; virtual bool operator>(const RegExpElement&) const; - virtual bool operator<(const Concatenation&) const; virtual bool operator<(const Alternation&) const; virtual bool operator==(const Alternation&) const; diff --git a/alib2/src/regexp/Iteration.cpp b/alib2/src/regexp/Iteration.cpp index 7119754ecd..b3b19fcc56 100644 --- a/alib2/src/regexp/Iteration.cpp +++ b/alib2/src/regexp/Iteration.cpp @@ -87,14 +87,6 @@ bool Iteration::operator>(const RegExpElement& other) const { return other < *this; } -bool Iteration::operator<(const Concatenation&) const { - return true; -} - -bool Iteration::operator<(const Alternation&) const { - return true; -} - bool Iteration::operator<(const Iteration& other) const { return *(this->element) < *(other.element); } diff --git a/alib2/src/regexp/Iteration.h b/alib2/src/regexp/Iteration.h index 985e9d6505..d1d21a7b2c 100644 --- a/alib2/src/regexp/Iteration.h +++ b/alib2/src/regexp/Iteration.h @@ -69,8 +69,6 @@ public: virtual bool operator==(const RegExpElement&) const; virtual bool operator>(const RegExpElement&) const; - virtual bool operator<(const Concatenation&) const; - virtual bool operator<(const Alternation&) const; virtual bool operator<(const Iteration&) const; virtual bool operator==(const Iteration&) const; diff --git a/alib2/src/regexp/RegExpElement.cpp b/alib2/src/regexp/RegExpElement.cpp index bebb38e03e..ae6a9ac24b 100644 --- a/alib2/src/regexp/RegExpElement.cpp +++ b/alib2/src/regexp/RegExpElement.cpp @@ -6,6 +6,9 @@ */ #include "RegExpElement.h" +#include <typeinfo> + +#include "RegExpElements.h" namespace regexp { @@ -31,27 +34,27 @@ bool RegExpElement::operator!=(const RegExpElement& other) const { bool RegExpElement::operator<(const Concatenation& other) const { - return false; + return typeid(this).before(typeid(other)); } bool RegExpElement::operator<(const Alternation& other) const { - return false; + return typeid(this).before(typeid(other)); } bool RegExpElement::operator<(const Iteration& other) const { - return false; + return typeid(this).before(typeid(other)); } bool RegExpElement::operator<(const RegExpSymbol& other) const { - return false; + return typeid(this).before(typeid(other)); } bool RegExpElement::operator<(const RegExpEpsilon& other) const { - return false; + return typeid(this).before(typeid(other)); } bool RegExpElement::operator<(const RegExpEmpty& other) const { - return false; + return typeid(this).before(typeid(other)); } diff --git a/alib2/src/regexp/RegExpEmpty.cpp b/alib2/src/regexp/RegExpEmpty.cpp index 916a7d1e72..aac9838617 100644 --- a/alib2/src/regexp/RegExpEmpty.cpp +++ b/alib2/src/regexp/RegExpEmpty.cpp @@ -30,24 +30,8 @@ bool RegExpEmpty::operator>(const RegExpElement& other) const { } -bool RegExpEmpty::operator<(const Concatenation&) const { - return true; -} - -bool RegExpEmpty::operator<(const Alternation&) const { - return true; -} - -bool RegExpEmpty::operator<(const Iteration&) const { - return true; -} - -bool RegExpEmpty::operator<(const RegExpSymbol&) const { - return true; -} - -bool RegExpEmpty::operator<(const RegExpEpsilon&) const { - return true; +bool RegExpEmpty::operator<(const RegExpEmpty&) const { + return false; } bool RegExpEmpty::operator==(const RegExpEmpty&) const { diff --git a/alib2/src/regexp/RegExpEmpty.h b/alib2/src/regexp/RegExpEmpty.h index e0aa81e559..52ec086c15 100644 --- a/alib2/src/regexp/RegExpEmpty.h +++ b/alib2/src/regexp/RegExpEmpty.h @@ -44,11 +44,7 @@ public: virtual bool operator==(const RegExpElement&) const; virtual bool operator>(const RegExpElement&) const; - virtual bool operator<(const Concatenation&) const; - virtual bool operator<(const Alternation&) const; - virtual bool operator<(const Iteration&) const; - virtual bool operator<(const RegExpSymbol&) const; - virtual bool operator<(const RegExpEpsilon&) const; + virtual bool operator<(const RegExpEmpty&) const; virtual bool operator==(const RegExpEmpty&) const; /** diff --git a/alib2/src/regexp/RegExpEpsilon.cpp b/alib2/src/regexp/RegExpEpsilon.cpp index 7666137a6c..eed1754a8c 100644 --- a/alib2/src/regexp/RegExpEpsilon.cpp +++ b/alib2/src/regexp/RegExpEpsilon.cpp @@ -30,20 +30,8 @@ bool RegExpEpsilon::operator>(const RegExpElement& other) const { } -bool RegExpEpsilon::operator<(const Alternation&) const { - return true; -} - -bool RegExpEpsilon::operator<(const Concatenation&) const { - return true; -} - -bool RegExpEpsilon::operator<(const Iteration&) const { - return true; -} - -bool RegExpEpsilon::operator<(const RegExpSymbol&) const { - return true; +bool RegExpEpsilon::operator<(const RegExpEpsilon&) const { + return false; } bool RegExpEpsilon::operator==(const RegExpEpsilon&) const { diff --git a/alib2/src/regexp/RegExpEpsilon.h b/alib2/src/regexp/RegExpEpsilon.h index fed42e6d95..585bab24b8 100644 --- a/alib2/src/regexp/RegExpEpsilon.h +++ b/alib2/src/regexp/RegExpEpsilon.h @@ -45,10 +45,7 @@ public: virtual bool operator==(const RegExpElement&) const; virtual bool operator>(const RegExpElement&) const; - virtual bool operator<(const Alternation&) const; - virtual bool operator<(const Concatenation&) const; - virtual bool operator<(const Iteration&) const; - virtual bool operator<(const RegExpSymbol&) const; + virtual bool operator<(const RegExpEpsilon&) const; virtual bool operator==(const RegExpEpsilon&) const; /** diff --git a/alib2/src/regexp/RegExpSymbol.cpp b/alib2/src/regexp/RegExpSymbol.cpp index 53f759e7b2..c26c82b1a8 100644 --- a/alib2/src/regexp/RegExpSymbol.cpp +++ b/alib2/src/regexp/RegExpSymbol.cpp @@ -26,7 +26,11 @@ RegExpElement* RegExpSymbol::plunder() && { } bool RegExpSymbol::operator==(const alphabet::Symbol& other) const { - return *this == other; + return (const alphabet::Symbol&) *this == other; +} + +bool operator==(const alphabet::Symbol& first, const RegExpSymbol& second) { + return first == (const alphabet::Symbol&) second; } bool RegExpSymbol::operator<(const RegExpElement& other) const { @@ -42,18 +46,6 @@ bool RegExpSymbol::operator>(const RegExpElement& other) const { } -bool RegExpSymbol::operator<(const Concatenation&) const { - return true; -} - -bool RegExpSymbol::operator<(const Alternation&) const { - return true; -} - -bool RegExpSymbol::operator<(const Iteration&) const { - return true; -} - bool RegExpSymbol::operator<(const RegExpSymbol& other) const { return this->symbol < other.symbol; } diff --git a/alib2/src/regexp/RegExpSymbol.h b/alib2/src/regexp/RegExpSymbol.h index ed232bf89a..653e84675a 100644 --- a/alib2/src/regexp/RegExpSymbol.h +++ b/alib2/src/regexp/RegExpSymbol.h @@ -44,15 +44,13 @@ public: RegExpSymbol(const std::string& symbol); RegExpSymbol(std::string&& symbol); - virtual bool operator==(const alphabet::Symbol&) const; + bool operator==(const alphabet::Symbol&) const; + friend bool operator==(const alphabet::Symbol&, const RegExpSymbol&); virtual bool operator<(const RegExpElement&) const; virtual bool operator==(const RegExpElement&) const; virtual bool operator>(const RegExpElement&) const; - virtual bool operator<(const Concatenation&) const; - virtual bool operator<(const Alternation&) const; - virtual bool operator<(const Iteration&) const; virtual bool operator<(const RegExpSymbol&) const; virtual bool operator==(const RegExpSymbol&) const; -- GitLab