#tree-traversal #tree #traversal #structure #annotations #data

无std 微开尔文

一个用于遍历注释数据结构的树遍历的crate

34个版本 (13个破坏性版本)

0.17.0 2022年10月19日
0.16.0-rkyv.32022年6月8日
0.15.0-rc.12022年2月24日
0.12.0-rc.5 2021年12月14日
0.5.3 2020年11月17日

#365数据结构

Download history 42/week @ 2024-04-20 15/week @ 2024-04-27 16/week @ 2024-05-04 22/week @ 2024-05-11 32/week @ 2024-05-18 29/week @ 2024-05-25 25/week @ 2024-06-01 17/week @ 2024-06-08 19/week @ 2024-06-15 21/week @ 2024-06-22 15/week @ 2024-06-29 8/week @ 2024-07-06 3/week @ 2024-07-13 87/week @ 2024-07-27 2/week @ 2024-08-03

每月92次下载
5 个crate中使用了(4个直接使用)

MPL-2.0 许可证

34KB
801

微开尔文

Repository Build Status codecov Documentation

用于创建和遍历递归注释结构的crate。

复合特质

/// A type that can recursively contain itself and leaves.
pub trait Compound<A>: Sized {
    /// The leaf type of the compound collection
    type Leaf;

    /// Returns a reference to a possible child at specified index
    fn child(&self, index: usize) -> Child<Self, A>;

    /// Returns a mutable reference to a possible child at specified index
    fn child_mut(&mut self, index: usize) -> ChildMut<Self, A>;
}

Compound特质定义了一个类型为集合类型。这意味着它可以被搜索,并且可以构建指向其元素的分支。

分支遍历

可以为以用户定义的方式遍历树实现Walker特质。以下是一个示例,即内部使用的AllLeaves - 一个实现

/// Walker that visits all leaves
pub struct AllLeaves;

impl<C, A> Walker<C, A> for AllLeaves
where
    C: Compound<A>,
{
    fn walk(&mut self, walk: Walk<C, A>) -> Step {
        for i in 0.. {
            match walk.child(i) {
                Child::Leaf(_) => return Step::Found(i),
                Child::Node(_) => return Step::Into(i),
                Child::Empty => (),
                Child::EndOfNode => return Step::Advance,
            }
        }
        unreachable!()
    }
}

用法

请查看nstack对栈/向量类型的实现,以获取更高级的示例。

依赖

~10KB