tabnas-alchemy 0.3.1

The alchemy grammar plugin and language for the tabnas parsing engine (declarative streaming transducers and renderers), and the types tabnas transducers and renderers share.
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
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
//! A persistent vector: the items in leaves of [`LEAF`] under a trie, and
//! the last up to [`LEAF`] of them in a tail, as Clojure's vector keeps
//! them. A push copies the tail, and once in [`LEAF`] pushes the path to
//! the leaf the full tail becomes, so it costs a bounded amount whatever
//! the vector's length, and the vector it pushed onto is unchanged and
//! shares every leaf with the new one. Every node keeps the [`Summary`] of
//! the items under it once something has asked for it, so a walk that sums
//! the items reads a leaf's sum instead of visiting its items again, and a
//! vector nothing sums costs nothing for it.

use std::fmt;
use std::sync::{Arc, OnceLock};

const BITS: u32 = 5;
/// The items in a leaf, and the most the tail holds.
pub const LEAF: usize = 1 << BITS;
const MASK: usize = LEAF - 1;

/// What a vector keeps about the items under each node.
pub trait Summary: Copy + Default + Send + Sync {
    type Item;
    /// One item's summary.
    fn of(item: &Self::Item) -> Self;
    /// Two summaries as one.
    fn join(self, other: Self) -> Self;
}

/// A vector whose every version shares what it has in common with the
/// others.
pub struct PVec<T, S> {
    /// The items before the tail, when there are any: whole leaves.
    trie: Option<Arc<Trie<T, S>>>,
    /// The last items, at least one unless the vector is empty, at most
    /// [`LEAF`].
    tail: Arc<[T]>,
}

struct Trie<T, S> {
    /// The items under the root, a multiple of [`LEAF`].
    len: usize,
    /// [`BITS`] for each level of branches above the leaves.
    shift: u32,
    root: Node<T, S>,
}

/// A node and, once asked for, the summary of the items under it, which
/// a copy of the node made after that keeps.
struct Node<T, S> {
    kind: Kind<T, S>,
    summary: OnceLock<S>,
}

enum Kind<T, S> {
    Branch(Arc<[Node<T, S>]>),
    Leaf(Arc<[T]>),
}

impl<T, S: Copy> Clone for Node<T, S> {
    fn clone(&self) -> Self {
        let kind = match &self.kind {
            Kind::Branch(children) => Kind::Branch(children.clone()),
            Kind::Leaf(items) => Kind::Leaf(items.clone()),
        };
        Node {
            kind,
            summary: self.summary.clone(),
        }
    }
}

impl<T, S> Clone for PVec<T, S> {
    fn clone(&self) -> Self {
        PVec {
            trie: self.trie.clone(),
            tail: self.tail.clone(),
        }
    }
}

impl<T: Clone, S: Summary<Item = T>> Node<T, S> {
    fn summary(&self) -> S {
        *self.summary.get_or_init(|| match &self.kind {
            Kind::Branch(children) => children
                .iter()
                .fold(S::default(), |sum, child| sum.join(child.summary())),
            Kind::Leaf(items) => items
                .iter()
                .fold(S::default(), |sum, item| sum.join(S::of(item))),
        })
    }

    fn leaf(items: Arc<[T]>) -> Self {
        Node {
            kind: Kind::Leaf(items),
            summary: OnceLock::new(),
        }
    }

    fn branch(children: Vec<Node<T, S>>) -> Self {
        Node {
            kind: Kind::Branch(children.into()),
            summary: OnceLock::new(),
        }
    }

    /// `leaf` under branches down from a node at `shift`.
    fn path(leaf: Node<T, S>, shift: u32) -> Self {
        if shift == 0 {
            leaf
        } else {
            Node::branch(vec![Node::path(leaf, shift - BITS)])
        }
    }

    /// This branch, at `shift`, with `leaf` as the leaf of item `index`,
    /// the first after every item under it.
    fn with_leaf(&self, shift: u32, index: usize, leaf: Node<T, S>) -> Self {
        let Kind::Branch(children) = &self.kind else {
            unreachable!("a leaf has no room for another");
        };
        let slot = (index >> shift) & MASK;
        let mut kids = children.to_vec();
        if shift == BITS {
            kids.push(leaf);
        } else if slot < kids.len() {
            kids[slot] = kids[slot].with_leaf(shift - BITS, index, leaf);
        } else {
            kids.push(Node::path(leaf, shift - BITS));
        }
        Node::branch(kids)
    }

