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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
use std::{
    borrow::Borrow,
    fmt::{Debug, Display},
};

use string_interner::{DefaultSymbol, StringInterner, Symbol};

use crate::types::LabelStore as _;

#[derive(Default)]
pub struct LabelStore {
    count: usize,
    internal: StringInterner, //VecMapStore<OwnedLabel, LabelIdentifier>,
}

impl Debug for LabelStore {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("LabelStore")
            .field("count", &self.count)
            .field("internal_len", &self.internal.len())
            .field("internal", &self.internal)
            .finish()
    }
}

impl Display for LabelStore {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        for (i, x) in self.internal.clone().into_iter() {
            writeln!(f, "{:?}:{:?}", i.to_usize(), x)?
        }
        Ok(())
    }
}

pub type DefaultLabelValue = str;
pub type DefaultLabelIdentifier = DefaultSymbol;

pub fn label_id_from_usize(x: usize) -> Option<DefaultLabelIdentifier> {
    DefaultLabelIdentifier::try_from_usize(x)
}

impl crate::types::LabelStore<DefaultLabelValue> for LabelStore {
    type I = DefaultLabelIdentifier;
    fn get_or_insert<T: Borrow<DefaultLabelValue>>(&mut self, node: T) -> Self::I {
        self.count += 1;
        self.internal.get_or_intern(node.borrow())
    }
    fn get<T: Borrow<DefaultLabelValue>>(&self, node: T) -> Option<Self::I> {
        self.internal.get(node.borrow())
    }

    fn resolve(&self, id: &Self::I) -> &DefaultLabelValue {
        self.internal.resolve(*id).unwrap()
    }
}

impl crate::types::LabelStore<DefaultLabelValue> for &LabelStore {
    type I = DefaultLabelIdentifier;
    fn get_or_insert<T: Borrow<DefaultLabelValue>>(&mut self, node: T) -> Self::I {
        unimplemented!()
    }
    fn get<T: Borrow<DefaultLabelValue>>(&self, node: T) -> Option<Self::I> {
        self.internal.get(node.borrow())
    }

    fn resolve(&self, id: &Self::I) -> &DefaultLabelValue {
        self.internal.resolve(*id).unwrap()
    }
}

impl LabelStore {
    pub fn new() -> Self {
        let mut r = Self {
            count: 1,
            internal: Default::default(),
        };
        r.get_or_insert("length"); // TODO verify/model statically
        r
    }
}