From 0f0f749d1d1c7a9bb7345291e81733fb25268e14 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Jan=20Tr=C3=A1vn=C3=AD=C4=8Dek?= <Jan.Travnicek@fit.cvut.cz>
Date: Sat, 17 Jul 2021 09:58:33 +0200
Subject: [PATCH] data: make alphabet mismatch detection more efficient

---
 alib2data/src/string/CyclicString.h         | 10 ++++------
 alib2data/src/string/LinearString.h         |  8 +++-----
 alib2data/src/string/WildcardLinearString.h |  8 +++-----
 3 files changed, 10 insertions(+), 16 deletions(-)

diff --git a/alib2data/src/string/CyclicString.h b/alib2data/src/string/CyclicString.h
index ebcfb40eec..f6fd787e4c 100644
--- a/alib2data/src/string/CyclicString.h
+++ b/alib2data/src/string/CyclicString.h
@@ -230,12 +230,10 @@ ext::vector < SymbolType > && CyclicString < SymbolType >::getContent ( ) && {
 //serves as both move and copy content setter
 template < class SymbolType >
 void CyclicString < SymbolType >::setContent(ext::vector<SymbolType> str) {
-	ext::set<SymbolType> minimalAlphabet(str.begin(), str.end());
-	ext::set<SymbolType> unknownSymbols;
-	std::set_difference(minimalAlphabet.begin(), minimalAlphabet.end(), getAlphabet().begin(), getAlphabet().end(), std::inserter(unknownSymbols, unknownSymbols.end()));
-
-	if(!unknownSymbols.empty ( ))
-		throw exception::CommonException("Input symbols not in the alphabet.");
+	ext::set < SymbolType > minimalAlphabet ( str.begin ( ), str.end ( ) );
+	std::set_difference ( minimalAlphabet.begin ( ), minimalAlphabet.end ( ), getAlphabet ( ).begin ( ), getAlphabet ( ).end ( ), ext::callback_iterator ( [ ] ( const SymbolType & ) {
+			throw exception::CommonException("Input symbols not in the alphabet.");
+		} ) );
 
 	m_Data = std::move(str);
 }
diff --git a/alib2data/src/string/LinearString.h b/alib2data/src/string/LinearString.h
index 343ad3a543..75f8067cfd 100644
--- a/alib2data/src/string/LinearString.h
+++ b/alib2data/src/string/LinearString.h
@@ -249,11 +249,9 @@ ext::vector < SymbolType > && LinearString < SymbolType >::getContent ( ) && {
 template < class SymbolType >
 void LinearString < SymbolType >::setContent ( ext::vector < SymbolType > str ) {
 	ext::set < SymbolType > minimalAlphabet ( str.begin ( ), str.end ( ) );
-	ext::set < SymbolType > unknownSymbols;
-	std::set_difference ( minimalAlphabet.begin ( ), minimalAlphabet.end ( ), getAlphabet().begin ( ), getAlphabet().end ( ), std::inserter ( unknownSymbols, unknownSymbols.end ( ) ) );
-
-	if ( !unknownSymbols.empty ( ) )
-		throw exception::CommonException ( "Input symbols not in the alphabet." );
+	std::set_difference ( minimalAlphabet.begin ( ), minimalAlphabet.end ( ), getAlphabet ( ).begin ( ), getAlphabet ( ).end ( ), ext::callback_iterator ( [ ] ( const SymbolType & ) {
+			throw exception::CommonException("Input symbols not in the alphabet.");
+		} ) );
 
 	this->m_Data = std::move ( str );
 }
diff --git a/alib2data/src/string/WildcardLinearString.h b/alib2data/src/string/WildcardLinearString.h
index 2c65bd8a8d..979a0f6d62 100644
--- a/alib2data/src/string/WildcardLinearString.h
+++ b/alib2data/src/string/WildcardLinearString.h
@@ -290,11 +290,9 @@ ext::vector < SymbolType > && WildcardLinearString < SymbolType >::getContent (
 template < class SymbolType >
 void WildcardLinearString < SymbolType >::setContent ( ext::vector < SymbolType > str ) {
 	ext::set < SymbolType > minimalAlphabet ( str.begin ( ), str.end ( ) );
-	ext::set < SymbolType > unknownSymbols;
-	std::set_difference ( minimalAlphabet.begin ( ), minimalAlphabet.end ( ), getAlphabet().begin ( ), getAlphabet().end ( ), std::inserter ( unknownSymbols, unknownSymbols.end ( ) ) );
-
-	if ( !unknownSymbols.empty ( ) )
-		throw exception::CommonException ( "Input symbols not in the alphabet." );
+	std::set_difference ( minimalAlphabet.begin ( ), minimalAlphabet.end ( ), getAlphabet ( ).begin ( ), getAlphabet ( ).end ( ), ext::callback_iterator ( [ ] ( const SymbolType & ) {
+			throw exception::CommonException("Input symbols not in the alphabet.");
+		} ) );
 
 	this->m_Data = std::move ( str );
 }
-- 
GitLab