Created
August 7, 2017 14:24
-
-
Save Randl/3bf0b61f53fd4b873d9c02598d70f391 to your computer and use it in GitHub Desktop.
BLIM string matching algorithm
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
Copyright 2017, Evgenii Zheltonozhskii | |
Redistribution and use in source and binary forms, with or without modification, | |
are permitted provided that the following conditions are met: | |
1. Redistributions of source code must retain the above copyright notice, this | |
list of conditions and the following disclaimer. | |
2. Redistributions in binary form must reproduce the above copyright notice, | |
this list of conditions and the following disclaimer in the documentation and/or | |
other materials provided with the distribution. | |
3. Neither the name of the copyright holder nor the names of its contributors | |
may be used to endorse or promote products derived from this software without | |
specific prior written permission. | |
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND | |
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | |
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | |
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR | |
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | |
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | |
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON | |
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | |
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
*/ | |
/** | |
* Külekci, M. Oğuzhan. "BLIM: A new bit-parallel pattern matching algorithm | |
* overcoming computer word size limitation." Mathematics in Computer | |
* Science 3.4 (2010): 407-420. | |
*/ | |
#include <algorithm> | |
#include <cstddef> | |
#include <iostream> | |
#include <map> | |
#include <set> | |
#include <vector> | |
constexpr uint_fast8_t W = sizeof(size_t); // Word size | |
template <typename ForwardIt1, typename SearchedType> | |
std::map<SearchedType, std::vector<size_t>> ComputeMaskMatrix( | |
ForwardIt1 p_first, const size_t m, const std::set<SearchedType> &Sigma) { | |
size_t ws = W + m - 1; | |
std::map<SearchedType, std::vector<size_t>> Mask; | |
for (auto &a : Sigma) { | |
Mask[a] = std::vector<size_t>(ws, ~(size_t)0); | |
} | |
for (size_t i = 0; i < W; ++i) { | |
size_t tmp = (size_t)1 << i; | |
auto p = p_first; | |
for (size_t j = 0; j < m; ++j, ++p) { | |
for (auto &a : Sigma) { | |
Mask[a][i + j] &= ~tmp; | |
} | |
Mask[*p][i + j] |= tmp; | |
} | |
} | |
return Mask; | |
} | |
template <typename ForwardIt1, typename SearchedType> | |
std::map<SearchedType, uint_fast64_t> ComputeShiftVector( | |
ForwardIt1 p_first, const size_t m, const std::set<SearchedType> &Sigma) { | |
size_t ws = W + m - 1; | |
std::map<SearchedType, uint_fast64_t> Shift; | |
for (auto &a : Sigma) { | |
Shift[a] = ws + 1; | |
} | |
for (size_t j = 0; j < m; ++j, ++p_first) { | |
Shift[*p_first] = ws - j; | |
} | |
return Shift; | |
} | |
std::vector<int_fast64_t> ComputeScanOrder(size_t m) { | |
size_t ws = W + m - 1; | |
size_t i = 0; | |
std::vector<int_fast64_t> ScanOrder(ws); | |
for (int_fast64_t j = m - 1; j >= 0; --j) { | |
int_fast64_t k = j; | |
while (k < ws) { | |
ScanOrder[i] = k; | |
k = k + m; | |
i = i + 1; | |
} | |
} | |
return ScanOrder; | |
} | |
/** | |
* | |
* @tparam ForwardIt1 | |
* @tparam ForwardIt2 | |
* @param p_first | |
* @param p_last | |
* @param s_first | |
* @param s_last | |
* @return | |
*/ | |
template <typename ForwardIt1, typename ForwardIt2> | |
ForwardIt2 BLIM(ForwardIt1 p_first, ForwardIt1 p_last, ForwardIt2 s_first, | |
ForwardIt2 s_last) { | |
size_t m = std::distance(p_first, p_last); | |
size_t n = std::distance(s_first, s_last); | |
size_t ws = W + m - 1; | |
using SearchedType = typename std::iterator_traits<ForwardIt2>::value_type; | |
std::set<SearchedType> Sigma(s_first, s_last); | |
if (!std::all_of(p_first, p_last, [&](const SearchedType &x) { | |
return Sigma.find(x) != Sigma.end(); | |
})) | |
return s_last; | |
auto Mask = ComputeMaskMatrix(p_first, m, Sigma); | |
auto Shift = ComputeShiftVector(p_first, m, Sigma); | |
auto ScanOrder = ComputeScanOrder(m); | |
size_t k = 0, i = 0; | |
uint_fast64_t flag = 0; | |
while (i < n) { | |
flag = Mask[*std::next(s_first, i + ScanOrder[0])][ScanOrder[0]]; | |
for (size_t j = 1; j < ws; ++j) { | |
flag &= Mask[*std::next(s_first, i + ScanOrder[j])][ScanOrder[j]]; | |
if (flag == 0) { | |
break; | |
} | |
} | |
if (flag != 0) { | |
for (size_t j = 0; j < W; ++j) { | |
if (flag & (1 << j)) return std::next(s_first, i + j); | |
} | |
} | |
i += Shift[*std::next(s_first, i + ws)]; | |
} | |
return s_last; | |
} |
Did you test it with Bitap algorithm?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
uint_fast64_t
- there is no typedef for this in the code.