From 30266f4ccc285f61738f06433555c4fb6a6546b4 Mon Sep 17 00:00:00 2001
From: Jan Travnicek <Jan.Travnicek@fit.cvut.cz>
Date: Mon, 14 Apr 2014 09:00:57 +0200
Subject: [PATCH] Alternation uses vector, fix string parser

---
 alib2/src/regexp/Alternation.cpp            | 14 +++---
 alib2/src/regexp/Alternation.h              | 21 +++++----
 alib2/src/regexp/RegExpFromStringParser.cpp | 50 ++++++++++++---------
 alib2/src/regexp/RegExpFromStringParser.h   |  4 +-
 alib2/src/regexp/RegExpFromXMLParser.cpp    |  2 +-
 5 files changed, 52 insertions(+), 39 deletions(-)

diff --git a/alib2/src/regexp/Alternation.cpp b/alib2/src/regexp/Alternation.cpp
index d12fce3d4c..67bc56459a 100644
--- a/alib2/src/regexp/Alternation.cpp
+++ b/alib2/src/regexp/Alternation.cpp
@@ -16,7 +16,7 @@ Alternation::Alternation() {
 
 Alternation::Alternation(const Alternation& other) {
 	for (const auto& element : other.elements) {
-		elements.insert(element->clone());
+		elements.push_back(element->clone());
 	}
 }
 
@@ -47,16 +47,16 @@ Alternation::~Alternation() noexcept {
 	elements.clear();
 }
 
-const std::set<const RegExpElement*, RegExpElement::PointerLess > & Alternation::getElements() const {
-	return * reinterpret_cast<const std::set<const RegExpElement*, RegExpElement::PointerLess > * > (&elements);
+const std::vector<const RegExpElement*> & Alternation::getElements() const {
+	return * reinterpret_cast<const std::vector<const RegExpElement*> * > (&elements);
 }
 
-void Alternation::insertElement(const RegExpElement& element) {
-	this->elements.insert(element.clone());
+void Alternation::appendElement(const RegExpElement& element) {
+	this->elements.push_back(element.clone());
 }
 
-void Alternation::insertElement(RegExpElement&& element) {
-	this->elements.insert(std::move(element).plunder());
+void Alternation::appendElement(RegExpElement&& element) {
+	this->elements.push_back(std::move(element).plunder());
 }
 
 RegExpElement* Alternation::clone() const {
diff --git a/alib2/src/regexp/Alternation.h b/alib2/src/regexp/Alternation.h
index da272b9ac3..3b9479f036 100644
--- a/alib2/src/regexp/Alternation.h
+++ b/alib2/src/regexp/Alternation.h
@@ -8,7 +8,7 @@
 #ifndef ALTERNATION_H_
 #define ALTERNATION_H_
 
-#include <set>
+#include <vector>
 #include "RegExpElement.h"
 
 namespace regexp {
@@ -24,9 +24,12 @@ protected:
 	 */
 	virtual RegExpElement* clone() const;
 	
+	/**
+	 * @copydoc RegExpElement::plunder() const
+	 */
 	virtual RegExpElement* plunder() &&;
 
-	std::set<RegExpElement*, RegExpElement::PointerLess > elements;
+	std::vector<RegExpElement*> elements;
 public:
 	Alternation();
 	Alternation(const Alternation& other);
@@ -38,15 +41,17 @@ public:
 	/**
 	 * @return elements
 	 */
-	//TODO mozna vratit vector aby slo reprezentovat a+a, optimalizace nechat na tomasovi
-	const std::set<const RegExpElement*, RegExpElement::PointerLess > & getElements() const;
+	const std::vector<const RegExpElement*> & getElements() const;
 
 	/**
-	 * @param element to insert
+	 * @param element to append
 	 */
-	void insertElement(const RegExpElement& element);
-	
-	void insertElement(RegExpElement&& element);
+	void appendElement(const RegExpElement& element);
+
+	/**
+	 * @param element to append
+	 */
+	void appendElement(RegExpElement&& element);
 
 	virtual bool operator<(const RegExpElement&) const;
 	virtual bool operator==(const RegExpElement&) const;
diff --git a/alib2/src/regexp/RegExpFromStringParser.cpp b/alib2/src/regexp/RegExpFromStringParser.cpp
index 49b7ab4d7c..7be14d866c 100644
--- a/alib2/src/regexp/RegExpFromStringParser.cpp
+++ b/alib2/src/regexp/RegExpFromStringParser.cpp
@@ -24,27 +24,32 @@ RegExpElement* RegExpFromStringParser::alternationCont(RegExpElement* left) {
   RegExpFromStringLexer::Token token = m_Lexer.token();
   if(token.type == RegExpFromStringLexer::PLUS) {
     m_Lexer.next();
-    Alternation* res = this->alternationContCont(this->concatenation());
-    res->insertElement(std::move(*left));
+    
+    Alternation* res = new Alternation;
+    res->appendElement(std::move(*left));
     delete left;
-    return res;
+    
+    left = this->concatenation();
+    res->appendElement(std::move(*left));
+    delete left;
+    
+    return this->alternationContCont(res);
   } else {
     return left;
   }
 }
 
-Alternation* RegExpFromStringParser::alternationContCont(RegExpElement* left) {
+RegExpElement* RegExpFromStringParser::alternationContCont(Alternation* res) {
   RegExpFromStringLexer::Token token = m_Lexer.token();
   if(token.type == RegExpFromStringLexer::PLUS) {
     m_Lexer.next();
-    Alternation* res = this->alternationContCont(this->concatenation());
-    res->insertElement(std::move(*left));
-    delete left;
-    return res;
+    
+    RegExpElement* next = this->concatenation();
+    res->appendElement(std::move(*next));
+    delete next;
+    
+    return this->alternationContCont(res);
   } else {
-    Alternation* res = new Alternation();
-    res->insertElement(std::move(*left));
-    delete left;
     return res;
   }
 }
@@ -57,28 +62,31 @@ RegExpElement* RegExpFromStringParser::concatenation() {
 RegExpElement* RegExpFromStringParser::concatenationCont(RegExpElement* left) {
   RegExpFromStringLexer::Token token = m_Lexer.token();
   if(token.type == RegExpFromStringLexer::SYMBOL || token.type == RegExpFromStringLexer::LPAR || token.type == RegExpFromStringLexer::EPS || token.type == RegExpFromStringLexer::EMPTY) {
+
+    Concatenation* res = new Concatenation;
+    res->appendElement(std::move(*left));
+    delete left;
     
-    Concatenation* res = this->concatenationContCont(this->factor());
+    left = this->factor();
     res->appendElement(std::move(*left));
     delete left;
-    return res;
+    
+    return this->concatenationContCont(res);
   } else {
     return left;
   }
 }
 
-Concatenation* RegExpFromStringParser::concatenationContCont(RegExpElement* left) {
+RegExpElement* RegExpFromStringParser::concatenationContCont(Concatenation* res) {
   RegExpFromStringLexer::Token token = m_Lexer.token();
   if(token.type == RegExpFromStringLexer::SYMBOL || token.type == RegExpFromStringLexer::LPAR || token.type == RegExpFromStringLexer::EPS || token.type == RegExpFromStringLexer::EMPTY) {
     
-    Concatenation* res = this->concatenationContCont(this->factor());
-    res->appendElement(std::move(*left));
-    delete left;
-    return res;
+    RegExpElement* next = this->factor();
+    res->appendElement(std::move(*next));
+    delete next;
+    
+    return this->concatenationContCont(res);
   } else {
-    Concatenation* res = new Concatenation();
-    res->appendElement(std::move(*left));
-    delete left;
     return res;
   }
 }
diff --git a/alib2/src/regexp/RegExpFromStringParser.h b/alib2/src/regexp/RegExpFromStringParser.h
index 59e394e3fd..b91969632e 100644
--- a/alib2/src/regexp/RegExpFromStringParser.h
+++ b/alib2/src/regexp/RegExpFromStringParser.h
@@ -18,11 +18,11 @@ namespace regexp {
 class RegExpFromStringParser {
 	RegExpElement* alternation();
 	RegExpElement* alternationCont(RegExpElement* left);
-	Alternation* alternationContCont(RegExpElement* left);
+	RegExpElement* alternationContCont(Alternation* left);
 	
 	RegExpElement* concatenation();
 	RegExpElement* concatenationCont(RegExpElement* left);
-	Concatenation* concatenationContCont(RegExpElement* left);
+	RegExpElement* concatenationContCont(Concatenation* left);
 	
 	RegExpElement* factor();
 	RegExpElement* star(RegExpElement* elem);
diff --git a/alib2/src/regexp/RegExpFromXMLParser.cpp b/alib2/src/regexp/RegExpFromXMLParser.cpp
index 50d7eff201..0e83087202 100644
--- a/alib2/src/regexp/RegExpFromXMLParser.cpp
+++ b/alib2/src/regexp/RegExpFromXMLParser.cpp
@@ -46,7 +46,7 @@ Alternation* RegExpFromXMLParser::parseAlternation(std::list<sax::Token>& input)
 	while (true) {
 		RegExpElement* element = parseElement(input);
 		if(!element) break;
-		alternation->insertElement(std::move(*element));
+		alternation->appendElement(std::move(*element));
 		delete element;
 	}
 
-- 
GitLab