-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
41 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
use std::collections::HashMap; | ||
|
||
|
||
pub struct UniqueNames { | ||
name_map : HashMap<String, i64> | ||
} | ||
|
||
impl UniqueNames { | ||
pub fn new() -> Self { | ||
let mut name_map : HashMap<String, i64> = HashMap::new(); | ||
name_map.insert(String::new(), 1); | ||
Self { | ||
name_map | ||
} | ||
} | ||
pub fn get_unique_name<S : Into<String> + AsRef<str>>(&mut self, name : S) -> String { | ||
let name_ref = name.as_ref(); | ||
if let Some(found_id) = self.name_map.get_mut(name_ref) { | ||
let result = format!("{name_ref}_{found_id}"); | ||
*found_id += 1; | ||
result | ||
} else { | ||
let result : String = name.into(); | ||
self.name_map.insert(result.clone(), 2); | ||
result | ||
} | ||
} | ||
} |