From 2ef3d26c9c18008ae323e732fc626a0c20f98439 Mon Sep 17 00:00:00 2001
From: Jan Travnicek <Jan.Travnicek@fit.cvut.cz>
Date: Tue, 19 Jul 2016 12:21:48 +0200
Subject: [PATCH] finish regexp optimize simplification

---
 .../src/regexp/simplify/RegExpOptimize.cpp    |  4 +-
 .../src/regexp/simplify/RegExpOptimize.h      | 53 +++++++---------
 .../simplify/RegExpOptimizeFormalPart.cxx     | 63 +++++++++----------
 3 files changed, 56 insertions(+), 64 deletions(-)

diff --git a/alib2algo/src/regexp/simplify/RegExpOptimize.cpp b/alib2algo/src/regexp/simplify/RegExpOptimize.cpp
index b3b390c235..17714e9740 100644
--- a/alib2algo/src/regexp/simplify/RegExpOptimize.cpp
+++ b/alib2algo/src/regexp/simplify/RegExpOptimize.cpp
@@ -23,7 +23,7 @@ regexp::RegExp RegExpOptimize::optimize(const regexp::RegExp& regexp) {
 }
 
 FormalRegExp RegExpOptimize::optimize( FormalRegExp const & regexp ) {
-	FormalRegExpElement* optimized = RegExpOptimize::REG_EXP_OPTIMIZE.optimize( & regexp.getRegExp( ) );
+	FormalRegExpElement* optimized = optimizeInner( regexp.getRegExp( ) );
 
 	return FormalRegExp ( std::manage_move ( optimized ) );
 }
@@ -38,8 +38,6 @@ UnboundedRegExp RegExpOptimize::optimize( UnboundedRegExp const & regexp ) {
 
 auto RegExpOptimizeUnboundedRegEpx = RegExpOptimize::RegistratorWrapper<UnboundedRegExp, UnboundedRegExp>(RegExpOptimize::optimize);
 
-const RegExpOptimize RegExpOptimize::REG_EXP_OPTIMIZE;
-
 } /* namespace regexp */
 
 } /* namespace simplify */
