Skip to content
Snippets Groups Projects
Commit e0047c8f authored by Tomáš Pecka's avatar Tomáš Pecka
Browse files

Fully remove RegExpComparator

parent 296fa116
No related branches found
No related tags found
No related merge requests found
......@@ -200,20 +200,14 @@ StateElimination::TransitionExtendedNFA::TransitionExtendedNFA( const State & fr
 
}
 
bool StateElimination::TransitionExtendedNFA::operator<( const StateElimination::TransitionExtendedNFA & rhs ) const
bool StateElimination::TransitionExtendedNFA::operator<( const StateElimination::TransitionExtendedNFA & x ) const
{
if( m_from.getName( ) < rhs.m_from.getName( ) )
return true;
if( m_from.getName( ) > rhs.m_from.getName( ) )
return false;
if( m_to.getName( ) < rhs.m_to.getName( ) )
return true;
if( m_to.getName( ) > rhs.m_to.getName( ) )
return false;
RegExpComparator cmp;
return cmp.compare( m_regexp, rhs.m_regexp ) == -1;
if( m_from != x.m_from )
return m_from < x.m_from;
else if( ! ( m_regexp == x.m_regexp ) )
return m_regexp < x.m_regexp;
else
return m_to < x.m_to;
}
 
} /* namespace conversions */
......@@ -21,7 +21,6 @@
#include <regexp/RegExpSymbol.h>
 
#include "AbstractFAtoREConverter.h"
#include "../re2fa/RegExpComparator.h"
 
#include "RegExpOptimize.h"
 
......
......@@ -16,7 +16,6 @@
#include <AlibException.h>
 
#include "AbstractREtoFAConverter.h"
#include "RegExpComparator.h"
 
