From 15823ad1cda8c2d1bdcf8947193d461ec326ebc8 Mon Sep 17 00:00:00 2001
From: Jan Travnicek <Jan.Travnicek@fit.cvut.cz>
Date: Sun, 3 May 2015 15:59:15 +0200
Subject: [PATCH] move semantics in NFA determinize

---
 .../determinize/DeterminizeNFAPart.cxx        | 28 +++++++++----------
 alib2std/src/extensions/set.hpp               | 28 ++++++++++++++++---
 2 files changed, 38 insertions(+), 18 deletions(-)

diff --git a/alib2algo/src/automaton/determinize/DeterminizeNFAPart.cxx b/alib2algo/src/automaton/determinize/DeterminizeNFAPart.cxx
index 09ed59fabd..15b93f17eb 100644
--- a/alib2algo/src/automaton/determinize/DeterminizeNFAPart.cxx
+++ b/alib2algo/src/automaton/determinize/DeterminizeNFAPart.cxx
@@ -28,19 +28,19 @@ automaton::DFA Determinize::determinize(const automaton::MultiInitialStateNFA& n
 
 	do {
 		// 3a, c
-		automaton::State state = todo.front();
+		automaton::State state = std::move(todo.front());
 		todo.pop_front();
 
 		// 3b
-		for (const auto& input : nfa.getInputAlphabet()) {
+		for (const alphabet::Symbol& input : nfa.getInputAlphabet()) {
 			std::set<automaton::State> targetNFAStates;
-			for(const auto& nfaState : recreateNFAStates(state)) {
-				auto iter = nfa.getTransitions().find(std::make_pair(nfaState, input));
+			for(automaton::State nfaState : std::make_moveable_set(recreateNFAStates(state))) {
+				auto iter = nfa.getTransitions().find(std::make_pair(std::move(nfaState), alphabet::Symbol(input)));
 				if(iter != nfa.getTransitions().end()) {
 					targetNFAStates.insert(iter->second.begin(), iter->second.end());
 				}
 			}
-			automaton::State dfaState = createDFAState(targetNFAStates);
+			automaton::State dfaState = createDFAState(std::move(targetNFAStates));
 
 			// 4
 			bool existed = !res.addState(dfaState);
@@ -48,12 +48,12 @@ automaton::DFA Determinize::determinize(const automaton::MultiInitialStateNFA& n
 			// 3b
 			res.addTransition(state, input, dfaState);
 
-			if(!existed) todo.push_back(dfaState);
+			if(!existed) todo.push_back(std::move(dfaState));
 		}
 	} while(!todo.empty());
 
 	// 5
-	for (const auto& dfaState : res.getStates()) {
+	for (const automaton::State& dfaState : res.getStates()) {
 		std::set<automaton::State> nfaStates = recreateNFAStates(dfaState);
 		if(std::any_of(nfaStates.begin(), nfaStates.end(), [&](const automaton::State& nfaState) { return nfa.getFinalStates().count(nfaState); })) {
 			res.addFinalState(dfaState);
@@ -75,19 +75,19 @@ automaton::DFA Determinize::determinize(const automaton::NFA& nfa) {
 
 	do {
 		// 3a, c
-		automaton::State state = todo.front();
+		automaton::State state = std::move(todo.front());
 		todo.pop_front();
 
 		// 3b
-		for (const auto& input : nfa.getInputAlphabet()) {
+		for (const alphabet::Symbol& input : nfa.getInputAlphabet()) {
 			std::set<automaton::State> targetNFAStates;
-			for(const auto& nfaState : recreateNFAStates(state)) {
-				auto iter = nfa.getTransitions().find(std::make_pair(nfaState, input));
+			for(automaton::State nfaState : std::make_moveable_set(recreateNFAStates(state))) {
+				auto iter = nfa.getTransitions().find(std::make_pair(std::move(nfaState), alphabet::Symbol(input)));
 				if(iter != nfa.getTransitions().end()) {
 					targetNFAStates.insert(iter->second.begin(), iter->second.end());
 				}
 			}
-			automaton::State dfaState = createDFAState(targetNFAStates);
+			automaton::State dfaState = createDFAState(std::move(targetNFAStates));
 
 			// 4
 			bool existed = !res.addState(dfaState);
@@ -95,12 +95,12 @@ automaton::DFA Determinize::determinize(const automaton::NFA& nfa) {
 			// 3b
 			res.addTransition(state, input, dfaState);
 
-			if(!existed) todo.push_back(dfaState);
+			if(!existed) todo.push_back(std::move(dfaState));
 		}
 	} while(!todo.empty());
 
 	// 5
-	for (const auto& dfaState : res.getStates()) {
+	for (const automaton::State& dfaState : res.getStates()) {
 		std::set<automaton::State> nfaStates = recreateNFAStates(dfaState);
 		if(std::any_of(nfaStates.begin(), nfaStates.end(), [&](const automaton::State& nfaState) { return nfa.getFinalStates().count(nfaState); })) {
 			res.addFinalState(dfaState);
diff --git a/alib2std/src/extensions/set.hpp b/alib2std/src/extensions/set.hpp
index 2180347130..3d7dd27bd8 100644
--- a/alib2std/src/extensions/set.hpp
+++ b/alib2std/src/extensions/set.hpp
@@ -130,10 +130,30 @@ set_move_iterator<Iterator> make_set_move_iterator (Iterator it) {
 }
 
 template<class T>
-class moveable_set {
+class moveable_set_ref {
 	set<T>& theSet;
 public:
-	moveable_set(set<T>& param) : theSet(param) {}
+	moveable_set_ref(set<T>& param) : theSet(param) {}
+
+	set_move_iterator<typename set<T>::iterator> begin() {
+		return make_set_move_iterator(theSet.begin());
+	}
+
+	set_move_iterator<typename set<T>::iterator> end() {
+		return make_set_move_iterator(theSet.end());
+	}
+};
+
+template<class T>
+moveable_set_ref<T> make_moveable_set (std::set<T>& set) {
+	return moveable_set_ref<T>(set);
+}
+
+template<class T>
+class moveable_set {
+	set<T> theSet;
+public:
+	moveable_set(set<T> param) : theSet(std::move(param)) {}
 
 	set_move_iterator<typename set<T>::iterator> begin() {
 		return make_set_move_iterator(theSet.begin());
@@ -145,8 +165,8 @@ public:
 };
 
 template<class T>
-moveable_set<T> make_moveable_set (std::set<T>& set) {
-	return moveable_set<T>(set);
+moveable_set<T> make_moveable_set (std::set<T>&& set) {
+	return moveable_set<T>(std::move(set));
 }
 
 } /* namespace std */
-- 
GitLab