Skip to content
Snippets Groups Projects
Commit 8c9aedb0 authored by Jan Trávníček's avatar Jan Trávníček
Browse files

continue refactoring

Conflicts:
	alib2algo/src/regexp/properties/RegExpEpsilon.h
parent c5de8f62
No related branches found
No related tags found
No related merge requests found
......@@ -13,38 +13,42 @@ namespace regexp {
 
namespace properties {
 
bool RegExpEmpty::languageIsEmpty(const regexp::RegExp& regexp)
{
bool out;
regexp.getData().Accept((void*) &out, RegExpEmpty::REG_EXP_EMPTY);
return out;
bool RegExpEmpty::languageIsEmpty(const regexp::RegExp& regexp) {
return getInstance().dispatch(regexp.getData());
}
 
bool RegExpEmpty::languageIsEmpty(const regexp::FormalRegExp& regexp)
{
bool RegExpEmpty::languageIsEmpty(const regexp::FormalRegExpElement& regexp) {
bool out;
regexp.getRegExp().Accept((void*) &out, RegExpEmpty::REG_EXP_EMPTY);
regexp.Accept((void*) &out, RegExpEmpty::REG_EXP_EMPTY);
return out;
}
 
bool RegExpEmpty::languageIsEmpty(const regexp::UnboundedRegExp& regexp)
{
bool RegExpEmpty::languageIsEmpty(const regexp::FormalRegExp& regexp) {
return languageIsEmpty(regexp.getRegExp());
}
auto RegExpEmptyFormalRegExp = RegExpEmpty::RegistratorWrapper<bool, regexp::FormalRegExp>(RegExpEmpty::getInstance(), RegExpEmpty::languageIsEmpty);
bool RegExpEmpty::languageIsEmpty(const regexp::UnboundedRegExpElement& regexp) {
bool out;
regexp.getRegExp().Accept((void*) &out, RegExpEmpty::REG_EXP_EMPTY);
regexp.Accept((void*) &out, RegExpEmpty::REG_EXP_EMPTY);
return out;
}
 
bool RegExpEmpty::languageIsEmpty(const regexp::UnboundedRegExp& regexp) {
return languageIsEmpty(regexp.getRegExp());
}
auto RegExpEmptyUnboundedRegExp = RegExpEmpty::RegistratorWrapper<bool, regexp::UnboundedRegExp>(RegExpEmpty::getInstance(), RegExpEmpty::languageIsEmpty);
// ----------------------------------------------------------------------------
 
void RegExpEmpty::Visit(void* data, const regexp::UnboundedRegExpAlternation& alternation) const
{
void RegExpEmpty::Visit(void* data, const regexp::UnboundedRegExpAlternation& alternation) const {
bool &ret = *(bool*) data;
 
for(const auto& element : alternation.getElements())
{
for(const auto& element : alternation.getElements()) {
static_cast<const regexp::UnboundedRegExpElement&>(*element).Accept(data, *this);
if(! ret)
{
if(! ret) {
ret = false;
return;
}
......@@ -52,15 +56,12 @@ void RegExpEmpty::Visit(void* data, const regexp::UnboundedRegExpAlternation& al
ret = true;
}
 
void RegExpEmpty::Visit(void* data, const regexp::UnboundedRegExpConcatenation& concatenation) const
{
void RegExpEmpty::Visit(void* data, const regexp::UnboundedRegExpConcatenation& concatenation) const {
bool &ret = *(bool*) data;
 
for(const auto& element : concatenation.getElements())
{
for(const auto& element : concatenation.getElements()) {
static_cast<const regexp::UnboundedRegExpElement&>(*element).Accept(data, *this);
if(ret)
{
if(ret) {
ret = true;
return;
}
......@@ -68,34 +69,29 @@ void RegExpEmpty::Visit(void* data, const regexp::UnboundedRegExpConcatenation&
ret = false;
}
 
void RegExpEmpty::Visit(void* data, const regexp::UnboundedRegExpIteration&) const
{
void RegExpEmpty::Visit(void* data, const regexp::UnboundedRegExpIteration&) const {
bool &ret = *(bool*) data;
ret = false;
}
 
void RegExpEmpty::Visit(void* data, const regexp::UnboundedRegExpSymbol&) const
{
void RegExpEmpty::Visit(void* data, const regexp::UnboundedRegExpSymbol&) const {
bool &ret = *(bool*) data;
ret = false;
}
 
void RegExpEmpty::Visit(void* data, const regexp::UnboundedRegExpEmpty&) const
{
void RegExpEmpty::Visit(void* data, const regexp::UnboundedRegExpEmpty&) const {
bool &ret = *(bool*) data;
ret = true;
}
 
void RegExpEmpty::Visit(void* data, const regexp::UnboundedRegExpEpsilon&) const
{
void RegExpEmpty::Visit(void* data, const regexp::UnboundedRegExpEpsilon&) const {
bool &ret = *(bool*) data;
ret = false;
}
 
// ----------------------------------------------------------------------------
 
void RegExpEmpty::Visit(void* data, const regexp::FormalRegExpAlternation& alternation) const
{
void RegExpEmpty::Visit(void* data, const regexp::FormalRegExpAlternation& alternation) const {
bool &ret = *(bool*) data;
 
static_cast<const regexp::FormalRegExpElement&>(alternation.getLeftElement()).Accept(data, *this);
......@@ -107,8 +103,7 @@ void RegExpEmpty::Visit(void* data, const regexp::FormalRegExpAlternation& alter
ret = retLeft && retRight;
}
 
void RegExpEmpty::Visit(void* data, const regexp::FormalRegExpConcatenation& concatenation) const
{
void RegExpEmpty::Visit(void* data, const regexp::FormalRegExpConcatenation& concatenation) const {
bool &ret = *(bool*) data;
 
static_cast<const regexp::FormalRegExpElement&>(concatenation.getLeftElement()).Accept(data, *this);
......@@ -120,44 +115,28 @@ void RegExpEmpty::Visit(void* data, const regexp::FormalRegExpConcatenation& con
ret = retLeft || retRight;
}
 
void RegExpEmpty::Visit(void* data, const regexp::FormalRegExpIteration&) const
{
void RegExpEmpty::Visit(void* data, const regexp::FormalRegExpIteration&) const {
bool &ret = *(bool*) data;
ret = false;
}
 
void RegExpEmpty::Visit(void* data, const regexp::FormalRegExpSymbol&) const
{
void RegExpEmpty::Visit(void* data, const regexp::FormalRegExpSymbol&) const {
bool &ret = *(bool*) data;
ret = false;
}
 
void RegExpEmpty::Visit(void* data, const regexp::FormalRegExpEmpty&) const
{
void RegExpEmpty::Visit(void* data, const regexp::FormalRegExpEmpty&) const {
bool &ret = *(bool*) data;
ret = true;
}
 
void RegExpEmpty::Visit(void* data, const regexp::FormalRegExpEpsilon&) const
{
void RegExpEmpty::Visit(void* data, const regexp::FormalRegExpEpsilon&) const {
bool &ret = *(bool*) data;
ret = false;
}
 
// ----------------------------------------------------------------------------
 
void RegExpEmpty::Visit(void* data, const regexp::FormalRegExp& regexp) const
{
bool &ret = *(bool*) data;
ret = RegExpEmpty::REG_EXP_EMPTY.languageIsEmpty(regexp);
}
void RegExpEmpty::Visit(void* data, const regexp::UnboundedRegExp& regexp) const
{
bool &ret = *(bool*) data;
ret = RegExpEmpty::REG_EXP_EMPTY.languageIsEmpty(regexp);
}
const RegExpEmpty RegExpEmpty::REG_EXP_EMPTY;
 
} /* namespace properties */
......
......@@ -8,6 +8,8 @@
#ifndef REG_EXP_EMPTY_H_
#define REG_EXP_EMPTY_H_
 
#include <common/multipleDispatch.hpp>
#include <regexp/RegExp.h>
#include <regexp/formal/FormalRegExp.h>
#include <regexp/unbounded/UnboundedRegExp.h>
......@@ -20,20 +22,19 @@ namespace properties {
* Determines whether regular expression is empty (regexp == \0)
*
*/
class RegExpEmpty : public regexp::VisitableRegExpBase::const_visitor_type, regexp::FormalRegExpElement::const_visitor_type, regexp::UnboundedRegExpElement::const_visitor_type
{
class RegExpEmpty : public std::SingleDispatch<bool, regexp::RegExpBase>, regexp::FormalRegExpElement::const_visitor_type, regexp::UnboundedRegExpElement::const_visitor_type {
public:
RegExpEmpty() {}
 
static bool languageIsEmpty(const regexp::RegExp& regexp);
 
static bool languageIsEmpty(const regexp::FormalRegExpElement& regexp);
static bool languageIsEmpty(const regexp::FormalRegExp& regexp);
static bool languageIsEmpty(const regexp::UnboundedRegExp& regexp);
static bool languageIsEmpty(const regexp::UnboundedRegExpElement& regexp);
 
private:
void Visit(void* data, const regexp::UnboundedRegExp& regexp) const;
void Visit(void* data, const regexp::FormalRegExp& regexp) const;
void Visit(void* data, const regexp::UnboundedRegExpAlternation& alternation) const;
void Visit(void* data, const regexp::UnboundedRegExpConcatenation& concatenation) const;
void Visit(void* data, const regexp::UnboundedRegExpIteration& iteration) const;
......@@ -49,6 +50,12 @@ private:
void Visit(void* data, const regexp::FormalRegExpEpsilon& epsilon) const;
 
static const RegExpEmpty REG_EXP_EMPTY;
public:
static RegExpEmpty& getInstance() {
static RegExpEmpty res;
return res;
}
};
 
} /* namespace properties */
......
......@@ -13,55 +13,42 @@ namespace regexp {
 
namespace properties {
 
bool RegExpEpsilon::languageContainsEpsilon(const regexp::RegExp& regexp)
{
bool RegExpEpsilon::languageContainsEpsilon(const regexp::RegExp& regexp) {
return getInstance().dispatch(regexp.getData());
}
 
bool RegExpEpsilon::languageContainsEpsilon(const regexp::FormalRegExp& regexp)
{
bool RegExpEpsilon::languageContainsEpsilon(const regexp::FormalRegExpElement& regexp) {
bool out;
regexp.getRegExp().Accept((void*) &out, RegExpEpsilon::REG_EXP_EPSILON);
regexp.Accept((void*) &out, RegExpEpsilon::REG_EXP_EPSILON);
return out;
}
 
auto RegExpEpsilonFormalRegExp = RegExpEpsilon::RegistratorWrapper<bool, regexp::FormalRegExp>(RegExpEpsilon::getInstance(), RegExpEpsilon::languageContainsEpsilon);
bool RegExpEpsilon::languageContainsEpsilon(const regexp::UnboundedRegExp& regexp)
{
bool out;
regexp.getRegExp().Accept((void*) &out, RegExpEpsilon::REG_EXP_EPSILON);
return out;
bool RegExpEpsilon::languageContainsEpsilon(const regexp::FormalRegExp& regexp) {
return languageContainsEpsilon(regexp.getRegExp());
}
 
auto RegExpEpsilonUnboundedRegExp = RegExpEpsilon::RegistratorWrapper<bool, regexp::UnboundedRegExp>(RegExpEpsilon::getInstance(), RegExpEpsilon::languageContainsEpsilon);
auto RegExpEpsilonFormalRegExp = RegExpEpsilon::RegistratorWrapper<bool, regexp::FormalRegExp>(RegExpEpsilon::getInstance(), RegExpEpsilon::languageContainsEpsilon);
 
bool RegExpEpsilon::languageContainsEpsilon(const regexp::FormalRegExpElement& element)
{
bool RegExpEpsilon::languageContainsEpsilon(const regexp::UnboundedRegExpElement& regexp) {
bool out;
element.Accept((void*) &out, RegExpEpsilon::REG_EXP_EPSILON);
regexp.Accept((void*) &out, RegExpEpsilon::REG_EXP_EPSILON);
return out;
}
 
bool RegExpEpsilon::languageContainsEpsilon(const regexp::UnboundedRegExpElement& element)
{
bool out;
element.Accept((void*) &out, RegExpEpsilon::REG_EXP_EPSILON);
return out;
bool RegExpEpsilon::languageContainsEpsilon(const regexp::UnboundedRegExp& regexp) {
return languageContainsEpsilon(regexp.getRegExp());
}
 
auto RegExpEpsilonUnboundedRegExp = RegExpEpsilon::RegistratorWrapper<bool, regexp::UnboundedRegExp>(RegExpEpsilon::getInstance(), RegExpEpsilon::languageContainsEpsilon);
// ---------------------------------------------------------------------------
 
void RegExpEpsilon::Visit(void* data, const regexp::UnboundedRegExpAlternation& alternation) const
{
void RegExpEpsilon::Visit(void* data, const regexp::UnboundedRegExpAlternation& alternation) const {
bool &ret = *(bool*) data;
 
for(const auto& element : alternation.getElements())
{
for(const auto& element : alternation.getElements()) {
static_cast<const regexp::UnboundedRegExpElement&>(*element).Accept(data, RegExpEpsilon::REG_EXP_EPSILON);
if(ret)
{
if(ret) {
ret = true;
return;
}
......@@ -70,15 +57,12 @@ void RegExpEpsilon::Visit(void* data, const regexp::UnboundedRegExpAlternation&
ret = false;
}
 
void RegExpEpsilon::Visit(void* data, const regexp::UnboundedRegExpConcatenation& concatenation) const
{
void RegExpEpsilon::Visit(void* data, const regexp::UnboundedRegExpConcatenation& concatenation) const {
bool &ret = *(bool*) data;
 
for(const auto& element : concatenation.getElements())
{
for(const auto& element : concatenation.getElements()) {
static_cast<const regexp::UnboundedRegExpElement&>(*element).Accept(data, RegExpEpsilon::REG_EXP_EPSILON);
if(!ret)
{
if(!ret) {
ret = false;
return;
}
......@@ -87,34 +71,29 @@ void RegExpEpsilon::Visit(void* data, const regexp::UnboundedRegExpConcatenation
ret = true;
}
 
void RegExpEpsilon::Visit(void* data, const regexp::UnboundedRegExpIteration&) const
{
void RegExpEpsilon::Visit(void* data, const regexp::UnboundedRegExpIteration&) const {
bool &ret = *(bool*) data;
ret = true;
}
 
void RegExpEpsilon::Visit(void* data, const regexp::UnboundedRegExpSymbol&) const
{
void RegExpEpsilon::Visit(void* data, const regexp::UnboundedRegExpSymbol&) const {
bool &ret = *(bool*) data;
ret = false;
}
 
void RegExpEpsilon::Visit(void* data, const regexp::UnboundedRegExpEpsilon&) const
{
void RegExpEpsilon::Visit(void* data, const regexp::UnboundedRegExpEpsilon&) const {
bool &ret = *(bool*) data;
ret = true;
}
 
void RegExpEpsilon::Visit(void* data, const regexp::UnboundedRegExpEmpty&) const
{
void RegExpEpsilon::Visit(void* data, const regexp::UnboundedRegExpEmpty&) const {
bool &ret = *(bool*) data;
ret = false;
}
 
// ----------------------------------------------------------------------------
 
void RegExpEpsilon::Visit(void* data, const regexp::FormalRegExpAlternation& alternation) const
{
void RegExpEpsilon::Visit(void* data, const regexp::FormalRegExpAlternation& alternation) const {
bool &ret = *(bool*) data;
 
static_cast<const regexp::FormalRegExpElement&>(alternation.getLeftElement()).Accept(data, RegExpEpsilon::REG_EXP_EPSILON);
......@@ -126,8 +105,7 @@ void RegExpEpsilon::Visit(void* data, const regexp::FormalRegExpAlternation& alt
ret = retLeft || retRight;
}
 
void RegExpEpsilon::Visit(void* data, const regexp::FormalRegExpConcatenation& concatenation) const
{
void RegExpEpsilon::Visit(void* data, const regexp::FormalRegExpConcatenation& concatenation) const {
bool &ret = *(bool*) data;
 
static_cast<const regexp::FormalRegExpElement&>(concatenation.getLeftElement()).Accept(data, RegExpEpsilon::REG_EXP_EPSILON);
......@@ -139,26 +117,22 @@ void RegExpEpsilon::Visit(void* data, const regexp::FormalRegExpConcatenation& c
ret = retLeft && retRight;
}
 
void RegExpEpsilon::Visit(void* data, const regexp::FormalRegExpIteration&) const
{
void RegExpEpsilon::Visit(void* data, const regexp::FormalRegExpIteration&) const {
bool &ret = *(bool*) data;
ret = true;
}
 
void RegExpEpsilon::Visit(void* data, const regexp::FormalRegExpSymbol&) const
{
void RegExpEpsilon::Visit(void* data, const regexp::FormalRegExpSymbol&) const {
bool &ret = *(bool*) data;
ret = false;
}
 
void RegExpEpsilon::Visit(void* data, const regexp::FormalRegExpEmpty&) const
{
void RegExpEpsilon::Visit(void* data, const regexp::FormalRegExpEmpty&) const {
bool &ret = *(bool*) data;
ret = false;
}
 
void RegExpEpsilon::Visit(void* data, const regexp::FormalRegExpEpsilon&) const
{
void RegExpEpsilon::Visit(void* data, const regexp::FormalRegExpEpsilon&) const {
bool &ret = *(bool*) data;
ret = true;
}
......
......@@ -29,11 +29,11 @@ public:
 
static bool languageContainsEpsilon(const regexp::RegExp& regexp);
 
static bool languageContainsEpsilon(const regexp::FormalRegExpElement& regexp);
static bool languageContainsEpsilon(const regexp::FormalRegExp& regexp);
static bool languageContainsEpsilon(const regexp::UnboundedRegExp& regexp);
 
static bool languageContainsEpsilon(const regexp::UnboundedRegExpElement& element);
static bool languageContainsEpsilon(const regexp::FormalRegExpElement& element);
static bool languageContainsEpsilon(const regexp::UnboundedRegExp& regexp);
static bool languageContainsEpsilon(const regexp::UnboundedRegExpElement& regexp);
 
private:
void Visit(void* data, const regexp::UnboundedRegExpAlternation& alternation) const;
......@@ -50,7 +50,6 @@ private:
void Visit(void* data, const regexp::FormalRegExpEmpty& empty) const;
void Visit(void* data, const regexp::FormalRegExpEpsilon& epsilon) const;
 
static const RegExpEpsilon REG_EXP_EPSILON;
 
public:
......
......@@ -14,13 +14,8 @@
namespace regexp
{
 
regexp::RegExp RegExpDerivation::derivation(const regexp::RegExp& regexp, const string::LinearString& string)
{
std::pair<regexp::RegExp*, const string::LinearString> out(nullptr, string);
regexp.getData().Accept((void*) &out, RegExpDerivation::REGEXP_DERIVATION);
regexp::RegExp res = std::move(*(out.first));
delete out.first;
return res;
regexp::RegExp RegExpDerivation::derivation(const regexp::RegExp& regexp, const string::LinearString& string) {
return getInstance().dispatch(regexp.getData(), string);
}
 
regexp::FormalRegExp RegExpDerivation::derivation(const regexp::FormalRegExp& regexp, const string::LinearString& string)
......@@ -40,6 +35,8 @@ regexp::FormalRegExp RegExpDerivation::derivation(const regexp::FormalRegExp& re
return res;
}
 
auto RegExpDerivationFormalRegExp = RegExpDerivation::RegistratorWrapper<regexp::FormalRegExp, regexp::FormalRegExp>(RegExpDerivation::getInstance(), RegExpDerivation::derivation);
regexp::UnboundedRegExp RegExpDerivation::derivation(const regexp::UnboundedRegExp& regexp, const string::LinearString& string)
{
const regexp::UnboundedRegExpElement* newRegExp = regexp.getRegExp().clone();
......@@ -57,19 +54,7 @@ regexp::UnboundedRegExp RegExpDerivation::derivation(const regexp::UnboundedRegE
return res;
}
 
// ----------------------------------------------------------------------------
void RegExpDerivation::Visit(void* userData, const regexp::FormalRegExp& regexp) const
{
std::pair<regexp::RegExp*, const string::LinearString>& out = *((std::pair<regexp::RegExp*, const string::LinearString>*) userData);
out.first = new regexp::RegExp(this->derivation(regexp, out.second));
}
void RegExpDerivation::Visit(void* userData, const regexp::UnboundedRegExp& regexp) const
{
std::pair<regexp::RegExp*, const string::LinearString>& out = *((std::pair<regexp::RegExp*, const string::LinearString>*) userData);
out.first = new regexp::RegExp(this->derivation(regexp, out.second));
}
auto RegExpDerivationUnboundedRegExp = RegExpDerivation::RegistratorWrapper<regexp::UnboundedRegExp, regexp::UnboundedRegExp>(RegExpDerivation::getInstance(), RegExpDerivation::derivation);
 
// ----------------------------------------------------------------------------
 
......
......@@ -8,14 +8,15 @@
#ifndef REGEXPDERIVATION_H_
#define REGEXPDERIVATION_H_
 
#include <common/multipleDispatch.hpp>
#include <regexp/RegExp.h>
#include <regexp/formal/FormalRegExp.h>
#include <regexp/unbounded/UnboundedRegExp.h>
 
#include <string/LinearString.h>
 
namespace regexp
{
namespace regexp {
 
/**
* Calculates derivation of regular expression.
......@@ -24,8 +25,7 @@ namespace regexp
* - Melichar, definition 2.91 in chapter 2.4.3
* - Brzozowski, J. A. - Derivatives of regular expressions (1964)
*/
class RegExpDerivation : public regexp::VisitableRegExpBase::const_visitor_type, regexp::FormalRegExpElement::const_visitor_type, regexp::UnboundedRegExpElement::const_visitor_type
{
class RegExpDerivation : public std::SingleDispatchLastStaticParam<regexp::RegExp, regexp::RegExpBase, const string::LinearString>, regexp::FormalRegExpElement::const_visitor_type, regexp::UnboundedRegExpElement::const_visitor_type {
public:
RegExpDerivation() {}
 
......@@ -40,9 +40,6 @@ public:
static regexp::UnboundedRegExp derivation(const regexp::UnboundedRegExp& regexp, const string::LinearString& string);
 
private:
void Visit(void*, const regexp::FormalRegExp& empty) const;
void Visit(void*, const regexp::UnboundedRegExp& empty) const;
void Visit(void*, const regexp::FormalRegExpAlternation& alternation) const;
void Visit(void*, const regexp::FormalRegExpConcatenation& concatenation) const;
void Visit(void*, const regexp::FormalRegExpIteration& iteration) const;
......@@ -58,6 +55,12 @@ private:
void Visit(void*, const regexp::UnboundedRegExpEmpty& empty) const;
 
static const RegExpDerivation REGEXP_DERIVATION;
public:
static RegExpDerivation& getInstance() {
static RegExpDerivation res;
return res;
}
};
 
} /* namespace regexp */
......
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