std::vector <int> Calculator::Acf (const std::string & stringView) { if (Validator::ValidateStringView (stringView) != viewIsValid) { throw ExceptionInvalidStringView (); } std::vector <int> acf {}; // |+--| // +-|- | // +|-- | // |+--| // | +-|- // | +|-- const int range = stringView.length () - 1; for (int i {range}; i >= -range; --i) { const int begin { - i > 0 ? -i : 0}; const int end {range - i < range ? range - i : range}; int sum {0}; for (int j {begin}; j <= end; ++j) { const int a {stringView [ j] == '+' ? 1 : -1}; const int b {stringView [i + j] == '+' ? 1 : -1}; sum += a * b; } acf.push_back (sum); } return acf; }
int Calculator::Ml (const std::string & stringView) { if (Validator::ValidateStringView (stringView) != viewIsValid) { throw ExceptionInvalidStringView (); } return stringView.length (); }
std::string Representer::DetectCodeId (const std::string & stringView) { if (Validator::ValidateStringView (stringView) != viewIsValid) { throw ExceptionInvalidStringView (); } const auto family = GenerateCodeFamily (stringView); return StringViewToHexView (* std::min_element (family.begin (), family.end () ) ); }
std::vector <int> Calculator::Ccf (const std::string & stringViewOne, const std::string & stringViewTwo) { if (Validator::ValidateStringView (stringViewOne) != viewIsValid) { throw ExceptionInvalidStringView (); } if (Validator::ValidateStringView (stringViewTwo) != viewIsValid) { throw ExceptionInvalidStringView (); } std::vector <int> ccf {}; // |+-++| // +-|- | // +|-- | // |+-- | // | +--| // | +-|- // | +|-- // |+--| // +-+|+ | // +-|++ | // +|-++| // |+-+|+ // | +-|++ // | +|-++ const int rangeOne = stringViewOne.length () - 1; const int rangeTwo = stringViewTwo.length () - 1; for (int i {-rangeTwo}; i <= rangeOne; ++i) { const int begin {i < 0 ? -i : 0}; const int end {i < rangeOne - rangeTwo ? rangeTwo : rangeOne - i}; int sum {0}; for (int j {begin}; j <= end; ++j) { const int a {stringViewTwo [ j] == '+' ? 1 : -1}; const int b {stringViewOne [i + j] == '+' ? 1 : -1}; sum += a * b; } ccf.push_back (sum); } return ccf; }
std::string Representer::ReverseCode (const std::string & stringView) { if (Validator::ValidateStringView (stringView) != viewIsValid) { throw ExceptionInvalidStringView (); } std::string reversedStringView (stringView); std::reverse (reversedStringView.begin (), reversedStringView.end () ); return reversedStringView; }
int Calculator::Psl (const std::string & stringView) { if (stringView.empty () ) { return 0; } if (Validator::ValidateStringView (stringView) != viewIsValid) { throw ExceptionInvalidStringView (); } const size_t range {stringView.length () - 1}; std::vector <int> acf {Acf (stringView)}; std::transform (acf.begin (), acf.begin () + range, acf.begin (), abs); return * std::max_element (acf.begin (), acf.begin () + range); }
std::array <std::string, codeFamilySize> Representer::GenerateCodeFamily (const std::string & stringView) { if (Validator::ValidateStringView (stringView) != viewIsValid) { throw ExceptionInvalidStringView (); } std::array <std::string, codeFamilySize> family; static_assert (codeFamilySize == 4, "Constant codeFamilySize must be equal to 4 (view, reversed view, inversed view, inversed and reversed view)."); family [0] = stringView; family [1] = ReverseCode (stringView); family [2] = InverseCode (stringView); family [3] = ReverseCode (InverseCode (stringView) ); return family; }
std::string Representer::InverseCode (const std::string & stringView) { if (Validator::ValidateStringView (stringView) != viewIsValid) { throw ExceptionInvalidStringView (); } return std::accumulate (stringView.begin (), stringView.end (), std::string (), [& stringView](std::string & result, const char & c) { switch (c) { case '+': return result.append ("-"); case '-': return result.append ("+"); default : return result; } } ); }
int Calculator::Ml (const std::string & stringViewOne, const std::string & stringViewTwo) { if (stringViewOne.empty () || stringViewTwo.empty () ) { return 0; } if (Validator::ValidateStringView (stringViewTwo) != viewIsValid || Validator::ValidateStringView (stringViewTwo) != viewIsValid) { throw ExceptionInvalidStringView (); } std::vector <int> ccf {Ccf (stringViewOne, stringViewTwo)}; std::transform (ccf.begin (), ccf.end (), ccf.begin (), abs); return * std::max_element (ccf.begin (), ccf.end () ); }
std::string Representer::StringViewToHexView (const std::string & stringView) { if (Validator::ValidateStringView (stringView) != viewIsValid) { throw ExceptionInvalidStringView (); } constexpr int tetradSize {4}; std::string hexView; const size_t shortageSize {stringView.length () % tetradSize}; const size_t extendedSize {shortageSize ? tetradSize - shortageSize : 0}; std::string extendedStringView (extendedSize, '-'); extendedStringView.append (stringView); for (size_t i {0}; i < stringView.length (); i += tetradSize) { hexView.append (1, conversionStringToHexTable [extendedStringView.substr(i, tetradSize)]); } return hexView; }