diff --git a/alib2algo/src/regexp/simplify/RegExpOptimize.h b/alib2algo/src/regexp/simplify/RegExpOptimize.h
index 76d1ce2512..bc7511042f 100644
--- a/alib2algo/src/regexp/simplify/RegExpOptimize.h
+++ b/alib2algo/src/regexp/simplify/RegExpOptimize.h
@@ -63,8 +63,6 @@ namespace simplify {
  */
 class RegExpOptimize : public std::SingleDispatch<RegExpOptimize, regexp::RegExp, regexp::RegExpBase> {
 public:
-	RegExpOptimize() {}
-
 	static regexp::RegExp optimize( const regexp::RegExp & regexp );
 
 	static regexp::UnboundedRegExp optimize( const regexp::UnboundedRegExp & regexp );
@@ -75,7 +73,7 @@ public:
 	static regexp::FormalRegExp optimize( const regexp::FormalRegExp & regexp );
 	static void optimize( regexp::FormalRegExpElement & regexp );
 private:
-	regexp::FormalRegExpElement * optimize( regexp::FormalRegExpElement const * const & node ) const;
+	static regexp::FormalRegExpElement * optimizeInner( const regexp::FormalRegExpElement & node );
 
 	static regexp::UnboundedRegExpElement * optimizeInner( const regexp::UnboundedRegExpElement & node );
 	static regexp::UnboundedRegExpElement * optimizeInner( const regexp::UnboundedRegExpAlternation & node );
@@ -85,7 +83,6 @@ private:
 	static regexp::UnboundedRegExpElement * optimizeInner( const regexp::UnboundedRegExpEpsilon & node );
 	static regexp::UnboundedRegExpElement * optimizeInner( const regexp::UnboundedRegExpEmpty & node );
 
-private:
 	static bool A1( regexp::UnboundedRegExpAlternation & node );
 	static bool A2( regexp::UnboundedRegExpAlternation & node );
 	static bool A3( regexp::UnboundedRegExpAlternation & node );
@@ -109,31 +106,29 @@ private:
 
 	static bool X1( regexp::UnboundedRegExpAlternation & node );
 
-	bool S( regexp::FormalRegExpElement * & node ) const;
-	bool A1( regexp::FormalRegExpElement * & node ) const;
-	bool A2( regexp::FormalRegExpElement * & node ) const;
-	bool A3( regexp::FormalRegExpElement * & node ) const;
-	bool A4( regexp::FormalRegExpElement * & node ) const;
-	bool A5( regexp::FormalRegExpElement * & node ) const;
-	bool A6( regexp::FormalRegExpElement * & node ) const;
-	bool A7( regexp::FormalRegExpElement * & node ) const;
-	bool A8( regexp::FormalRegExpElement * & node ) const;
-	bool A9( regexp::FormalRegExpElement * & node ) const;
-	bool A10( regexp::FormalRegExpElement * & node ) const;
-	bool A11( regexp::FormalRegExpElement * & node ) const;
-	bool V1( regexp::FormalRegExpElement * & node ) const;
-	bool V2( regexp::FormalRegExpElement * & node ) const;
-	bool V3( regexp::FormalRegExpElement * & node ) const;
-	bool V4( regexp::FormalRegExpElement * & node ) const;
-	bool V5( regexp::FormalRegExpElement * & node ) const;
-	bool V6( regexp::FormalRegExpElement * & node ) const;
-	bool V8( regexp::FormalRegExpElement * & node ) const;
-	bool V9( regexp::FormalRegExpElement * & node ) const;
-	bool V10( regexp::FormalRegExpElement * & node ) const;
-
-	bool X1( regexp::FormalRegExpElement * & node ) const;
-
-	static const RegExpOptimize REG_EXP_OPTIMIZE;
+	static bool S( regexp::FormalRegExpElement * & node );
+	static bool A1( regexp::FormalRegExpElement * & node );
+	static bool A2( regexp::FormalRegExpElement * & node );
+	static bool A3( regexp::FormalRegExpElement * & node );
+	static bool A4( regexp::FormalRegExpElement * & node );
+	static bool A5( regexp::FormalRegExpElement * & node );
+	static bool A6( regexp::FormalRegExpElement * & node );
+	static bool A7( regexp::FormalRegExpElement * & node );
+	static bool A8( regexp::FormalRegExpElement * & node );
+	static bool A9( regexp::FormalRegExpElement * & node );
+	static bool A10( regexp::FormalRegExpElement * & node );
+	static bool A11( regexp::FormalRegExpElement * & node );
+	static bool V1( regexp::FormalRegExpElement * & node );
+	static bool V2( regexp::FormalRegExpElement * & node );
+	static bool V3( regexp::FormalRegExpElement * & node );
+	static bool V4( regexp::FormalRegExpElement * & node );
+	static bool V5( regexp::FormalRegExpElement * & node );
+	static bool V6( regexp::FormalRegExpElement * & node );
+	static bool V8( regexp::FormalRegExpElement * & node );
+	static bool V9( regexp::FormalRegExpElement * & node );
+	static bool V10( regexp::FormalRegExpElement * & node );
+
+	static bool X1( regexp::FormalRegExpElement * & node );
 };
 
 } /* namespace simplify */
