My hashmap has a lot of duplicate values in it, and instead of leaving them as their own variables, I want to create another hashmap of every possible unique value, then in the original hashmap, it simply just references one of the values in the unique vector.
let mut styles: HashMap<String, Character> = Vec::new();
Then it my function return the new optimized vector in a struct
pub fn loadtheme(filepath: &str) -> Theme {
[...]
mut styles: HashMap<String, Character> = Vec::new(); //
[...]
Theme {
version,
name,
styledata,
}
}
Theme Struct:
#[derive(Debug)]
pub struct Theme {
version: u16,
name: String,
styledata: HashMap<(String, u32), HashMap<String, Character>>,
}
Here is my current function that removes duplicates and replaces them with references
// Get every possible style with no duplicates
let mut nodupstyles: Vec<&HashMap<String, Character>> = Vec::new();
for (_, style) in elementsetupcharacters.iter() {
if !nodupstyles.contains(&style) {
nodupstyles.push(style);
}
}
// Compile Style Data
let mut styledata: HashMap<(String, u32), HashMap<String, Character>> = HashMap::new();
for (props, style) in elementsetupcharacters.clone().into_iter() {
// Loop through possible styles and get one that matches the style
let stylereference: HashMap<String, Character> = HashMap::new();
for nodupstyle in nodupstyles.iter() {
if nodupstyle.keys().cloned().collect::<Vec<String>>()
== style.keys().cloned().collect::<Vec<String>>()
&& nodupstyle.values().cloned().collect::<Vec<Character>>()
== style.values().cloned().collect::<Vec<Character>>()
{
let stylereference = &nodupstyle;
break;
}
}
if props.1.len() > 0 {
styledata.insert((String::from(props.0), bitstou32(props.1)), stylereference);
} else {
styledata.insert((String::from(props.0), 0), stylereference);
}
}
What is the best way I could achieve this functionality?