Skip to content

Commit

Permalink
reformatted code with rustfmt, no functional changes
Browse files Browse the repository at this point in the history
  • Loading branch information
proycon committed Feb 21, 2023
1 parent 38bb7ba commit 294820d
Show file tree
Hide file tree
Showing 12 changed files with 2,307 additions and 1,161 deletions.
579 changes: 378 additions & 201 deletions bindings/python/src/lib.rs

Large diffs are not rendered by default.

84 changes: 61 additions & 23 deletions src/anahash.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use ibig::UBig;
use num_traits::{Zero, One};
use num_traits::{One, Zero};
use std::collections::HashSet;

use crate::types::*;
use crate::iterators::*;
use crate::types::*;

///Trait for objects that can be anahashed (string-like)
pub trait Anahashable {
Expand All @@ -25,12 +25,12 @@ impl Anahashable for str {
'abciter: for (seqnr, chars) in alphabet.iter().enumerate() {
for element in chars.iter() {
let l = element.chars().count();
if let Some(slice) = self.get(pos..pos+l) {
if let Some(slice) = self.get(pos..pos + l) {
if slice == element {
let charvalue = AnaValue::character(seqnr as CharIndexType);
hash = hash.insert(&charvalue);
matched = true;
skip = l-1;
skip = l - 1;
break 'abciter;
}
}
Expand All @@ -45,7 +45,6 @@ impl Anahashable for str {
hash
}


///Normalize a string via the alphabet
fn normalize_to_alphabet(&self, alphabet: &Alphabet) -> NormString {
let mut result = Vec::with_capacity(self.chars().count());
Expand All @@ -60,11 +59,11 @@ impl Anahashable for str {
'abciter: for (i, chars) in alphabet.iter().enumerate() {
for element in chars.iter() {
let l = element.chars().count();
if let Some(slice) = self.get(pos..pos+l) {
if let Some(slice) = self.get(pos..pos + l) {
if slice == element {
result.push(i as CharIndexType);
matched = true;
skip = l-1;
skip = l - 1;
break 'abciter;
}
}
Expand All @@ -77,10 +76,8 @@ impl Anahashable for str {
}
result
}

}


/// This trait can be applied to types
/// that can function as anahashes.
/// It can be implemented for integer types.
Expand All @@ -93,15 +90,23 @@ pub trait Anahash: One + Zero {
fn contains(&self, value: &AnaValue) -> bool;
fn iter(&self, alphabet_size: CharIndexType) -> RecurseDeletionIterator;
fn iter_parents(&self, alphabet_size: CharIndexType) -> DeletionIterator;
fn iter_recursive(&self, alphabet_size: CharIndexType, params: &SearchParams) -> RecurseDeletionIterator;
fn iter_recursive_external_cache<'a>(&self, alphabet_size: CharIndexType, params: &SearchParams, cache: &'a mut HashSet<AnaValue>) -> RecurseDeletionIterator<'a>;
fn iter_recursive(
&self,
alphabet_size: CharIndexType,
params: &SearchParams,
) -> RecurseDeletionIterator;
fn iter_recursive_external_cache<'a>(
&self,
alphabet_size: CharIndexType,
params: &SearchParams,
cache: &'a mut HashSet<AnaValue>,
) -> RecurseDeletionIterator<'a>;

/// Computes the number of characters in this anagram
fn char_count(&self, alphabet_size: CharIndexType) -> u16 {
self.iter(alphabet_size).count() as u16
}


/// Count how many times an anagram value occurs in this anagram
fn count_matches(&self, value: &AnaValue) -> usize {
if let Some(result) = self.delete(value) {
Expand Down Expand Up @@ -183,7 +188,17 @@ impl Anahash for AnaValue {
/// }
/// ```
fn iter(&self, alphabet_size: CharIndexType) -> RecurseDeletionIterator {
RecurseDeletionIterator::new(self.clone(), alphabet_size, true, None, None, false,false,true, None)
RecurseDeletionIterator::new(
self.clone(),
alphabet_size,
true,
None,
None,
false,
false,
true,
None,
)
}

/// Iterator over all the parents that are generated when applying all deletions within edit distance 1
Expand All @@ -192,13 +207,42 @@ impl Anahash for AnaValue {
}

/// Iterator over all the possible deletions within the specified anagram distance
fn iter_recursive(&self, alphabet_size: CharIndexType, params: &SearchParams) -> RecurseDeletionIterator {
RecurseDeletionIterator::new(self.clone(), alphabet_size, false, params.min_distance, params.max_distance, params.breadthfirst, !params.allow_duplicates, params.allow_empty_leaves, None )
fn iter_recursive(
&self,
alphabet_size: CharIndexType,
params: &SearchParams,
) -> RecurseDeletionIterator {
RecurseDeletionIterator::new(
self.clone(),
alphabet_size,
false,
params.min_distance,
params.max_distance,
params.breadthfirst,
!params.allow_duplicates,
params.allow_empty_leaves,
None,
)
}

/// Iterator over all the possible deletions within the specified anagram distance
fn iter_recursive_external_cache<'a>(&self, alphabet_size: CharIndexType, params: &SearchParams, cache: &'a mut HashSet<AnaValue>) -> RecurseDeletionIterator<'a> {
RecurseDeletionIterator::new(self.clone(), alphabet_size, false, params.min_distance, params.max_distance, params.breadthfirst, !params.allow_duplicates, params.allow_empty_leaves, Some(cache) )
fn iter_recursive_external_cache<'a>(
&self,
alphabet_size: CharIndexType,
params: &SearchParams,
cache: &'a mut HashSet<AnaValue>,
) -> RecurseDeletionIterator<'a> {
RecurseDeletionIterator::new(
self.clone(),
alphabet_size,
false,
params.min_distance,
params.max_distance,
params.breadthfirst,
!params.allow_duplicates,
params.allow_empty_leaves,
Some(cache),
)
}

/// The value of an empty anahash
Expand All @@ -212,13 +256,8 @@ impl Anahash for AnaValue {
fn is_empty(&self) -> bool {
self == &AnaValue::empty() || self == &AnaValue::zero()
}



}



/// Search parameters used to pass to the Anahash::iter_recursive() function
pub struct SearchParams {
pub min_distance: Option<u32>,
Expand All @@ -239,4 +278,3 @@ impl Default for SearchParams {
}
}
}

Loading

0 comments on commit 294820d

Please sign in to comment.