hara-native 0.1.8

HAL-free native host runtime and package launcher for Hara
Documentation
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
use std::cell::Cell;
use std::collections::BTreeMap;
use std::rc::Rc;

use crate::lang::hash::JavaHash;
use crate::lang::protocol::{
    HashType, IAssoc, IColl, IConj, ICount, IDisplay, IDissoc, IEmpty, IEquality, IFind, IHash,
    ILookup, IMetadata, IMutable, IObjType, IPersistent, IToMutable, IToPersistent, MetaType,
    ObjType,
};

#[derive(Debug, Clone)]
struct Node<V> {
    children: BTreeMap<char, Rc<Node<V>>>,
    value: Option<V>,
}
impl<V> Default for Node<V> {
    fn default() -> Self {
        Self {
            children: BTreeMap::new(),
            value: None,
        }
    }
}
fn assoc_node<V: Clone>(node: &Rc<Node<V>>, chars: &[char], value: V) -> Rc<Node<V>> {
    if chars.is_empty() {
        return Rc::new(Node {
            children: node.children.clone(),
            value: Some(value),
        });
    }
    let mut children = node.children.clone();
    let child = children
        .get(&chars[0])
        .cloned()
        .unwrap_or_else(|| Rc::new(Node::default()));
    children.insert(chars[0], assoc_node(&child, &chars[1..], value));
    Rc::new(Node {
        children,
        value: node.value.clone(),
    })
}
fn dissoc_node<V: Clone>(node: &Rc<Node<V>>, chars: &[char]) -> Option<Rc<Node<V>>> {
    let mut children = node.children.clone();
    let value = if chars.is_empty() {
        None
    } else {
        let child = children.get(&chars[0])?;
        match dissoc_node(child, &chars[1..]) {
            Some(next) => {
                children.insert(chars[0], next);
            }
            None => {
                children.remove(&chars[0]);
            }
        }
        node.value.clone()
    };
    if value.is_none() && children.is_empty() {
        None
    } else {
        Some(Rc::new(Node { children, value }))
    }
}
#[derive(Debug, Clone)]
pub struct Standard<V> {
    metadata: Option<Rc<crate::lang::data::Metadata>>,
    root: Rc<Node<V>>,
    size: usize,
}
impl<V> Default for Standard<V> {
    fn default() -> Self {
        Self {
            metadata: None,
            root: Rc::new(Node::default()),
            size: 0,
        }
    }
}
impl<V: Clone> Standard<V> {
    pub fn new() -> Self {
        Self::default()
    }
    pub fn len(&self) -> usize {
        self.size
    }
    pub fn is_empty(&self) -> bool {
        self.size == 0
    }
    pub fn get(&self, key: &str) -> Option<&V> {
        let mut node = self.root.as_ref();
        for ch in key.chars() {
            node = node.children.get(&ch)?.as_ref();
        }
        node.value.as_ref()
    }
    pub fn assoc_value(&self, key: impl Into<String>, value: V) -> Self {
        let key = key.into();
        let added = self.get(&key).is_none();
        let chars = key.chars().collect::<Vec<_>>();
        Self {
            metadata: self.metadata.clone(),
            root: assoc_node(&self.root, &chars, value),
            size: self.size + usize::from(added),
        }
    }
    pub fn dissoc_value(&self, key: &str) -> Self {
        if self.get(key).is_none() {
            return self.clone();
        }
        let chars = key.chars().collect::<Vec<_>>();
        Self {
            metadata: self.metadata.clone(),
            root: dissoc_node(&self.root, &chars).unwrap_or_else(|| Rc::new(Node::default())),
            size: self.size - 1,
        }
    }
    pub fn entries(&self) -> Vec<(String, &V)> {
        fn collect<'a, V>(n: &'a Node<V>, prefix: &mut String, out: &mut Vec<(String, &'a V)>) {
            if let Some(v) = &n.value {
                out.push((prefix.clone(), v));
            }
            for (ch, child) in &n.children {
                prefix.push(*ch);
                collect(child, prefix, out);
                prefix.pop();
            }
        }
        let mut out = Vec::with_capacity(self.size);
        collect(&self.root, &mut String::new(), &mut out);
        out
    }
    pub fn iter(&self) -> impl Iterator<Item = String> + '_ {
        self.entries().into_iter().map(|(key, _)| key)
    }
}
impl<V: Clone> ICount for Standard<V> {
    fn count(&self) -> usize {
        self.len()
    }
}
impl<V: Clone> IFind<String> for Standard<V> {
    type Output = (String, V);
    fn find(&self, key: &String) -> Option<Self::Output> {
        self.get(key).map(|v| (key.clone(), v.clone()))
    }
}
impl<V: Clone> ILookup<String, V> for Standard<V> {
    type Keys = std::vec::IntoIter<String>;
    type Values = std::vec::IntoIter<V>;
    fn keys(&self) -> Self::Keys {
        self.iter().collect::<Vec<_>>().into_iter()
    }
    fn vals(&self) -> Self::Values {
        self.entries()
            .into_iter()
            .map(|(_, v)| v.clone())
            .collect::<Vec<_>>()
            .into_iter()
    }
}
impl<V: Clone> IAssoc<String, V> for Standard<V> {
    type Output = Self;
    fn assoc(&self, k: String, v: V) -> Self {
        self.assoc_value(k, v)
    }
}
impl<V: Clone> IDissoc<String> for Standard<V> {
    type Output = Self;
    fn dissoc(&self, k: &String) -> Self {
        self.dissoc_value(k)
    }
}
impl<V: Clone + Default> IConj<String> for Standard<V> {
    type Output = Self;
    fn conj(&self, k: String) -> Self {
        self.assoc_value(k, V::default())
    }
}
impl<V: Clone> IEmpty for Standard<V> {
    type Output = Self;
    fn empty(&self) -> Self {
        Self::new().with_meta(self.metadata.clone())
    }
}
impl<V: Clone> IMetadata for Standard<V> {
    type Metadata = Rc<crate::lang::data::Metadata>;
    fn meta(&self) -> Option<&Self::Metadata> {
        self.metadata.as_ref()
    }
    fn with_meta(&self, metadata: Option<Self::Metadata>) -> Self {
        Self {
            metadata,
            ..self.clone()
        }
    }

