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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
use std::borrow::Borrow;

use crate::types::{SimpleHyperAST, TypeStore};

// use crate::nodes::TypeIdentifier;

pub mod handle;
pub mod labels;
// pub mod mapped_world;
pub mod nodes;
// pub mod ecs; // TODO try a custom ecs ?
// pub mod radix_hash_store; // TODO yet another WIP store
// pub mod vec_map_store; // TODO yet another WIP store

// struct T(TYPE_INTERNAL_SIZE);

// pub struct TypeStore {
//     langs: Vec<Lang>,
// }

pub struct Lang {
    id_l: usize,
    strs: Vec<&'static str>,
    inner: Vec<u16>,
}

pub trait HLang {
    type Id;
    fn lang_id(&self) -> HLangId;
    fn new(idl: HLangId) -> Self;
}
pub struct HLangId(usize);
pub struct HLangFactory(usize);
impl HLangFactory {
    fn make<L: HLang>(&mut self) -> L {
        let res = L::new(HLangId(self.0));
        self.0 += 1;
        res
    }
}

pub struct SimpleStores<TS, NS = nodes::DefaultNodeStore, LS = labels::LabelStore> {
    pub label_store: LS,
    pub type_store: TS,
    pub node_store: NS,
}

impl<TS: Default, NS: Default, LS: Default> Default for SimpleStores<TS, NS, LS> {
    fn default() -> Self {
        Self {
            label_store: Default::default(),
            type_store: Default::default(),
            node_store: Default::default(),
        }
    }
}

impl<IdN, TS, NS, LS> crate::types::NodeStore<IdN> for SimpleStores<TS, NS, LS>
where
    for<'a> NS::R<'a>: crate::types::Tree<TreeId = IdN>,
    IdN: crate::types::NodeId<IdN = IdN>,
    NS: crate::types::NodeStore<IdN>,
{
    type R<'a> = NS::R<'a>
    where
        Self: 'a;

    fn resolve(&self, id: &IdN) -> Self::R<'_> {
        self.node_store.resolve(id)
    }
}

impl<'store, TS, NS, LS> crate::types::LabelStore<str> for SimpleStores<TS, NS, LS>
where
    LS: crate::types::LabelStore<str>,
{
    type I = LS::I;

    fn get_or_insert<U: Borrow<str>>(&mut self, node: U) -> Self::I {
        self.label_store.get_or_insert(node)
    }

    fn get<U: Borrow<str>>(&self, node: U) -> Option<Self::I> {
        self.label_store.get(node)
    }

    fn resolve(&self, id: &Self::I) -> &str {
        self.label_store.resolve(id)
    }
}

impl<'store, T, TS, NS, LS> crate::types::TypeStore<T> for SimpleStores<TS, NS, LS>
where
    T: crate::types::Tree,
    T::TreeId: crate::types::NodeId<IdN = T::TreeId>,
    T::Type: 'static + std::hash::Hash,
    TS: TypeStore<T, Ty = T::Type>,
    NS: crate::types::NodeStore<T::TreeId>,
{
    type Ty = TS::Ty;

    const MASK: u16 = TS::MASK;

    fn resolve_type(&self, n: &T) -> Self::Ty {
        self.type_store.resolve_type(n)
    }
    fn resolve_lang(&self, n: &T) -> crate::types::LangWrapper<Self::Ty> {
        self.type_store.resolve_lang(n)
    }

    type Marshaled = TS::Marshaled;

    fn marshal_type(&self, n: &T) -> Self::Marshaled {
        self.type_store.marshal_type(n)
    }
}

pub mod defaults {
    pub type LabelIdentifier = super::labels::DefaultLabelIdentifier;
    pub type LabelValue = super::labels::DefaultLabelValue;
    pub type NodeIdentifier = super::nodes::DefaultNodeIdentifier;
}

impl<'store, TS> From<&'store SimpleStores<TS, nodes::DefaultNodeStore>>
    for SimpleHyperAST<
        self::nodes::HashedNodeRef<'store>,
        &'store TS,
        &'store nodes::DefaultNodeStore,
        &'store labels::LabelStore,
    >
{
    fn from(value: &'store SimpleStores<TS, nodes::DefaultNodeStore>) -> Self {
        Self {
            type_store: &value.type_store,
            node_store: &value.node_store,
            label_store: &value.label_store,
            _phantom: std::marker::PhantomData,
        }
    }
}