1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277
|
/* SPDX-License-Identifier: MIT */
/* Copyright © 2022-present Max Bachmann */
#pragma once
#include <cstdint>
#include <rapidfuzz/details/PatternMatchVector.hpp>
#include <rapidfuzz/details/Range.hpp>
#include <rapidfuzz/details/common.hpp>
#include <rapidfuzz/details/distance.hpp>
#include <rapidfuzz/details/simd.hpp>
namespace rapidfuzz {
namespace detail {
/**
* @brief Bitparallel implementation of the OSA distance.
*
* This implementation requires the first string to have a length <= 64.
* The algorithm used is described @cite hyrro_2002 and has a time complexity
* of O(N). Comments and variable names in the implementation follow the
* paper. This implementation is used internally when the strings are short enough
*
* @tparam CharT1 This is the char type of the first sentence
* @tparam CharT2 This is the char type of the second sentence
*
* @param s1
* string to compare with s2 (for type info check Template parameters above)
* @param s2
* string to compare with s1 (for type info check Template parameters above)
*
* @return returns the OSA distance between s1 and s2
*/
template <typename PM_Vec, typename InputIt1, typename InputIt2>
size_t osa_hyrroe2003(const PM_Vec& PM, const Range<InputIt1>& s1, const Range<InputIt2>& s2, size_t max)
{
/* VP is set to 1^m. Shifting by bitwidth would be undefined behavior */
uint64_t VP = ~UINT64_C(0);
uint64_t VN = 0;
uint64_t D0 = 0;
uint64_t PM_j_old = 0;
size_t currDist = s1.size();
assert(s1.size() != 0);
/* mask used when computing D[m,j] in the paper 10^(m-1) */
uint64_t mask = UINT64_C(1) << (s1.size() - 1);
/* Searching */
for (const auto& ch : s2) {
/* Step 1: Computing D0 */
uint64_t PM_j = PM.get(0, ch);
uint64_t TR = (((~D0) & PM_j) << 1) & PM_j_old;
D0 = (((PM_j & VP) + VP) ^ VP) | PM_j | VN;
D0 = D0 | TR;
/* Step 2: Computing HP and HN */
uint64_t HP = VN | ~(D0 | VP);
uint64_t HN = D0 & VP;
/* Step 3: Computing the value D[m,j] */
currDist += bool(HP & mask);
currDist -= bool(HN & mask);
/* Step 4: Computing Vp and VN */
HP = (HP << 1) | 1;
HN = (HN << 1);
VP = HN | ~(D0 | HP);
VN = HP & D0;
PM_j_old = PM_j;
}
return (currDist <= max) ? currDist : max + 1;
}
#ifdef RAPIDFUZZ_SIMD
template <typename VecType, typename InputIt, int _lto_hack = RAPIDFUZZ_LTO_HACK>
void osa_hyrroe2003_simd(Range<size_t*> scores, const detail::BlockPatternMatchVector& block,
const std::vector<size_t>& s1_lengths, const Range<InputIt>& s2,
size_t score_cutoff) noexcept
{
# ifdef RAPIDFUZZ_AVX2
using namespace simd_avx2;
# else
using namespace simd_sse2;
# endif
static constexpr size_t alignment = native_simd<VecType>::alignment;
static constexpr size_t vec_width = native_simd<VecType>::size;
static constexpr size_t vecs = native_simd<uint64_t>::size;
assert(block.size() % vecs == 0);
native_simd<VecType> zero(VecType(0));
native_simd<VecType> one(1);
size_t result_index = 0;
for (size_t cur_vec = 0; cur_vec < block.size(); cur_vec += vecs) {
/* VP is set to 1^m */
native_simd<VecType> VP(static_cast<VecType>(-1));
native_simd<VecType> VN(VecType(0));
native_simd<VecType> D0(VecType(0));
native_simd<VecType> PM_j_old(VecType(0));
alignas(alignment) std::array<VecType, vec_width> currDist_;
unroll<size_t, vec_width>(
[&](size_t i) { currDist_[i] = static_cast<VecType>(s1_lengths[result_index + i]); });
native_simd<VecType> currDist(reinterpret_cast<uint64_t*>(currDist_.data()));
/* mask used when computing D[m,j] in the paper 10^(m-1) */
alignas(alignment) std::array<VecType, vec_width> mask_;
unroll<size_t, vec_width>([&](size_t i) {
if (s1_lengths[result_index + i] == 0)
mask_[i] = 0;
else
mask_[i] = static_cast<VecType>(UINT64_C(1) << (s1_lengths[result_index + i] - 1));
});
native_simd<VecType> mask(reinterpret_cast<uint64_t*>(mask_.data()));
for (const auto& ch : s2) {
/* Step 1: Computing D0 */
alignas(alignment) std::array<uint64_t, vecs> stored;
unroll<size_t, vecs>([&](size_t i) { stored[i] = block.get(cur_vec + i, ch); });
native_simd<VecType> PM_j(stored.data());
auto TR = (andnot(PM_j, D0) << 1) & PM_j_old;
D0 = (((PM_j & VP) + VP) ^ VP) | PM_j | VN;
D0 = D0 | TR;
/* Step 2: Computing HP and HN */
auto HP = VN | ~(D0 | VP);
auto HN = D0 & VP;
/* Step 3: Computing the value D[m,j] */
currDist += andnot(one, (HP & mask) == zero);
currDist -= andnot(one, (HN & mask) == zero);
/* Step 4: Computing Vp and VN */
HP = (HP << 1) | one;
HN = (HN << 1);
VP = HN | ~(D0 | HP);
VN = HP & D0;
PM_j_old = PM_j;
}
alignas(alignment) std::array<VecType, vec_width> distances;
currDist.store(distances.data());
unroll<size_t, vec_width>([&](size_t i) {
size_t score = 0;
/* strings of length 0 are not handled correctly */
if (s1_lengths[result_index] == 0) {
score = s2.size();
}
/* calculate score under consideration of wraparounds in parallel counter */
else {
RAPIDFUZZ_IF_CONSTEXPR (std::numeric_limits<VecType>::max() <
std::numeric_limits<size_t>::max())
{
size_t min_dist = abs_diff(s1_lengths[result_index], s2.size());
size_t wraparound_score = static_cast<size_t>(std::numeric_limits<VecType>::max()) + 1;
score = (min_dist / wraparound_score) * wraparound_score;
VecType remainder = static_cast<VecType>(min_dist % wraparound_score);
if (distances[i] < remainder) score += wraparound_score;
}
score += distances[i];
}
scores[result_index] = (score <= score_cutoff) ? score : score_cutoff + 1;
result_index++;
});
}
}
#endif
template <typename InputIt1, typename InputIt2>
size_t osa_hyrroe2003_block(const BlockPatternMatchVector& PM, const Range<InputIt1>& s1,
const Range<InputIt2>& s2, size_t max = std::numeric_limits<size_t>::max())
{
struct Row {
uint64_t VP;
uint64_t VN;
uint64_t D0;
uint64_t PM;
Row() : VP(~UINT64_C(0)), VN(0), D0(0), PM(0)
{}
};
size_t word_size = sizeof(uint64_t) * 8;
size_t words = PM.size();
uint64_t Last = UINT64_C(1) << ((s1.size() - 1) % word_size);
size_t currDist = s1.size();
std::vector<Row> old_vecs(words + 1);
std::vector<Row> new_vecs(words + 1);
/* Searching */
auto iter_s2 = s2.begin();
for (size_t row = 0; row < s2.size(); ++iter_s2, ++row) {
uint64_t HP_carry = 1;
uint64_t HN_carry = 0;
for (size_t word = 0; word < words; word++) {
/* retrieve bit vectors from last iterations */
uint64_t VN = old_vecs[word + 1].VN;
uint64_t VP = old_vecs[word + 1].VP;
uint64_t D0 = old_vecs[word + 1].D0;
/* D0 last word */
uint64_t D0_last = old_vecs[word].D0;
/* PM of last char same word */
uint64_t PM_j_old = old_vecs[word + 1].PM;
/* PM of last word */
uint64_t PM_last = new_vecs[word].PM;
uint64_t PM_j = PM.get(word, *iter_s2);
uint64_t X = PM_j;
uint64_t TR = ((((~D0) & X) << 1) | (((~D0_last) & PM_last) >> 63)) & PM_j_old;
X |= HN_carry;
D0 = (((X & VP) + VP) ^ VP) | X | VN | TR;
uint64_t HP = VN | ~(D0 | VP);
uint64_t HN = D0 & VP;
if (word == words - 1) {
currDist += bool(HP & Last);
currDist -= bool(HN & Last);
}
uint64_t HP_carry_temp = HP_carry;
HP_carry = HP >> 63;
HP = (HP << 1) | HP_carry_temp;
uint64_t HN_carry_temp = HN_carry;
HN_carry = HN >> 63;
HN = (HN << 1) | HN_carry_temp;
new_vecs[word + 1].VP = HN | ~(D0 | HP);
new_vecs[word + 1].VN = HP & D0;
new_vecs[word + 1].D0 = D0;
new_vecs[word + 1].PM = PM_j;
}
std::swap(new_vecs, old_vecs);
}
return (currDist <= max) ? currDist : max + 1;
}
class OSA : public DistanceBase<OSA, size_t, 0, std::numeric_limits<int64_t>::max()> {
friend DistanceBase<OSA, size_t, 0, std::numeric_limits<int64_t>::max()>;
friend NormalizedMetricBase<OSA>;
template <typename InputIt1, typename InputIt2>
static size_t maximum(const Range<InputIt1>& s1, const Range<InputIt2>& s2)
{
return std::max(s1.size(), s2.size());
}
template <typename InputIt1, typename InputIt2>
static size_t _distance(Range<InputIt1> s1, Range<InputIt2> s2, size_t score_cutoff, size_t score_hint)
{
if (s2.size() < s1.size()) return _distance(s2, s1, score_cutoff, score_hint);
remove_common_affix(s1, s2);
if (s1.empty())
return (s2.size() <= score_cutoff) ? s2.size() : score_cutoff + 1;
else if (s1.size() < 64)
return osa_hyrroe2003(PatternMatchVector(s1), s1, s2, score_cutoff);
else
return osa_hyrroe2003_block(BlockPatternMatchVector(s1), s1, s2, score_cutoff);
}
};
} // namespace detail
} // namespace rapidfuzz
|