0

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?

James Gaunt
  • 119
  • 1
  • 14
  • 1
    Thank you for asking, but your question is unclear. Please provide minimal reproducible example https://stackoverflow.com/help/minimal-reproducible-example. Try removing all your business-logic-related details and keep it simple. Do you want to build HashMap from HashMap ? – Evgeniy Aug 20 '22 at 06:51
  • Also, something like `let styles: HashMap<...> = Vec::new()` is just not going to work... – jthulhu Aug 20 '22 at 08:40
  • You'd have to use Rc/Arc to do this. A HashMap can't be self-referential. – cdhowie Aug 20 '22 at 13:11

0 Answers0