From ec36a62d3421e54507d6b51e044c8c3ebdacb1e9 Mon Sep 17 00:00:00 2001
From: Jan Travnicek <Jan.Travnicek@fit.cvut.cz>
Date: Fri, 20 Oct 2017 17:12:02 +0200
Subject: [PATCH] take out types comparison from algo getabstraction

---
 .../src/abstraction/AlgorithmRegistry.cpp     | 22 +-----
 alib2std/src/extensions/typeinfo.cpp          | 20 ++++++
 alib2std/src/extensions/typeinfo.hpp          | 18 ++---
 .../test-src/extensions/AlgorithmTest.cpp     | 68 ++++++++++++++++---
 4 files changed, 85 insertions(+), 43 deletions(-)

diff --git a/alib2common/src/abstraction/AlgorithmRegistry.cpp b/alib2common/src/abstraction/AlgorithmRegistry.cpp
index 6211e56e84..4a794a6562 100644
--- a/alib2common/src/abstraction/AlgorithmRegistry.cpp
+++ b/alib2common/src/abstraction/AlgorithmRegistry.cpp
@@ -35,28 +35,8 @@ void AlgorithmRegistry::registerInternal ( std::string algorithm, AlgorithmCateg
 std::shared_ptr < abstraction::OperationAbstraction > AlgorithmRegistry::getAbstraction ( const std::string & name, const ext::vector < std::string > & paramTypes, AlgorithmCategories::AlgorithmCategory ) {
 	auto group = getEntries ( ).find ( name );
 	if ( group == getEntries ( ).end ( ) ) {
-		ext::vector < std::string > explodedName = ext::explode ( name, "::" );
-
 		for ( auto iter = getEntries ( ).begin ( ); iter != getEntries ( ).end ( ); ++ iter ) {
-			ext::vector < std::string > explodedCandidate = ext::explode ( iter->first, "::" );
-
-			if ( explodedName.size ( ) > explodedCandidate.size ( ) )
-				continue;
-
-			unsigned offset = explodedCandidate.size ( ) - explodedName.size ( );
-
-			bool matches = true;
-			for ( unsigned index = 0; index < explodedName.size ( ); ++ index ) {
-				if ( explodedName [ index ] == "" )
-					continue;
-
-				if ( explodedName [ index ] != explodedCandidate [ index + offset ] ) {
-					matches = false;
-					break;
-				}
-			}
-
-			if ( matches ) {
+			if ( ext::is_same_type ( name, iter->first ) ) {
 				if ( group == getEntries ( ).end ( ) )
 					group = iter;
 				else
diff --git a/alib2std/src/extensions/typeinfo.cpp b/alib2std/src/extensions/typeinfo.cpp
index b5e6077efc..d11d6c29d0 100644
--- a/alib2std/src/extensions/typeinfo.cpp
+++ b/alib2std/src/extensions/typeinfo.cpp
@@ -22,4 +22,24 @@ std::string erase_template_info ( std::string str ) {
 	return str;
 }
 
+bool is_same_type ( const std::string & first, const std::string & second ) {
+	ext::vector < std::string > explodedFirst = ext::explode ( first, "::" );
+	ext::vector < std::string > explodedSecond = ext::explode ( second, "::" );
+
+	if ( explodedFirst.size ( ) > explodedSecond.size ( ) )
+		return false;
+
+	unsigned offset = explodedSecond.size ( ) - explodedFirst.size ( );
+
+	for ( unsigned index = 0; index < explodedFirst.size ( ); ++ index ) {
+		if ( explodedFirst [ index ] == "" )
+			continue;
+
+		if ( explodedFirst [ index ] != explodedSecond [ index + offset ] )
+			return false;
+	}
+
+	return true;
+}
+
 } /* namespace ext */
diff --git a/alib2std/src/extensions/typeinfo.hpp b/alib2std/src/extensions/typeinfo.hpp
index 1b4ff8c1e6..87788250fd 100644
--- a/alib2std/src/extensions/typeinfo.hpp
+++ b/alib2std/src/extensions/typeinfo.hpp
@@ -11,8 +11,6 @@
 #include <typeinfo>
 #include <typeindex>
 #include <string>
-#include <cstdio>
-#include <cstring>
 
 #include "string.hpp"
 #include "typeindex.h"
@@ -24,18 +22,12 @@ std::string to_string ( ) {
 	return ext::to_string ( ext::type_index ( typeid ( T ) ) );
 }
 
-template<typename T>
-bool is_same_type(const char* name) {
-	char namespaceId[100];
-	char classId[100];
-	std::string ret = to_string < T > ( );
-	sscanf(ret.c_str(), "%[a-zA-Z]::%[a-zA-Z]", namespaceId, classId);
+bool is_same_type ( const std::string & first, const std::string & second );
 
-	if(strcmp(classId, name) == 0) {
-		return true;
-	} else {
-		return false;
-	}
+template < typename T >
+bool is_same_type ( const std::string & name ) {
+	std::string ret = to_string < T > ( );
+	return is_same_type ( ret, name );
 }
 
 std::string erase_template_info ( std::string str );
diff --git a/alib2std/test-src/extensions/AlgorithmTest.cpp b/alib2std/test-src/extensions/AlgorithmTest.cpp
index 2a0952edc6..d4d95db7de 100644
--- a/alib2std/test-src/extensions/AlgorithmTest.cpp
+++ b/alib2std/test-src/extensions/AlgorithmTest.cpp
@@ -62,18 +62,68 @@ void AlgorithmTest::testTransform() {
 }
 
 void AlgorithmTest::testFindRange ( ) {
-	std::string str = "abc<dee<fd<<>>>th>::rrr";
+	{
+		std::string str = "abc<dee<fd<<>>>th>::rrr";
+
+		std::pair < std::string::iterator, std::string::iterator > range = ext::find_range ( str.begin ( ), str.end ( ), '<', '>' );
+
+		std::cerr << range.first - str.begin ( ) << std::endl;
+		std::cerr << range.second - str.begin ( ) << std::endl;
+
+		CPPUNIT_ASSERT ( range.first - str.begin ( ) == 3 );
+		CPPUNIT_ASSERT ( range.second - str.begin ( ) == 18 );
+
+		str.erase ( range.first, range.second );
+		std::cerr << str << std::endl;
+
+		CPPUNIT_ASSERT ( str == "abc::rrr" );
+	}
+	{
+		std::string str = "aaa::abc<dee<fd<<>>>th>";
+
+		std::pair < std::string::iterator, std::string::iterator > range = ext::find_range ( str.begin ( ), str.end ( ), '<', '>' );
 
-	std::pair < std::string::iterator, std::string::iterator > range = ext::find_range ( str.begin ( ), str.end ( ), '<', '>' );
+		std::cerr << range.first - str.begin ( ) << std::endl;
+		std::cerr << range.second - str.begin ( ) << std::endl;
 
-	std::cerr << range.first - str.begin ( ) << std::endl;
-	std::cerr << range.second - str.begin ( ) << std::endl;
+		CPPUNIT_ASSERT ( range.first - str.begin ( ) == 8 );
+		CPPUNIT_ASSERT ( range.second - str.begin ( ) == 23 );
 
-	CPPUNIT_ASSERT ( range.first - str.begin ( ) == 3 );
-	CPPUNIT_ASSERT ( range.second - str.begin ( ) == 18 );
+		str.erase ( range.first, range.second );
+		std::cerr << str << std::endl;
+
+		CPPUNIT_ASSERT ( str == "aaa::abc" );
+	}
+	{
+		std::string str = "<dee<fd<<>>>th>aaa";
 
-	str.erase ( range.first, range.second );
-	std::cerr << str << std::endl;
+		std::pair < std::string::iterator, std::string::iterator > range = ext::find_range ( str.begin ( ), str.end ( ), '<', '>' );
 
-	CPPUNIT_ASSERT ( str == "abc::rrr" );
+		std::cerr << range.first - str.begin ( ) << std::endl;
+		std::cerr << range.second - str.begin ( ) << std::endl;
+
+		CPPUNIT_ASSERT ( range.first - str.begin ( ) == 0 );
+		CPPUNIT_ASSERT ( range.second - str.begin ( ) == 15 );
+
+		str.erase ( range.first, range.second );
+		std::cerr << str << std::endl;
+
+		CPPUNIT_ASSERT ( str == "aaa" );
+	}
+	{
+		std::string str = "<dee<fd<<>>>th>";
+
+		std::pair < std::string::iterator, std::string::iterator > range = ext::find_range ( str.begin ( ), str.end ( ), '<', '>' );
+
+		std::cerr << range.first - str.begin ( ) << std::endl;
+		std::cerr << range.second - str.begin ( ) << std::endl;
+
+		CPPUNIT_ASSERT ( range.first - str.begin ( ) == 0 );
+		CPPUNIT_ASSERT ( range.second - str.begin ( ) == 15 );
+
+		str.erase ( range.first, range.second );
+		std::cerr << str << std::endl;
+
+		CPPUNIT_ASSERT ( str == "" );
+	}
 }
-- 
GitLab