    fn metatype(&self) -> MetaType {
        MetaType::Map
    }
}
impl<V: Clone> IPersistent for Standard<V> {}
impl<V: Clone> IntoIterator for Standard<V> {
    type Item = String;
    type IntoIter = std::vec::IntoIter<String>;
    fn into_iter(self) -> Self::IntoIter {
        self.iter().collect::<Vec<_>>().into_iter()
    }
}
impl<V: Clone + PartialEq> IEquality for Standard<V> {
    fn equality(&self, other: &Self) -> bool {
        self.len() == other.len() && self.entries().iter().all(|(k, v)| other.get(k) == Some(*v))
    }
}
impl<V: Clone + std::fmt::Debug> IDisplay for Standard<V> {
    fn display(&self) -> String {
        format!(
            "#{{{}}}",
            self.entries()
                .iter()
                .map(|(k, v)| format!("{k:?} {v:?}"))
                .collect::<Vec<_>>()
                .join(" ")
        )
    }
}
impl<V: Clone + std::hash::Hash + JavaHash> IHash for Standard<V> {
    fn hash_calc(&self, hash_type: HashType) -> u64 {
        // Java Trie.hashCalc override: acc = "::MAP".hashCode(), then
        // acc += hash(key) + hash(value) per entry (NOT the MapEntry
        // composition used by maps). Keys are plain Strings, so they hash
        // via Java String.hashCode under every hash type (see lang::hash).
        let mut acc = crate::lang::hash::hash_seed("MAP") as i64;
        for (k, v) in self.entries() {
            acc = acc
                .wrapping_add(crate::lang::hash::java_string_hash(&k) as i64)
                .wrapping_add(v.java_hash(hash_type));
        }
        acc as u64
    }
}
impl<V: Clone + std::fmt::Debug> IObjType for Standard<V> {
    fn obj_type(&self) -> ObjType {
        ObjType::Map
    }
}
impl<V> IColl<String> for Standard<V>
where
    V: Clone + Default + PartialEq + std::hash::Hash + JavaHash + std::fmt::Debug,
{
    fn start_string(&self) -> &'static str {
        "#{"
    }
    fn end_string(&self) -> &'static str {
        "}"
    }
}
impl<V: Clone> IToMutable for Standard<V> {
    type Mutable = Mutable<V>;
    fn to_mutable(&self) -> Self::Mutable {
        Mutable {
            editable: Cell::new(true),
            trie: self.clone(),
        }
    }
}
#[derive(Debug, Clone)]
pub struct Mutable<V> {
    editable: Cell<bool>,
    trie: Standard<V>,
}
impl<V: Clone> Mutable<V> {
    fn check(&self) {
        assert!(self.editable.get(), "mutable trie used after to_persistent")
    }
    pub fn assoc(&mut self, k: impl Into<String>, v: V) -> &mut Self {
        self.check();
        self.trie = self.trie.assoc_value(k, v);
        self
    }
    pub fn dissoc(&mut self, k: &str) -> &mut Self {
        self.check();
        self.trie = self.trie.dissoc_value(k);
        self
    }
}
impl<V: Clone> std::ops::Deref for Mutable<V> {
    type Target = Standard<V>;
    fn deref(&self) -> &Self::Target {
        self.check();
        &self.trie
    }
}
impl<V> IMutable for Mutable<V> {}
impl<V: Clone> IToPersistent for Mutable<V> {
    type Persistent = Standard<V>;
    fn to_persistent(&mut self) -> Self::Persistent {
        self.check();
        self.editable.set(false);
        self.trie.clone()
    }
}
#[cfg(test)]
mod tests {
    use super::Standard;
    #[test]
    fn updates_empty_and_mutable_preserve_metadata() {
        use crate::lang::protocol::{IEmpty, IMetadata, IToMutable, IToPersistent};
        let trie = Standard::new()
            .assoc_value("cat", 1)
            .with_meta(Some(crate::lang::data::Metadata::document("doc")));
        assert_eq!(
            trie.assoc_value("car", 2).meta().map(|m| m.doc().unwrap()),
            Some("doc")
        );
        assert_eq!(
            trie.dissoc_value("cat").meta().map(|m| m.doc().unwrap()),
            Some("doc")
        );
        assert_eq!(trie.empty().meta().map(|m| m.doc().unwrap()), Some("doc"));
        let mut mutable = trie.to_mutable();
        mutable.assoc("car", 2);
        assert_eq!(
            mutable.to_persistent().meta().map(|m| m.doc().unwrap()),
            Some("doc")
        );
    }

