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 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594
|
/* SPDX-License-Identifier: MIT */
/* Copyright © 2020 Max Bachmann */
#pragma once
#include <algorithm>
#include <rapidfuzz/details/config.hpp>
#include <stddef.h>
#include <stdexcept>
#include <vector>
namespace rapidfuzz {
struct StringAffix {
size_t prefix_len;
size_t suffix_len;
};
struct LevenshteinWeightTable {
size_t insert_cost;
size_t delete_cost;
size_t replace_cost;
};
/**
* @brief Edit operation types used by the Levenshtein distance
*/
enum class EditType {
None = 0, /**< No Operation required */
Replace = 1, /**< Replace a character if a string by another character */
Insert = 2, /**< Insert a character into a string */
Delete = 3 /**< Delete a character from a string */
};
/**
* @brief Edit operations used by the Levenshtein distance
*
* This represents an edit operation of type type which is applied to
* the source string
*
* Replace: replace character at src_pos with character at dest_pos
* Insert: insert character from dest_pos at src_pos
* Delete: delete character at src_pos
*/
struct EditOp {
EditType type; /**< type of the edit operation */
size_t src_pos; /**< index into the source string */
size_t dest_pos; /**< index into the destination string */
EditOp() : type(EditType::None), src_pos(0), dest_pos(0)
{}
EditOp(EditType type_, size_t src_pos_, size_t dest_pos_)
: type(type_), src_pos(src_pos_), dest_pos(dest_pos_)
{}
};
inline bool operator==(EditOp a, EditOp b)
{
return (a.type == b.type) && (a.src_pos == b.src_pos) && (a.dest_pos == b.dest_pos);
}
inline bool operator!=(EditOp a, EditOp b)
{
return !(a == b);
}
/**
* @brief Edit operations used by the Levenshtein distance
*
* This represents an edit operation of type type which is applied to
* the source string
*
* None: s1[src_begin:src_end] == s1[dest_begin:dest_end]
* Replace: s1[i1:i2] should be replaced by s2[dest_begin:dest_end]
* Insert: s2[dest_begin:dest_end] should be inserted at s1[src_begin:src_begin].
* Note that src_begin==src_end in this case.
* Delete: s1[src_begin:src_end] should be deleted.
* Note that dest_begin==dest_end in this case.
*/
struct Opcode {
EditType type; /**< type of the edit operation */
size_t src_begin; /**< index into the source string */
size_t src_end; /**< index into the source string */
size_t dest_begin; /**< index into the destination string */
size_t dest_end; /**< index into the destination string */
Opcode() : type(EditType::None), src_begin(0), src_end(0), dest_begin(0), dest_end(0)
{}
Opcode(EditType type_, size_t src_begin_, size_t src_end_, size_t dest_begin_, size_t dest_end_)
: type(type_), src_begin(src_begin_), src_end(src_end_), dest_begin(dest_begin_), dest_end(dest_end_)
{}
};
inline bool operator==(Opcode a, Opcode b)
{
return (a.type == b.type) && (a.src_begin == b.src_begin) && (a.src_end == b.src_end) &&
(a.dest_begin == b.dest_begin) && (a.dest_end == b.dest_end);
}
inline bool operator!=(Opcode a, Opcode b)
{
return !(a == b);
}
namespace detail {
template <typename Vec>
auto vector_slice(const Vec& vec, int start, int stop, int step) -> Vec
{
Vec new_vec;
if (step == 0) throw std::invalid_argument("slice step cannot be zero");
if (step < 0) throw std::invalid_argument("step sizes below 0 lead to an invalid order of editops");
if (start < 0)
start = std::max<int>(start + static_cast<int>(vec.size()), 0);
else if (start > static_cast<int>(vec.size()))
start = static_cast<int>(vec.size());
if (stop < 0)
stop = std::max<int>(stop + static_cast<int>(vec.size()), 0);
else if (stop > static_cast<int>(vec.size()))
stop = static_cast<int>(vec.size());
if (start >= stop) return new_vec;
int count = (stop - 1 - start) / step + 1;
new_vec.reserve(static_cast<size_t>(count));
for (int i = start; i < stop; i += step)
new_vec.push_back(vec[static_cast<size_t>(i)]);
return new_vec;
}
template <typename Vec>
void vector_remove_slice(Vec& vec, int start, int stop, int step)
{
if (step == 0) throw std::invalid_argument("slice step cannot be zero");
if (step < 0) throw std::invalid_argument("step sizes below 0 lead to an invalid order of editops");
if (start < 0)
start = std::max<int>(start + static_cast<int>(vec.size()), 0);
else if (start > static_cast<int>(vec.size()))
start = static_cast<int>(vec.size());
if (stop < 0)
stop = std::max<int>(stop + static_cast<int>(vec.size()), 0);
else if (stop > static_cast<int>(vec.size()))
stop = static_cast<int>(vec.size());
if (start >= stop) return;
auto iter = vec.begin() + start;
for (int i = start; i < static_cast<int>(vec.size()); i++)
if (i >= stop || ((i - start) % step != 0)) *(iter++) = vec[static_cast<size_t>(i)];
vec.resize(static_cast<size_t>(std::distance(vec.begin(), iter)));
vec.shrink_to_fit();
}
} // namespace detail
class Opcodes;
class Editops : private std::vector<EditOp> {
public:
using std::vector<EditOp>::size_type;
Editops() noexcept : src_len(0), dest_len(0)
{}
Editops(size_type count, const EditOp& value) : std::vector<EditOp>(count, value), src_len(0), dest_len(0)
{}
explicit Editops(size_type count) : std::vector<EditOp>(count), src_len(0), dest_len(0)
{}
Editops(const Editops& other)
: std::vector<EditOp>(other), src_len(other.src_len), dest_len(other.dest_len)
{}
Editops(const Opcodes& other);
Editops(Editops&& other) noexcept
{
swap(other);
}
Editops& operator=(Editops other) noexcept
{
swap(other);
return *this;
}
/* Element access */
using std::vector<EditOp>::at;
using std::vector<EditOp>::operator[];
using std::vector<EditOp>::front;
using std::vector<EditOp>::back;
using std::vector<EditOp>::data;
/* Iterators */
using std::vector<EditOp>::begin;
using std::vector<EditOp>::cbegin;
using std::vector<EditOp>::end;
using std::vector<EditOp>::cend;
using std::vector<EditOp>::rbegin;
using std::vector<EditOp>::crbegin;
using std::vector<EditOp>::rend;
using std::vector<EditOp>::crend;
/* Capacity */
using std::vector<EditOp>::empty;
using std::vector<EditOp>::size;
using std::vector<EditOp>::max_size;
using std::vector<EditOp>::reserve;
using std::vector<EditOp>::capacity;
using std::vector<EditOp>::shrink_to_fit;
/* Modifiers */
using std::vector<EditOp>::clear;
using std::vector<EditOp>::insert;
using std::vector<EditOp>::emplace;
using std::vector<EditOp>::erase;
using std::vector<EditOp>::push_back;
using std::vector<EditOp>::emplace_back;
using std::vector<EditOp>::pop_back;
using std::vector<EditOp>::resize;
void swap(Editops& rhs) noexcept
{
std::swap(src_len, rhs.src_len);
std::swap(dest_len, rhs.dest_len);
std::vector<EditOp>::swap(rhs);
}
Editops slice(int start, int stop, int step = 1) const
{
Editops ed_slice = detail::vector_slice(*this, start, stop, step);
ed_slice.src_len = src_len;
ed_slice.dest_len = dest_len;
return ed_slice;
}
void remove_slice(int start, int stop, int step = 1)
{
detail::vector_remove_slice(*this, start, stop, step);
}
Editops reverse() const
{
Editops reversed = *this;
std::reverse(reversed.begin(), reversed.end());
return reversed;
}
size_t get_src_len() const noexcept
{
return src_len;
}
void set_src_len(size_t len) noexcept
{
src_len = len;
}
size_t get_dest_len() const noexcept
{
return dest_len;
}
void set_dest_len(size_t len) noexcept
{
dest_len = len;
}
Editops inverse() const
{
Editops inv_ops = *this;
std::swap(inv_ops.src_len, inv_ops.dest_len);
for (auto& op : inv_ops) {
std::swap(op.src_pos, op.dest_pos);
if (op.type == EditType::Delete)
op.type = EditType::Insert;
else if (op.type == EditType::Insert)
op.type = EditType::Delete;
}
return inv_ops;
}
Editops remove_subsequence(const Editops& subsequence) const
{
Editops result;
result.set_src_len(src_len);
result.set_dest_len(dest_len);
if (subsequence.size() > size()) throw std::invalid_argument("subsequence is not a subsequence");
result.resize(size() - subsequence.size());
/* offset to correct removed edit operations */
int offset = 0;
auto op_iter = begin();
auto op_end = end();
size_t result_pos = 0;
for (const auto& sop : subsequence) {
for (; op_iter != op_end && sop != *op_iter; op_iter++) {
result[result_pos] = *op_iter;
result[result_pos].src_pos =
static_cast<size_t>(static_cast<ptrdiff_t>(result[result_pos].src_pos) + offset);
result_pos++;
}
/* element of subsequence not part of the sequence */
if (op_iter == op_end) throw std::invalid_argument("subsequence is not a subsequence");
if (sop.type == EditType::Insert)
offset++;
else if (sop.type == EditType::Delete)
offset--;
op_iter++;
}
/* add remaining elements */
for (; op_iter != op_end; op_iter++) {
result[result_pos] = *op_iter;
result[result_pos].src_pos =
static_cast<size_t>(static_cast<ptrdiff_t>(result[result_pos].src_pos) + offset);
result_pos++;
}
return result;
}
private:
size_t src_len;
size_t dest_len;
};
inline bool operator==(const Editops& lhs, const Editops& rhs)
{
if (lhs.get_src_len() != rhs.get_src_len() || lhs.get_dest_len() != rhs.get_dest_len()) return false;
if (lhs.size() != rhs.size()) return false;
return std::equal(lhs.begin(), lhs.end(), rhs.begin());
}
inline bool operator!=(const Editops& lhs, const Editops& rhs)
{
return !(lhs == rhs);
}
inline void swap(Editops& lhs, Editops& rhs) noexcept(noexcept(lhs.swap(rhs)))
{
lhs.swap(rhs);
}
class Opcodes : private std::vector<Opcode> {
public:
using std::vector<Opcode>::size_type;
Opcodes() noexcept : src_len(0), dest_len(0)
{}
Opcodes(size_type count, const Opcode& value) : std::vector<Opcode>(count, value), src_len(0), dest_len(0)
{}
explicit Opcodes(size_type count) : std::vector<Opcode>(count), src_len(0), dest_len(0)
{}
Opcodes(const Opcodes& other)
: std::vector<Opcode>(other), src_len(other.src_len), dest_len(other.dest_len)
{}
Opcodes(const Editops& other);
Opcodes(Opcodes&& other) noexcept
{
swap(other);
}
Opcodes& operator=(Opcodes other) noexcept
{
swap(other);
return *this;
}
/* Element access */
using std::vector<Opcode>::at;
using std::vector<Opcode>::operator[];
using std::vector<Opcode>::front;
using std::vector<Opcode>::back;
using std::vector<Opcode>::data;
/* Iterators */
using std::vector<Opcode>::begin;
using std::vector<Opcode>::cbegin;
using std::vector<Opcode>::end;
using std::vector<Opcode>::cend;
using std::vector<Opcode>::rbegin;
using std::vector<Opcode>::crbegin;
using std::vector<Opcode>::rend;
using std::vector<Opcode>::crend;
/* Capacity */
using std::vector<Opcode>::empty;
using std::vector<Opcode>::size;
using std::vector<Opcode>::max_size;
using std::vector<Opcode>::reserve;
using std::vector<Opcode>::capacity;
using std::vector<Opcode>::shrink_to_fit;
/* Modifiers */
using std::vector<Opcode>::clear;
using std::vector<Opcode>::insert;
using std::vector<Opcode>::emplace;
using std::vector<Opcode>::erase;
using std::vector<Opcode>::push_back;
using std::vector<Opcode>::emplace_back;
using std::vector<Opcode>::pop_back;
using std::vector<Opcode>::resize;
void swap(Opcodes& rhs) noexcept
{
std::swap(src_len, rhs.src_len);
std::swap(dest_len, rhs.dest_len);
std::vector<Opcode>::swap(rhs);
}
Opcodes slice(int start, int stop, int step = 1) const
{
Opcodes ed_slice = detail::vector_slice(*this, start, stop, step);
ed_slice.src_len = src_len;
ed_slice.dest_len = dest_len;
return ed_slice;
}
Opcodes reverse() const
{
Opcodes reversed = *this;
std::reverse(reversed.begin(), reversed.end());
return reversed;
}
size_t get_src_len() const noexcept
{
return src_len;
}
void set_src_len(size_t len) noexcept
{
src_len = len;
}
size_t get_dest_len() const noexcept
{
return dest_len;
}
void set_dest_len(size_t len) noexcept
{
dest_len = len;
}
Opcodes inverse() const
{
Opcodes inv_ops = *this;
std::swap(inv_ops.src_len, inv_ops.dest_len);
for (auto& op : inv_ops) {
std::swap(op.src_begin, op.dest_begin);
std::swap(op.src_end, op.dest_end);
if (op.type == EditType::Delete)
op.type = EditType::Insert;
else if (op.type == EditType::Insert)
op.type = EditType::Delete;
}
return inv_ops;
}
private:
size_t src_len;
size_t dest_len;
};
inline bool operator==(const Opcodes& lhs, const Opcodes& rhs)
{
if (lhs.get_src_len() != rhs.get_src_len() || lhs.get_dest_len() != rhs.get_dest_len()) return false;
if (lhs.size() != rhs.size()) return false;
return std::equal(lhs.begin(), lhs.end(), rhs.begin());
}
inline bool operator!=(const Opcodes& lhs, const Opcodes& rhs)
{
return !(lhs == rhs);
}
inline void swap(Opcodes& lhs, Opcodes& rhs) noexcept(noexcept(lhs.swap(rhs)))
{
lhs.swap(rhs);
}
inline Editops::Editops(const Opcodes& other)
{
src_len = other.get_src_len();
dest_len = other.get_dest_len();
for (const auto& op : other) {
switch (op.type) {
case EditType::None: break;
case EditType::Replace:
for (size_t j = 0; j < op.src_end - op.src_begin; j++)
push_back({EditType::Replace, op.src_begin + j, op.dest_begin + j});
break;
case EditType::Insert:
for (size_t j = 0; j < op.dest_end - op.dest_begin; j++)
push_back({EditType::Insert, op.src_begin, op.dest_begin + j});
break;
case EditType::Delete:
for (size_t j = 0; j < op.src_end - op.src_begin; j++)
push_back({EditType::Delete, op.src_begin + j, op.dest_begin});
break;
}
}
}
inline Opcodes::Opcodes(const Editops& other)
{
src_len = other.get_src_len();
dest_len = other.get_dest_len();
size_t src_pos = 0;
size_t dest_pos = 0;
for (size_t i = 0; i < other.size();) {
if (src_pos < other[i].src_pos || dest_pos < other[i].dest_pos) {
push_back({EditType::None, src_pos, other[i].src_pos, dest_pos, other[i].dest_pos});
src_pos = other[i].src_pos;
dest_pos = other[i].dest_pos;
}
size_t src_begin = src_pos;
size_t dest_begin = dest_pos;
EditType type = other[i].type;
do {
switch (type) {
case EditType::None: break;
case EditType::Replace:
src_pos++;
dest_pos++;
break;
case EditType::Insert: dest_pos++; break;
case EditType::Delete: src_pos++; break;
}
i++;
} while (i < other.size() && other[i].type == type && src_pos == other[i].src_pos &&
dest_pos == other[i].dest_pos);
push_back({type, src_begin, src_pos, dest_begin, dest_pos});
}
if (src_pos < other.get_src_len() || dest_pos < other.get_dest_len()) {
push_back({EditType::None, src_pos, other.get_src_len(), dest_pos, other.get_dest_len()});
}
}
template <typename T>
struct ScoreAlignment {
T score; /**< resulting score of the algorithm */
size_t src_start; /**< index into the source string */
size_t src_end; /**< index into the source string */
size_t dest_start; /**< index into the destination string */
size_t dest_end; /**< index into the destination string */
ScoreAlignment() : score(T()), src_start(0), src_end(0), dest_start(0), dest_end(0)
{}
ScoreAlignment(T score_, size_t src_start_, size_t src_end_, size_t dest_start_, size_t dest_end_)
: score(score_),
src_start(src_start_),
src_end(src_end_),
dest_start(dest_start_),
dest_end(dest_end_)
{}
};
template <typename T>
inline bool operator==(const ScoreAlignment<T>& a, const ScoreAlignment<T>& b)
{
return (a.score == b.score) && (a.src_start == b.src_start) && (a.src_end == b.src_end) &&
(a.dest_start == b.dest_start) && (a.dest_end == b.dest_end);
}
} // namespace rapidfuzz
|