Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not require Copy trait #2

Merged
merged 1 commit into from
May 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "textdistance"
version = "1.0.0"
version = "1.0.1"
edition = "2021"
authors = ["Gram <[email protected]>"]
description = "Lots of algorithms to compare how similar two sequences are"
Expand Down
4 changes: 2 additions & 2 deletions src/algorithm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ pub trait Algorithm<R> {
fn for_iter<C, E>(&self, s1: C, s2: C) -> Result<R>
where
C: Iterator<Item = E>,
E: Eq + Copy + Hash,
E: Eq + Hash,
{
let s1: Vec<E> = s1.collect();
let s2: Vec<E> = s2.collect();
Expand All @@ -35,7 +35,7 @@ pub trait Algorithm<R> {
///
fn for_vec<E>(&self, s1: &[E], s2: &[E]) -> Result<R>
where
E: Eq + Copy + Hash,
E: Eq + Hash,
{
self.for_iter(s1.iter(), s2.iter())
}
Expand Down
2 changes: 1 addition & 1 deletion src/algorithms/bag.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ impl Algorithm<usize> for Bag {
fn for_iter<C, E>(&self, s1: C, s2: C) -> Result<usize>
where
C: Iterator<Item = E>,
E: Eq + Copy + std::hash::Hash,
E: Eq + std::hash::Hash,
{
let c1 = Counter::from_iter(s1);
let c2 = Counter::from_iter(s2);
Expand Down
2 changes: 1 addition & 1 deletion src/algorithms/cosine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ impl Algorithm<f64> for Cosine {
fn for_iter<C, E>(&self, s1: C, s2: C) -> Result<f64>
where
C: Iterator<Item = E>,
E: Eq + Copy + std::hash::Hash,
E: Eq + std::hash::Hash,
{
let c1 = Counter::from_iter(s1);
let c2 = Counter::from_iter(s2);
Expand Down
4 changes: 2 additions & 2 deletions src/algorithms/entropy_ncd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ impl Default for EntropyNCD {
}

impl EntropyNCD {
fn compress<E: Hash + Eq + Copy>(&self, c: &Counter<E>) -> f64 {
fn compress<E: Hash + Eq>(&self, c: &Counter<E>) -> f64 {
debug_assert!(self.correction >= 0.);
let total_count = c.count();
let mut entropy = 0.0;
Expand All @@ -46,7 +46,7 @@ impl Algorithm<f64> for EntropyNCD {
fn for_iter<C, E>(&self, s1: C, s2: C) -> Result<f64>
where
C: Iterator<Item = E>,
E: Eq + Copy + std::hash::Hash,
E: Eq + std::hash::Hash,
{
let c1 = Counter::from_iter(s1);
let c2 = Counter::from_iter(s2);
Expand Down
2 changes: 1 addition & 1 deletion src/algorithms/jaccard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ impl Algorithm<f64> for Jaccard {
fn for_iter<C, E>(&self, s1: C, s2: C) -> Result<f64>
where
C: Iterator<Item = E>,
E: Eq + Copy + std::hash::Hash,
E: Eq + std::hash::Hash,
{
let c1 = Counter::from_iter(s1);
let c2 = Counter::from_iter(s2);
Expand Down
4 changes: 2 additions & 2 deletions src/algorithms/jaro_winkler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ impl JaroWinkler {
fn winklerize<C, E>(&self, jaro: f64, s1: C, s2: C) -> f64
where
C: Iterator<Item = E>,
E: Eq + Copy + std::hash::Hash,
E: Eq + std::hash::Hash,
{
debug_assert!(self.prefix_weight * self.max_prefix as f64 <= 1.0);
let mut prefix_len = 0;
Expand All @@ -55,7 +55,7 @@ impl JaroWinkler {
impl Algorithm<f64> for JaroWinkler {
fn for_vec<E>(&self, s1: &[E], s2: &[E]) -> Result<f64>
where
E: Eq + Copy + std::hash::Hash,
E: Eq + std::hash::Hash,
{
let jaro = self.jaro.for_vec(s1, s2).nval();
Result {
Expand Down
6 changes: 3 additions & 3 deletions src/algorithms/lcsseq.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use crate::{Algorithm, Result};
pub struct LCSSeq {}

impl Algorithm<usize> for LCSSeq {
fn for_vec<E: Eq + Copy>(&self, s1: &[E], s2: &[E]) -> Result<usize> {
fn for_vec<E: Eq>(&self, s1: &[E], s2: &[E]) -> Result<usize> {
let l1 = s1.len();
let l2 = s2.len();
let mut lengths = vec![vec![0; l2 + 1]; l1 + 1];
Expand All @@ -26,7 +26,7 @@ impl Algorithm<usize> for LCSSeq {
}
}

let mut result = Vec::<E>::new();
let mut result = Vec::<&E>::new();
let mut i = l1;
let mut j = l2;
while i != 0 && j != 0 {
Expand All @@ -36,7 +36,7 @@ impl Algorithm<usize> for LCSSeq {
j -= 1;
} else {
assert!(s1[i - 1] == s2[j - 1]);
result.push(s1[i - 1]);
result.push(&s1[i - 1]);
i -= 1;
j -= 1;
}
Expand Down
2 changes: 1 addition & 1 deletion src/algorithms/lig3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ impl Default for LIG3 {
impl Algorithm<f64> for LIG3 {
fn for_vec<E>(&self, s1: &[E], s2: &[E]) -> Result<f64>
where
E: Eq + Copy + Hash,
E: Eq + Hash,
{
let lev_res = self.levenshtein.for_vec(s1, s2);
let lev = lev_res.dist();
Expand Down
2 changes: 1 addition & 1 deletion src/algorithms/mlipns.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ impl Algorithm<usize> for MLIPNS {
fn for_iter<C, E>(&self, s1: C, s2: C) -> Result<usize>
where
C: Iterator<Item = E>,
E: Eq + Copy + Hash,
E: Eq + Hash,
{
let ham = self.hamming.for_iter(s1, s2);
Result {
Expand Down
2 changes: 1 addition & 1 deletion src/algorithms/overlap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ impl Algorithm<f64> for Overlap {
fn for_iter<C, E>(&self, s1: C, s2: C) -> Result<f64>
where
C: Iterator<Item = E>,
E: Eq + Copy + std::hash::Hash,
E: Eq + std::hash::Hash,
{
let c1 = Counter::from_iter(s1);
let c2 = Counter::from_iter(s2);
Expand Down
2 changes: 1 addition & 1 deletion src/algorithms/roberts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ impl Algorithm<f64> for Roberts {
fn for_iter<C, E>(&self, s1: C, s2: C) -> Result<f64>
where
C: Iterator<Item = E>,
E: Eq + Copy + std::hash::Hash,
E: Eq + std::hash::Hash,
{
let c1 = Counter::from_iter(s1);
let c2 = Counter::from_iter(s2);
Expand Down
2 changes: 1 addition & 1 deletion src/algorithms/sorensen_dice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ impl Algorithm<f64> for SorensenDice {
fn for_iter<C, E>(&self, s1: C, s2: C) -> Result<f64>
where
C: Iterator<Item = E>,
E: Eq + Copy + std::hash::Hash,
E: Eq + std::hash::Hash,
{
let c1 = Counter::from_iter(s1);
let c2 = Counter::from_iter(s2);
Expand Down
2 changes: 1 addition & 1 deletion src/algorithms/tversky.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ impl Algorithm<f64> for Tversky {
fn for_iter<C, E>(&self, s1: C, s2: C) -> Result<f64>
where
C: Iterator<Item = E>,
E: Eq + Copy + std::hash::Hash,
E: Eq + std::hash::Hash,
{
let c1 = Counter::from_iter(s1);
let c2 = Counter::from_iter(s2);
Expand Down
3 changes: 1 addition & 2 deletions src/algorithms/yujian_bo.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
//! Yujian-Bo distance
use super::levenshtein::Levenshtein;
use crate::{Algorithm, Result};
use std::hash::Hash;

/// [Yujian-Bo distance] is a normalization of [Levenshtein].
///
Expand All @@ -16,7 +15,7 @@ impl Algorithm<f64> for YujianBo {
fn for_iter<C, E>(&self, s1: C, s2: C) -> Result<f64>
where
C: Iterator<Item = E>,
E: Eq + Copy + Hash,
E: Eq + std::hash::Hash,
{
let lev = self.levenshtein.for_iter(s1, s2);
let dc: usize = self.levenshtein.del_cost;
Expand Down
12 changes: 6 additions & 6 deletions src/counter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ pub struct Counter<K> {

impl<K> Counter<K>
where
K: Hash + Eq + Copy,
K: Hash + Eq,
{
/// make an empty Counter
pub fn new() -> Counter<K> {
Expand Down Expand Up @@ -59,15 +59,15 @@ where
}

/// Merge two counters together.
pub fn merge(&self, rhs: &Counter<K>) -> Counter<K> {
let mut result: HashMap<K, usize> = HashMap::new();
pub fn merge<'a>(&'a self, rhs: &'a Counter<K>) -> Counter<&'a K> {
let mut result: HashMap<&K, usize> = HashMap::new();
for (key, lhs_count) in &self.map {
let rhs_count = rhs.map.get(key).unwrap_or(&0);
result.insert(*key, *lhs_count + rhs_count);
result.insert(key, *lhs_count + rhs_count);
}
for (key, rhs_count) in &rhs.map {
if self.map.get(key).is_none() {
result.insert(*key, *rhs_count);
result.insert(key, *rhs_count);
}
}
Counter { map: result }
Expand Down Expand Up @@ -118,7 +118,7 @@ mod tests {
use assert2::assert;
use rstest::rstest;

pub fn eq<K: Hash + Eq + Copy>(lhs: &Counter<K>, rhs: &Counter<K>) -> bool {
pub fn eq<K: Hash + Eq>(lhs: &Counter<K>, rhs: &Counter<K>) -> bool {
for (key, lhs_count) in &lhs.map {
if let Some(rhs_count) = rhs.map.get(key) {
if lhs_count != rhs_count {
Expand Down