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

Make ScoredPath a struct #38

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
95 changes: 40 additions & 55 deletions src/alt/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use alt::path::scoring::{score_paths, ScoredPath};
use std::cmp::Ordering;
use std::thread;

pub mod path;
Expand All @@ -14,7 +13,7 @@ pub fn find_alt(
let mut possible_paths_with_scores: Vec<ScoredPath> =
score_paths(paths, cleansed_path, filename_weight, path_weight);

possible_paths_with_scores.sort_by(order_scored_paths);
possible_paths_with_scores.sort_by(|x, y| x.partial_cmp(y).unwrap());

truncate_scored_paths(&mut possible_paths_with_scores, truncate_len);

Expand Down Expand Up @@ -71,23 +70,13 @@ pub fn find_alt_with_threads(

let mut scored_paths = collections_of_scored_paths.concat();

scored_paths.sort_by(order_scored_paths);
scored_paths.sort_by(|x, y| x.partial_cmp(y).unwrap());

truncate_scored_paths(&mut scored_paths, truncate_len);

Ok(scored_paths)
}

fn order_scored_paths(scored_path_a: &ScoredPath, scored_path_b: &ScoredPath) -> Ordering {
if scored_path_a.0 > scored_path_b.0 {
Ordering::Less
} else if scored_path_a.0 < scored_path_b.0 {
Ordering::Greater
} else {
Ordering::Equal
}
}

fn truncate_scored_paths(scored_paths: &mut Vec<ScoredPath>, len: usize) {
match len {
0 => (),
Expand All @@ -97,18 +86,16 @@ fn truncate_scored_paths(scored_paths: &mut Vec<ScoredPath>, len: usize) {

#[cfg(test)]
mod tests {
use super::{
find_alt, find_alt_with_threads, order_scored_paths, truncate_scored_paths, ScoredPath,
};
use super::{find_alt, find_alt_with_threads, truncate_scored_paths, ScoredPath};

#[test]
fn truncate_scored_paths_with_zero_len() {
let mut scored_paths: Vec<ScoredPath> = vec![
(0.8, "some/path/to/a/file.ts".to_owned()),
(0.4, "some/path/to/another/foo.ts".to_owned()),
(0.2, "some/other_path/to/a/bar.ts".to_owned()),
(0.1, "some/short/path/zoo.ts".to_owned()),
(0.023, "some/blue/fortytwo/sports_ball.ts".to_owned()),
ScoredPath::new(0.8, "some/path/to/a/file.ts".to_owned()),
ScoredPath::new(0.4, "some/path/to/another/foo.ts".to_owned()),
ScoredPath::new(0.2, "some/other_path/to/a/bar.ts".to_owned()),
ScoredPath::new(0.1, "some/short/path/zoo.ts".to_owned()),
ScoredPath::new(0.023, "some/blue/fortytwo/sports_ball.ts".to_owned()),
];

truncate_scored_paths(&mut scored_paths, 0);
Expand All @@ -119,11 +106,11 @@ mod tests {
#[test]
fn truncate_scored_paths_with_non_zero_len() {
let mut scored_paths: Vec<ScoredPath> = vec![
(0.8, "some/path/to/a/file.ts".to_owned()),
(0.4, "some/path/to/another/foo.ts".to_owned()),
(0.2, "some/other_path/to/a/bar.ts".to_owned()),
(0.1, "some/short/path/zoo.ts".to_owned()),
(0.023, "some/blue/fortytwo/sports_ball.ts".to_owned()),
ScoredPath::new(0.8, "some/path/to/a/file.ts".to_owned()),
ScoredPath::new(0.4, "some/path/to/another/foo.ts".to_owned()),
ScoredPath::new(0.2, "some/other_path/to/a/bar.ts".to_owned()),
ScoredPath::new(0.1, "some/short/path/zoo.ts".to_owned()),
ScoredPath::new(0.023, "some/blue/fortytwo/sports_ball.ts".to_owned()),
];

truncate_scored_paths(&mut scored_paths, 3);
Expand All @@ -133,32 +120,30 @@ mod tests {

#[test]
fn order_scored_paths_with_a_larger() {
let ordering = order_scored_paths(
&(0.3, "some/path/to/a/file.ts".to_owned()),
&(0.2, "some/other/path/bar.ts".to_owned()),
let ordering = ScoredPath::partial_cmp(
&(0.3, "some/path/to/a/file.ts".to_owned()).into(),
&(0.2, "some/other/path/bar.ts".to_owned()).into(),
);

assert_eq!(ordering, std::cmp::Ordering::Less);
assert_eq!(ordering, Some(std::cmp::Ordering::Less))
}

#[test]
fn order_scored_paths_with_a_smaller() {
let ordering = order_scored_paths(
&(0.2, "some/path/to/a/file.ts".to_owned()),
&(0.3, "some/other/path/bar.ts".to_owned()),
let ordering = ScoredPath::partial_cmp(
&(0.2, "some/path/to/a/file.ts".to_owned()).into(),
&(0.3, "some/other/path/bar.ts".to_owned()).into(),
);

assert_eq!(ordering, std::cmp::Ordering::Greater);
assert_eq!(ordering, Some(std::cmp::Ordering::Greater));
}

#[test]
fn order_scored_paths_with_a_and_b_equal() {
let ordering = order_scored_paths(
&(0.3, "some/path/to/a/file.ts".to_owned()),
&(0.3, "some/other/path/bar.ts".to_owned()),
let ordering = ScoredPath::partial_cmp(
&(0.3, "some/path/to/a/file.ts".to_owned()).into(),
&(0.3, "some/other/path/bar.ts".to_owned()).into(),
);

assert_eq!(ordering, std::cmp::Ordering::Equal);
assert_eq!(ordering, Some(std::cmp::Ordering::Equal));
}

#[test]
Expand All @@ -173,12 +158,12 @@ mod tests {
let scored_paths: Vec<ScoredPath> =
find_alt("src/models/nft-wallet.ts", paths, 0, 10.0, 1.0);
assert_eq!(scored_paths.len(), 5);
assert!(scored_paths[0].0 > scored_paths[1].0);
assert!(scored_paths[1].0 > scored_paths[2].0);
assert!(scored_paths[2].0 > scored_paths[3].0);
assert!(scored_paths[3].0 > scored_paths[4].0);
assert!(scored_paths[0].score > scored_paths[1].score);
assert!(scored_paths[1].score > scored_paths[2].score);
assert!(scored_paths[2].score > scored_paths[3].score);
assert!(scored_paths[3].score > scored_paths[4].score);

let stripped_scored_paths: Vec<String> = scored_paths.into_iter().map(|s| s.1).collect();
let stripped_scored_paths: Vec<String> = scored_paths.into_iter().map(|s| s.path).collect();
assert_eq!(
stripped_scored_paths,
vec![
Expand All @@ -205,7 +190,7 @@ mod tests {
find_alt("src/models/nft-wallet.ts", paths, 0, 10.0, 1.0);
assert_eq!(scored_paths.len(), 5);

let stripped_scored_paths: Vec<String> = scored_paths.into_iter().map(|s| s.1).collect();
let stripped_scored_paths: Vec<String> = scored_paths.into_iter().map(|s| s.path).collect();
assert_eq!(
stripped_scored_paths,
vec![
Expand All @@ -230,10 +215,10 @@ mod tests {
let scored_paths: Vec<ScoredPath> =
find_alt("src/models/nft-wallet.ts", paths, 3, 10.0, 1.0);
assert_eq!(scored_paths.len(), 3);
assert!(scored_paths[0].0 > scored_paths[1].0);
assert!(scored_paths[1].0 > scored_paths[2].0);
assert!(scored_paths[0].score > scored_paths[1].score);
assert!(scored_paths[1].score > scored_paths[2].score);

let stripped_scored_paths: Vec<String> = scored_paths.into_iter().map(|s| s.1).collect();
let stripped_scored_paths: Vec<String> = scored_paths.into_iter().map(|s| s.path).collect();
assert_eq!(
stripped_scored_paths,
vec![
Expand All @@ -256,12 +241,12 @@ mod tests {
let scored_paths: Vec<ScoredPath> =
find_alt("src/models/nft-wallet.ts", paths, 0, 10.0, 1.0);
assert_eq!(scored_paths.len(), 5);
assert!(scored_paths[0].0 > scored_paths[1].0);
assert!(scored_paths[1].0 > scored_paths[2].0);
assert!(scored_paths[2].0 > scored_paths[3].0);
assert!(scored_paths[3].0 > scored_paths[4].0);
assert!(scored_paths[0].score > scored_paths[1].score);
assert!(scored_paths[1].score > scored_paths[2].score);
assert!(scored_paths[2].score > scored_paths[3].score);
assert!(scored_paths[3].score > scored_paths[4].score);

let stripped_scored_paths: Vec<String> = scored_paths.into_iter().map(|s| s.1).collect();
let stripped_scored_paths: Vec<String> = scored_paths.into_iter().map(|s| s.path).collect();
assert_eq!(
stripped_scored_paths,
vec![
Expand All @@ -287,7 +272,7 @@ mod tests {
find_alt("src/models/nft-wallet.ts", paths, 0, 1.0, 10.0);
assert_eq!(scored_paths.len(), 5);

let stripped_scored_paths: Vec<String> = scored_paths.into_iter().map(|s| s.1).collect();
let stripped_scored_paths: Vec<String> = scored_paths.into_iter().map(|s| s.path).collect();
assert_eq!(
stripped_scored_paths,
vec![
Expand Down
60 changes: 43 additions & 17 deletions src/alt/path/scoring/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,35 @@
use super::utils::cleanse_path;
use std::path::Path;
use std::{cmp::Ordering, path::Path};

pub type ScoredPath = (f32, String);
#[derive(Clone, Debug, PartialEq)]
pub struct ScoredPath {
pub score: f32,
pub path: String,
}

impl From<(f32, String)> for ScoredPath {
fn from(value: (f32, String)) -> Self {
ScoredPath::new(value.0, value.1)
}
}

impl ScoredPath {
pub fn new(score: f32, path: String) -> Self {
ScoredPath { score, path }
}
}

impl PartialOrd for ScoredPath {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
if self.score > other.score {
Some(Ordering::Less)
} else if self.score < other.score {
Some(Ordering::Greater)
} else {
Some(Ordering::Equal)
}
}
}

fn find_longest_common_substring_length(s1: &str, s2: &str) -> i32 {
// Currently this is implemented using a dynamic programming solution similar
Expand Down Expand Up @@ -100,11 +128,9 @@ pub fn score_paths(
.iter()
.map(|path| cleanse_path(path))
.filter(|path| path != cleansed_path)
.map(|path| {
(
score(cleansed_path, &path, filename_weight, path_weight),
path,
)
.map(|path| ScoredPath {
score: score(cleansed_path, &path, filename_weight, path_weight),
path,
})
.collect()
}
Expand All @@ -124,10 +150,10 @@ mod tests {
let scored_paths = score_paths(paths, "hoopty/doopty/foopty.ts", 10.0, 1.0);

assert_eq!(scored_paths.len(), 2);
assert_eq!(scored_paths[0].1, "foo/bar/car.ts".to_owned());
assert_eq!(scored_paths[1].1, "home/away/lets_play.ts".to_owned());
assert!(scored_paths[0].0 > 0.0);
assert!(scored_paths[1].0 > 0.0);
assert_eq!(scored_paths[0].path, "foo/bar/car.ts".to_owned());
assert_eq!(scored_paths[1].path, "home/away/lets_play.ts".to_owned());
assert!(scored_paths[0].score > 0.0);
assert!(scored_paths[1].score > 0.0);
}

#[test]
Expand All @@ -141,12 +167,12 @@ mod tests {
let scored_paths = score_paths(paths, "person/place/thing.ts", 10.0, 1.0);

assert_eq!(scored_paths.len(), 3);
assert_eq!(scored_paths[0].1, "foo/bar/car.ts".to_owned());
assert_eq!(scored_paths[1].1, "hoopty/doopty/foopty.ts".to_owned());
assert_eq!(scored_paths[2].1, "home/away/lets_play.ts".to_owned());
assert!(scored_paths[0].0 > 0.0);
assert!(scored_paths[1].0 > 0.0);
assert!(scored_paths[2].0 > 0.0);
assert_eq!(scored_paths[0].path, "foo/bar/car.ts".to_owned());
assert_eq!(scored_paths[1].path, "hoopty/doopty/foopty.ts".to_owned());
assert_eq!(scored_paths[2].path, "home/away/lets_play.ts".to_owned());
assert!(scored_paths[0].score > 0.0);
assert!(scored_paths[1].score > 0.0);
assert!(scored_paths[2].score > 0.0);
}

#[test]
Expand Down
20 changes: 7 additions & 13 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,17 +106,11 @@ fn parse_args_or_exit() -> Options {
}

fn scored_paths_to_string(scored_paths: &[ScoredPath]) -> String {
let matches: Vec<String> = scored_paths
scored_paths
.iter()
.map(|(_, path)| path.to_string())
// .map(|(score, path)| format!("{:?} {}", score, path.to_string()))
.collect();

if matches.is_empty() {
String::new()
} else {
matches.join("\n")
}
.map(|scored_path| scored_path.path.to_string())
.collect::<Vec<String>>()
.join("\n")
}

fn main() {
Expand Down Expand Up @@ -217,9 +211,9 @@ mod tests {
#[test]
fn scored_paths_to_string_with_some_scored_paths() {
let scored_paths: Vec<ScoredPath> = vec![
(0.3, "some/path/to/a/file.ts".to_owned()),
(0.2, "another/path/to/a/foo.ts".to_owned()),
(0.1, "foo/bar/car/zar.ts".to_owned()),
ScoredPath::new(0.3, "some/path/to/a/file.ts".to_owned()),
ScoredPath::new(0.2, "another/path/to/a/foo.ts".to_owned()),
ScoredPath::new(0.1, "foo/bar/car/zar.ts".to_owned()),
];
let val = scored_paths_to_string(&scored_paths);

Expand Down