From 4d2e6d8cb99d31d52aafdeb371e9ef747ec5bfc6 Mon Sep 17 00:00:00 2001 From: Jan Travnicek <Jan.Travnicek@fit.cvut.cz> Date: Tue, 18 Nov 2014 12:53:18 +0100 Subject: [PATCH] visitors for string algorithms --- .../string/compare/CyclicStringCompare.cpp | 46 ----------- .../src/string/compare/CyclicStringCompare.h | 28 ------- alib2algo/src/string/naive/ExactCompare.cpp | 79 +++++++++++++++++++ alib2algo/src/string/naive/ExactCompare.h | 37 +++++++++ alib2algo/src/string/naive/ExactEqual.cpp | 72 +++++++++++++++++ alib2algo/src/string/naive/ExactEqual.h | 37 +++++++++ alib2algo/src/string/naive/ExactMatch.h | 3 +- .../test-src/string/compare/compareTest.cpp | 21 ++--- 8 files changed, 237 insertions(+), 86 deletions(-) delete mode 100644 alib2algo/src/string/compare/CyclicStringCompare.cpp delete mode 100644 alib2algo/src/string/compare/CyclicStringCompare.h create mode 100644 alib2algo/src/string/naive/ExactCompare.cpp create mode 100644 alib2algo/src/string/naive/ExactCompare.h create mode 100644 alib2algo/src/string/naive/ExactEqual.cpp create mode 100644 alib2algo/src/string/naive/ExactEqual.h diff --git a/alib2algo/src/string/compare/CyclicStringCompare.cpp b/alib2algo/src/string/compare/CyclicStringCompare.cpp deleted file mode 100644 index 418bc5cbc1..0000000000 --- a/alib2algo/src/string/compare/CyclicStringCompare.cpp +++ /dev/null @@ -1,46 +0,0 @@ -/* - * CyclicStringCompare.cpp - * - * Created on: Oct 9, 2014 - * Author: Radomir Polach */ - -#include "CyclicStringCompare.h" - -namespace string { - -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 != (int) 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; - last = u.getContent()[(i + k) % n] > v.getContent()[(j + k) % m]; - if (last) i += k; else j += k; - } - return last ? 1 : - 1; -} - -} /* namespace compare */ - -} /* namespace string */ diff --git a/alib2algo/src/string/compare/CyclicStringCompare.h b/alib2algo/src/string/compare/CyclicStringCompare.h deleted file mode 100644 index 2e54502155..0000000000 --- a/alib2algo/src/string/compare/CyclicStringCompare.h +++ /dev/null @@ -1,28 +0,0 @@ -/* - * CyclicStringCompare.h - * - * Created on: Oct 14, 2014 - * Author: Radomir Polach - */ - -#ifndef CYCLIC_STRING_COMPARE_H_ -#define CYCLIC_STRING_COMPARE_H_ - -#include "string/LinearString.h" - -namespace string { - -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); - -}; - -} /* namespace compare */ - -} /* namespace string */ - -#endif /* CYCLIC_STRING_COMPARE_H_ */ diff --git a/alib2algo/src/string/naive/ExactCompare.cpp b/alib2algo/src/string/naive/ExactCompare.cpp new file mode 100644 index 0000000000..02999dd3f0 --- /dev/null +++ b/alib2algo/src/string/naive/ExactCompare.cpp @@ -0,0 +1,79 @@ +/* + * ExactCompare.cpp + * + * Created on: Oct 9, 2014 + * Author: Radomir Polach */ + +#include "ExactCompare.h" + +#include "string/LinearString.h" +#include "string/CyclicString.h" + +namespace string { + +namespace naive { + +int ExactCompare::compare(const string::String& u, const string::String& v) { + int data; + Accept((void*) &data, u.getData(), v.getData(), ExactCompare::EXACT_COMPARE); + return data; +} + +int ExactCompare::compare(const string::Epsilon&, const string::Epsilon&) { + return 0; +} + +int ExactCompare::compare(const string::LinearString& u, const string::LinearString& v) { + int n = (int) u.getContent().size(), m = (int)v.getContent().size(); + int k = 0; + + while(k < n && k < m && u.getContent()[k] == v.getContent()[k]) k++; + if (k == m && k == n) { + return 0; + } else if(k == m) { + return -1; + } else if(k == n) { + return 1; + } else if(u.getContent()[k] < v.getContent()[k]) { + return -1; + } else { + return 1; + } +} + +int ExactCompare::compare(const string::CyclicString& u, const string::CyclicString& 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; + last = u.getContent()[(i + k) % n] > v.getContent()[(j + k) % m]; + if (last) i += k; else j += k; + } + return last ? 1 : - 1; +} + +void ExactCompare::Visit(void* data, const string::Epsilon& u, const string::Epsilon& v) const { + int & res = *((int*) data); + res = this->compare(u, v); +} + +void ExactCompare::Visit(void* data, const string::LinearString& u, const string::LinearString& v) const { + int & res = *((int*) data); + res = this->compare(u, v); +} + +void ExactCompare::Visit(void* data, const string::CyclicString& u, const string::CyclicString& v) const { + int & res = *((int*) data); + res = this->compare(u, v); +} + +const ExactCompare ExactCompare::EXACT_COMPARE; + +} /* namespace naive */ + +} /* namespace string */ diff --git a/alib2algo/src/string/naive/ExactCompare.h b/alib2algo/src/string/naive/ExactCompare.h new file mode 100644 index 0000000000..3e6cdc880b --- /dev/null +++ b/alib2algo/src/string/naive/ExactCompare.h @@ -0,0 +1,37 @@ +/* + * ExactCompare.h + * + * Created on: Oct 14, 2014 + * Author: Radomir Polach + */ + +#ifndef EXACT_COMPARE_H_ +#define EXACT_COMPARE_H_ + +#include <string/String.h> + +namespace string { + +namespace naive { + +class ExactCompare : public string::VisitableStringBase::const_promoting_visitor_type { +public: + static int compare(const string::String& u, const string::String& v); + + static int compare(const string::Epsilon& u, const string::Epsilon& v); + static int compare(const string::LinearString& u, const string::LinearString& v); + static int compare(const string::CyclicString& u, const string::CyclicString& v); + +private: + void Visit(void*, const Epsilon& u, const Epsilon& v) const; + void Visit(void*, const LinearString& u, const LinearString& v) const; + void Visit(void*, const CyclicString& u, const CyclicString& v) const; + + static const ExactCompare EXACT_COMPARE; +}; + +} /* namespace naive */ + +} /* namespace string */ + +#endif /* EXACT_COMPARE_H_ */ diff --git a/alib2algo/src/string/naive/ExactEqual.cpp b/alib2algo/src/string/naive/ExactEqual.cpp new file mode 100644 index 0000000000..746bb375c4 --- /dev/null +++ b/alib2algo/src/string/naive/ExactEqual.cpp @@ -0,0 +1,72 @@ +/* + * ExactEqual.cpp + * + * Created on: Oct 9, 2014 + * Author: Radomir Polach */ + +#include "ExactEqual.h" + +#include "string/LinearString.h" +#include "string/CyclicString.h" + +namespace string { + +namespace naive { + +bool ExactEqual::equals(const string::String& u, const string::String& v) { + bool data; + Accept((void*) &data, u.getData(), v.getData(), ExactEqual::EXACT_EQUAL); + return data; +} + +bool ExactEqual::equals(const string::Epsilon&, const string::Epsilon&) { + return true; +} + +bool ExactEqual::equals(const string::LinearString& u, const string::LinearString& v) { + int n = (int) u.getContent().size(), m = (int)v.getContent().size(); + int k = 0; + + while(k < n && k < m && u.getContent()[k] == v.getContent()[k]) k++; + if (k == m && k == n) { + return true; + } else { + return false; + } +} + +bool ExactEqual::equals(const string::CyclicString& u, const string::CyclicString& v) { + int n = (int)u.getContent().size(); + int i = -1, j = -1, k; + if (n != (int) 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; +} + +void ExactEqual::Visit(void* data, const string::Epsilon& u, const string::Epsilon& v) const { + bool & res = *((bool*) data); + res = this->equals(u, v); +} + +void ExactEqual::Visit(void* data, const string::LinearString& u, const string::LinearString& v) const { + bool & res = *((bool*) data); + res = this->equals(u, v); +} + +void ExactEqual::Visit(void* data, const string::CyclicString& u, const string::CyclicString& v) const { + bool & res = *((bool*) data); + res = this->equals(u, v); +} + +const ExactEqual ExactEqual::EXACT_EQUAL; + +} /* namespace naive */ + +} /* namespace string */ diff --git a/alib2algo/src/string/naive/ExactEqual.h b/alib2algo/src/string/naive/ExactEqual.h new file mode 100644 index 0000000000..b131b5fdaf --- /dev/null +++ b/alib2algo/src/string/naive/ExactEqual.h @@ -0,0 +1,37 @@ +/* + * ExactEqual.h + * + * Created on: Oct 14, 2014 + * Author: Radomir Polach + */ + +#ifndef EXACT_EQUAL_H_ +#define EXACT_EQUAL_H_ + +#include <string/String.h> + +namespace string { + +namespace naive { + +class ExactEqual : public string::VisitableStringBase::const_promoting_visitor_type { +public: + static bool equals(const string::String& u, const string::String& v); + + static bool equals(const string::Epsilon& u, const string::Epsilon& v); + static bool equals(const string::LinearString& u, const string::LinearString& v); + static bool equals(const string::CyclicString& u, const string::CyclicString& v); + +private: + void Visit(void*, const Epsilon& u, const Epsilon& v) const; + void Visit(void*, const LinearString& u, const LinearString& v) const; + void Visit(void*, const CyclicString& u, const CyclicString& v) const; + + static const ExactEqual EXACT_EQUAL; +}; + +} /* namespace naive */ + +} /* namespace string */ + +#endif /* EXACT_EQUAL_H_ */ diff --git a/alib2algo/src/string/naive/ExactMatch.h b/alib2algo/src/string/naive/ExactMatch.h index dae0b062d2..bf6d43bc40 100644 --- a/alib2algo/src/string/naive/ExactMatch.h +++ b/alib2algo/src/string/naive/ExactMatch.h @@ -8,14 +8,13 @@ #ifndef _EXACT_MATCH_H__ #define _EXACT_MATCH_H__ -#include <string/LinearString.h> #include <string/String.h> namespace string { namespace naive { -class ExactMatch : public string::VisitableStringBase::const_same_visitor_type { +class ExactMatch : public string::VisitableStringBase::const_promoting_visitor_type { public: /** * Performs conversion. diff --git a/alib2algo/test-src/string/compare/compareTest.cpp b/alib2algo/test-src/string/compare/compareTest.cpp index fed1655bd4..957247a971 100644 --- a/alib2algo/test-src/string/compare/compareTest.cpp +++ b/alib2algo/test-src/string/compare/compareTest.cpp @@ -1,7 +1,8 @@ #include <list> #include "compareTest.h" -#include "string/compare/CyclicStringCompare.h" +#include "string/naive/ExactEqual.h" +#include "string/naive/ExactCompare.h" #include "string/simplify/NormalizeRotation.h" #include "string/LinearString.h" #include "string/CyclicString.h" @@ -17,29 +18,29 @@ void compareTest::tearDown() { } void compareTest::testCyclicStringCompareBoolean() { - string::LinearString str1("alfa"); - string::LinearString str2("aalf"); + string::CyclicString str1("alfa"); + string::CyclicString str2("aalf"); str2.addSymbolsToAlphabet(str1.getAlphabet()); str1.addSymbolsToAlphabet(str2.getAlphabet()); - CPPUNIT_ASSERT(string::compare::CyclicStringCompare::equals(str1, str2)); + CPPUNIT_ASSERT(string::naive::ExactEqual::equals(str1, str2)); } void compareTest::testCyclicStringCompareInt() { - string::LinearString str1("alfa"); - string::LinearString str2("aalf"); + string::CyclicString str1("alfa"); + string::CyclicString str2("aalf"); str2.addSymbolsToAlphabet(str1.getAlphabet()); str1.addSymbolsToAlphabet(str2.getAlphabet()); - CPPUNIT_ASSERT(string::compare::CyclicStringCompare::compare(str1, str2) == 0); + CPPUNIT_ASSERT(string::naive::ExactCompare::compare(str1, str2) == 0); - string::LinearString str3("ccbcccb"); - string::LinearString str4("cbcccbc"); + string::CyclicString str3("ccbcccb"); + string::CyclicString str4("cbcccbc"); str2.addSymbolsToAlphabet(str1.getAlphabet()); str1.addSymbolsToAlphabet(str2.getAlphabet()); - CPPUNIT_ASSERT(string::compare::CyclicStringCompare::compare(str3, str4) == 0); + CPPUNIT_ASSERT(string::naive::ExactCompare::compare(str3, str4) == 0); } void compareTest::testCompareCyclic() { -- GitLab