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
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
//
// Copyright (c) DUSK NETWORK. All rights reserved.

use core::borrow::Borrow;
use core::marker::PhantomData;

use canonical::{Canon, Store};

use crate::annotation::{Annotated, Annotation, Cardinality};
use crate::branch::{Branch, Step, Walk};
use crate::branch_mut::{BranchMut, StepMut, WalkMut};

/// The response of the `child` method on a `Compound` node.
pub enum Child<'a, C, S>
where
    C: Compound<S>,
    S: Store,
{
    /// Child is a leaf
    Leaf(&'a C::Leaf),
    /// Child is an annotated subtree node
    Node(&'a Annotated<C, S>),
    /// No more children
    EndOfNode,
}

/// The response of the `child_mut` method on a `Compound` node.
pub enum ChildMut<'a, C, S>
where
    C: Compound<S>,
    S: Store,
{
    /// Child is a leaf
    Leaf(&'a mut C::Leaf),
    /// Child is an annotated node
    Node(&'a mut Annotated<C, S>),
    /// No more children
    EndOfNode,
}

/// Trait for compound datastructures
pub trait Compound<S>
where
    Self: Canon<S>,
    S: Store,
{
    /// The leaf type of the collection
    type Leaf;

    /// The annotation type of the connection
    type Annotation;

    /// Returns a reference to a possible child at specified offset
    fn child(&self, ofs: usize) -> Child<Self, S>;
    /// Returns an iterator over all the available offsets for this compound
    fn child_iter(&self) -> ChildIterator<Self, S> {
        self.into()
    }
    /// Returns a mutable reference to a possible child at specified offset
    fn child_mut(&mut self, ofs: usize) -> ChildMut<Self, S>;
}

pub struct ChildIterator<'a, C, S>
where
    C: Compound<S>,
    S: Store,
{
    ofs: usize,
    compound: &'a C,
    store: PhantomData<S>,
}

impl<'a, C, S> From<&'a C> for ChildIterator<'a, C, S>
where
    C: Compound<S>,
    S: Store,
{
    fn from(c: &C) -> ChildIterator<C, S> {
        ChildIterator {
            ofs: 0,
            compound: c,
            store: PhantomData,
        }
    }
}

impl<'a, C, S> Iterator for ChildIterator<'a, C, S>
where
    C: Compound<S>,
    S: Store,
{
    type Item = Child<'a, C, S>;

    fn next(&mut self) -> Option<Self::Item> {
        let c = self.compound.child(self.ofs);
        self.ofs += 1;

        match c {
            Child::EndOfNode => None,
            _ => Some(c),
        }
    }
}

/// Find the nth element of any collection satisfying the given annotation
/// constraints
pub trait Nth<'a, S>
where
    Self: Compound<S> + Sized,
    Self::Annotation: Annotation<Self, S>,
    S: Store,
{
    /// Construct a `Branch` pointing to the `nth` element, if any
    fn nth<const N: usize>(
        &'a self,
        n: u64,
    ) -> Result<Option<Branch<'a, Self, S, N>>, S::Error>;

    /// Construct a `BranchMut` pointing to the `nth` element, if any
    fn nth_mut<const N: usize>(
        &'a mut self,
        n: u64,
    ) -> Result<Option<BranchMut<'a, Self, S, N>>, S::Error>;
}

impl<'a, C, S> Nth<'a, S> for C
where
    C: Compound<S>,
    C::Annotation: Annotation<C, S> + Borrow<Cardinality>,
    S: Store,
{
    fn nth<const N: usize>(
        &'a self,
        mut index: u64,
    ) -> Result<Option<Branch<'a, Self, S, N>>, S::Error> {
        Branch::walk(self, |f| match f {
            Walk::Leaf(l) => {
                if index == 0 {
                    Step::Found(l)
                } else {
                    index -= 1;
                    Step::Next
                }
            }
            Walk::Node(n) => {
                let &Cardinality(card) = n.annotation().borrow();
                if card <= index {
                    index -= card;
                    Step::Next
                } else {
                    Step::Into(n)
                }
            }
        })
    }

    fn nth_mut<const N: usize>(
        &'a mut self,
        mut index: u64,
    ) -> Result<Option<BranchMut<'a, Self, S, N>>, S::Error> {
        BranchMut::walk(self, |f| match f {
            WalkMut::Leaf(l) => {
                if index == 0 {
                    StepMut::Found(l)
                } else {
                    index -= 1;
                    StepMut::Next
                }
            }
            WalkMut::Node(n) => {
                let &Cardinality(card) = n.annotation().borrow();
                if card <= index {
                    index -= card;
                    StepMut::Next
                } else {
                    StepMut::Into(n)
                }
            }
        })
    }
}