From 62f488c24e346837168c9e106e0f80ab9559229a Mon Sep 17 00:00:00 2001 From: Max Bradbury Date: Tue, 23 Jun 2020 16:36:33 +0100 Subject: [PATCH] sort and dedupe IDs --- src/lib.rs | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 1795a02..430fbfe 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -147,7 +147,10 @@ fn segments_from_string(string: String) -> Vec { /// e.g. pass all tile IDs into this to get a new non-conflicting tile ID #[inline] -fn new_unique_id(ids: Vec) -> String { +fn new_unique_id(mut ids: Vec) -> String { + ids.sort(); + ids.dedup(); + let mut new_id: u64 = 0; for id in ids { @@ -237,8 +240,13 @@ mod test { #[test] fn test_new_unique_id() { - assert_eq!(new_unique_id(vec!["0".to_string(), "2".to_string()]), "1".to_string()); - assert_eq!(new_unique_id(vec!["0".to_string(), "1".to_string()]), "2".to_string()); + // start 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()); } }