Skip to content
Snippets Groups Projects
Commit c95c9876 authored by Ing. Radomír Polách's avatar Ing. Radomír Polách
Browse files

FEATURE: cylcic string comparison

parent efa5497c
No related branches found
No related tags found
No related merge requests found
/*
* CyclicStringCompare.cpp
*
* Created on: Oct 9, 2014
* Author: Radomír Polcáh */
#include "CyclicStringCompare.h"
namespace compare {
bool CyclicStringCompare::equals(const string::LinearString& u, const string::LinearString& v) {
int n = (int)u.getContent().size();
int i = -1, j = -1, k;
if (n != v.getContent().size()) return false;
while(i < n - 1 && j < n - 1)
{
k = 1;
while(k <= n && u.getContent()[(i + k) % n] == v.getContent()[(j + k) % n]) k++;
if (k > n) return true;
if (u.getContent()[(i + k) % n] > v.getContent()[(j + k) % n]) i += k; else j += k;
}
return false;
}
int CyclicStringCompare::compare(const string::LinearString& u, const string::LinearString& v) {
int n = (int)u.getContent().size(), m = (int)v.getContent().size();
int i = -1, j = -1, k;
bool last = 0;
while(i < n - 1 && j < m - 1)
{
k = 1;
while(k <= n && u.getContent()[(i + k) % n] == v.getContent()[(j + k) % m]) k++;
if (k > n) return 0;
if (last = u.getContent()[(i + k) % n] > v.getContent()[(j + k) % m]) i += k; else j += k;
}
return last ? 1 : - 1;
}
}
/*
* CyclicStringCompare.h
*
* Created on: Oct 14, 2014
* Author: Radomír Polách
*/
#ifndef CYCLIC_STRING_COMPARE_H_
#define CYCLIC_STRING_COMPARE_H_
#include "string/LinearString.h"
namespace compare {
class CyclicStringCompare {
public:
static bool equals(const string::LinearString& u, const string::LinearString& v);
static int compare(const string::LinearString& u, const string::LinearString& v);
};
}
#endif /* CYCLIC_STRING_COMPARE_H_ */
#include <list>
#include "compareTest.h"
#include "compare/string/CyclicStringCompare.h"
#include "string/LinearString.h"
#define CPPUNIT_IMPLY(x, y) CPPUNIT_ASSERT(!(x) || (y))
CPPUNIT_TEST_SUITE_REGISTRATION( compareTest );
void compareTest::setUp() {
}
void compareTest::tearDown() {
}
void compareTest::testCyclicStringCompareBoolean() {
string::LinearString str1("alfa");
string::LinearString str2("aalf");
str2.addSymbolsToAlphabet(str1.getAlphabet());
str1.addSymbolsToAlphabet(str2.getAlphabet());
CPPUNIT_ASSERT(compare::CyclicStringCompare::equals(str1, str2));
}
void compareTest::testCyclicStringCompareInt() {
string::LinearString str1("alfa");
string::LinearString str2("aalf");
str2.addSymbolsToAlphabet(str1.getAlphabet());
str1.addSymbolsToAlphabet(str2.getAlphabet());
CPPUNIT_ASSERT(compare::CyclicStringCompare::compare(str1, str2) == 0);
}
#ifndef COMPARE_TEST_H_
#define COMPARE_TEST_H_
#include <cppunit/extensions/HelperMacros.h>
class compareTest : public CppUnit::TestFixture
{
CPPUNIT_TEST_SUITE( compareTest );
CPPUNIT_TEST( testCyclicStringCompareBoolean );
CPPUNIT_TEST( testCyclicStringCompareInt );
CPPUNIT_TEST_SUITE_END();
public:
void setUp();
void tearDown();
void testCyclicStringCompareBoolean();
void testCyclicStringCompareInt();
};
#endif // COMPARE_TEST_H_
...@@ -7,6 +7,9 @@ ...@@ -7,6 +7,9 @@
   
#include "CyclicString.h" #include "CyclicString.h"
#include "../exception/AlibException.h" #include "../exception/AlibException.h"
#include "../label/Label.h"
#include "../label/CharacterLabel.h"
#include "../alphabet/LabeledSymbol.h"
   
#include <cassert> #include <cassert>
#include <sstream> #include <sstream>
...@@ -43,6 +46,14 @@ CyclicString::CyclicString(std::vector<alphabet::Symbol>&& data) { ...@@ -43,6 +46,14 @@ CyclicString::CyclicString(std::vector<alphabet::Symbol>&& data) {
setContent(std::move(data)); setContent(std::move(data));
} }
   
CyclicString::CyclicString(const std::string& str) {
for (unsigned i = 0; i < str.length(); ++i)
{
m_Data.push_back(alphabet::Symbol(alphabet::LabeledSymbol(label::Label(label::CharacterLabel(str[i])))));
}
alphabet = std::set<alphabet::Symbol>(m_Data.begin(), m_Data.end());
}
StringBase* CyclicString::clone() const { StringBase* CyclicString::clone() const {
return new CyclicString(*this); return new CyclicString(*this);
} }
......
...@@ -31,6 +31,7 @@ public: ...@@ -31,6 +31,7 @@ public:
explicit CyclicString(std::set<alphabet::Symbol>&& alphabet, std::vector<alphabet::Symbol>&& data); explicit CyclicString(std::set<alphabet::Symbol>&& alphabet, std::vector<alphabet::Symbol>&& data);
explicit CyclicString(const std::vector<alphabet::Symbol>& data); explicit CyclicString(const std::vector<alphabet::Symbol>& data);
explicit CyclicString(std::vector<alphabet::Symbol>&& data); explicit CyclicString(std::vector<alphabet::Symbol>&& data);
explicit CyclicString(const std::string& str);
   