    /// This node, at `shift`, without its last leaf, the leaf of item
    /// `index`, and that leaf's items; `None` for a node left empty.
    fn without_last(&self, shift: u32, index: usize) -> (Option<Self>, Arc<[T]>) {
        match &self.kind {
            Kind::Leaf(items) => (None, items.clone()),
            Kind::Branch(children) => {
                let slot = (index >> shift) & MASK;
                let (child, items) = children[slot].without_last(shift - BITS, index);
                let mut kids = children[..slot].to_vec();
                kids.extend(child);
                let node = (!kids.is_empty()).then(|| Node::branch(kids));
                (node, items)
            }
        }
    }
}

impl<T, S> Trie<T, S> {
    /// The leaf holding item `index`, which is under the root.
    fn leaf(&self, index: usize) -> &[T] {
        let mut node = &self.root;
        let mut shift = self.shift;
        loop {
            match &node.kind {
                Kind::Leaf(items) => return items,
                Kind::Branch(children) => {
                    node = &children[(index >> shift) & MASK];
                    shift -= BITS;
                }
            }
        }
    }
}

impl<T: Clone, S: Summary<Item = T>> Trie<T, S> {
    fn with_leaf(&self, leaf: Node<T, S>) -> Self {
        let len = self.len + LEAF;
        if self.len == 1 << (self.shift + BITS) {
            // The root is full: a root above it and the new leaf's path.
            let root = Node::branch(vec![self.root.clone(), Node::path(leaf, self.shift)]);
            Trie {
                len,
                shift: self.shift + BITS,
                root,
            }
        } else {
            Trie {
                len,
                shift: self.shift,
                root: self.root.with_leaf(self.shift, self.len, leaf),
            }
        }
    }

    /// The trie without its last leaf, `None` when that was its only one,
    /// and the leaf's items.
    fn without_last(&self) -> (Option<Self>, Arc<[T]>) {
        let (root, items) = self.root.without_last(self.shift, self.len - LEAF);
        let Some(root) = root else {
            return (None, items);
        };
        // A root left with one child is that child, a level lower.
        let (root, shift) = match &root.kind {
            Kind::Branch(children) if children.len() == 1 => {
                (children[0].clone(), self.shift - BITS)
            }
            _ => (root, self.shift),
        };
        let trie = Trie {
            len: self.len - LEAF,
            shift,
            root,
        };
        (Some(trie), items)
    }
}

impl<T, S> PVec<T, S> {
    fn trie_len(&self) -> usize {
        self.trie.as_ref().map_or(0, |trie| trie.len)
    }
    /// Leaf `n` of the trie, or the tail after the last.
    fn chunk_at(&self, n: usize) -> &[T] {
        match self.trie.as_deref() {
            Some(trie) if n * LEAF < trie.len => trie.leaf(n * LEAF),
            _ => &self.tail,
        }
    }
}

impl<T: Clone, S: Summary<Item = T>> Default for PVec<T, S> {
    fn default() -> Self {
        PVec::new()
    }
}

impl<T: Clone, S: Summary<Item = T>> PVec<T, S> {
    pub fn new() -> Self {
        PVec {
            trie: None,
            tail: Arc::from(Vec::new()),
        }
    }

    pub fn len(&self) -> usize {
        self.trie_len() + self.tail.len()
    }

    pub fn is_empty(&self) -> bool {
        self.tail.is_empty()
    }

    pub fn get(&self, index: usize) -> Option<&T> {
        let before = self.trie_len();
        if index >= before {
            return self.tail.get(index - before);
        }
        let trie = self.trie.as_deref()?;
        trie.leaf(index).get(index & MASK)
    }

    pub fn first(&self) -> Option<&T> {
        self.get(0)
    }

    pub fn last(&self) -> Option<&T> {
        self.tail.last()
    }

