-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathrapidfuzz_amalgamated.hpp
5774 lines (4865 loc) · 192 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 v0.0.1
// Generated: 2022-01-24 22:53:25.415558
// ----------------------------------------------------------
// 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 <algorithm>
#include <algorithm>
#include <cassert>
#include <stddef.h>
#include <stdexcept>
#include <stdint.h>
#include <type_traits>
#include <vector>
namespace rapidfuzz {
template <typename InputIt>
class IteratorView {
public:
IteratorView(InputIt first_, InputIt last_) : first(first_), last(last_)
{}
InputIt first;
InputIt last;
};
template <typename InputIt1, typename InputIt2>
inline bool operator==(const IteratorView<InputIt1>& a, const IteratorView<InputIt2>& b)
{
return std::equal(a.first, a.last, b.first, b.last);
}
template <typename InputIt1, typename InputIt2>
inline bool operator!=(const IteratorView<InputIt1>& a, const IteratorView<InputIt2>& b)
{
return !(a == b);
}
template <typename InputIt1, typename InputIt2>
inline bool operator<(const IteratorView<InputIt1>& a, const IteratorView<InputIt2>& b)
{
return (std::lexicographical_compare(a.first, a.last, b.first, b.last));
}
template <typename InputIt1, typename InputIt2>
inline bool operator>(const IteratorView<InputIt1>& a, const IteratorView<InputIt2>& b)
{
return b < a;
}
template <typename InputIt1, typename InputIt2>
inline bool operator<=(const IteratorView<InputIt1>& a, const IteratorView<InputIt2>& b)
{
return !(b < a);
}
template <typename InputIt1, typename InputIt2>
inline bool operator>=(const IteratorView<InputIt1>& a, const IteratorView<InputIt2>& b)
{
return !(a < b);
}
template <typename InputIt>
using IteratorViewVec = std::vector<IteratorView<InputIt>>;
struct StringAffix {
int64_t prefix_len;
int64_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 */
int64_t src_pos; /**< index into the source string */
int64_t dest_pos; /**< index into the destination string */
EditOp() : type(EditType::None), src_pos(0), dest_pos(0)
{}
EditOp(EditType type_, int64_t src_pos_, int64_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);
}
/**
* @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 */
int64_t src_begin; /**< index into the source string */
int64_t src_end; /**< index into the source string */
int64_t dest_begin; /**< index into the destination string */
int64_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_, int64_t src_begin_, int64_t src_end_, int64_t dest_begin_,
int64_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);
}
namespace detail {
template <typename T>
void vector_slice(std::vector<T>& new_vec, const std::vector<T>& vec, int start, int stop, int step)
{
if (step > 0) {
if (start < 0) {
start = std::max((int)(start + (int)vec.size()), 0);
}
else if (start > (int)vec.size()) {
start = (int)vec.size();
}
if (stop < 0) {
stop = std::max((int)(stop + (int)vec.size()), 0);
}
else if (stop > (int)vec.size()) {
stop = (int)vec.size();
}
if (start >= stop) {
return;
}
int count = (stop - 1 - start) / step + 1;
new_vec.reserve(count);
for (int i = start; i < stop; i += step) {
new_vec.push_back(vec[i]);
}
}
else if (step < 0) {
if (start < 0) {
start = std::max((int)(start + (int)vec.size()), -1);
}
else if (start >= (int)vec.size()) {
start = (int)vec.size() - 1;
}
if (stop < 0) {
stop = std::max((int)(stop + (int)vec.size()), -1);
}
else if (stop >= (int)vec.size()) {
stop = (int)vec.size() - 1;
}
if (start <= stop) {
return;
}
int count = (stop + 1 - start) / step + 1;
new_vec.reserve(count);
for (int i = start; i > stop; i += step) {
new_vec.push_back(vec[i]);
}
}
else {
throw std::invalid_argument("slice step cannot be zero");
}
}
} // namespace detail
class Opcodes;
class Editops : private std::vector<EditOp> {
public:
using std::vector<EditOp>::size_type;
Editops() : 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)
{
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;
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(ed_slice, *this, start, stop, step);
ed_slice.src_len = src_len;
ed_slice.dest_len = dest_len;
return ed_slice;
}
Editops reverse() const
{
Editops reversed = *this;
std::reverse(reversed.begin(), reversed.end());
return reversed;
}
int64_t get_src_len() const
{
return src_len;
}
void set_src_len(int64_t len)
{
src_len = len;
}
int64_t get_dest_len() const
{
return dest_len;
}
void set_dest_len(int64_t len)
{
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;
}
private:
int64_t src_len;
int64_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)
{
lhs.swap(rhs);
}
class Opcodes : private std::vector<Opcode> {
public:
using std::vector<Opcode>::size_type;
Opcodes() : 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)
{
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;
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(ed_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;
}
int64_t get_src_len() const
{
return src_len;
}
void set_src_len(int64_t len)
{
src_len = len;
}
int64_t get_dest_len() const
{
return dest_len;
}
void set_dest_len(int64_t len)
{
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:
int64_t src_len;
int64_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)
{
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 (int64_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 (int64_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 (int64_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();
int64_t src_pos = 0;
int64_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;
}
int64_t src_begin = src_pos;
int64_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()});
}
}
} // 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&>()));
template <typename T>
using plain = std::remove_cv_t<std::remove_reference_t<T>>;
template <typename T>
using iterator_type = plain<decltype(*std::declval<T>())>;
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 = iterator_type<InputIt>;
SplittedSentenceView(IteratorViewVec<InputIt> sentence) : m_sentence(std::move(sentence))
{}
int64_t dedupe();
int64_t size() const;
int64_t length() const
{
return size();
}
bool empty() const
{
return m_sentence.empty();
}
int64_t word_count() const
{
return m_sentence.size();
}
std::basic_string<CharT> join() const;
const IteratorViewVec<InputIt>& words() const
{
return m_sentence;
}
private:
IteratorViewVec<InputIt> m_sentence;
};
template <typename InputIt>
int64_t SplittedSentenceView<InputIt>::dedupe()
{
int64_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>
int64_t SplittedSentenceView<InputIt>::size() const
{
if (m_sentence.empty()) return 0;
// there is a whitespace between each word
int64_t result = m_sentence.size() - 1;
for (const auto& word : m_sentence) {
result += std::distance(word.first, word.last);
}
return result;
}
template <typename InputIt>
auto SplittedSentenceView<InputIt>::join() const -> std::basic_string<CharT>
{
if (m_sentence.empty()) {
return std::basic_string<CharT>();
}
auto sentence_iter = m_sentence.begin();
std::basic_string<CharT> joined(sentence_iter->first, sentence_iter->last);
const std::basic_string<CharT> whitespace{0x20};
++sentence_iter;
for (; sentence_iter != m_sentence.end(); ++sentence_iter) {
joined.append(whitespace)
.append(std::basic_string<CharT>(sentence_iter->first, sentence_iter->last));
}
return joined;
}
} // namespace rapidfuzz#include <tuple>
#include <unordered_map>
#include <vector>
namespace rapidfuzz {
template <typename InputIt1, typename InputIt2, typename InputIt3>
struct DecomposedSet {
SplittedSentenceView<InputIt1> difference_ab;
SplittedSentenceView<InputIt2> difference_ba;
SplittedSentenceView<InputIt3> intersection;
DecomposedSet(SplittedSentenceView<InputIt1> diff_ab, SplittedSentenceView<InputIt2> diff_ba,
SplittedSentenceView<InputIt3> intersect)
: difference_ab(std::move(diff_ab)),
difference_ba(std::move(diff_ba)),
intersection(std::move(intersect))
{}
};
namespace common {
/**
* @defgroup Common Common
* Common utilities shared among multiple functions
* @{
*/
template <typename InputIt1, typename InputIt2>
DecomposedSet<InputIt1, InputIt2, InputIt1> set_decomposition(SplittedSentenceView<InputIt1> a,
SplittedSentenceView<InputIt2> b);
constexpr double result_cutoff(double result, double score_cutoff)
{
return (result >= score_cutoff) ? result : 0;
}
template <int Max = 1>
constexpr double norm_distance(int64_t dist, int64_t lensum, double score_cutoff = 0)
{
double max = static_cast<double>(Max);
return result_cutoff((lensum > 0) ? (max - max * dist / lensum) : max, score_cutoff);
}
template <int Max = 1>
static inline int64_t score_cutoff_to_distance(double score_cutoff, int64_t lensum)
{
return static_cast<int64_t>(
std::ceil(static_cast<double>(lensum) * (1.0 - score_cutoff / Max)));
}
template <typename T>
constexpr bool is_zero(T a, T tolerance = std::numeric_limits<T>::epsilon())
{
return std::fabs(a) <= tolerance;
}
template <typename Sentence, typename CharT = char_type<Sentence>,
typename = std::enable_if_t<
is_explicitly_convertible<Sentence, std::basic_string<CharT>>::value>>
std::basic_string<CharT> to_string(Sentence&& str);
template <typename Sentence, typename CharT = char_type<Sentence>,
typename = std::enable_if_t<
!is_explicitly_convertible<Sentence, std::basic_string<CharT>>::value &&
has_data_and_size<Sentence>::value>>
std::basic_string<CharT> to_string(const Sentence& str);
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);
}
/**
* @brief Finds the first mismatching pair of elements from two ranges:
* one defined by [first1, last1) and another defined by [first2,last2).
* Similar implementation to std::mismatch from C++14
*
* @param first1, last1 - the first range of the elements
* @param first2, last2 - the second range of the elements
*
* @return std::pair with iterators to the first two non-equal elements.
*/
template <typename InputIterator1, typename InputIterator2>
std::pair<InputIterator1, InputIterator2> mismatch(InputIterator1 first1, InputIterator1 last1,
InputIterator2 first2, InputIterator2 last2);
template <typename InputIt1, typename InputIt2>
StringAffix remove_common_affix(InputIt1& first1, InputIt1& last1, InputIt2& first2,
InputIt2& last2);
template <typename InputIt1, typename InputIt2>
int64_t remove_common_prefix(InputIt1& first1, InputIt1 last1, InputIt2& first2, InputIt2 last2);
template <typename InputIt1, typename InputIt2>
int64_t remove_common_suffix(InputIt1 first1, InputIt1& last1, InputIt2 first2, InputIt2& last2);
template <typename InputIt, typename CharT = iterator_type<InputIt>>
SplittedSentenceView<InputIt> sorted_split(InputIt first, InputIt last);
template <typename T>
constexpr auto to_unsigned(T value) -> typename std::make_unsigned<T>::type
{
return typename std::make_unsigned<T>::type(value);
}
template <typename T>
constexpr auto to_signed(T value) -> typename std::make_unsigned<T>::type
{
return typename std::make_signed<T>::type(value);
}
/*
* taken from https://stackoverflow.com/a/17251989/11335032
*/
template <typename T, typename U>
bool CanTypeFitValue(const U value)
{
const intmax_t botT = intmax_t(std::numeric_limits<T>::min());
const intmax_t botU = intmax_t(std::numeric_limits<U>::min());
const uintmax_t topT = uintmax_t(std::numeric_limits<T>::max());
const uintmax_t topU = uintmax_t(std::numeric_limits<U>::max());
return !((botT > botU && value < static_cast<U>(botT)) ||
(topT < topU && value > static_cast<U>(topT)));
}
struct PatternMatchVector {
struct MapElem {