sort and dedupe IDs

This commit is contained in:
Max Bradbury 2020-06-23 16:36:33 +01:00
parent 0626d8d069
commit 62f488c24e
1 changed files with 11 additions and 3 deletions

View File

@ -147,7 +147,10 @@ fn segments_from_string(string: String) -> Vec<String> {
/// e.g. pass all tile IDs into this to get a new non-conflicting tile ID /// e.g. pass all tile IDs into this to get a new non-conflicting tile ID
#[inline] #[inline]
fn new_unique_id(ids: Vec<String>) -> String { fn new_unique_id(mut ids: Vec<String>) -> String {
ids.sort();
ids.dedup();
let mut new_id: u64 = 0; let mut new_id: u64 = 0;
for id in ids { for id in ids {
@ -237,8 +240,13 @@ mod test {
#[test] #[test]
fn test_new_unique_id() { fn test_new_unique_id() {
assert_eq!(new_unique_id(vec!["0".to_string(), "2".to_string()]), "1".to_string()); // start
assert_eq!(new_unique_id(vec!["0".to_string(), "1".to_string()]), "2".to_string());
assert_eq!(new_unique_id(vec!["1".to_string(), "z".to_string()]), "0".to_string()); assert_eq!(new_unique_id(vec!["1".to_string(), "z".to_string()]), "0".to_string());
// middle
assert_eq!(new_unique_id(vec!["0".to_string(), "2".to_string()]), "1".to_string());
// end
assert_eq!(new_unique_id(vec!["0".to_string(), "1".to_string()]), "2".to_string());
// check deduplication
assert_eq!(new_unique_id(vec!["0".to_string(), "0".to_string()]), "1".to_string());
} }
} }