diff --git a/alib2data/src/alphabet/Alphabet.hpp b/alib2data/src/alphabet/Alphabet.hpp deleted file mode 100644 index 1f2feff01f56cb129ba825697c47abd59e98638a..0000000000000000000000000000000000000000 --- a/alib2data/src/alphabet/Alphabet.hpp +++ /dev/null @@ -1,398 +0,0 @@ -/* - * Alphabet.hpp - * - * Created on: Mar 16, 2016 - * Author: Jan Travnicek - */ - -#ifndef ALPHABET_HPP_ -#define ALPHABET_HPP_ - -#include <set> -#include <algorithm> -#include "SymbolException.h" - -namespace alphabet { - -/** - * Represents an alphabet of symbols. - * @param Derived class representing datatype using this alphabet. - * @param SymbolType underlying type of symbols in the alphabet. - * @param AlphabetType arbirtrary type used to distinguish different alphabets. - */ -template < class Derived, class SymbolType, class AlphabetType > -class Alphabet { - /** - * The alphabet. - */ - std::set < SymbolType > data; - - /** - * Checks whether symbol can be added to the alphabet. Calls valid and available functions. - * @throws SymbolException if symbol cannot be added. - */ - void checkAdd ( const SymbolType & symbol ) { - valid ( symbol ); - - if ( !available ( symbol ) ) - throw SymbolException ( "Symbol " + ( std::string ) symbol + " is not available." ); - } - - /** - * Checks whether symbol can be removed to the alphabet. Calls used function. - * @throws SymbolException if symbol cannot be removed. - */ - void checkRemove ( const SymbolType & symbol ) { - if ( used ( symbol ) ) - throw SymbolException ( "Symbol " + ( std::string ) symbol + " is used." ); - } - -public: - /* - * Constructs an empty alphabet. - */ - Alphabet ( ) { - } - - /** - * Constructs an alphabet containing given symbols. - * @throw SymbolException if symbols are not available in context of datatype where the alphabet is used - */ - Alphabet ( std::set < SymbolType > symbols ) : data ( std::move ( symbols ) ) { - for ( const SymbolType & symbol : data ) - checkAdd ( symbol ); - } - - /** - * Adds a symbol to the alphabet. - * @param symbol to add to the alphabet - * @throw SymbolException if symbol is not available in context of datatype where the alphabet is used - * @return true if symbol was indeed added - * false if symbol was present in the alphabet - */ - bool add ( SymbolType symbol ) { - checkAdd ( symbol ); - return data.insert ( std::move ( symbol ) ).second; - } - - /** - * Adds a set of symbols to the alphabet. - * @param symbols to add to the alphabet - * @throw SymbolException if one of the symbols is not available in context of datatype where the alphabet is used - */ - void add ( std::set < SymbolType > symbols ) { - for ( SymbolType symbol : std::make_moveable_set ( symbols ) ) - add ( std::move ( symbol ) ); - } - - /** - * Changes the alphabet. - * @param symbols by which to replace those currently in the alphabet - * @throw SymbolException if one of the removed symbols is used in context of datatype instance using the alphabet - * SymbolException if one of the added symbols is not available in context of datatype where the alphabet is used - */ - void set ( std::set < SymbolType > symbols ) { - std::set < SymbolType > removed; - std::set_difference ( data.begin ( ), data.end ( ), symbols.begin ( ), symbols.end ( ), std::inserter ( removed, removed.end ( ) ) ); - - for ( const SymbolType & symbol : removed ) - checkRemove ( symbol ); - - for ( const SymbolType & symbol : symbols ) - checkAdd ( symbol ); - - data = std::move ( symbols ); - } - - /** - * @return set of symbols in the alphabet. - */ - std::set < SymbolType > & get ( ) { - return data; - } - - /** - * @return set of symbols in the alphabet. - */ - const std::set < SymbolType > & get ( ) const { - return data; - } - - /** - * Removes a symbol from alphabet if not used. - * @throw SymbolException if symbol is used in context of datatype instance using the alphabet - * @return true if symbol was indeed removed - * false if symbol was not present in the alphabet - */ - bool remove ( const SymbolType & symbol ) { - checkRemove ( symbol ); - return data.erase ( symbol ); - } - - /** - * Removes a set of symbols from alphabet if not used. - * @throw SymbolException if symbol is used in context of datatype instance using the alphabet - */ - void remove ( const std::set < SymbolType > & symbols ) { - for ( const SymbolType & symbol : symbols ) - remove ( symbol ); - } - - /** - * Alphabet emptines checker. - * @return true if alphabet is an empty - */ - bool empty ( ) const { - return data.empty ( ); - } - - /** - * Checks whether a concrete symbol is used in context of the datatype instance using this alphabet - * - * To be implemented by all template instantiations explicitly - * - * @param symbol symbol to check - * @return true if symbol is used - * false if symbol is not used - */ - bool used ( const SymbolType & symbol ) const; - - /** - * Checks whether a concrete symbol is available in context of the datatype instance using this alphabet - * - * To be implemented by all template instantiations explicitly - * - * @param symbol symbol to check - * @return true if symbol is available - * false if symbol is not available - */ - bool available ( const SymbolType & symbol ) const; - - /** - * Checks whether a concrete symbol is valid in context of the datatype instance using this alphabet - * - * To be implemented by all template instantiations explicitly - * - * @param symbol symbol to check - * @throw SymbolException if the symbol in any way invalid - */ - void valid ( const SymbolType & symbol ) const; -}; - -/** - * Auxilary class packing multiple alphabets. - */ -template < class Derived, class SymbolType, class ... AlphabetTypes > -class AlphabetPack; - -/** - * Auxilary class packing single alphabets. - */ -template < class Derived, class SymbolType, class AlphabetType1 > -class AlphabetPack < Derived, SymbolType, AlphabetType1 > : public Alphabet < Derived, SymbolType, AlphabetType1 > { -public: - /** - * Construct an alphabet pack from single alphabet. - */ - AlphabetPack ( std::set < SymbolType > first ) : Alphabet < Derived, SymbolType, AlphabetType1 > ( std::move ( first ) ) { - } - -}; - -/** - * Auxilary class packing two alphabets. - */ -template < class Derived, class SymbolType, class AlphabetType1, class AlphabetType2 > -class AlphabetPack < Derived, SymbolType, AlphabetType1, AlphabetType2 > : public Alphabet < Derived, SymbolType, AlphabetType1 >, public Alphabet < Derived, SymbolType, AlphabetType2 > { -public: - /** - * Construct an alphabet pack from two alphabets. - */ - AlphabetPack ( std::set < SymbolType > first, std::set < SymbolType > second ) : Alphabet < Derived, SymbolType, AlphabetType1 > ( std::move ( first ) ), Alphabet < Derived, SymbolType, AlphabetType2 > ( std::move ( second ) ) { - } - -}; - -/** - * Auxilary class allowing simple access to the alphabets. - */ -template < class Derived, class SymbolType, class ... AlphabetTypes > -class Alphabets : public AlphabetPack < Derived, SymbolType, AlphabetTypes ... > { -public: - /** - * Reuse constructor of the base class. - */ - using AlphabetPack < Derived, SymbolType, AlphabetTypes ... >::AlphabetPack; - - /** - * Allow acces to subalphabet using its type. - * @param AlphabetType alphabet type used to distinguish different sub alphabets - * @return subalphabet - */ - template < class AlphabetType > - const Alphabet < Derived, SymbolType, AlphabetType > & accessAlphabet ( ) const { - return static_cast < const Alphabet < Derived, SymbolType, AlphabetType > & > ( * this ); - } - - /** - * Allow acces to subalphabet using its type. - * @param AlphabetType alphabet type used to distinguish different sub alphabets - * @return subalphabet - */ - template < class AlphabetType > - Alphabet < Derived, SymbolType, AlphabetType > & accessAlphabet ( ) { - return static_cast < Alphabet < Derived, SymbolType, AlphabetType > & > ( * this ); - } - -}; - -/** - * Represents an notable alphabet element. - * @param Derived class representing datatype using this alphabet. - * @param SymbolType underlying type of symbols in the alphabet. - * @param ElementType arbirtrary type used to distinguish different alphabet elements. - */ -template < class Derived, class SymbolType, class ElementType > -class AlphabetElement { - /** - * The element. - */ - SymbolType data; - -public: - /** - * Constructs an alphabet element from symbol. - * @throw SymbolException if symbol is not available in context of datatype where the alphabet element is used - */ - AlphabetElement ( SymbolType symbol ) : data ( std::move ( symbol ) ) { - valid ( data ); - - if ( !available ( data ) ) - throw SymbolException ( "Symbol " + ( std::string ) symbol + " is not available." ); - } - - /** - * Changes the alphabet element. - * @param new value of the symbol element - * @throw SymbolException if the new symbol is not available in context of datatype instance using the alphabet - */ - bool set ( SymbolType symbol ) { - valid ( symbol ); - - if ( !available ( symbol ) ) { - throw SymbolException ( "Symbol " + ( std::string ) symbol + " is used." ); - } else { - bool res = data == symbol; - data = std::move ( symbol ); - return res; - } - } - - /** - * Returns the current alphabet element of ElementType. - * @return the notable symbol from the alphabet - */ - SymbolType & get ( ) { - return data; - } - - /** - * Returns the current alphabet element of ElementType. - * @return the notable symbol from the alphabet - */ - const SymbolType & get ( ) const { - return data; - } - - /** - * Checks whether a concrete symbol is available in context of the datatype instance using this alphabet - * - * To be implemented by all template instantiations explicitly - * - * @param symbol symbol to check - * @return true if symbol is available - * false if symbol is not available - */ - bool available ( const SymbolType & symbol ) const; - - /** - * Checks whether a concrete symbol is valid in context of the datatype instance using this alphabet - * - * To be implemented by all template instantiations explicitly - * - * @param symbol symbol to check - * @throw SymbolException if the symbol in any way invalid - */ - void valid ( const SymbolType & symbol ) const; -}; - -/** - * Auxilary class packing multiple alphabet elements. - */ -template < class Derived, class SymbolType, class ... AlphabetElementTypes > -class AlphabetElementPack; - -/** - * Auxilary class packing single alphabet element. - */ -template < class Derived, class SymbolType, class AlphabetElementType1 > -class AlphabetElementPack < Derived, SymbolType, AlphabetElementType1 > : public AlphabetElement < Derived, SymbolType, AlphabetElementType1 > { -public: - /** - * Construct an alphabet element pack from single alphabet element. - */ - AlphabetElementPack ( SymbolType first ) : AlphabetElement < Derived, SymbolType, AlphabetElementType1 > ( std::move ( first ) ) { - } - -}; - -/** - * Auxilary class packing two alphabet elements. - */ -template < class Derived, class SymbolType, class AlphabetElementType1, class AlphabetElementType2 > -class AlphabetElementPack < Derived, SymbolType, AlphabetElementType1, AlphabetElementType2 > : public AlphabetElement < Derived, SymbolType, AlphabetElementType1 >, public AlphabetElement < Derived, SymbolType, AlphabetElementType2 > { -public: - /** - * Construct an alphabet element pack from two alphabet elements. - */ - AlphabetElementPack ( SymbolType first, SymbolType second ) : AlphabetElement < Derived, SymbolType, AlphabetElementType1 > ( std::move ( first ) ), AlphabetElement < Derived, SymbolType, AlphabetElementType2 > ( std::move ( second ) ) { - } - -}; - -/** - * Auxilary class allowing simple access to the alphabet elements. - */ -template < class Derived, class SymbolType, class ... AlphabetElementTypes > -class AlphabetElements : public AlphabetElementPack < Derived, SymbolType, AlphabetElementTypes ... > { -public: - /** - * Reuse constructor of the base class. - */ - using AlphabetElementPack < Derived, SymbolType, AlphabetElementTypes ... >::AlphabetElementPack; - - /** - * Allow acces to subalphabet element using its type. - * @param AlphabetElementType alphabet type used to distinguish different sub alphabet elements - * @return subalphabet element - */ - template < class AlphabetElementType > - const AlphabetElement < Derived, SymbolType, AlphabetElementType > & accessAlphabetElement ( ) const { - return static_cast < const AlphabetElement < Derived, SymbolType, AlphabetElementType > & > ( * this ); - } - - /** - * Allow acces to subalphabet element using its type. - * @param AlphabetElementType alphabet type used to distinguish different sub alphabet elements - * @return subalphabet element - */ - template < class AlphabetElementType > - AlphabetElement < Derived, SymbolType, AlphabetElementType > & accessAlphabetElement ( ) { - return static_cast < AlphabetElement < Derived, SymbolType, AlphabetElementType > & > ( * this ); - } - -}; - -} /* namespace alphabet */ - -#endif /* ALPHABET_HPP_ */ diff --git a/alib2data/test-src/alphabet/AlphabetTest.cpp b/alib2data/test-src/alphabet/AlphabetTest.cpp deleted file mode 100644 index 7d51853aa8d4bc50e9c1a2d23dfc9d3732552010..0000000000000000000000000000000000000000 --- a/alib2data/test-src/alphabet/AlphabetTest.cpp +++ /dev/null @@ -1,120 +0,0 @@ -#include "AlphabetTest.h" -#include <alphabet/Alphabet.hpp> - -CPPUNIT_TEST_SUITE_NAMED_REGISTRATION ( AlphabetTest, "alphabet" ); -CPPUNIT_TEST_SUITE_REGISTRATION ( AlphabetTest ); - -struct GeneralAlphabet { -}; - -struct NonlinearAlphabet { -}; - -struct SubtreeWildcard { -}; - -class A : public alphabet::Alphabets < A, std::string, GeneralAlphabet, NonlinearAlphabet >, public alphabet::AlphabetElements < A, std::string, SubtreeWildcard > { -public: - A ( std::string string ) : Alphabets < A, std::string, GeneralAlphabet, NonlinearAlphabet > ( { string }, { } ), AlphabetElements < A, std::string, SubtreeWildcard > ( string ) { - } -}; - -namespace alphabet { - -template < > -bool A::Alphabet < A, std::string, GeneralAlphabet >::used ( const std::string & string ) const { - return static_cast < const A * > ( this )->accessAlphabet < NonlinearAlphabet > ( ).get ( ).count ( string ) || static_cast < const A * > ( this )->accessAlphabetElement < SubtreeWildcard > ( ).get ( ) == string; -} - -template < > -bool A::Alphabet < A, std::string, NonlinearAlphabet >::used ( const std::string & ) const { - return false; -} - -template < > -bool A::Alphabet < A, std::string, GeneralAlphabet >::available ( const std::string & ) const { - return true; -} - -template < > -bool A::Alphabet < A, std::string, NonlinearAlphabet >::available ( const std::string & string ) const { - return static_cast < const A * > ( this )->accessAlphabet < GeneralAlphabet > ( ).get ( ).count ( string ); -} - -template < > -bool A::AlphabetElement < A, std::string, SubtreeWildcard >::available ( const std::string & string ) const { - return static_cast < const A * > ( this )->accessAlphabet < GeneralAlphabet > ( ).get ( ).count ( string ); -} - -template < > -void A::Alphabet < A, std::string, GeneralAlphabet >::valid ( const std::string & ) const { -} - -template < > -void A::Alphabet < A, std::string, NonlinearAlphabet >::valid ( const std::string & ) const { -} - -template < > -void A::AlphabetElement < A, std::string, SubtreeWildcard >::valid ( const std::string & ) const { -} - -} - -class B : public alphabet::Alphabets < B, std::string, GeneralAlphabet, NonlinearAlphabet > { -}; - -namespace alphabet { - -template < > -bool B::Alphabet < B, std::string, GeneralAlphabet >::used ( const std::string & ) const { - return false; -} - -template < > -bool B::Alphabet < B, std::string, NonlinearAlphabet >::used ( const std::string & ) const { - return false; -} - -template < > -bool B::Alphabet < B, std::string, GeneralAlphabet >::available ( const std::string & ) const { - return true; -} - -template < > -bool B::Alphabet < B, std::string, NonlinearAlphabet >::available ( const std::string & ) const { - return true; -} - -template < > -void B::Alphabet < B, std::string, GeneralAlphabet >::valid ( const std::string & ) const { -} - -template < > -void B::Alphabet < B, std::string, NonlinearAlphabet >::valid ( const std::string & ) const { -} - -} - -void AlphabetTest::setUp ( ) { -} - -void AlphabetTest::tearDown ( ) { -} - -void AlphabetTest::testAdd ( ) { - A tmp ( "2" ); - - CPPUNIT_ASSERT_THROW ( tmp.accessAlphabet < NonlinearAlphabet > ( ).add ( "1" ), alphabet::SymbolException ); - tmp.accessAlphabet < GeneralAlphabet > ( ).add ( "1" ); - tmp.accessAlphabet < NonlinearAlphabet > ( ).add ( "1" ); -} - -void AlphabetTest::testRemove ( ) { - A tmp ( "2" ); - - tmp.accessAlphabet < GeneralAlphabet > ( ).add ( "1" ); - tmp.accessAlphabet < NonlinearAlphabet > ( ).add ( "1" ); - CPPUNIT_ASSERT_THROW ( tmp.accessAlphabet < GeneralAlphabet > ( ).remove ( "1" ), alphabet::SymbolException ); - tmp.accessAlphabet < NonlinearAlphabet > ( ).remove ( "1" ); - tmp.accessAlphabet < GeneralAlphabet > ( ).remove ( "1" ); -} diff --git a/alib2data/test-src/alphabet/AlphabetTest.h b/alib2data/test-src/alphabet/AlphabetTest.h deleted file mode 100644 index d42a0cd132151c0b097ad58991b17c3dc1c2184f..0000000000000000000000000000000000000000 --- a/alib2data/test-src/alphabet/AlphabetTest.h +++ /dev/null @@ -1,20 +0,0 @@ -#ifndef ALPHABET_TEST_H_ -#define ALPHABET_TEST_H_ - -#include <cppunit/extensions/HelperMacros.h> - -class AlphabetTest : public CppUnit::TestFixture { - CPPUNIT_TEST_SUITE ( AlphabetTest ); - CPPUNIT_TEST ( testAdd ); - CPPUNIT_TEST ( testRemove ); - CPPUNIT_TEST_SUITE_END ( ); - -public: - void setUp ( ); - void tearDown ( ); - - void testAdd ( ); - void testRemove ( ); -}; - -#endif // ALPHABET_TEST_H_