virtual StringBase* clone() const; virtual StringBase* clone() const;
virtual StringBase* plunder() &&; virtual StringBase* plunder() &&;
......
...@@ -7,6 +7,9 @@ ...@@ -7,6 +7,9 @@
   
#include "LinearString.h" #include "LinearString.h"
#include "../exception/AlibException.h" #include "../exception/AlibException.h"
#include "../label/Label.h"
#include "../label/CharacterLabel.h"
#include "../alphabet/LabeledSymbol.h"
   
#include <cassert> #include <cassert>
#include <sstream> #include <sstream>
...@@ -43,6 +46,14 @@ LinearString::LinearString(std::vector<alphabet::Symbol>&& data) { ...@@ -43,6 +46,14 @@ LinearString::LinearString(std::vector<alphabet::Symbol>&& data) {
m_Data = std::move(data); m_Data = std::move(data);
} }
   
LinearString::LinearString(const std::string& str) {
for (unsigned i = 0; i < str.length(); ++i)
{
m_Data.push_back(alphabet::Symbol(alphabet::LabeledSymbol(label::Label(label::CharacterLabel(str[i])))));
}
alphabet = std::set<alphabet::Symbol>(m_Data.begin(), m_Data.end());
}
StringBase* LinearString::clone() const { StringBase* LinearString::clone() const {
return new LinearString(*this); return new LinearString(*this);
} }
......
...@@ -32,6 +32,7 @@ public: ...@@ -32,6 +32,7 @@ public:
explicit LinearString(std::set<alphabet::Symbol>&& alphabet, std::vector<alphabet::Symbol>&& data); explicit LinearString(std::set<alphabet::Symbol>&& alphabet, std::vector<alphabet::Symbol>&& data);
explicit LinearString(const std::vector<alphabet::Symbol>& data); explicit LinearString(const std::vector<alphabet::Symbol>& data);
explicit LinearString(std::vector<alphabet::Symbol>&& data); explicit LinearString(std::vector<alphabet::Symbol>&& data);
explicit LinearString(const std::string& str);
   
virtual StringBase* clone() const; virtual StringBase* clone() const;
virtual StringBase* plunder() &&; virtual StringBase* plunder() &&;
......
...@@ -27,7 +27,7 @@ public: ...@@ -27,7 +27,7 @@ public:
   
virtual StringBase* clone() const = 0; virtual StringBase* clone() const = 0;
virtual StringBase* plunder() && = 0; virtual StringBase* plunder() && = 0;
}; };
   
} /* namespace string */ } /* namespace string */
......
...@@ -19,6 +19,12 @@ bool StringAlphabet::addSymbolToAlphabet(const alphabet::Symbol& symbol) { ...@@ -19,6 +19,12 @@ bool StringAlphabet::addSymbolToAlphabet(const alphabet::Symbol& symbol) {
return alphabet.insert(symbol).second; return alphabet.insert(symbol).second;
} }
   
void StringAlphabet::addSymbolsToAlphabet(const std::set<alphabet::Symbol>& symbols) {
for(const alphabet::Symbol& addedSymbol : symbols) {
addSymbolToAlphabet(addedSymbol);
}
}
void StringAlphabet::setAlphabet(const std::set<alphabet::Symbol>& newSymbols) { void StringAlphabet::setAlphabet(const std::set<alphabet::Symbol>& newSymbols) {
std::set<alphabet::Symbol> removed; std::set<alphabet::Symbol> removed;
std::set_difference(alphabet.begin(), alphabet.end(), newSymbols.begin(), newSymbols.end(), std::inserter(removed, removed.end())); std::set_difference(alphabet.begin(), alphabet.end(), newSymbols.begin(), newSymbols.end(), std::inserter(removed, removed.end()));
......
...@@ -24,14 +24,18 @@ public: ...@@ -24,14 +24,18 @@ public:
/** /**
* Adds input symbol to input alphabet. * Adds input symbol to input alphabet.
* @param symbol Symbol to add * @param symbol Symbol to add
* @throws AutomatonException when symbol already exists
*/ */
bool addSymbolToAlphabet(const alphabet::Symbol& symbol); bool addSymbolToAlphabet(const alphabet::Symbol& symbol);
/**
* Adds input symbols to input alphabet.
* @param symbols Symbol to add
*/
void addSymbolsToAlphabet(const std::set<alphabet::Symbol>& symbols);
   
/** /**
* Sets input symbols of the string. * Sets input symbols of the string.
* @param symbols Symbols to set * @param symbols Symbols to set
* @throws AutomatonException when symbol already exists
*/ */
void setAlphabet(const std::set<alphabet::Symbol>& symbols); void setAlphabet(const std::set<alphabet::Symbol>& symbols);
   
......
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