RapidFuzz
Loading...
Searching...
No Matches
Prefix.hpp
1/* SPDX-License-Identifier: MIT */
2/* Copyright © 2021 Max Bachmann */
3
4#pragma once
5
6#include <limits>
7#include <rapidfuzz/details/common.hpp>
8#include <rapidfuzz/distance/Prefix_impl.hpp>
9
10namespace rapidfuzz {
11
17template <typename InputIt1, typename InputIt2>
18size_t prefix_distance(InputIt1 first1, InputIt1 last1, InputIt2 first2, InputIt2 last2,
19 size_t score_cutoff = std::numeric_limits<size_t>::max())
20{
21 return detail::Prefix::distance(first1, last1, first2, last2, score_cutoff, score_cutoff);
22}
23
24template <typename Sentence1, typename Sentence2>
25size_t prefix_distance(const Sentence1& s1, const Sentence2& s2,
26 size_t score_cutoff = std::numeric_limits<size_t>::max())
27{
28 return detail::Prefix::distance(s1, s2, score_cutoff, score_cutoff);
29}
30
31template <typename InputIt1, typename InputIt2>
32size_t prefix_similarity(InputIt1 first1, InputIt1 last1, InputIt2 first2, InputIt2 last2,
33 size_t score_cutoff = 0)
34{
35 return detail::Prefix::similarity(first1, last1, first2, last2, score_cutoff, score_cutoff);
36}
37
38template <typename Sentence1, typename Sentence2>
39size_t prefix_similarity(const Sentence1& s1, const Sentence2& s2, size_t score_cutoff = 0)
40{
41 return detail::Prefix::similarity(s1, s2, score_cutoff, score_cutoff);
42}
43
44template <typename InputIt1, typename InputIt2>
45double prefix_normalized_distance(InputIt1 first1, InputIt1 last1, InputIt2 first2, InputIt2 last2,
46 double score_cutoff = 1.0)
47{
48 return detail::Prefix::normalized_distance(first1, last1, first2, last2, score_cutoff, score_cutoff);
49}
50
51template <typename Sentence1, typename Sentence2>
52double prefix_normalized_distance(const Sentence1& s1, const Sentence2& s2, double score_cutoff = 1.0)
53{
54 return detail::Prefix::normalized_distance(s1, s2, score_cutoff, score_cutoff);
55}
56
57template <typename InputIt1, typename InputIt2>
58double prefix_normalized_similarity(InputIt1 first1, InputIt1 last1, InputIt2 first2, InputIt2 last2,
59 double score_cutoff = 0.0)
60{
61 return detail::Prefix::normalized_similarity(first1, last1, first2, last2, score_cutoff, score_cutoff);
62}
63
64template <typename Sentence1, typename Sentence2>
65double prefix_normalized_similarity(const Sentence1& s1, const Sentence2& s2, double score_cutoff = 0.0)
66{
67 return detail::Prefix::normalized_similarity(s1, s2, score_cutoff, score_cutoff);
68}
69
70template <typename CharT1>
71struct CachedPrefix : public detail::CachedSimilarityBase<CachedPrefix<CharT1>, size_t, 0,
72 std::numeric_limits<int64_t>::max()> {
73 template <typename Sentence1>
74 explicit CachedPrefix(const Sentence1& s1_) : CachedPrefix(detail::to_begin(s1_), detail::to_end(s1_))
75 {}
76
77 template <typename InputIt1>
78 CachedPrefix(InputIt1 first1, InputIt1 last1) : s1(first1, last1)
79 {}
80
81private:
82 friend detail::CachedSimilarityBase<CachedPrefix<CharT1>, size_t, 0, std::numeric_limits<int64_t>::max()>;
83 friend detail::CachedNormalizedMetricBase<CachedPrefix<CharT1>>;
84
85 template <typename InputIt2>
86 size_t maximum(const detail::Range<InputIt2>& s2) const
87 {
88 return std::max(s1.size(), s2.size());
89 }
90
91 template <typename InputIt2>
92 size_t _similarity(detail::Range<InputIt2> s2, size_t score_cutoff, size_t) const
93 {
94 return detail::Prefix::similarity(s1, s2, score_cutoff, score_cutoff);
95 }
96
97 std::vector<CharT1> s1;
98};
99
100#ifdef RAPIDFUZZ_DEDUCTION_GUIDES
101template <typename Sentence1>
102explicit CachedPrefix(const Sentence1& s1_) -> CachedPrefix<char_type<Sentence1>>;
103
104template <typename InputIt1>
105CachedPrefix(InputIt1 first1, InputIt1 last1) -> CachedPrefix<iter_value_t<InputIt1>>;
106#endif
107
110} // namespace rapidfuzz