    #[test]
    fn shares_prefixes_and_iterates_lexically() {
        let a = Standard::new()
            .assoc_value("car", 1)
            .assoc_value("cat", 2)
            .assoc_value("dog", 3);
        assert_eq!(a.iter().collect::<Vec<_>>(), vec!["car", "cat", "dog"]);
        let b = a.dissoc_value("cat");
        assert_eq!(b.get("cat"), None);
        assert_eq!(a.get("cat"), Some(&2));
        assert_eq!(b.get("car"), Some(&1));
    }

    #[test]
    fn dissoc_prunes_childless_ancestors_bottom_up() {
        // Java dissocHelper: a node is removed once it is non-terminal and
        // childless, and the removal cascades up the spine.
        let trie = Standard::new()
            .assoc_value("cat", 1)
            .assoc_value("cats", 2)
            .assoc_value("car", 3);
        // removing the leaf word prunes the 's' node; "cat" stays terminal
        let trie = trie.dissoc_value("cats");
        assert_eq!(trie.get("cats"), None);
        assert_eq!(trie.get("cat"), Some(&1));
        // removing "car" prunes the 'r' node but keeps the "cat" spine
        let trie = trie.dissoc_value("car");
        assert_eq!(trie.get("car"), None);
        assert_eq!(trie.entries().len(), 1);
        // removing the last word prunes the whole spine back to an empty root
        let trie = trie.dissoc_value("cat");
        assert_eq!(trie.len(), 0);
        assert!(trie.root.children.is_empty());
        assert!(trie.root.value.is_none());
    }

    #[test]
    fn dissoc_prefix_clears_terminal_but_keeps_children() {
        // Java: dissoc of a word that is a prefix of another word only
        // clears the terminal flag/value; the subtree is untouched.
        let trie = Standard::new()
            .assoc_value("cat", 1)
            .assoc_value("cats", 2)
            .dissoc_value("cat");
        assert_eq!(trie.get("cat"), None);
        assert_eq!(trie.get("cats"), Some(&2));
        assert_eq!(trie.len(), 1);
        assert_eq!(trie.iter().collect::<Vec<_>>(), vec!["cats"]);
    }

    #[test]
    fn empty_string_key_sets_the_root_terminal() {
        // Java assoc("") walks zero chars and marks the root node terminal;
        // iteration yields "" first (it is a prefix of every key).
        let trie = Standard::new().assoc_value("", 7).assoc_value("a", 1);
        assert_eq!(trie.get(""), Some(&7));
        assert_eq!(trie.len(), 2);
        assert_eq!(trie.iter().collect::<Vec<_>>(), vec!["", "a"]);
        // dissoc("") clears the root terminal but keeps the children
        let trie = trie.dissoc_value("");
        assert_eq!(trie.get(""), None);
        assert_eq!(trie.get("a"), Some(&1));
        assert_eq!(trie.len(), 1);
    }

    #[test]
    fn dissoc_absent_prefix_or_divergent_key_is_a_noop() {
        let trie = Standard::new().assoc_value("cat", 1);
        for key in ["cow", "ca", "cats", "dog"] {
            let next = trie.dissoc_value(key);
            assert_eq!(next.len(), 1);
            assert_eq!(next.get("cat"), Some(&1));
        }
    }
}