[][src]Module broccoli::collections

Overview

For most usecases, using broccoli::Tree is enough. But in certain cases we want more control. The container trees in this module are for this purpose.

For example, with the regular broccoli::Tree, is lifetimed, so it can't act as a container. You also can't do the the following.

This example deliberately fails to compile
use axgeom::*;
use broccoli::prelude::*;
let mut k=[bbox(rect(0,10,0,10),8)];
let mut b=broccoli::new(&mut k);
b.find_intersections_mut(|a,b|{});
k[0].inner=4;    //<---cannot re-borrow
b.find_intersections_mut(|a,b|{});

This is because broccoli::Tree constructs itself by splitting up the passed mutable slice to the point where the original mutable slice can't be retrieved.

If we use TreeRef, we can do the above like this:

use axgeom::*;
use broccoli::prelude::*;
let mut k=[bbox(rect(0,10,0,10),8)];
let mut b=broccoli::collections::TreeRef::new(&mut k);
b.find_intersections_mut(|a,b|{});
*b.get_elements_mut().get_index_mut(0).inner_mut()=5;
b.find_intersections_mut(|a,b|{});

This is good and all, but having to work around the PMut pointer that protect the invariants of the tree is cumbersome. To get around that we can use TreeRefInd which adds a layer of indirection. Unintuitively, This version that adds a layer of indirection is typically faster. Check the crate's book for more analysis. This does have some drawbacks in the sense that the bounding boxes must be constructed or copied each time the tree is created, though.

use axgeom::*;
use broccoli::prelude::*;
let mut k=[0];
let mut b=broccoli::collections::TreeRefInd::new(&mut k,|&mut d|rect(d,d,d,d));
b.find_intersections_mut(|a,b|{});
b.get_elements_mut()[0]=5;
b.find_intersections_mut(|a,b|{});

TreeRef and TreeRefInd are both lifetimed. If you want to store the tree inside an object there are TreeOwned and TreeOwnedInd equivalents.

An owned (Rect<N>,T) example

use broccoli::{prelude::*,collections::*,DefaultA};
use axgeom::*;

fn not_lifetimed()->TreeOwned<DefaultA,BBox<i32,f32>>
{
    let a=vec![bbox(rect(0,10,0,10),0.0)];
    TreeOwned::new(a)
}

not_lifetimed();

An owned (Rect<N>,*mut T) example

use broccoli::{*,collections::*,DefaultA};
use axgeom::*;

fn not_lifetimed()->TreeOwnedInd<DefaultA,i32,Vec2<i32>>
{
    let rect=vec![vec2(0,10),vec2(3,30)];
    TreeOwnedInd::new(rect,|&p|{
        let radius=vec2(10,10);
        Rect::from_point(p,radius)
    })
}

not_lifetimed();

Structs

TreeOwned

An owned Tree componsed of T:Aabb

TreeOwnedInd
TreeRef
TreeRefInd