hit_data/utils/
model_property_vectors.rs

1// NOT USED FOR NOW
2
3use std::collections::HashMap;
4
5type PropertyMap<T> = HashMap<String, Vec<T>>;
6
7type EntryMap<T> = HashMap<String, PropertyMap<T>>;
8
9#[derive(Clone)]
10pub struct ModelPropertyVectors<T: PartialEq> {
11    entry_map: EntryMap<T>,
12}
13
14impl<T: PartialEq> ModelPropertyVectors<T> {
15    pub fn new() -> ModelPropertyVectors<T> {
16        ModelPropertyVectors {
17            entry_map: EntryMap::new(),
18        }
19    }
20
21    fn get_property_map(&self, id: &str) -> Option<&PropertyMap<T>> {
22        self.entry_map.get(&id.to_string())
23    }
24
25    fn get_property_map_mut(&mut self, id: &str) -> Option<&mut PropertyMap<T>> {
26        self.entry_map.get_mut(&id.to_string())
27    }
28
29    fn get_or_create_property_map_mut(&mut self, id: &str) -> &mut PropertyMap<T> {
30        self.entry_map
31            .entry(id.into())
32            .or_insert_with(PropertyMap::new)
33    }
34
35    pub fn get(&self, id: &str, property: &str) -> Option<&Vec<T>> {
36        let properties = self.get_property_map(id)?;
37        properties.get(property)
38    }
39
40    fn get_or_create_mut(&mut self, id: &str, property: &str) -> &mut Vec<T> {
41        let property_map = self.get_or_create_property_map_mut(id);
42        property_map.entry(property.into()).or_insert_with(Vec::new)
43    }
44
45    /*    fn get_mut(&mut self, id: &str, property: &str) -> Option<&mut Vec<T>> {
46        let properties = self.get_property_map_mut(id)?;
47        properties.get_mut(property)
48    } */
49
50    pub fn add(&mut self, id: &str, property: &str, value: T) {
51        let vector = self.get_or_create_mut(id, property);
52        vector.push(value);
53    }
54
55    /*  pub fn remove(&mut self, id: &str, property: &str, value: &T) {
56        let vector = self.get_mut(id, property);
57        match vector {
58            Some(vector) => {
59                vector.retain(|v| v != value);
60                if vector.len() == 0 {
61                    self.delete(id, property);
62                }
63            }
64            None => {}
65        }
66    } */
67
68    pub fn delete(&mut self, id: &str, property: &str) {
69        let property_map = self.get_property_map_mut(id);
70        match property_map {
71            Some(property_map) => {
72                property_map.remove(property);
73                if property_map.len() == 0 {
74                    self.entry_map.remove(id);
75                }
76            }
77            None => {}
78        }
79    }
80}