RapidFuzz
Loading...
Searching...
No Matches
Postfix.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/Postfix_impl.hpp>
9
10namespace rapidfuzz {
11
17template <typename InputIt1, typename InputIt2>
18size_t postfix_distance(InputIt1 first1, InputIt1 last1, InputIt2 first2, InputIt2 last2,
19 size_t score_cutoff = std::numeric_limits<size_t>::max())
20{
21 return detail::Postfix::distance(first1, last1, first2, last2, score_cutoff, score_cutoff);
22}
23
24template <typename Sentence1, typename Sentence2>
25size_t postfix_distance(const Sentence1& s1, const Sentence2& s2,
26 size_t score_cutoff = std::numeric_limits<size_t>::max())
27{
28 return detail::Postfix::distance(s1, s2, score_cutoff, score_cutoff);
29}
30
31template <typename InputIt1, typename InputIt2>
32size_t postfix_similarity(InputIt1 first1, InputIt1 last1, InputIt2 first2, InputIt2 last2,
33 size_t score_cutoff = 0)
34{
35 return detail::Postfix::similarity(first1, last1, first2, last2, score_cutoff, score_cutoff);
36}
37
38template <typename Sentence1, typename Sentence2>
39size_t postfix_similarity(const Sentence1& s1, const Sentence2& s2, size_t score_cutoff = 0)
40{
41 return detail::Postfix::similarity(s1, s2, score_cutoff, score_cutoff);
42}
43
44template <typename InputIt1, typename InputIt2>
45double postfix_normalized_distance(InputIt1 first1, InputIt1 last1, InputIt2 first2, InputIt2 last2,
46 double score_cutoff = 1.0)
47{
48 return detail::Postfix::normalized_distance(first1, last1, first2, last2, score_cutoff, score_cutoff);
49}
50
51template <typename Sentence1, typename Sentence2>
52double postfix_normalized_distance(const Sentence1& s1, const Sentence2& s2, double score_cutoff = 1.0)
53{
54 return detail::Postfix::normalized_distance(s1, s2, score_cutoff, score_cutoff);
55}
56
57template <typename InputIt1, typename InputIt2>
58double postfix_normalized_similarity(InputIt1 first1, InputIt1 last1, InputIt2 first2, InputIt2 last2,
59 double score_cutoff = 0.0)
60{
61 return detail::Postfix::normalized_similarity(first1, last1, first2, last2, score_cutoff, score_cutoff);
62}
63
64template <typename Sentence1, typename Sentence2>
65double postfix_normalized_similarity(const Sentence1& s1, const Sentence2& s2, double score_cutoff = 0.0)
66{
67 return detail::Postfix::normalized_similarity(s1, s2, score_cutoff, score_cutoff);
68}
69
70template <typename CharT1>
71struct CachedPostfix : public detail::CachedSimilarityBase<CachedPostfix<CharT1>, size_t, 0,
72 std::numeric_limits<int64_t>::max()> {
73 template <typename Sentence1>
74 explicit CachedPostfix(const Sentence1& s1_) : CachedPostfix(detail::to_begin(s1_), detail::to_end(s1_))
75 {}
76
77 template <typename InputIt1>
78 CachedPostfix(InputIt1 first1, InputIt1 last1) : s1(first1, last1)
79 {}
80
81private:
82 friend detail::CachedSimilarityBase<CachedPostfix<CharT1>, size_t, 0,
83 std::numeric_limits<int64_t>::max()>;
84 friend detail::CachedNormalizedMetricBase<CachedPostfix<CharT1>>;
85
86 template <typename InputIt2>
87 size_t maximum(const detail::Range<InputIt2>& s2) const
88 {
89 return std::max(s1.size(), s2.size());
90 }
91
92 template <typename InputIt2>
93 size_t _similarity(detail::Range<InputIt2> s2, size_t score_cutoff, size_t score_hint) const
94 {
95 return detail::Postfix::similarity(s1, s2, score_cutoff, score_hint);
96 }
97
98 std::vector<CharT1> s1;
99};
100
101#ifdef RAPIDFUZZ_DEDUCTION_GUIDES
102template <typename Sentence1>
103explicit CachedPostfix(const Sentence1& s1_) -> CachedPostfix<char_type<Sentence1>>;
104
105template <typename InputIt1>
106CachedPostfix(InputIt1 first1, InputIt1 last1) -> CachedPostfix<iter_value_t<InputIt1>>;
107#endif
108
111} // namespace rapidfuzz