diff --git a/alib2data/src/alphabet/Alphabet.hpp b/alib2data/src/alphabet/Alphabet.hpp index 76f06e0d56cf6ba2620b0e072fc1cd3dfbcdc1c2..036870beb0df93de94be8a5a3e77bcdca5848842 100644 --- a/alib2data/src/alphabet/Alphabet.hpp +++ b/alib2data/src/alphabet/Alphabet.hpp @@ -27,6 +27,26 @@ class Alphabet { */ std::set < SymbolType > data; + /** + * Checks whether symbol can be added to the alphabet. Calls valid and available functions. + * @throws AlibException if symbol cannot be added. + */ + void checkAdd ( const SymbolType & symbol ) { + valid ( symbol ); + + if ( !available ( symbol ) ) + throw exception::AlibException ( "Symbol " + ( std::string ) symbol + " is not available." ); + } + + /** + * Checks whether symbol can be removed to the alphabet. Calls used function. + * @throws AlibException if symbol cannot be removed. + */ + void checkRemove ( const SymbolType & symbol ) { + if ( used ( symbol ) ) + throw exception::AlibException ( "Symbol " + ( std::string ) symbol + " is used." ); + } + public: /* * Constructs an empty alphabet. @@ -39,42 +59,49 @@ public: * @throw AlibException 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 ) { - valid ( symbol ); - - if ( !available ( symbol ) ) - throw exception::AlibException ( "Symbol " + ( std::string ) symbol + " is not available." ); - } + for ( const SymbolType & symbol : data ) + checkAdd ( symbol ); } /** * Adds a symbol to the alphabet. * @param symbol to add to the alphabet - * @throw AlibException if symbols are not available in context of datatype where the alphabet is used + * @throw AlibException 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 ) { - valid ( symbol ); + checkAdd ( symbol ); + return data.insert ( std::move ( symbol ) ).second; + } - if ( !available ( symbol ) ) - throw exception::AlibException ( "Symbol " + ( std::string ) symbol + " is not available." ); - else - return data.insert ( std::move ( symbol ) ).second; + /** + * Adds a set of symbols to the alphabet. + * @param symbols to add to the alphabet + * @throw AlibException 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 AlibException if symbol is used in context of datatype instance using the alphabet + * @throw AlibException if one of the removed symbols is used in context of datatype instance using the alphabet + * AlibException 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 ) - remove ( symbol ); + checkRemove ( symbol ); for ( const SymbolType & symbol : symbols ) - add ( std::move ( symbols ) ); + checkAdd ( symbol ); + + data = std::move ( symbols ); } /** @@ -98,10 +125,17 @@ public: * false if symbol was not present in the alphabet */ bool remove ( const SymbolType & symbol ) { - if ( used ( symbol ) ) - throw exception::AlibException ( "Symbol " + ( std::string ) symbol + " is used." ); - else - return data.erase ( symbol ); + checkRemove ( symbol ); + return data.erase ( symbol ); + } + + /** + * Removes a set of symbols from alphabet if not used. + * @throw AlibException 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 ); } /** @@ -143,7 +177,6 @@ public: * @throw AlibException if the symbol in any way invalid */ void valid ( const SymbolType & symbol ) const; - }; /**