RapidFuzz
Loading...
Searching...
No Matches
Hamming.hpp
1/* SPDX-License-Identifier: MIT */
2/* Copyright © 2021 Max Bachmann */
3
4#pragma once
5#include <limits>
6#include <rapidfuzz/details/common.hpp>
7#include <rapidfuzz/distance/Hamming_impl.hpp>
8
9namespace rapidfuzz {
10
40template <typename InputIt1, typename InputIt2>
41size_t hamming_distance(InputIt1 first1, InputIt1 last1, InputIt2 first2, InputIt2 last2, bool pad_ = true,
42 size_t score_cutoff = std::numeric_limits<size_t>::max())
43{
44 return detail::Hamming::distance(first1, last1, first2, last2, pad_, score_cutoff, score_cutoff);
45}
46
47template <typename Sentence1, typename Sentence2>
48size_t hamming_distance(const Sentence1& s1, const Sentence2& s2, bool pad_ = true,
49 size_t score_cutoff = std::numeric_limits<size_t>::max())
50{
51 return detail::Hamming::distance(s1, s2, pad_, score_cutoff, score_cutoff);
52}
53
54template <typename InputIt1, typename InputIt2>
55size_t hamming_similarity(InputIt1 first1, InputIt1 last1, InputIt2 first2, InputIt2 last2, bool pad_ = true,
56 size_t score_cutoff = 0)
57{
58 return detail::Hamming::similarity(first1, last1, first2, last2, pad_, score_cutoff, score_cutoff);
59}
60
61template <typename Sentence1, typename Sentence2>
62size_t hamming_similarity(const Sentence1& s1, const Sentence2& s2, bool pad_ = true, size_t score_cutoff = 0)
63{
64 return detail::Hamming::similarity(s1, s2, pad_, score_cutoff, score_cutoff);
65}
66
67template <typename InputIt1, typename InputIt2>
68double hamming_normalized_distance(InputIt1 first1, InputIt1 last1, InputIt2 first2, InputIt2 last2,
69 bool pad_ = true, double score_cutoff = 1.0)
70{
71 return detail::Hamming::normalized_distance(first1, last1, first2, last2, pad_, score_cutoff,
72 score_cutoff);
73}
74
75template <typename Sentence1, typename Sentence2>
76double hamming_normalized_distance(const Sentence1& s1, const Sentence2& s2, bool pad_ = true,
77 double score_cutoff = 1.0)
78{
79 return detail::Hamming::normalized_distance(s1, s2, pad_, score_cutoff, score_cutoff);
80}
81
82template <typename InputIt1, typename InputIt2>
83Editops hamming_editops(InputIt1 first1, InputIt1 last1, InputIt2 first2, InputIt2 last2, bool pad_ = true,
84 size_t score_hint = std::numeric_limits<size_t>::max())
85{
86 return detail::hamming_editops(detail::make_range(first1, last1), detail::make_range(first2, last2), pad_,
87 score_hint);
88}
89
90template <typename Sentence1, typename Sentence2>
91Editops hamming_editops(const Sentence1& s1, const Sentence2& s2, bool pad_ = true,
92 size_t score_hint = std::numeric_limits<size_t>::max())
93{
94 return detail::hamming_editops(detail::make_range(s1), detail::make_range(s2), pad_, score_hint);
95}
96
121template <typename InputIt1, typename InputIt2>
122double hamming_normalized_similarity(InputIt1 first1, InputIt1 last1, InputIt2 first2, InputIt2 last2,
123 bool pad_ = true, double score_cutoff = 0.0)
124{
125 return detail::Hamming::normalized_similarity(first1, last1, first2, last2, pad_, score_cutoff,
126 score_cutoff);
127}
128
129template <typename Sentence1, typename Sentence2>
130double hamming_normalized_similarity(const Sentence1& s1, const Sentence2& s2, bool pad_ = true,
131 double score_cutoff = 0.0)
132{
133 return detail::Hamming::normalized_similarity(s1, s2, pad_, score_cutoff, score_cutoff);
134}
135
136template <typename CharT1>
137struct CachedHamming : public detail::CachedDistanceBase<CachedHamming<CharT1>, size_t, 0,
138 std::numeric_limits<int64_t>::max()> {
139 template <typename Sentence1>
140 explicit CachedHamming(const Sentence1& s1_, bool pad_ = true)
141 : CachedHamming(detail::to_begin(s1_), detail::to_end(s1_), pad_)
142 {}
143
144 template <typename InputIt1>
145 CachedHamming(InputIt1 first1, InputIt1 last1, bool pad_ = true) : s1(first1, last1), pad(pad_)
146 {}
147
148private:
149 friend detail::CachedDistanceBase<CachedHamming<CharT1>, size_t, 0, std::numeric_limits<int64_t>::max()>;
150 friend detail::CachedNormalizedMetricBase<CachedHamming<CharT1>>;
151
152 template <typename InputIt2>
153 size_t maximum(const detail::Range<InputIt2>& s2) const
154 {
155 return std::max(s1.size(), s2.size());
156 }
157
158 template <typename InputIt2>
159 size_t _distance(const detail::Range<InputIt2>& s2, size_t score_cutoff, size_t score_hint) const
160 {
161 return detail::Hamming::distance(s1, s2, pad, score_cutoff, score_hint);
162 }
163
164 std::vector<CharT1> s1;
165 bool pad;
166};
167
168#ifdef RAPIDFUZZ_DEDUCTION_GUIDES
169template <typename Sentence1>
170explicit CachedHamming(const Sentence1& s1_, bool pad_ = true) -> CachedHamming<char_type<Sentence1>>;
171
172template <typename InputIt1>
173CachedHamming(InputIt1 first1, InputIt1 last1, bool pad_ = true) -> CachedHamming<iter_value_t<InputIt1>>;
174#endif
175
178} // namespace rapidfuzz
double hamming_normalized_similarity(InputIt1 first1, InputIt1 last1, InputIt2 first2, InputIt2 last2, bool pad_=true, double score_cutoff=0.0)
Calculates a normalized hamming similarity.
Definition Hamming.hpp:122
size_t hamming_distance(InputIt1 first1, InputIt1 last1, InputIt2 first2, InputIt2 last2, bool pad_=true, size_t score_cutoff=std::numeric_limits< size_t >::max())
Calculates the Hamming distance between two strings.
Definition Hamming.hpp:41