    /// The items, when they are all in the tail, as a vector of at most
    /// [`LEAF`] items has them.
    pub fn as_slice(&self) -> Option<&[T]> {
        self.trie.is_none().then_some(&*self.tail)
    }

    /// This vector with `item` after its last.
    pub fn push(&self, item: T) -> Self {
        if self.tail.len() < LEAF {
            let mut tail = Vec::with_capacity(self.tail.len() + 1);
            tail.extend_from_slice(&self.tail);
            tail.push(item);
            return PVec {
                trie: self.trie.clone(),
                tail: tail.into(),
            };
        }
        let leaf = Node::leaf(self.tail.clone());
        let trie = match self.trie.as_deref() {
            None => Trie {
                len: LEAF,
                shift: 0,
                root: leaf,
            },
            Some(trie) => trie.with_leaf(leaf),
        };
        PVec {
            trie: Some(Arc::new(trie)),
            tail: Arc::from(vec![item]),
        }
    }

    /// This vector without its last item; `None` when it is empty.
    pub fn pop(&self) -> Option<Self> {
        match self.tail.len() {
            0 => None,
            1 => Some(match self.trie.as_deref() {
                None => PVec::new(),
                Some(trie) => {
                    let (trie, tail) = trie.without_last();
                    PVec {
                        trie: trie.map(Arc::new),
                        tail,
                    }
                }
            }),
            n => Some(PVec {
                trie: self.trie.clone(),
                tail: Arc::from(&self.tail[..n - 1]),
            }),
        }
    }

    /// The summary of the items before the tail, which every node keeps;
    /// `None` when there are none.
    pub fn trie_summary(&self) -> Option<S> {
        self.trie.as_ref().map(|trie| trie.root.summary())
    }

    /// The last items, those [`PVec::trie_summary`] does not sum.
    pub fn tail(&self) -> &[T] {
        &self.tail
    }

    /// The summary of every item.
    pub fn summary(&self) -> S {
        self.tail
            .iter()
            .fold(self.trie_summary().unwrap_or_default(), |sum, item| {
                sum.join(S::of(item))
            })
    }

    pub fn iter(&self) -> Iter<'_, T, S> {
        Iter(match self.as_slice() {
            Some(items) => Items::Slice(items.iter()),
            None => Items::Chunked {
                items: self.chunks().flatten(),
                left: self.len(),
            },
        })
    }

    /// The items a leaf at a time, in order, the tail last: what a walk
    /// over every item reads as slices.
    pub fn chunks(&self) -> Chunks<'_, T, S> {
        Chunks {
            vec: self,
            front: 0,
            back: self.trie_len() / LEAF + 1,
        }
    }

    /// Whether the two are one vector, not merely equal.
    pub fn ptr_eq(&self, other: &Self) -> bool {
        Arc::ptr_eq(&self.tail, &other.tail)
            && match (&self.trie, &other.trie) {
                (None, None) => true,
                (Some(a), Some(b)) => Arc::ptr_eq(a, b),
                _ => false,
            }
    }

    pub fn to_vec(&self) -> Vec<T> {
        self.iter().cloned().collect()
    }
}

impl<T: Clone, S: Summary<Item = T>> From<Vec<T>> for PVec<T, S> {
    /// The items moved into leaves, the last up to [`LEAF`] of them, full
    /// or not, the tail.
    fn from(items: Vec<T>) -> Self {
        if items.len() <= LEAF {
            return PVec {
                trie: None,
                tail: Arc::from(items),
            };
        }
        let in_tail = match items.len() % LEAF {
            0 => LEAF,
            rest => rest,
        };
        let leaves = (items.len() - in_tail) / LEAF;
        let mut items = items.into_iter();
        let mut vec = PVec::new();
        for _ in 0..leaves {
            let leaf = Node::leaf(items.by_ref().take(LEAF).collect());
            let trie = match vec.trie.as_deref() {
                None => Trie {
                    len: LEAF,
                    shift: 0,
                    root: leaf,
                },
                Some(trie) => trie.with_leaf(leaf),
            };
            vec.trie = Some(Arc::new(trie));
        }
        vec.tail = items.collect();
        vec
    }
}

