-
Notifications
You must be signed in to change notification settings - Fork 0
/
rapidfuzz_amalgamated.hpp
7014 lines (5832 loc) · 239 KB
/
rapidfuzz_amalgamated.hpp
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
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Licensed under the MIT License <http://opensource.org/licenses/MIT>.
// SPDX-License-Identifier: MIT
// RapidFuzz v1.2.0
// Generated: 2022-08-20 03:56:31.798204
// ----------------------------------------------------------
// This file is an amalgamation of multiple different files.
// You probably shouldn't edit it directly.
// ----------------------------------------------------------
#ifndef RAPIDFUZZ_AMALGAMATED_HPP_INCLUDED
#define RAPIDFUZZ_AMALGAMATED_HPP_INCLUDED
#include <cmath>
#include <numeric>
#include <array>
#include <cmath>
#include <cstring>
#include <limits>
#include <iterator>
#include <limits>
#include <stdexcept>
#include <vector>
namespace rapidfuzz {
namespace detail {
template <typename Iter>
class Range {
Iter _first;
Iter _last;
public:
using iterator = Iter;
using reverse_iterator = std::reverse_iterator<iterator>;
constexpr Range(Iter first, Iter last) : _first(first), _last(last)
{}
constexpr iterator begin() const noexcept
{
return _first;
}
constexpr iterator end() const noexcept
{
return _last;
}
constexpr reverse_iterator rbegin() const noexcept
{
return reverse_iterator(end());
}
constexpr reverse_iterator rend() const noexcept
{
return reverse_iterator(begin());
}
constexpr ptrdiff_t size() const
{
return std::distance(_first, _last);
}
constexpr bool empty() const
{
return size() == 0;
}
explicit constexpr operator bool() const
{
return !empty();
}
constexpr decltype(auto) operator[](ptrdiff_t n) const
{
return _first[n];
}
constexpr void remove_prefix(ptrdiff_t n)
{
_first += n;
}
constexpr void remove_suffix(ptrdiff_t n)
{
_last -= n;
}
constexpr Range subseq(ptrdiff_t pos = 0, ptrdiff_t count = std::numeric_limits<ptrdiff_t>::max())
{
if (pos > size()) throw std::out_of_range("Index out of range in Range::substr");
/* count + pos can overflow */
return {_first + pos, _first + pos + std::min(count - pos, size() - pos)};
}
constexpr Range<reverse_iterator> reversed() const
{
return {rbegin(), rend()};
}
};
template <typename CharT>
CharT* to_begin(CharT* s)
{
return s;
}
template <typename T>
auto to_begin(T& x)
{
using std::begin;
return begin(x);
}
template <typename CharT>
CharT* to_end(CharT* s)
{
while (*s != 0) {
++s;
}
return s;
}
template <typename T>
auto to_end(T& x)
{
using std::end;
return end(x);
}
template <typename Iter>
constexpr auto make_range(Iter first, Iter last)
{
return Range<Iter>(first, last);
}
template <typename T>
constexpr auto make_range(T& x)
{
auto first = to_begin(x);
return Range<decltype(first)>(first, to_end(x));
}
template <typename InputIt1, typename InputIt2>
inline bool operator==(const Range<InputIt1>& a, const Range<InputIt2>& b)
{
return std::equal(a.begin(), a.end(), b.begin(), b.end());
}
template <typename InputIt1, typename InputIt2>
inline bool operator!=(const Range<InputIt1>& a, const Range<InputIt2>& b)
{
return !(a == b);
}
template <typename InputIt1, typename InputIt2>
inline bool operator<(const Range<InputIt1>& a, const Range<InputIt2>& b)
{
return (std::lexicographical_compare(a.begin(), a.end(), b.begin(), b.end()));
}
template <typename InputIt1, typename InputIt2>
inline bool operator>(const Range<InputIt1>& a, const Range<InputIt2>& b)
{
return b < a;
}
template <typename InputIt1, typename InputIt2>
inline bool operator<=(const Range<InputIt1>& a, const Range<InputIt2>& b)
{
return !(b < a);
}
template <typename InputIt1, typename InputIt2>
inline bool operator>=(const Range<InputIt1>& a, const Range<InputIt2>& b)
{
return !(a < b);
}
template <typename InputIt>
using RangeVec = std::vector<Range<InputIt>>;
} // namespace detail
} // namespace rapidfuzz#include <algorithm>
#include <algorithm>
#include <cassert>
#include <stddef.h>
#include <stdexcept>
#include <stdint.h>
#include <type_traits>
#include <vector>
namespace rapidfuzz {
struct StringAffix {
size_t prefix_len;
size_t suffix_len;
};
struct LevenshteinWeightTable {
int64_t insert_cost;
int64_t delete_cost;
int64_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
#include <functional>
#include <iterator>
#include <type_traits>
#include <utility>
namespace rapidfuzz {
namespace detail {
template <typename T>
auto inner_type(T const*) -> T;
template <typename T>
auto inner_type(T const&) -> typename T::value_type;
} // namespace detail
template <typename T>
using char_type = decltype(detail::inner_type(std::declval<T const&>()));
/* backport of std::iter_value_t from C++20
* This does not cover the complete functionality, but should be enough for
* the use cases in this library
*/
template <typename T>
using iter_value_t = typename std::iterator_traits<T>::value_type;
template <typename... Conds>
struct satisfies_all : std::true_type {};
template <typename Cond, typename... Conds>
struct satisfies_all<Cond, Conds...>
: std::conditional<Cond::value, satisfies_all<Conds...>, std::false_type>::type {};
template <typename... Conds>
struct satisfies_any : std::false_type {};
template <typename Cond, typename... Conds>
struct satisfies_any<Cond, Conds...>
: std::conditional<Cond::value, std::true_type, satisfies_any<Conds...>>::type {};
// taken from
// https://stackoverflow.com/questions/16893992/check-if-type-can-be-explicitly-converted
template <typename From, typename To>
struct is_explicitly_convertible {
template <typename T>
static void f(T);
template <typename F, typename T>
static constexpr auto test(int /*unused*/) -> decltype(f(static_cast<T>(std::declval<F>())), true)
{
return true;
}
template <typename F, typename T>
static constexpr auto test(...) -> bool
{
return false;
}
static bool const value = test<From, To>(0);
};
#define GENERATE_HAS_MEMBER(member) \
\
template <typename T> \
struct has_member_##member { \
private: \
using yes = std::true_type; \
using no = std::false_type; \
\
struct Fallback { \
int member; \
}; \
struct Derived : T, Fallback {}; \
\
template <class U> \
static no test(decltype(U::member)*); \
template <typename U> \
static yes test(U*); \
\
template <typename U, typename = std::enable_if_t<std::is_class<U>::value>> \
static constexpr bool class_test(U*) \
{ \
return std::is_same<decltype(test<Derived>(nullptr)), yes>::value; \
} \
\
template <typename U, typename = std::enable_if_t<!std::is_class<U>::value>> \
static constexpr bool class_test(const U&) \
{ \
return false; \
} \
\
public: \
static constexpr bool value = class_test(static_cast<T*>(nullptr)); \
};
GENERATE_HAS_MEMBER(data) // Creates 'has_member_data'
GENERATE_HAS_MEMBER(size) // Creates 'has_member_size'
template <typename Sentence>
using has_data_and_size = satisfies_all<has_member_data<Sentence>, has_member_size<Sentence>>;
// This trait checks if a given type is a standard collection of hashable types
// SFINAE ftw
template <class T>
class is_hashable_sequence {
is_hashable_sequence() = delete;
using hashable = char;
struct not_hashable {
char t[2];
}; // Ensured to work on any platform
template <typename C>
static hashable matcher(decltype(&std::hash<typename C::value_type>::operator()));
template <typename C>
static not_hashable matcher(...);
public:
static bool const value = (sizeof(matcher<T>(nullptr)) == sizeof(hashable));
};
template <class T>
class is_standard_iterable {
is_standard_iterable() = delete;
using iterable = char;
struct not_iterable {
char t[2];
}; // Ensured to work on any platform
template <typename C>
static iterable matcher(typename C::const_iterator*);
template <typename C>
static not_iterable matcher(...);
public:
static bool const value = (sizeof(matcher<T>(nullptr)) == sizeof(iterable));
};
template <typename C>
void* sub_matcher(typename C::value_type const& (C::*)(int64_t) const);
// TODO: Not a real SFINAE, because of the ambiguity between
// value_type const& operator[](int64_t) const;
// and value_type& operator[](int64_t);
// Not really important
template <class T>
class has_bracket_operator {
has_bracket_operator() = delete;
using has_op = char;
struct hasnt_op {
char t[2];
}; // Ensured to work on any platform
template <typename C>
static has_op matcher(decltype(sub_matcher<T>(&T::at)));
template <typename C>
static hasnt_op matcher(...);
public:
static bool const value = (sizeof(matcher<T>(nullptr)) == sizeof(has_op));
};
} // namespace rapidfuzz
#include <string>
namespace rapidfuzz {
template <typename InputIt>
class SplittedSentenceView {
public:
using CharT = iter_value_t<InputIt>;
SplittedSentenceView(detail::RangeVec<InputIt> sentence) noexcept(
std::is_nothrow_move_constructible<detail::RangeVec<InputIt>>::value)
: m_sentence(std::move(sentence))
{}
size_t dedupe();
size_t size() const;
size_t length() const
{
return size();
}
bool empty() const
{
return m_sentence.empty();
}
size_t word_count() const
{
return m_sentence.size();
}
std::basic_string<CharT> join() const;
const detail::RangeVec<InputIt>& words() const
{
return m_sentence;
}
private:
detail::RangeVec<InputIt> m_sentence;
};
template <typename InputIt>
size_t SplittedSentenceView<InputIt>::dedupe()
{
size_t old_word_count = word_count();
m_sentence.erase(std::unique(m_sentence.begin(), m_sentence.end()), m_sentence.end());
return old_word_count - word_count();
}
template <typename InputIt>
size_t SplittedSentenceView<InputIt>::size() const
{
if (m_sentence.empty()) return 0;
// there is a whitespace between each word
size_t result = m_sentence.size() - 1;
for (const auto& word : m_sentence) {