algae_graph/store/
mod.rs

1/*
2    Appellation: store <module>
3    Contrib: FL03 <jo3mccain@icloud.com>
4    Description:
5*/
6pub use self::{matrix::*, table::*};
7
8mod matrix;
9mod table;
10
11use crate::{Contain, Edge, Node};
12use serde::{Deserialize, Serialize};
13use std::ops::IndexMut;
14
15#[derive(Clone, Debug, Default, Deserialize, Eq, Hash, Ord, PartialEq, PartialOrd, Serialize)]
16pub struct Entry<N = String, V = ()> {
17    key: N,
18    value: Vec<(N, V)>,
19}
20
21impl<N, V> Entry<N, V> {
22    pub fn new(key: N, value: Vec<(N, V)>) -> Self {
23        Self { key, value }
24    }
25    pub fn key(&self) -> &N {
26        &self.key
27    }
28    pub fn value(&self) -> &Vec<(N, V)> {
29        &self.value
30    }
31    pub fn value_mut(&mut self) -> &mut Vec<(N, V)> {
32        &mut self.value
33    }
34}
35
36impl<N, V> Contain<(N, V)> for Entry<N, V>
37where
38    N: PartialEq,
39    V: PartialEq,
40{
41    fn contains(&self, elem: &(N, V)) -> bool {
42        self.value.contains(elem)
43    }
44}
45
46pub trait Store<N, V>: Extend<Edge<N, V>> + IndexMut<N, Output = Vec<(N, V)>>
47where
48    N: Node,
49{
50    fn clear(&mut self);
51    fn contains_key(&self, key: &N) -> bool;
52    fn drain(&mut self);
53    fn entry(&mut self, key: N);
54    fn get(&self, key: &N) -> Option<&Vec<(N, V)>> {
55        if self.contains_key(key) {
56            Some(&self[key.clone()])
57        } else {
58            None
59        }
60    }
61}