diff --git a/alib2algo/src/regexp/simplify/RegExpOptimizeFormalPart.cxx b/alib2algo/src/regexp/simplify/RegExpOptimizeFormalPart.cxx
index 5773a20dcb..ca2005e2e6 100644
--- a/alib2algo/src/regexp/simplify/RegExpOptimizeFormalPart.cxx
+++ b/alib2algo/src/regexp/simplify/RegExpOptimizeFormalPart.cxx
@@ -9,9 +9,8 @@ namespace regexp {
 
 namespace simplify {
 
-void RegExpOptimize::optimize( FormalRegExpElement & element )
-{
-	FormalRegExpElement* optimized = RegExpOptimize::REG_EXP_OPTIMIZE.optimize( & element );
+void RegExpOptimize::optimize( FormalRegExpElement & element ) {
+	FormalRegExpElement* optimized = optimizeInner( element );
 
 	FormalRegExpAlternation * alternation = dynamic_cast<FormalRegExpAlternation *>( & element );
 	if( alternation ) {
@@ -53,8 +52,8 @@ void RegExpOptimize::optimize( FormalRegExpElement & element )
 	return;
 }
 
-FormalRegExpElement* RegExpOptimize::optimize( FormalRegExpElement const * const & node ) const {
-	FormalRegExpElement* elem = node->clone();
+FormalRegExpElement* RegExpOptimize::optimizeInner( const FormalRegExpElement & node ) {
+	FormalRegExpElement* elem = node.clone();
 
 	// optimize while you can
 	while(    A1( elem ) || A2( elem ) || A3( elem ) || A4( elem ) || A10( elem ) || V2( elem ) || V5( elem ) || V6( elem ) || X1( elem )
@@ -64,17 +63,17 @@ FormalRegExpElement* RegExpOptimize::optimize( FormalRegExpElement const * const
 	return elem;
 }
 
-bool RegExpOptimize::S( FormalRegExpElement * & node ) const {
+bool RegExpOptimize::S( FormalRegExpElement * & node ) {
 	bool optimized = false;
 	FormalRegExpAlternation * alternation = dynamic_cast<FormalRegExpAlternation*>( node );
 	if( alternation ) {
-		FormalRegExpElement * tmp = optimize ( & alternation->getLeftElement ( ) );
+		FormalRegExpElement * tmp = optimizeInner ( alternation->getLeftElement ( ) );
 		if(* tmp != alternation->getLeftElement ( ) ) {
 			optimized = true;
 			alternation->setLeftElement ( * std::smart_ptr < FormalRegExpElement > ( tmp ) );
 		}
 
-		tmp = optimize ( & alternation->getRightElement ( ) );
+		tmp = optimizeInner ( alternation->getRightElement ( ) );
 		if(* tmp != alternation->getRightElement ( ) ) {
 			optimized = true;
 			alternation->setRightElement ( * std::smart_ptr < FormalRegExpElement > ( tmp ) );
@@ -85,13 +84,13 @@ bool RegExpOptimize::S( FormalRegExpElement * & node ) const {
 
 	FormalRegExpConcatenation * concatenation = dynamic_cast<FormalRegExpConcatenation*>( node );
 	if( concatenation ) {
-		FormalRegExpElement* tmp = optimize ( & concatenation->getLeftElement() );
+		FormalRegExpElement* tmp = optimizeInner ( concatenation->getLeftElement() );
 		if(* tmp != concatenation->getLeftElement ( ) ) {
 			optimized = true;
 			concatenation->setLeftElement ( * std::smart_ptr < FormalRegExpElement > ( tmp ) );
 		}
 
-		tmp = optimize ( & concatenation->getRightElement ( ));
+		tmp = optimizeInner ( concatenation->getRightElement ( ));
 		if(* tmp != concatenation->getRightElement ( )) {
 			optimized = true;
 			concatenation->setRightElement ( * std::smart_ptr < FormalRegExpElement > ( tmp ) );
@@ -102,7 +101,7 @@ bool RegExpOptimize::S( FormalRegExpElement * & node ) const {
 
 	FormalRegExpIteration * iteration = dynamic_cast<FormalRegExpIteration*>( node );
 	if( iteration ) {
-		FormalRegExpElement* tmp = optimize ( & iteration->getElement() );
+		FormalRegExpElement* tmp = optimizeInner ( iteration->getElement() );
 
 		if(* tmp != iteration->getElement ( ) ) {
 			optimized = true;
@@ -120,7 +119,7 @@ bool RegExpOptimize::S( FormalRegExpElement * & node ) const {
   * @param node FormalRegExpElement node
   * @return bool true if optimization applied else false
   */
-bool RegExpOptimize::A1( FormalRegExpElement * & n ) const {
+bool RegExpOptimize::A1( FormalRegExpElement * & n ) {
 	FormalRegExpAlternation * node = dynamic_cast<FormalRegExpAlternation *>( n );
 	if( ! node ) return false;
 
@@ -147,7 +146,7 @@ bool RegExpOptimize::A1( FormalRegExpElement * & n ) const {
   * @param node FormalRegExpElement node
   * @return bool true if optimization applied else false
   */
-bool RegExpOptimize::A2( FormalRegExpElement * & n ) const {
+bool RegExpOptimize::A2( FormalRegExpElement * & n ) {
 	FormalRegExpAlternation * node = dynamic_cast<FormalRegExpAlternation *>( n );
 	if( ! node ) return false;
 
@@ -176,7 +175,7 @@ bool RegExpOptimize::A2( FormalRegExpElement * & n ) const {
   * @param node FormalRegExpElement node
   * @return bool true if optimization applied else false
   */
-bool RegExpOptimize::A3( FormalRegExpElement * & n ) const {
+bool RegExpOptimize::A3( FormalRegExpElement * & n ) {
 	FormalRegExpAlternation * node = dynamic_cast<FormalRegExpAlternation *>( n );
 	if( ! node ) return false;
 
@@ -202,7 +201,7 @@ bool RegExpOptimize::A3( FormalRegExpElement * & n ) const {
   * @param node FormalRegExpElement node
   * @return bool true if optimization applied else false
   */
-bool RegExpOptimize::A4( FormalRegExpElement * & n ) const {
+bool RegExpOptimize::A4( FormalRegExpElement * & n ) {
 	/*
 	 * two ways of implementing this opitimization:
 	 * - sort and call std::unique ( O(n lg n) + O(n) ), but it also sorts...
@@ -228,7 +227,7 @@ bool RegExpOptimize::A4( FormalRegExpElement * & n ) const {
   * @param node FormalRegExpElement node
   * @return bool true if optimization applied else false
   */
-bool RegExpOptimize::A5( FormalRegExpElement * & n ) const {
+bool RegExpOptimize::A5( FormalRegExpElement * & n ) {
 	FormalRegExpConcatenation * node = dynamic_cast<FormalRegExpConcatenation *>( n );
 	if( ! node ) return false;
 
@@ -255,7 +254,7 @@ bool RegExpOptimize::A5( FormalRegExpElement * & n ) const {
   * @param node FormalRegExpElement node
   * @return bool true if optimization applied else false
   */
-bool RegExpOptimize::A6( FormalRegExpElement * & n ) const {
+bool RegExpOptimize::A6( FormalRegExpElement * & n ) {
 	FormalRegExpConcatenation * node = dynamic_cast<FormalRegExpConcatenation *>( n );
 	if( ! node ) return false;
 
@@ -281,7 +280,7 @@ bool RegExpOptimize::A6( FormalRegExpElement * & n ) const {
   * @param node FormalRegExpElement node
   * @return bool true if optimization applied else false
   */
-bool RegExpOptimize::A7( FormalRegExpElement * & n ) const {
+bool RegExpOptimize::A7( FormalRegExpElement * & n ) {
 	FormalRegExpConcatenation * node = dynamic_cast<FormalRegExpConcatenation *>( n );
 	if( ! node ) return false;
 
@@ -299,7 +298,7 @@ bool RegExpOptimize::A7( FormalRegExpElement * & n ) const {
   * @param node FormalRegExpElement node
   * @return bool true if optimization applied else false
   */
-bool RegExpOptimize::A8( FormalRegExpElement * & /* n */) const {
+bool RegExpOptimize::A8( FormalRegExpElement * & /* n */) {
 	return false; //TODO
 }
 
@@ -308,7 +307,7 @@ bool RegExpOptimize::A8( FormalRegExpElement * & /* n */) const {
   * @param node FormalRegExpElement node
   * @return bool true if optimization applied else false
   */
-bool RegExpOptimize::A9( FormalRegExpElement * & /* n */) const {
+bool RegExpOptimize::A9( FormalRegExpElement * & /* n */) {
 	return false; //TODO
 }
 
@@ -317,7 +316,7 @@ bool RegExpOptimize::A9( FormalRegExpElement * & /* n */) const {
   * @param node FormalRegExpElement node
   * @return bool true if optimization applied else false
   */
-bool RegExpOptimize::A10( FormalRegExpElement * & n ) const {
+bool RegExpOptimize::A10( FormalRegExpElement * & n ) {
 	/*
 	 * problem:
 	 * - \e + x*x = x*
@@ -381,7 +380,7 @@ bool RegExpOptimize::A10( FormalRegExpElement * & n ) const {
   * @param node FormalRegExpElement node
   * @return bool true if optimization applied else false
   */
-bool RegExpOptimize::A11( FormalRegExpElement * & n ) const {
+bool RegExpOptimize::A11( FormalRegExpElement * & n ) {
 	FormalRegExpIteration * node = dynamic_cast<FormalRegExpIteration *>( n );
 	if( ! node ) return false;
 
@@ -406,7 +405,7 @@ bool RegExpOptimize::A11( FormalRegExpElement * & n ) const {
   * @param node FormalRegExpElement node
   * @return bool true if optimization applied else false
   */
-bool RegExpOptimize::V1( FormalRegExpElement * & n ) const {
+bool RegExpOptimize::V1( FormalRegExpElement * & n ) {
 	FormalRegExpIteration * node = dynamic_cast<FormalRegExpIteration *>( n );
 	if( ! node ) return false;
 
@@ -428,7 +427,7 @@ bool RegExpOptimize::V1( FormalRegExpElement * & n ) const {
   * @param node FormalRegExpElement node
   * @return bool true if optimization applied else false
   */
-bool RegExpOptimize::V2( FormalRegExpElement * & n ) const {
+bool RegExpOptimize::V2( FormalRegExpElement * & n ) {
 	FormalRegExpAlternation * node = dynamic_cast<FormalRegExpAlternation *>( n );
 	if( ! node ) return false;
 
@@ -458,7 +457,7 @@ bool RegExpOptimize::V2( FormalRegExpElement * & n ) const {
   * @param node FormalRegExpElement node
   * @return bool true if optimization applied else false
   */
-bool RegExpOptimize::V3( FormalRegExpElement * & n ) const {
+bool RegExpOptimize::V3( FormalRegExpElement * & n ) {
 	FormalRegExpIteration * node = dynamic_cast<FormalRegExpIteration *>( n );
 	if( ! node ) return false;
 
@@ -476,7 +475,7 @@ bool RegExpOptimize::V3( FormalRegExpElement * & n ) const {
   * @param node FormalRegExpElement node
   * @return bool true if optimization applied else false
   */
-bool RegExpOptimize::V4( FormalRegExpElement * & n ) const {
+bool RegExpOptimize::V4( FormalRegExpElement * & n ) {
 	FormalRegExpIteration * node = dynamic_cast<FormalRegExpIteration *>( n );
 	if( ! node ) return false;
 
@@ -500,7 +499,7 @@ bool RegExpOptimize::V4( FormalRegExpElement * & n ) const {
   * @param node FormalRegExpElement node
   * @return bool true if optimization applied else false
   */
-bool RegExpOptimize::V5( FormalRegExpElement * & /* n */) const {
+bool RegExpOptimize::V5( FormalRegExpElement * & /* n */) {
 	return false; //TODO
 }
 
@@ -509,7 +508,7 @@ bool RegExpOptimize::V5( FormalRegExpElement * & /* n */) const {
   * @param node FormalRegExpElement node
   * @return bool true if optimization applied else false
   */
-bool RegExpOptimize::V6( FormalRegExpElement * & /* n */) const {
+bool RegExpOptimize::V6( FormalRegExpElement * & /* n */) {
 	return false; //TODO
 }
 
@@ -518,7 +517,7 @@ bool RegExpOptimize::V6( FormalRegExpElement * & /* n */) const {
   * @param node FormalRegExpElement node
   * @return bool true if optimization applied else false
   */
-bool RegExpOptimize::V8( FormalRegExpElement * & /* n */) const {
+bool RegExpOptimize::V8( FormalRegExpElement * & /* n */) {
 	return false; //TODO
 }
 
@@ -527,7 +526,7 @@ bool RegExpOptimize::V8( FormalRegExpElement * & /* n */) const {
   * @param node FormalRegExpElement node
   * @return bool true if optimization applied else false
   */
-bool RegExpOptimize::V9( FormalRegExpElement * & /* n */) const {
+bool RegExpOptimize::V9( FormalRegExpElement * & /* n */) {
 	return false; //TODO
 }
 
@@ -536,7 +535,7 @@ bool RegExpOptimize::V9( FormalRegExpElement * & /* n */) const {
   * @param node FormalRegExpElement node
   * @return bool true if optimization applied else false
   */
-bool RegExpOptimize::V10( FormalRegExpElement * & n ) const {
+bool RegExpOptimize::V10( FormalRegExpElement * & n ) {
 	FormalRegExpAlternation * node = dynamic_cast<FormalRegExpAlternation *>( n );
 	if( ! node ) return false;
 
@@ -557,7 +556,7 @@ bool RegExpOptimize::V10( FormalRegExpElement * & n ) const {
   * @param node FormalRegExpElement node
   * @return bool true if optimization applied else false
   */
-bool RegExpOptimize::X1( FormalRegExpElement * & n ) const {
+bool RegExpOptimize::X1( FormalRegExpElement * & n ) {
 	FormalRegExpAlternation * node = dynamic_cast<FormalRegExpAlternation *>( n );
 	if( ! node ) return false;
 
-- 
GitLab