Skip to main content

ViewTree

Enum ViewTree 

Source
pub enum ViewTree {
    Leaf(Leaf),
    And(Box<Tree>, Box<Tree>),
    Or(Box<Tree>, Box<Tree>),
    Diff(Box<Tree>, Box<Tree>),
}
Expand description

The composition tree. Depth ≤ 3, leaves ≤ 4 (declarative caps, enforced at CREATE).

use kevy_index::{IndexValue, Leaf, Segment, Tree};

let mut age = Segment::new();
age.set_key_dir(true);
age.apply(b"u:1", None, Some(IndexValue::I64(30)));
age.apply(b"u:2", None, Some(IndexValue::I64(70)));
let mut city = Segment::new();
city.set_key_dir(true);
city.apply(b"u:1", None, Some(IndexValue::Str(b"kyoto".to_vec())));
city.apply(b"u:2", None, Some(IndexValue::Str(b"kyoto".to_vec())));

let kyoto = IndexValue::Str(b"kyoto".to_vec());
let t = Tree::And(
    Box::new(Tree::Leaf(Leaf::new("city", kyoto.clone(), kyoto))),
    Box::new(Tree::Leaf(Leaf::new("age", IndexValue::I64(18), IndexValue::I64(65)))),
);
let seg = |name: &[u8]| match name {
    b"age" => Some(&age),
    b"city" => Some(&city),
    _ => None,
};
assert_eq!(t.eval(&seg), vec![b"u:1".to_vec()]);
assert!(t.contains(b"u:1", &seg) && !t.contains(b"u:2", &seg));
assert_eq!((t.leaves(), t.depth()), (2, 2));

let mut names = Vec::new();
t.each_leaf(&mut |l: &Leaf| names.push(l.index.clone()));
assert_eq!(names, [b"city".to_vec(), b"age".to_vec()]);

Variants§

§

Leaf(Leaf)

A single index shape.

use kevy_index::{IndexValue, Leaf, Tree};
let t = Tree::Leaf(Leaf::new("age", IndexValue::I64(0), IndexValue::I64(9)));
assert_eq!((t.leaves(), t.depth()), (1, 1));
§

And(Box<Tree>, Box<Tree>)

Intersection.

use kevy_index::{IndexValue, Leaf, Tree};
let leaf = |lo, hi| Box::new(Tree::Leaf(Leaf::new("n", IndexValue::I64(lo), IndexValue::I64(hi))));
let t = Tree::And(leaf(0, 10), leaf(5, 20));
assert!(t.contains_values(&|_: &[u8]| Some(IndexValue::I64(7))));
assert!(!t.contains_values(&|_: &[u8]| Some(IndexValue::I64(3))));
§

Or(Box<Tree>, Box<Tree>)

Union.

use kevy_index::{IndexValue, Leaf, Tree};
let leaf = |lo, hi| Box::new(Tree::Leaf(Leaf::new("n", IndexValue::I64(lo), IndexValue::I64(hi))));
let t = Tree::Or(leaf(0, 1), leaf(8, 9));
assert!(t.contains_values(&|_: &[u8]| Some(IndexValue::I64(9))));
assert!(!t.contains_values(&|_: &[u8]| Some(IndexValue::I64(5))));
§

Diff(Box<Tree>, Box<Tree>)

Left minus right (NOT commutative — order is fixed).

use kevy_index::{IndexValue, Leaf, Tree};
let leaf = |lo, hi| Box::new(Tree::Leaf(Leaf::new("n", IndexValue::I64(lo), IndexValue::I64(hi))));
let t = Tree::Diff(leaf(0, 10), leaf(5, 10));
assert!(t.contains_values(&|_: &[u8]| Some(IndexValue::I64(4))));
assert!(!t.contains_values(&|_: &[u8]| Some(IndexValue::I64(6))));

Implementations§

Source§

impl Tree

Source

pub fn eval<'a>( &self, seg: &impl Fn(&[u8]) -> Option<&'a Segment>, ) -> Vec<Vec<u8>>

Evaluate tree against one shard’s segments: seg resolves an index name to its Segment (None = unknown index → empty leaf — the runtime validates names at CREATE, so this is defensive). Returns the member keys (unordered set semantics).

use kevy_index::{IndexValue, Leaf, Segment, Tree};
let mut age = Segment::new();
age.set_key_dir(true);
age.apply(b"u:1", None, Some(IndexValue::I64(30)));
age.apply(b"u:2", None, Some(IndexValue::I64(70)));
let t = Tree::Leaf(Leaf::new("age", IndexValue::I64(18), IndexValue::I64(65)));
let seg = |name: &[u8]| (name == b"age").then_some(&age);
assert_eq!(t.eval(&seg), vec![b"u:1".to_vec()]);
assert!(t.contains(b"u:1", &seg) && !t.contains(b"u:2", &seg));
Source

pub fn contains<'a>( &self, key: &[u8], seg: &impl Fn(&[u8]) -> Option<&'a Segment>, ) -> bool

Re-evaluate ONE key’s membership (the materialized write hook): every leaf is a point probe in the segment’s key directory (Segment::set_key_dir); a segment without one holds no key.

Source

pub fn contains_values( &self, vals: &impl Fn(&[u8]) -> Option<IndexValue>, ) -> bool

Tree::contains variant over PRE-FETCHED per-index values — the write hook probes each referenced index ONCE per key and evaluates every view against the same small table (bounds compares only; no per-view re-hashing).

use kevy_index::{IndexValue, Leaf, Tree};
let t = Tree::Leaf(Leaf::new("age", IndexValue::I64(18), IndexValue::I64(65)));
assert!(t.contains_values(&|_: &[u8]| Some(IndexValue::I64(40))));
assert!(!t.contains_values(&|_: &[u8]| None));
Source

pub fn leaves(&self) -> usize

Number of leaves.

Source

pub fn depth(&self) -> usize

Depth (a leaf is 1).

Source

pub fn each_leaf<F>(&self, f: &mut F)
where F: FnMut(&Leaf),

Visit every leaf.

Trait Implementations§

Source§

impl Clone for Tree

Source§

fn clone(&self) -> Tree

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Tree

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
Source§

impl PartialEq for Tree

Source§

fn eq(&self, other: &Tree) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl StructuralPartialEq for Tree

Auto Trait Implementations§

§

impl Freeze for Tree

§

impl RefUnwindSafe for Tree

§

impl Send for Tree

§

impl Sync for Tree

§

impl Unpin for Tree

§

impl UnsafeUnpin for Tree

§

impl UnwindSafe for Tree

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.