diff options
author | denplusplus <denplusplus@yandex-team.ru> | 2022-02-10 16:47:34 +0300 |
---|---|---|
committer | Daniil Cherednik <dcherednik@yandex-team.ru> | 2022-02-10 16:47:34 +0300 |
commit | 57c20d143e8a438cd76b9fdc3ca2e8ee3ac1f32a (patch) | |
tree | cc63639f8e502db19a82c20e2861c6d1edbf9fea /library/cpp/deprecated/kmp/kmp.h | |
parent | 464ba3814a83db4f2d5327393b0b6eaf0c86bfd7 (diff) | |
download | ydb-57c20d143e8a438cd76b9fdc3ca2e8ee3ac1f32a.tar.gz |
Restoring authorship annotation for <denplusplus@yandex-team.ru>. Commit 1 of 2.
Diffstat (limited to 'library/cpp/deprecated/kmp/kmp.h')
-rw-r--r-- | library/cpp/deprecated/kmp/kmp.h | 172 |
1 files changed, 86 insertions, 86 deletions
diff --git a/library/cpp/deprecated/kmp/kmp.h b/library/cpp/deprecated/kmp/kmp.h index a7f72eece6..71b554516d 100644 --- a/library/cpp/deprecated/kmp/kmp.h +++ b/library/cpp/deprecated/kmp/kmp.h @@ -1,108 +1,108 @@ #pragma once - + #include <util/generic/ptr.h> #include <util/generic/string.h> #include <util/generic/vector.h> #include <util/generic/yexception.h> - + template <typename T> -void ComputePrefixFunction(const T* begin, const T* end, ssize_t** result) { +void ComputePrefixFunction(const T* begin, const T* end, ssize_t** result) { Y_ENSURE(begin != end, TStringBuf("empty pattern")); - ssize_t len = end - begin; - TArrayHolder<ssize_t> resultHolder(new ssize_t[len + 1]); - ssize_t i = 0; - ssize_t j = -1; - resultHolder[0] = -1; - while (i < len) { - while ((j >= 0) && (begin[j] != begin[i])) - j = resultHolder[j]; - ++i; - ++j; + ssize_t len = end - begin; + TArrayHolder<ssize_t> resultHolder(new ssize_t[len + 1]); + ssize_t i = 0; + ssize_t j = -1; + resultHolder[0] = -1; + while (i < len) { + while ((j >= 0) && (begin[j] != begin[i])) + j = resultHolder[j]; + ++i; + ++j; Y_ASSERT(i >= 0); Y_ASSERT(j >= 0); Y_ASSERT(j < len); - if ((i < len) && (begin[i] == begin[j])) - resultHolder[i] = resultHolder[j]; - else - resultHolder[i] = j; - } - *result = resultHolder.Release(); -} - -class TKMPMatcher { -private: - TArrayHolder<ssize_t> PrefixFunction; + if ((i < len) && (begin[i] == begin[j])) + resultHolder[i] = resultHolder[j]; + else + resultHolder[i] = j; + } + *result = resultHolder.Release(); +} + +class TKMPMatcher { +private: + TArrayHolder<ssize_t> PrefixFunction; TString Pattern; - - void ComputePrefixFunction(); - -public: - TKMPMatcher(const char* patternBegin, const char* patternEnd); + + void ComputePrefixFunction(); + +public: + TKMPMatcher(const char* patternBegin, const char* patternEnd); TKMPMatcher(const TString& pattern); - - bool SubStr(const char* begin, const char* end, const char*& result) const { + + bool SubStr(const char* begin, const char* end, const char*& result) const { Y_ASSERT(begin <= end); ssize_t m = Pattern.size(); - ssize_t n = end - begin; - ssize_t i, j; - for (i = 0, j = 0; (i < n) && (j < m); ++i, ++j) { - while ((j >= 0) && (Pattern[j] != begin[i])) - j = PrefixFunction[j]; - } - if (j == m) { - result = begin + i - m; - return true; - } else { - return false; - } - } -}; - + ssize_t n = end - begin; + ssize_t i, j; + for (i = 0, j = 0; (i < n) && (j < m); ++i, ++j) { + while ((j >= 0) && (Pattern[j] != begin[i])) + j = PrefixFunction[j]; + } + if (j == m) { + result = begin + i - m; + return true; + } else { + return false; + } + } +}; + template <typename T> -class TKMPStreamMatcher { -public: - class ICallback { - public: - virtual void OnMatch(const T* begin, const T* end) = 0; +class TKMPStreamMatcher { +public: + class ICallback { + public: + virtual void OnMatch(const T* begin, const T* end) = 0; virtual ~ICallback() = default; - }; - -private: - ICallback* Callback; - TArrayHolder<ssize_t> PrefixFunction; + }; + +private: + ICallback* Callback; + TArrayHolder<ssize_t> PrefixFunction; using TTVector = TVector<T>; - TTVector Pattern; - ssize_t State; - TTVector Candidate; - -public: - TKMPStreamMatcher(const T* patternBegin, const T* patternEnd, ICallback* callback) - : Callback(callback) - , Pattern(patternBegin, patternEnd) - , State(0) + TTVector Pattern; + ssize_t State; + TTVector Candidate; + +public: + TKMPStreamMatcher(const T* patternBegin, const T* patternEnd, ICallback* callback) + : Callback(callback) + , Pattern(patternBegin, patternEnd) + , State(0) , Candidate(Pattern.size()) - { - ssize_t* pf; - ComputePrefixFunction(patternBegin, patternEnd, &pf); - PrefixFunction.Reset(pf); - } + { + ssize_t* pf; + ComputePrefixFunction(patternBegin, patternEnd, &pf); + PrefixFunction.Reset(pf); + } - void Push(const T& symbol) { - while ((State >= 0) && (Pattern[State] != symbol)) { + void Push(const T& symbol) { + while ((State >= 0) && (Pattern[State] != symbol)) { Y_ASSERT(State <= (ssize_t) Pattern.size()); - State = PrefixFunction[State]; + State = PrefixFunction[State]; Y_ASSERT(State <= (ssize_t) Pattern.size()); - } - if (State >= 0) - Candidate[State] = symbol; - ++State; + } + if (State >= 0) + Candidate[State] = symbol; + ++State; if (State == (ssize_t) Pattern.size()) { - Callback->OnMatch(Candidate.begin(), Candidate.end()); - State = 0; - } - } - - void Clear() { - State = 0; - } -}; + Callback->OnMatch(Candidate.begin(), Candidate.end()); + State = 0; + } + } + + void Clear() { + State = 0; + } +}; |