impl<T: Clone, S: Summary<Item = T>> std::ops::Index<usize> for PVec<T, S> {
    type Output = T;

    /// Item `index`; past the last item it panics, as a slice's does.
    fn index(&self, index: usize) -> &T {
        match self.get(index) {
            Some(item) => item,
            None => panic!(
                "index {index} is past the end of a vector of {} items",
                self.len()
            ),
        }
    }
}

impl<T: Clone, S: Summary<Item = T>> FromIterator<T> for PVec<T, S> {
    /// The items in a tail of their own when there are at most [`LEAF`]
    /// of them and the iterator knows how many, as most are: one
    /// allocation, as `Arc<[T]>` collects them.
    fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Self {
        let iter = iter.into_iter();
        match iter.size_hint() {
            (n, Some(m)) if n == m && n <= LEAF => PVec {
                trie: None,
                tail: iter.collect(),
            },
            _ => PVec::from(iter.collect::<Vec<T>>()),
        }
    }
}

impl<T: Clone + PartialEq, S: Summary<Item = T>> PartialEq for PVec<T, S> {
    fn eq(&self, other: &Self) -> bool {
        if let (Some(a), Some(b)) = (self.as_slice(), other.as_slice()) {
            return a == b;
        }
        self.len() == other.len() && self.iter().eq(other.iter())
    }
}

impl<T: Clone + fmt::Debug, S: Summary<Item = T>> fmt::Debug for PVec<T, S> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_list().entries(self.iter()).finish()
    }
}

impl<'v, T: Clone, S: Summary<Item = T>> IntoIterator for &'v PVec<T, S> {
    type Item = &'v T;
    type IntoIter = Iter<'v, T, S>;

    fn into_iter(self) -> Iter<'v, T, S> {
        self.iter()
    }
}

/// The leaves of a [`PVec`] in order, then its tail, each a slice.
pub struct Chunks<'v, T, S> {
    vec: &'v PVec<T, S>,
    /// The next chunk from the front, and the one after the next from
    /// the back, the tail numbered after the last leaf.
    front: usize,
    back: usize,
}

impl<'v, T, S> Iterator for Chunks<'v, T, S> {
    type Item = &'v [T];

    fn next(&mut self) -> Option<&'v [T]> {
        if self.front >= self.back {
            return None;
        }
        self.front += 1;
        Some(self.vec.chunk_at(self.front - 1))
    }

    fn size_hint(&self) -> (usize, Option<usize>) {
        let n = self.back - self.front;
        (n, Some(n))
    }
}

impl<T, S> DoubleEndedIterator for Chunks<'_, T, S> {
    fn next_back(&mut self) -> Option<Self::Item> {
        if self.front >= self.back {
            return None;
        }
        self.back -= 1;
        Some(self.vec.chunk_at(self.back))
    }
}

/// The items of a [`PVec`] in order: its chunks' items, each chunk read
/// as a slice.
pub struct Iter<'v, T, S>(Items<'v, T, S>);

enum Items<'v, T, S> {
    /// A vector all in its tail, as most are.
    Slice(std::slice::Iter<'v, T>),
    Chunked {
        items: std::iter::Flatten<Chunks<'v, T, S>>,
        /// The items not yet given from either end.
        left: usize,
    },
}

impl<'v, T, S> Iterator for Iter<'v, T, S> {
    type Item = &'v T;

    fn next(&mut self) -> Option<&'v T> {
        match &mut self.0 {
            Items::Slice(items) => items.next(),
            Items::Chunked { items, left } => {
                let item = items.next()?;
                *left -= 1;
                Some(item)
            }
        }
    }

    fn size_hint(&self) -> (usize, Option<usize>) {
        match &self.0 {
            Items::Slice(items) => items.size_hint(),
            Items::Chunked { left, .. } => (*left, Some(*left)),
        }
    }
}

impl<T, S> DoubleEndedIterator for Iter<'_, T, S> {
    fn next_back(&mut self) -> Option<Self::Item> {
        match &mut self.0 {
            Items::Slice(items) => items.next_back(),
            Items::Chunked { items, left } => {
                let item = items.next_back()?;
                *left -= 1;
                Some(item)
            }
        }
    }
}

