zenoh_keyexpr/keyexpr_tree/
mod.rs

1//
2// Copyright (c) 2023 ZettaScale Technology
3//
4// This program and the accompanying materials are made available under the
5// terms of the Eclipse Public License 2.0 which is available at
6// http://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
7// which is available at https://www.apache.org/licenses/LICENSE-2.0.
8//
9// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
10//
11// Contributors:
12//   ZettaScale Zenoh Team, <zenoh@zettascale.tech>
13//
14
15//! KeTrees are specialized data structures to work with sets of values addressed by key expressions.
16//!
17//! Think of it like HashMaps that were specifically designed to allow iterating over key expressions related to a given query
18//! in a fast and semantically correct way.
19//!
20//! # KeTrees
21//! Key Expression Trees (KeTrees) are trees that split any KE inserted into them into its chunks.
22//!
23//! For example, if you take an empty KeTree, and insert the `(a/b, 0)`, `(a/b/c, 1)`, `(a/b/d, 2)`, `(a/e, 3)` and (a/@f, 4) pairs into it,
24//! the tree will be as follows:
25//! ```text
26//! a --- e  = 3
27//!    |- b  = 0 --- c = 1
28//!    |          |- d = 2
29//!    |- @f = 4
30//! ```
31//!
32//! Note that the `a` node exists, despite no value being assigned to it. If you iterate over all nodes, the iterator may yield
33//! `(a, None), (a/e, Some(3)), (a/b, Some(0)), (a/b/c, Some(1)), (a/b/d, Some(2)), (a/@f, Some(4))`.
34//!
35//! # Ownership
36//! KeTrees come in two flavours:
37//! - [`KeBoxTree`] is the easier flavour. Much like a HashMap, it uniquely owns all of its nodes and data.
38//! - [`KeArcTree`] allows the shared ownership of nodes, allowing you to store subsections of the tree elsewhere
39//!   without worrying about lifetimes.
40//!
41//! # Usage
42//! KeTrees were designed to maximize code reuse. As such, their core properties are reflected through the [`IKeyExprTree`] and [`IKeyExprTreeMut`] traits.
43//!
44//! KeTrees are made up of node, where nodes may or may not have a value (called `weight`) associated with them. To access these weighs, as well as other
45//! properties of a node, you can go through the [`IKeyExprTreeNode`] and [`IKeyExprTreeNodeMut`] traits.
46//!
47//! # Iterators
48//! KeTrees provide iterators for the following operations:
49//! - Iterating on all nodes ([`IKeyExprTree::tree_iter`]/[`IKeyExprTreeMut::tree_iter_mut`])
50//! - Iterating on key-value pairs in the KeTree ([`IKeyExprTree::key_value_pairs`])
51//! - Iterating on nodes whose KE intersects with a queried KE ([`IKeyExprTree::intersecting_nodes`], [`IKeyExprTreeMut::intersecting_nodes_mut`])
52//! - Iterating on nodes whose KE are included by a queried KE ([`IKeyExprTree::included_nodes`], [`IKeyExprTreeMut::included_nodes_mut`])
53//! - Iterating on nodes whose KE includes a queried KE ([`IKeyExprTree::nodes_including`], [`IKeyExprTreeMut::nodes_including_mut`])
54//!
55//! While the order of iteration is not guaranteed, a node will never be yielded before one of its parents if both are to appear.  
56//! For example, iterating on nodes that intersect with `**` in the previously defined tree is guaranteed to yield one of the following sequences:
57//! - `(a, None), (a/e, Some(3)), (a/b, Some(0)), (a/b/c, Some(1)), (a/b/d, Some(2))`
58//! - `(a, None), (a/e, Some(3)), (a/b, Some(0)), (a/b/d, Some(2)), (a/b/c, Some(1))`
59//! - `(a, None), (a/b, Some(0)), (a/b/c, Some(1)), (a/b/d, Some(2)), (a/e, Some(3))`
60//! - `(a, None), (a/b, Some(0)), (a/b/d, Some(2)), (a/b/c, Some(1)), (a/e, Some(3))`
61
62use crate::{keyexpr, OwnedKeyExpr};
63/// Allows importing all of the KeTree traits at once.
64pub mod traits;
65pub use traits::*;
66
67/// An implementation of a KeTree with shared-ownership of nodes, using [`token_cell`] to allow safe access to the tree's data.
68///
69/// This implementation allows sharing ownership of members of the KeTree.
70pub mod arc_tree;
71pub use arc_tree::{DefaultToken, KeArcTree};
72/// An implementation of a KeTree that owns all of its nodes.
73pub mod box_tree;
74pub use box_tree::KeBoxTree;
75/// KeTrees can store their children in different manners.
76///
77/// This module contains a few implementations.
78pub mod impls;
79pub use impls::DefaultChildrenProvider;
80mod iters;
81pub use iters::*;
82
83#[cfg(test)]
84mod test;
85
86pub mod support;