MCPcopy Create free account
hub / github.com/bytecodealliance/wasmtime / verify_tree

Method verify_tree

cranelift/bforest/src/pool.rs:82–206  ·  view source on GitHub ↗

Verify the consistency of the tree rooted at `node`.

(&self, node: Node, comp: &C)

Source from the content-addressed store, hash-verified

80impl<F: Forest> NodePool<F> {
81 /// Verify the consistency of the tree rooted at `node`.
82 pub fn verify_tree<C: Comparator<F::Key>>(&self, node: Node, comp: &C)
83 where
84 NodeData<F>: fmt::Display,
85 F::Key: fmt::Display,
86 {
87 use crate::entity::EntitySet;
88 use alloc::vec::Vec;
89 use core::borrow::Borrow;
90 use core::cmp::Ordering;
91
92 // The root node can't be an inner node with just a single sub-tree. It should have been
93 // pruned.
94 if let NodeData::Inner { size, .. } = self[node] {
95 assert!(size > 0, "Root must have more than one sub-tree");
96 }
97
98 let mut done = match self[node] {
99 NodeData::Inner { size, .. } | NodeData::Leaf { size, .. } => {
100 EntitySet::with_capacity(size.into())
101 }
102 _ => EntitySet::new(),
103 };
104
105 let mut todo = Vec::new();
106
107 // Todo-list entries are:
108 // 1. Optional LHS key which must be <= all node entries.
109 // 2. The node reference.
110 // 3. Optional RHS key which must be > all node entries.
111 todo.push((None, node, None));
112
113 while let Some((lkey, node, rkey)) = todo.pop() {
114 assert!(done.insert(node), "Node appears more than once in tree");
115 let mut lower = lkey;
116
117 match self[node] {
118 NodeData::Inner { size, keys, tree } => {
119 let size = size as usize;
120 let capacity = tree.len();
121 let keys = &keys[0..size];
122
123 // Verify occupancy.
124 // Right-most nodes can be small, but others must be at least half full.
125 assert!(
126 rkey.is_none() || (size + 1) * 2 >= capacity,
127 "Only {}/{} entries in {}:{}, upper={}",
128 size + 1,
129 capacity,
130 node,
131 self[node],
132 rkey.unwrap()
133 );
134
135 // Queue up the sub-trees, checking for duplicates.
136 for i in 0..size + 1 {
137 // Get an upper bound for node[i].
138 let upper = keys.get(i).cloned().or(rkey);
139

Callers 2

verifyMethod · 0.80
verifyMethod · 0.80

Calls 7

newFunction · 0.50
pushMethod · 0.45
popMethod · 0.45
lenMethod · 0.45
orMethod · 0.45
getMethod · 0.45
borrowMethod · 0.45

Tested by

no test coverage detected