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

Binary search indexes for artist #52

Open
fsktom opened this issue Jul 10, 2023 · 1 comment
Open

Binary search indexes for artist #52

fsktom opened this issue Jul 10, 2023 · 1 comment
Assignees
Labels
library Regarding `endsong` crate low prio Low priority question Further information is requested wontfix This will not be worked on

Comments

@fsktom
Copy link
Owner

fsktom commented Jul 10, 2023

Similar to #39

https://youtu.be/KXJSjte_OAI

Don't know if it's necessary because the performance should now be good enough and it might bring unncessary overhead.
But it'd be fun to create an index for using binary search for artists as well.

Probably not really feasible, because the functions accept [SongEntry] and not SongEntries - meaning I'd have to put the index as another argument or make the argument SongEntries with the index as an attribute...

You could create the index while parsing the endsong files.

pub fn gather_plays<Asp: Music>(
entries: &[SongEntry],
aspect: &Asp,
start: &DateTime<Tz>,
end: &DateTime<Tz>,
) -> usize {
assert!(start <= end, "Start date is after end date!");
let begin = match entries.binary_search_by(|entry| entry.timestamp.cmp(start)) {
// timestamp from entry
Ok(i) => i,
// user inputted date - i because you want it to begin at the closest entry
Err(i) if i != entries.len() => i,
// user inputted date that's after the last entry
Err(_) => entries.len() - 1,
};
let stop = match entries.binary_search_by(|entry| entry.timestamp.cmp(end)) {
// timestamp from entry
Ok(i) => i,
// user inputted date - i-1 becuase i would include one entry too much
Err(i) if i != 0 => i - 1,
// user inputted date that's before the first entry
Err(_) => 0,
};
entries[begin..=stop]
.iter()
.filter(|entry| aspect.is_entry(entry))
.count()
}

instead of that linear scan at the bottom you could do something like (or similar, haven't thought exactly about how you'd access it)
artist_index[artist].unwrap()[begin..=stop].len()
And the index would be a collection of artists with a list of the indexes those artists appear at
Implementation details tbd

Ofc, this would also only work for artists, while this function accepts Artists, Albums and Songs... So you could create other indexes for them or separate the function
...or probably just let it be because it isn't a good idea :P

@fsktom fsktom added question Further information is requested wontfix This will not be worked on low prio Low priority labels Jul 10, 2023
@fsktom fsktom self-assigned this Jul 10, 2023
@fsktom
Copy link
Owner Author

fsktom commented Jul 10, 2023

Probably the best structure for the index would be a HashMap where the keys are the artist names (as Strings) and the values are Vec<DateTime<Tz>> with the timestamps of the corresponding entries

@fsktom fsktom added the library Regarding `endsong` crate label Jul 27, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
library Regarding `endsong` crate low prio Low priority question Further information is requested wontfix This will not be worked on
Projects
None yet
Development

No branches or pull requests

1 participant