Struct fork_tree::ForkTree [−][src]
A tree data structure that stores several nodes across multiple branches. Top-level branches are called roots. The tree has functionality for finalizing nodes, which means that that node is traversed, and all competing branches are pruned. It also guarantees that nodes in the tree are finalized in order. Each node is uniquely identified by its hash but can be ordered by its number. In order to build the tree an external function must be provided when interacting with the tree to establish a node’s ancestry.
Implementations
impl<H, N, V> ForkTree<H, N, V> where
H: PartialEq + Clone,
N: Ord + Clone,
V: Clone,
[src]
H: PartialEq + Clone,
N: Ord + Clone,
V: Clone,
pub fn prune<F, E, P>(
&mut self,
hash: &H,
number: &N,
is_descendent_of: &F,
predicate: &P
) -> Result<impl Iterator<Item = (H, N, V)>, Error<E>> where
E: Error,
F: Fn(&H, &H) -> Result<bool, E>,
P: Fn(&V) -> bool,
[src]
&mut self,
hash: &H,
number: &N,
is_descendent_of: &F,
predicate: &P
) -> Result<impl Iterator<Item = (H, N, V)>, Error<E>> where
E: Error,
F: Fn(&H, &H) -> Result<bool, E>,
P: Fn(&V) -> bool,
Prune the tree, removing all non-canonical nodes. We find the node in the
tree that is the deepest ancestor of the given hash and that passes the
given predicate. If such a node exists, we re-root the tree to this
node. Otherwise the tree remains unchanged. The given function
is_descendent_of
should return true
if the second hash (target) is a
descendent of the first hash (base).
Returns all pruned node data.
impl<H, N, V> ForkTree<H, N, V> where
H: PartialEq,
N: Ord,
[src]
H: PartialEq,
N: Ord,
pub fn new() -> ForkTree<H, N, V>
[src]
Create a new empty tree.
pub fn rebalance(&mut self)
[src]
Rebalance the tree, i.e. sort child nodes by max branch depth (decreasing).
Most operations in the tree are performed with depth-first search starting from the leftmost node at every level, since this tree is meant to be used in a blockchain context, a good heuristic is that the node we’ll be looking for at any point will likely be in one of the deepest chains (i.e. the longest ones).
pub fn import<F, E>(
&mut self,
hash: H,
number: N,
data: V,
is_descendent_of: &F
) -> Result<bool, Error<E>> where
E: Error,
F: Fn(&H, &H) -> Result<bool, E>,
[src]
&mut self,
hash: H,
number: N,
data: V,
is_descendent_of: &F
) -> Result<bool, Error<E>> where
E: Error,
F: Fn(&H, &H) -> Result<bool, E>,
Import a new node into the tree. The given function is_descendent_of
should return true
if the second hash (target) is a descendent of the
first hash (base). This method assumes that nodes in the same branch are
imported in order.
Returns true
if the imported node is a root.
pub fn roots(&self) -> impl Iterator<Item = (&H, &N, &V)>
[src]
Iterates over the existing roots in the tree.
pub fn iter(&self) -> impl Iterator<Item = (&H, &N, &V)>
[src]
Iterates the nodes in the tree in pre-order.
pub fn find_node_where<F, E, P>(
&self,
hash: &H,
number: &N,
is_descendent_of: &F,
predicate: &P
) -> Result<Option<&Node<H, N, V>>, Error<E>> where
E: Error,
F: Fn(&H, &H) -> Result<bool, E>,
P: Fn(&V) -> bool,
[src]
&self,
hash: &H,
number: &N,
is_descendent_of: &F,
predicate: &P
) -> Result<Option<&Node<H, N, V>>, Error<E>> where
E: Error,
F: Fn(&H, &H) -> Result<bool, E>,
P: Fn(&V) -> bool,
Find a node in the tree that is the deepest ancestor of the given
block hash and which passes the given predicate. The given function
is_descendent_of
should return true
if the second hash (target)
is a descendent of the first hash (base).
pub fn map<VT, F>(self, f: &mut F) -> ForkTree<H, N, VT> where
F: FnMut(&H, &N, V) -> VT,
[src]
F: FnMut(&H, &N, V) -> VT,
Map fork tree into values of new types.
pub fn find_node_where_mut<F, E, P>(
&mut self,
hash: &H,
number: &N,
is_descendent_of: &F,
predicate: &P
) -> Result<Option<&mut Node<H, N, V>>, Error<E>> where
E: Error,
F: Fn(&H, &H) -> Result<bool, E>,
P: Fn(&V) -> bool,
[src]
&mut self,
hash: &H,
number: &N,
is_descendent_of: &F,
predicate: &P
) -> Result<Option<&mut Node<H, N, V>>, Error<E>> where
E: Error,
F: Fn(&H, &H) -> Result<bool, E>,
P: Fn(&V) -> bool,
Same as find_node_where
, but returns mutable reference.
pub fn find_node_index_where<F, E, P>(
&self,
hash: &H,
number: &N,
is_descendent_of: &F,
predicate: &P
) -> Result<Option<Vec<usize>>, Error<E>> where
E: Error,
F: Fn(&H, &H) -> Result<bool, E>,
P: Fn(&V) -> bool,
[src]
&self,
hash: &H,
number: &N,
is_descendent_of: &F,
predicate: &P
) -> Result<Option<Vec<usize>>, Error<E>> where
E: Error,
F: Fn(&H, &H) -> Result<bool, E>,
P: Fn(&V) -> bool,
Same as find_node_where
, but returns indexes.
pub fn finalize_root(&mut self, hash: &H) -> Option<V>
[src]
Finalize a root in the tree and return it, return None
in case no root
with the given hash exists. All other roots are pruned, and the children
of the finalized node become the new roots.
pub fn finalize<F, E>(
&mut self,
hash: &H,
number: N,
is_descendent_of: &F
) -> Result<FinalizationResult<V>, Error<E>> where
E: Error,
F: Fn(&H, &H) -> Result<bool, E>,
[src]
&mut self,
hash: &H,
number: N,
is_descendent_of: &F
) -> Result<FinalizationResult<V>, Error<E>> where
E: Error,
F: Fn(&H, &H) -> Result<bool, E>,
Finalize a node in the tree. This method will make sure that the node
being finalized is either an existing root (and return its data), or a
node from a competing branch (not in the tree), tree pruning is done
accordingly. The given function is_descendent_of
should return true
if the second hash (target) is a descendent of the first hash (base).
pub fn finalize_with_ancestors<F, E>(
&mut self,
hash: &H,
number: N,
is_descendent_of: &F
) -> Result<FinalizationResult<V>, Error<E>> where
E: Error,
F: Fn(&H, &H) -> Result<bool, E>,
[src]
&mut self,
hash: &H,
number: N,
is_descendent_of: &F
) -> Result<FinalizationResult<V>, Error<E>> where
E: Error,
F: Fn(&H, &H) -> Result<bool, E>,
Finalize a node in the tree and all its ancestors. The given function
is_descendent_of
should return true
if the second hash (target) is
pub fn finalizes_any_with_descendent_if<F, P, E>(
&self,
hash: &H,
number: N,
is_descendent_of: &F,
predicate: P
) -> Result<Option<bool>, Error<E>> where
E: Error,
F: Fn(&H, &H) -> Result<bool, E>,
P: Fn(&V) -> bool,
[src]
&self,
hash: &H,
number: N,
is_descendent_of: &F,
predicate: P
) -> Result<Option<bool>, Error<E>> where
E: Error,
F: Fn(&H, &H) -> Result<bool, E>,
P: Fn(&V) -> bool,
Checks if any node in the tree is finalized by either finalizing the
node itself or a child node that’s not in the tree, guaranteeing that
the node being finalized isn’t a descendent of any of the node’s
children. Returns Some(true)
if the node being finalized is a root,
Some(false)
if the node being finalized is not a root, and None
if
no node in the tree is finalized. The given predicate
is checked on
the prospective finalized root and must pass for finalization to occur.
The given function is_descendent_of
should return true
if the second
hash (target) is a descendent of the first hash (base).
pub fn finalize_with_descendent_if<F, P, E>(
&mut self,
hash: &H,
number: N,
is_descendent_of: &F,
predicate: P
) -> Result<FinalizationResult<V>, Error<E>> where
E: Error,
F: Fn(&H, &H) -> Result<bool, E>,
P: Fn(&V) -> bool,
[src]
&mut self,
hash: &H,
number: N,
is_descendent_of: &F,
predicate: P
) -> Result<FinalizationResult<V>, Error<E>> where
E: Error,
F: Fn(&H, &H) -> Result<bool, E>,
P: Fn(&V) -> bool,
Finalize a root in the tree by either finalizing the node itself or a
child node that’s not in the tree, guaranteeing that the node being
finalized isn’t a descendent of any of the root’s children. The given
predicate
is checked on the prospective finalized root and must pass for
finalization to occur. The given function is_descendent_of
should
return true
if the second hash (target) is a descendent of the first
hash (base).
Trait Implementations
impl<H: Clone, N: Clone, V: Clone> Clone for ForkTree<H, N, V>
[src]
impl<H: Debug, N: Debug, V: Debug> Debug for ForkTree<H, N, V>
[src]
impl<H, N, V> Decode for ForkTree<H, N, V> where
Vec<Node<H, N, V>>: Decode,
Vec<Node<H, N, V>>: Decode,
Option<N>: Decode,
Option<N>: Decode,
[src]
Vec<Node<H, N, V>>: Decode,
Vec<Node<H, N, V>>: Decode,
Option<N>: Decode,
Option<N>: Decode,
fn decode<__CodecInputEdqy: Input>(
__codec_input_edqy: &mut __CodecInputEdqy
) -> Result<Self, Error>
[src]
__codec_input_edqy: &mut __CodecInputEdqy
) -> Result<Self, Error>
pub fn skip<I>(input: &mut I) -> Result<(), Error> where
I: Input,
[src]
I: Input,
pub fn encoded_fixed_size() -> Option<usize>
[src]
impl<H, N, V> Encode for ForkTree<H, N, V> where
Vec<Node<H, N, V>>: Encode,
Vec<Node<H, N, V>>: Encode,
Option<N>: Encode,
Option<N>: Encode,
[src]
Vec<Node<H, N, V>>: Encode,
Vec<Node<H, N, V>>: Encode,
Option<N>: Encode,
Option<N>: Encode,
fn encode_to<__CodecOutputEdqy: Output + ?Sized>(
&self,
__codec_dest_edqy: &mut __CodecOutputEdqy
)
[src]
&self,
__codec_dest_edqy: &mut __CodecOutputEdqy
)
pub fn size_hint(&self) -> usize
[src]
pub fn encode(&self) -> Vec<u8, Global>
[src]
pub fn using_encoded<R, F>(&self, f: F) -> R where
F: FnOnce(&[u8]) -> R,
[src]
F: FnOnce(&[u8]) -> R,
pub fn encoded_size(&self) -> usize
[src]
impl<H, N, V> EncodeLike<ForkTree<H, N, V>> for ForkTree<H, N, V> where
Vec<Node<H, N, V>>: Encode,
Vec<Node<H, N, V>>: Encode,
Option<N>: Encode,
Option<N>: Encode,
[src]
Vec<Node<H, N, V>>: Encode,
Vec<Node<H, N, V>>: Encode,
Option<N>: Encode,
Option<N>: Encode,
impl<H: PartialEq, N: PartialEq, V: PartialEq> PartialEq<ForkTree<H, N, V>> for ForkTree<H, N, V>
[src]
fn eq(&self, other: &ForkTree<H, N, V>) -> bool
[src]
fn ne(&self, other: &ForkTree<H, N, V>) -> bool
[src]
impl<H, N, V> StructuralPartialEq for ForkTree<H, N, V>
[src]
Auto Trait Implementations
impl<H, N, V> RefUnwindSafe for ForkTree<H, N, V> where
H: RefUnwindSafe,
N: RefUnwindSafe,
V: RefUnwindSafe,
H: RefUnwindSafe,
N: RefUnwindSafe,
V: RefUnwindSafe,
impl<H, N, V> Send for ForkTree<H, N, V> where
H: Send,
N: Send,
V: Send,
H: Send,
N: Send,
V: Send,
impl<H, N, V> Sync for ForkTree<H, N, V> where
H: Sync,
N: Sync,
V: Sync,
H: Sync,
N: Sync,
V: Sync,
impl<H, N, V> Unpin for ForkTree<H, N, V> where
H: Unpin,
N: Unpin,
V: Unpin,
H: Unpin,
N: Unpin,
V: Unpin,
impl<H, N, V> UnwindSafe for ForkTree<H, N, V> where
H: UnwindSafe,
N: UnwindSafe,
V: UnwindSafe,
H: UnwindSafe,
N: UnwindSafe,
V: UnwindSafe,
Blanket Implementations
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
pub fn borrow_mut(&mut self) -> &mut T
[src]
impl<S> Codec for S where
S: Decode + Encode,
[src]
S: Decode + Encode,
impl<T> DecodeAll for T where
T: Decode,
[src]
T: Decode,
impl<T> DecodeLimit for T where
T: Decode,
[src]
T: Decode,
pub fn decode_all_with_depth_limit(limit: u32, input: &[u8]) -> Result<T, Error>
[src]
pub fn decode_with_depth_limit(limit: u32, input: &[u8]) -> Result<T, Error>
[src]
impl<'_, '_, T> EncodeLike<&'_ &'_ T> for T where
T: Encode,
[src]
T: Encode,
impl<'_, T> EncodeLike<&'_ T> for T where
T: Encode,
[src]
T: Encode,
impl<'_, T> EncodeLike<&'_ mut T> for T where
T: Encode,
[src]
T: Encode,
impl<T> EncodeLike<Arc<T>> for T where
T: Encode,
[src]
T: Encode,
impl<T> EncodeLike<Box<T, Global>> for T where
T: Encode,
[src]
T: Encode,
impl<'a, T> EncodeLike<Cow<'a, T>> for T where
T: ToOwned + Encode,
[src]
T: ToOwned + Encode,
impl<T> EncodeLike<Rc<T>> for T where
T: Encode,
[src]
T: Encode,
impl<T> From<T> for T
[src]
impl<S> FullCodec for S where
S: Decode + FullEncode,
[src]
S: Decode + FullEncode,
impl<S> FullEncode for S where
S: Encode + EncodeLike<S>,
[src]
S: Encode + EncodeLike<S>,
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
impl<T> KeyedVec for T where
T: Codec,
[src]
T: Codec,
impl<T> ToOwned for T where
T: Clone,
[src]
T: Clone,
type Owned = T
The resulting type after obtaining ownership.
pub fn to_owned(&self) -> T
[src]
pub fn clone_into(&self, target: &mut T)
[src]
impl<T, U> TryFrom<U> for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
pub fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,