impl<T, S> ExactSizeIterator for Iter<'_, T, S> {}

#[cfg(test)]
mod tests {
    use super::*;

    /// How many items, their sum and the largest.
    #[derive(Clone, Copy, Debug, Default, PartialEq)]
    struct Sum {
        count: u64,
        total: u64,
        max: u64,
    }

    impl Summary for Sum {
        type Item = u64;
        fn of(item: &u64) -> Self {
            Sum {
                count: 1,
                total: *item,
                max: *item,
            }
        }
        fn join(self, other: Self) -> Self {
            Sum {
                count: self.count + other.count,
                total: self.total + other.total,
                max: self.max.max(other.max),
            }
        }
    }

    type V = PVec<u64, Sum>;

    fn check(vec: &V, model: &[u64]) {
        assert_eq!(vec.len(), model.len());
        assert_eq!(vec.is_empty(), model.is_empty());
        assert_eq!(vec.to_vec(), model);
        assert_eq!(
            vec.iter().rev().copied().collect::<Vec<_>>(),
            model.iter().rev().copied().collect::<Vec<_>>()
        );
        for (i, item) in model.iter().enumerate() {
            assert_eq!(vec.get(i), Some(item), "item {i} of {}", model.len());
            assert_eq!(&vec[i], item);
        }
        assert_eq!(vec.get(model.len()), None);
        assert_eq!(vec.last(), model.last());
        assert_eq!(vec.first(), model.first());
        let want = model.iter().fold(Sum::default(), |s, i| s.join(Sum::of(i)));
        assert_eq!(vec.summary(), want, "the summary of {} items", model.len());
        assert_eq!(vec.as_slice().is_some(), model.len() <= LEAF);
        // The chunks are the items in order, every leaf full.
        let chunks: Vec<&[u64]> = vec.chunks().collect();
        assert_eq!(chunks.concat(), *model);
        assert!(chunks[..chunks.len() - 1].iter().all(|c| c.len() == LEAF));
    }

    #[test]
    fn pushes_and_pops_agree_with_a_vec_through_every_level() {
        // Past a root of leaves (1,024) and a root of those (32,768).
        let mut vec = V::new();
        let mut model = Vec::new();
        let mut versions = Vec::new();
        for i in 0..40_000u64 {
            vec = vec.push(i * 7 % 1_000);
            model.push(i * 7 % 1_000);
            if i % 997 == 0 || (31..34).contains(&(i % 1024)) {
                check(&vec, &model);
                versions.push((vec.clone(), model.clone()));
            }
        }
        check(&vec, &model);
        while let Some(shorter) = vec.pop() {
            model.pop();
            vec = shorter;
            if model.len() % 991 == 0 || (31..34).contains(&(model.len() % 1024)) {
                check(&vec, &model);
            }
        }
        assert!(model.is_empty() && vec.is_empty());
        assert!(vec.pop().is_none());
        // Every version kept is as it was: a push or a pop made a new one.
        for (version, model) in &versions {
            check(version, model);
        }
    }

    #[test]
    fn a_vec_built_at_once_is_the_one_pushed() {
        for n in [
            0usize, 1, 31, 32, 33, 64, 65, 1023, 1024, 1025, 1056, 2049, 33_000,
        ] {
            let model: Vec<u64> = (0..n as u64).collect();
            let built = V::from(model.clone());
            check(&built, &model);
            let pushed = model.iter().fold(V::new(), |v, i| v.push(*i));
            assert!(built == pushed, "{n}");
            // And either pops back to the empty vector.
            let mut v = built;
            for _ in 0..n {
                v = v.pop().unwrap();
            }
            assert!(v.is_empty());
        }
    }

    #[test]
    fn a_push_onto_an_old_version_leaves_the_new_one_alone() {
        let base: V = (0..100u64).collect();
        let a = base.push(1);
        let b = base.push(2);
        assert_eq!(a.last(), Some(&1));
        assert_eq!(b.last(), Some(&2));
        assert_eq!(base.len(), 100);
        assert!(a.pop().unwrap() == base && b.pop().unwrap() == base);
        assert!(!a.ptr_eq(&b) && base.ptr_eq(&base.clone()));
    }
}