Skip to content
Snippets Groups Projects
Commit 4d3d989a authored by Tomáš Pecka's avatar Tomáš Pecka
Browse files

alib: Work on Word/String

 - Fix StringPrinter
 - RemoveAlphabetSymbol() now checks for symbol presence in both alphabet and content.
 - some constness
 - Doxygen (original was obviously copy-pasted)
parent 38b9e100
No related branches found
No related tags found
No related merge requests found
......@@ -5,6 +5,8 @@
* Author: Jan Travnicek
*/
 
#include <algorithm>
#include "String.h"
#include "StringPrinter.h"
#include "../AlibException.h"
......@@ -19,16 +21,21 @@ String::String() {
void String::addAlphabetSymbol(const alphabet::Symbol& symbol) {
std::pair<std::set<alphabet::Symbol>::iterator, bool> ret = this->alphabet.insert(symbol);
if (!ret.second)
throw alib::AlibException("Input symbol \"" + symbol.getSymbol() + "\" already exists.");
throw alib::AlibException("Alphabet symbol \"" + symbol.getSymbol() + "\" already exists.");
}
 
void String::removeAlphabetSymbol(const alphabet::Symbol& symbol) {
auto it = this->alphabet.find (symbol);
if(it != this->alphabet.end())
throw alib::AlibException("Input symbol \"" + symbol.getSymbol() + "\" used in string.");
auto it = this->alphabet.find( symbol );
if( it == this->alphabet.end())
throw alib::AlibException("Symbol \"" + symbol.getSymbol() + "\" is not in alphabet.");
auto it2 = std::find( this->symbols.begin( ), this->symbols.end( ), symbol );
if(it2 != this->symbols.end())
throw alib::AlibException("Symbol \"" + symbol.getSymbol() + "\" is used in string.");
this->alphabet.erase(symbol);
}
const std::set<alphabet::Symbol>& String::getAlphabet() const {
return this->alphabet;
}
......@@ -45,11 +52,11 @@ bool String::isEmpty() const {
return this->symbols.size() == 0;
}
 
void String::toXML(std::ostream& out) {
void String::toXML(std::ostream& out) const {
StringPrinter::toXML(*this, out);
}
 
std::ostream& operator <<(std::ostream& out, String& string) {
std::ostream& operator <<(std::ostream& out, const String& string) {
string.toXML(out);
return out;
}
......
......@@ -28,39 +28,39 @@ private:
 
public:
String();
/**
* Adds input symbol to the alphabet.
* @param symbol Symbol to add
* @throws AutomatonException when symbol already exists
* @throws AlibException if symbol already exists
*/
void addAlphabetSymbol(const alphabet::Symbol& symbol);
 
/**
* Removes input symbol from the the alphabet.
* @param symbol Symbol to remove
* @throws AutomatonException when symbol is not present in input alphabet
* or when symbol is part of the transition
* @throws AlibException if symbol is not present in input alphabet
* or when symbol is present in content of the string.
*/
void removeAlphabetSymbol(const alphabet::Symbol& symbol);
/**
* @return the input alphabet
*/
const std::set<alphabet::Symbol>& getAlphabet() const;
 
/**
* @return Root node of the regular expression tree
* @return List of symbols forming string.
*/
std::list<alphabet::Symbol>& getContent();
 
/**
* @return Root node of the regular expression tree
* @return List of symbols forming string (const version).
*/
const std::list<alphabet::Symbol>& getContent() const;
 
/**
* @return true if regexp represents empty language
* @return true if string is an empty word (its length is 0)
*/
bool isEmpty() const;
 
......@@ -68,14 +68,14 @@ public:
* Prints XML representation of the String to the output stream.
* @param out output stream to which print the String
*/
void toXML(std::ostream& out);
void toXML(std::ostream& out) const;
 
/**
* Prints XML representation of the String to the output stream.
* @param out output stream to which print the String
* @param regexp String to print
* @param string String to print
*/
friend std::ostream& operator<<(std::ostream& out, String& regexp);
friend std::ostream& operator<<(std::ostream& out, const String& string);
 
};
 
......
......@@ -12,14 +12,14 @@ namespace string {
 
const std::string StringPrinter::INDENTATION = "\t";
 
void StringPrinter::toXML(String& string, std::ostream& out) {
void StringPrinter::toXML(const String& string, std::ostream& out) {
out << "<string>\n";
printAlphabet(string, out, INDENTATION);
printContent(string, out, INDENTATION);
out << "</string>\n";
}
 
void StringPrinter::printAlphabet(String& string, std::ostream& out, std::string prefix) {
void StringPrinter::printAlphabet(const String& string, std::ostream& out, const std::string & prefix) {
out << prefix << "<alphabet>\n";
for (auto element : string.getAlphabet()) {
printSymbol(element, out, prefix + INDENTATION);
......@@ -27,7 +27,7 @@ void StringPrinter::printAlphabet(String& string, std::ostream& out, std::string
out << prefix << "</alphabet>\n";
}
 
void StringPrinter::printContent(String& string, std::ostream& out, std::string prefix) {
void StringPrinter::printContent(const String& string, std::ostream& out, const std::string & prefix) {
out << prefix << "<content>\n";
for (auto element : string.getContent()) {
printSymbol(element, out, prefix + INDENTATION);
......@@ -35,10 +35,8 @@ void StringPrinter::printContent(String& string, std::ostream& out, std::string
out << prefix << "</content>\n";
}
 
void StringPrinter::printSymbol(alphabet::Symbol& element, std::ostream& out, std::string prefix) {
out << prefix << "<symbol>\n";
out << prefix << element.getSymbol();
out << prefix << "</symbol>\n";
void StringPrinter::printSymbol(const alphabet::Symbol& element, std::ostream& out, const std::string & prefix) {
out << prefix << "<symbol>" << element.getSymbol() << "</symbol>\n";
}
 
} /* namespace string */
......
......@@ -15,21 +15,22 @@ namespace notions {
namespace string {
 
/**
* This class contains methods to print XML representation of regular expression to the output stream.
* This class contains methods to print XML representation of string to the output stream.
*/
class StringPrinter {
protected:
static const std::string INDENTATION;
static void printContent(String& element, std::ostream& out, std::string prefix);
static void printAlphabet(String& content, std::ostream& out, std::string prefix);
static void printSymbol(alphabet::Symbol& symbol, std::ostream& out, std::string prefix);
static void printContent(const String& element, std::ostream& out, const std::string & prefix);
static void printAlphabet(const String& content, std::ostream& out, const std::string & prefix);
static void printSymbol(const alphabet::Symbol& symbol, std::ostream& out, const std::string & prefix);
public:
/**
* Prints XML representation of RegExp to the output stream.
* @param regexp RegExp to print
* @param out output stream to which print the RegExp
* Prints XML representation of String to the output stream.
* @param string String to print
* @param out output stream to which print the String
*/
static void toXML(notions::string::String& regexp, std::ostream& out);
static void toXML(const notions::string::String& regexp, std::ostream& out);
};
 
} /* namespace string */
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment