From 0afa7a6bbad38c63e74ea6ec61710dc98ce24c1a Mon Sep 17 00:00:00 2001
From: Jan Travnicek <Jan.Travnicek@fit.cvut.cz>
Date: Fri, 1 May 2015 18:49:30 +0200
Subject: [PATCH] moveable set and used in NFA determinisation algo

---
 .../determinize/common/NFACommon.cpp          |  6 +--
 .../automaton/determinize/common/NFACommon.h  |  2 +-
 alib2std/src/extensions/set.hpp               | 20 ++++++++++
 alib2std/test-src/extensions/SetTest.cpp      | 39 +++++++++++++++++++
 alib2std/test-src/extensions/SetTest.h        |  1 +
 5 files changed, 64 insertions(+), 4 deletions(-)

diff --git a/alib2algo/src/automaton/determinize/common/NFACommon.cpp b/alib2algo/src/automaton/determinize/common/NFACommon.cpp
index 52d945fdf2..6f371a466d 100644
--- a/alib2algo/src/automaton/determinize/common/NFACommon.cpp
+++ b/alib2algo/src/automaton/determinize/common/NFACommon.cpp
@@ -16,10 +16,10 @@ namespace automaton {
 
 namespace determinize {
 
-automaton::State createDFAState(const std::set<automaton::State>& nfaStates) {
+automaton::State createDFAState(std::set<automaton::State> nfaStates) {
 	std::set<label::Label> labelSet;
-	for(const automaton::State& state : nfaStates) {
-		labelSet.insert(state.getName());
+	for(automaton::State state : std::make_moveable_set(nfaStates)) {
+		labelSet.insert(std::move(state.getName()));
 	}
 	return automaton::State(label::Label(label::LabelSetLabel(std::move(labelSet))));
 }
diff --git a/alib2algo/src/automaton/determinize/common/NFACommon.h b/alib2algo/src/automaton/determinize/common/NFACommon.h
index 2265efdf84..b24e343972 100644
--- a/alib2algo/src/automaton/determinize/common/NFACommon.h
+++ b/alib2algo/src/automaton/determinize/common/NFACommon.h
@@ -22,7 +22,7 @@ namespace determinize {
  * @param originalStates set of states from nondeterministic fsm which represents state of deterministic fsm
  * @return state of deterministic fsm
  */
-automaton::State createDFAState(const std::set<automaton::State>& nfaStates);
+automaton::State createDFAState(std::set<automaton::State> nfaStates);
 
 /**
  * Finds states from nondeterministic fsm to which at least one state from given set of states have transition
diff --git a/alib2std/src/extensions/set.hpp b/alib2std/src/extensions/set.hpp
index 96904208e3..2180347130 100644
--- a/alib2std/src/extensions/set.hpp
+++ b/alib2std/src/extensions/set.hpp
@@ -129,6 +129,26 @@ set_move_iterator<Iterator> make_set_move_iterator (Iterator it) {
 	return set_move_iterator<Iterator>(it);
 }
 
+template<class T>
+class moveable_set {
+	set<T>& theSet;
+public:
+	moveable_set(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<T> make_moveable_set (std::set<T>& set) {
+	return moveable_set<T>(set);
+}
+
 } /* namespace std */
 
 #endif /* __SET_HPP_ */
diff --git a/alib2std/test-src/extensions/SetTest.cpp b/alib2std/test-src/extensions/SetTest.cpp
index f5c4cbcc78..712b4cd16a 100644
--- a/alib2std/test-src/extensions/SetTest.cpp
+++ b/alib2std/test-src/extensions/SetTest.cpp
@@ -42,3 +42,42 @@ void SetTest::test2() {
 	CPPUNIT_ASSERT_EQUAL(firstMinusSecond.size(), 0ul);
 	CPPUNIT_ASSERT_EQUAL(secondMinusFirst.size(), 2ul);
 }
+
+class Moveable {
+	int& moves;
+	int& copies;
+
+public:
+	Moveable(int& moves, int& copies) : moves(moves), copies(copies) {
+		moves = 0;
+		copies = 0;
+	}
+
+	Moveable(const Moveable& src) : moves(src.moves), copies(src.copies) {
+		copies++;
+	}
+
+	Moveable(Moveable&& src) : moves(src.moves), copies(src.copies) {
+		moves++;
+	}
+
+	bool operator<(const Moveable&) const {
+		return false;
+	}
+};
+
+void SetTest::test3() {
+	int moves;
+	int copies;
+
+	std::set<Moveable> set = {Moveable(moves, copies)};
+	std::set<Moveable> set2;
+
+	for(Moveable moveable : std::make_moveable_set(set)) {
+		set2.insert(std::move(moveable));
+	}
+
+	CPPUNIT_ASSERT(copies == 0);
+	CPPUNIT_ASSERT(copies == 2);
+}
+
diff --git a/alib2std/test-src/extensions/SetTest.h b/alib2std/test-src/extensions/SetTest.h
index bdad1fd298..ce6f31b5ee 100644
--- a/alib2std/test-src/extensions/SetTest.h
+++ b/alib2std/test-src/extensions/SetTest.h
@@ -16,6 +16,7 @@ public:
 
   void test1();
   void test2();
+  void test3();
 };
 
 #endif  // SET_TEST_H_
-- 
GitLab