#include "RegExpDerivation.h"
#include "RegExpAlphabet.h"
......
/*
* RegExpComparator.cpp
*
* Created on: 5. 2. 2014
* Author: tomas
*/
#include "RegExpComparator.h"
using namespace alib;
using namespace regexp;
namespace conversions
{
RegExpComparator::RegExpComparator( void )
{
}
bool RegExpComparator::operator() ( const RegExp & lhs, const RegExp & rhs ) //const
{
RegExpElement * leftRoot = const_cast<RegExp&>( lhs ).getRegExp( );
RegExpElement * rightRoot = const_cast<RegExp&>( rhs ).getRegExp( );
return compare( leftRoot, rightRoot ) == -1;
}
int RegExpComparator::compare( RegExpElement * lhs, RegExpElement * rhs ) const
{
Alternation* lhsAlt = dynamic_cast<Alternation*>( lhs ), *rhsAlt = dynamic_cast<Alternation*>( rhs );
Concatenation* lhsConcat = dynamic_cast<Concatenation*>( lhs ), *rhsConcat = dynamic_cast<Concatenation*>( rhs );
Iteration* lhsIter = dynamic_cast<Iteration*>( lhs ), *rhsIter = dynamic_cast<Iteration*>( rhs );
RegExpSymbol* lhsSymb = dynamic_cast<RegExpSymbol*>( lhs ), *rhsSymb = dynamic_cast<RegExpSymbol*>( rhs );
RegExpEmpty* lhsEmpty = dynamic_cast<RegExpEmpty*>( lhs ), *rhsEmpty = dynamic_cast<RegExpEmpty*>( rhs );
RegExpEpsilon* lhsEps = dynamic_cast<RegExpEpsilon*>( lhs ), *rhsEps = dynamic_cast<RegExpEpsilon*>( rhs );
if( ( lhsAlt && rhsAlt ) || ( lhsConcat && rhsConcat ) || ( lhsIter && rhsIter ) || ( lhsSymb && rhsSymb ) ||
( lhsEmpty && rhsEmpty ) || ( lhsEps && rhsEps ) )
{
if( lhsAlt )
return compare( lhsAlt, rhsAlt );
if( lhsConcat )
return compare( lhsConcat, rhsConcat );
if( lhsIter )
return compare( lhsIter, rhsIter );
if( lhsSymb )
return compare( lhsSymb, rhsSymb );
if( lhsEmpty )
return compare( lhsEmpty, rhsEmpty );
if( lhsEps )
return compare( lhsEps, rhsEps );
}
else
{
NodeType leftNodeType = UNKNOWN, rightNodeType = UNKNOWN;
if( lhsAlt ) leftNodeType = ALTERNATION;
if( lhsConcat ) leftNodeType = CONCATENATION;
if( lhsIter ) leftNodeType = ITERATION;
if( lhsSymb ) leftNodeType = SYMBOL;
if( lhsEmpty ) leftNodeType = EMPTY;
if( lhsEps ) leftNodeType = EPSILON;
if( rhsAlt ) rightNodeType = ALTERNATION;
if( rhsConcat ) rightNodeType = CONCATENATION;
if( rhsIter ) rightNodeType = ITERATION;
if( rhsSymb ) rightNodeType = SYMBOL;
if( rhsEmpty ) rightNodeType = EMPTY;
if( rhsEps ) rightNodeType = EPSILON;
if ( leftNodeType < rightNodeType ) return -1;
if ( leftNodeType > rightNodeType ) return 1;
return 0;
}
throw AlibException( "RegExpComparator::compare - Don't know how to compare subtrees." );
}
int RegExpComparator::compare( Alternation * lhs, Alternation * rhs ) const
{
auto lhsEnd = lhs->getElements( ).end( );
auto rhsEnd = rhs->getElements( ).end( );
for( auto lhsIt = lhs->getElements( ).begin( ), rhsIt = rhs->getElements( ).begin( ); ; lhsIt ++, rhsIt ++ )
{
if( lhsIt == lhsEnd && rhsIt != rhsEnd )
return -1;
if( lhsIt != lhsEnd && rhsIt == rhsEnd )
return 1;
if( lhsIt == lhsEnd && rhsIt == rhsEnd )
return 0;
int res = compare ( *lhsIt, *rhsIt );
if( res != 0 )
return res;
}
return 0;
}
int RegExpComparator::compare( Concatenation * lhs, Concatenation * rhs ) const
{
auto lhsEnd = lhs->getElements( ).end( );
auto rhsEnd = rhs->getElements( ).end( );
for( auto lhsIt = lhs->getElements( ).begin( ), rhsIt = rhs->getElements( ).begin( ); ; lhsIt ++, rhsIt ++ )
{
if( lhsIt == lhsEnd && rhsIt != rhsEnd )
return -1;
if( lhsIt != lhsEnd && rhsIt == rhsEnd )
return 1;
if( lhsIt == lhsEnd && rhsIt == rhsEnd )
return 0;
int res = compare ( *lhsIt, *rhsIt );
if( res != 0 )
return res;
}
return 0;
}
int RegExpComparator::compare( Iteration * lhs, Iteration * rhs ) const
{
return compare( lhs->getElement( ), rhs->getElement( ) );
}
int RegExpComparator::compare( RegExpSymbol * lhs, RegExpSymbol * rhs ) const
{
if( lhs->getSymbol( ) < rhs->getSymbol( ) )
return -1;
if( lhs->getSymbol( ) > rhs->getSymbol( ) )
return 1;
return 0;
}
int RegExpComparator::compare( RegExpEmpty * lhs, RegExpEmpty * rhs ) const
{
return 0;
}
int RegExpComparator::compare( RegExpEpsilon * lhs, RegExpEpsilon * rhs ) const
{
return 0;
}
} /* namespace conversions */
/*
* RegExpComparator.h
*
* Created on: 5. 2. 2014
* Author: tomas
*/
#ifndef REGEXPCOMPARATOR_H_
#define REGEXPCOMPARATOR_H_
#include <map>
#include <string>
#include <regexp/RegExp.h>
#include <regexp/RegExpElement.h>
#include <regexp/Alternation.h>
#include <regexp/Concatenation.h>
#include <regexp/Iteration.h>
#include <regexp/RegExpSymbol.h>
#include <regexp/RegExpEmpty.h>
#include <regexp/RegExpEpsilon.h>
#include <AlibException.h>
namespace conversions
{
class RegExpComparator
{
public:
RegExpComparator( void );
bool operator() ( const regexp::RegExp & lhs, const regexp::RegExp & rhs ); //const;
int compare( regexp::RegExpElement * lhs, regexp::RegExpElement * rhs ) const;
private:
int compare( regexp::Alternation * lhs, regexp::Alternation * rhs ) const;
int compare( regexp::Concatenation * lhs, regexp::Concatenation * rhs ) const;
int compare( regexp::Iteration * lhs, regexp::Iteration * rhs ) const;
int compare( regexp::RegExpSymbol * lhs, regexp::RegExpSymbol * rhs ) const;
int compare( regexp::RegExpEmpty * lhs, regexp::RegExpEmpty * rhs ) const;
int compare( regexp::RegExpEpsilon * lhs, regexp::RegExpEpsilon * rhs ) const;
enum NodeType {
UNKNOWN = 0,
ALTERNATION = 1,
CONCATENATION = 2,
ITERATION = 3,
SYMBOL = 4,
EMPTY = 5,
EPSILON = 6
};
};
} /* namespace conversions */
#endif /* REGEXPCOMPARATOR_H_ */
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment