1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
use crate::svec::{Store, StoreMut};
use std::collections::HashMap;

pub struct HashStore<T> {
    values: HashMap<usize, T>,
}

impl<T> HashStore<T> {
    pub fn new() -> Self {
        HashStore { values: HashMap::new() }
    }

    pub fn new_with_capacity(capacity: usize) -> Self {
        HashStore {
            values: HashMap::with_capacity(capacity),
        }
    }
}

impl<T> Default for HashStore<T> {
    fn default() -> Self {
        Self::new()
    }
}

impl<T> Store for HashStore<T> {
    type Item = T;

    fn get(&self, idx: usize) -> &Self::Item {
        #[allow(clippy::get_unwrap)]
        &self.values[&idx]
    }
}

impl<T> StoreMut for HashStore<T> {
    fn clear(&mut self) {
        self.values.clear();
    }

    fn add(&mut self, idx: usize, value: Self::Item) {
        self.values.insert(idx, value);
    }

    fn remove(&mut self, idx: usize) -> Self::Item {
        self.values.remove(&idx).unwrap()
    }

    fn replace(&mut self, idx: usize, value: Self::Item) -> Self::Item {
        self.values.insert(idx, value).unwrap()
    }

    fn get_mut(&mut self, idx: usize) -> &mut Self::Item {
        self.values.get_mut(&idx).unwrap()
    }
}