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

Fix assignments #8

Merged
merged 2 commits into from
Oct 31, 2024
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 src/cli/ui/handle_candidate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ pub fn show_candidate<B: ReleaseLike, C: ReleaseLike>(
//
// First, show the matched tracks.
let track_assignment = candidate.similarity().track_assignment();
let matched_track_map = track_assignment.matched_tracks_map();
let matched_track_map = track_assignment.map_rhs_indices_to_lhs();
let mut rhs_track_index: usize = 0;
for (media_index, media) in release.media().enumerate() {
let format = media.media_format().unwrap_or_else(|| "Medium".into());
Expand Down
14 changes: 11 additions & 3 deletions src/distance/release.rs
Original file line number Diff line number Diff line change
Expand Up @@ -233,9 +233,17 @@ impl TrackAssignment {
self.matched_tracks.iter()
}

/// Returns a [`HashMap`] that maps the matched tracks from the left hand side side to the
/// corresponding track on the right hand side.
pub fn matched_tracks_map(&self) -> HashMap<usize, (usize, &TrackSimilarity)> {
/// Returns a [`HashMap`] that allows retrieving the matched tracks from the right hand side
/// by the corresponding track index from the left hand side.
pub fn map_lhs_indices_to_rhs(&self) -> HashMap<usize, (usize, &TrackSimilarity)> {
self.matched_tracks()
.map(|pair| (pair.lhs, (pair.rhs, &pair.similarity)))
.collect()
}

/// Returns a [`HashMap`] that allows retrieving the matched tracks from the left hand side
/// by the corresponding track index from the right hand side.
pub fn map_rhs_indices_to_lhs(&self) -> HashMap<usize, (usize, &TrackSimilarity)> {
self.matched_tracks()
.map(|pair| (pair.rhs, (pair.lhs, &pair.similarity)))
.collect()
Expand Down
8 changes: 4 additions & 4 deletions src/scanner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use crate::release_candidate::ReleaseCandidateCollection;
use crate::util::walk_dir;
use crate::Cache;
use crate::{Config, TaggedFile, TaggedFileCollection};
use std::collections::{HashMap, HashSet};
use std::collections::{BinaryHeap, HashMap, HashSet};
use std::path::PathBuf;
use std::time::Duration;
use tokio::sync::mpsc::{error::TryRecvError, Receiver};
Expand Down Expand Up @@ -103,7 +103,7 @@ impl Scanner {
};

group_track_counts.insert(group_id, num_tracks);
group_tracks.insert(group_id, Vec::with_capacity(num_tracks));
group_tracks.insert(group_id, BinaryHeap::with_capacity(num_tracks));
}

while analyzer_output_rx_connected {
Expand Down Expand Up @@ -140,7 +140,7 @@ impl Scanner {
continue;
};

let collection = TaggedFileCollection::new(tracks);
let collection = TaggedFileCollection::new(tracks.into_sorted_vec());
if let Err(err) = post_analysis_tx.send(collection).await {
log::error!("Receiver dropped on sending collection: {err}");
continue;
Expand All @@ -159,7 +159,7 @@ impl Scanner {
log::error!("Missing track count for group {group_id}");
}

let collection = TaggedFileCollection::new(tracks);
let collection = TaggedFileCollection::new(tracks.into_sorted_vec());
if let Err(err) = post_analysis_tx.send(collection).await {
log::error!("Receiver dropped on sending collection: {err}");
}
Expand Down
21 changes: 21 additions & 0 deletions src/taggedfile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use crate::release::ReleaseLike;
use crate::tag::{read_tags_from_path, Tag, TagKey};
use crate::track::{AnalyzedTrackMetadata, TrackLike};
use std::borrow::Cow;
use std::cmp::Ordering;
use std::ffi::OsStr;
use std::fmt;
use std::path::{Path, PathBuf};
Expand Down Expand Up @@ -269,6 +270,26 @@ impl TaggedFile {
}
}

impl PartialEq for TaggedFile {
fn eq(&self, other: &Self) -> bool {
self.path.as_path().eq(other.path.as_path())
}
}

impl Eq for TaggedFile {}

impl Ord for TaggedFile {
fn cmp(&self, other: &Self) -> Ordering {
self.path.as_path().cmp(other.path.as_path())
}
}

impl PartialOrd for TaggedFile {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}

impl TrackLike for TaggedFile {
fn acoustid(&self) -> Option<Cow<'_, str>> {
self.first_tag_value(TagKey::AcoustId).map(Cow::from)
Expand Down
2 changes: 1 addition & 1 deletion src/taggedfilecollection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ impl TaggedFileCollection {
let matched_track_map = release_candidate
.similarity()
.track_assignment()
.matched_tracks_map();
.map_lhs_indices_to_rhs();
self = self
.into_iter()
.enumerate()
Expand Down
Loading