Struct charcoal::Quadtree[][src]

pub struct Quadtree<B, L = B, K = usize, S = DefaultStorage<Node<B, L, K>>> where
    S: Storage<Element = Node<B, L, K>, Key = K>,
    K: Clone + Debug + Eq
{ /* fields omitted */ }

A quadtree.

See the module-level documentation for more.

Implementations

impl<B, L, K, S> Quadtree<B, L, K, S> where
    S: Storage<Element = Node<B, L, K>, Key = K>,
    K: Clone + Debug + Eq
[src]

pub fn new(root: L) -> Self[src]

This is supported on crate feature quadtree only.

Creates a quadtree with the specified value for the root node.

Example

// The only way to create a tree...
let tree = Quadtree::<_>::new(87);
// ...is to simply create the root leaf node and storage. The turbofish there is needed to
// state that we are using the default storage method instead of asking the compiler to
// infer it, which would be impossible.

// No other nodes have been created yet:
assert!(tree.root().is_leaf());

pub fn with_capacity(capacity: usize, root: L) -> Self[src]

This is supported on crate feature quadtree only.

Creates a quadtree with the specified capacity for the storage.

Panics

The storage may panic if it has fixed capacity and the specified value does not match it.

Example

// Let's create a tree, but with some preallocated space for more nodes:
let mut tree = Quadtree::<_>::with_capacity(5, "Variable Names");
// The turbofish there is needed to state that we are using the default storage method
// instead of asking the compiler to infer it, which would be impossible.

// Capacity does not affect the actual nodes:
assert!(tree.root().is_leaf());

// Not until we create them ourselves:
tree.root_mut().make_branch([
    "Foo", "Bar", "Baz", "Quux",
]);

// If the default storage is backed by a dynamic memory allocation,
// at most one has happened to this point.

pub fn root(&self) -> NodeRef<'_, B, L, K, S>[src]

This is supported on crate feature quadtree only.

Returns a reference to the root node of the tree.

Example

// A tree always has a root node:
let tree = Quadtree::<_>::new("Root");

assert_eq!(
    // The into_inner() call extracts data from a NodeValue, which is used to generalize
    // tres to both work with same and different types for payloads of leaf and branch
    // nodes.
    *tree.root().value().into_inner(),
    "Root",
);

pub fn root_mut(&mut self) -> NodeRefMut<'_, B, L, K, S>[src]

This is supported on crate feature quadtree only.

Returns a mutable reference to the root node of the tree, allowing modifications to the entire tree.

Example

// A tree always has a root node:
let mut tree = Quadtree::<_>::new("Root");

let mut root_mut = tree.root_mut();
// The into_inner() call extracts data from a NodeValue, which is used to generalize trees
// to both work with same and different types for payloads of leaf and branch nodes.
*(root_mut.value_mut().into_inner()) = "The Source of the Beer";

pub fn num_nodes(&self) -> usize[src]

This is supported on crate feature quadtree only.

Returns the number of nodes in the tree.

pub fn capacity(&self) -> usize[src]

This is supported on crate feature quadtree only.

Returns the additional number of nodes which the tree can store without the need to reallocate.

pub fn reserve(&mut self, additional: usize)[src]

This is supported on crate feature quadtree only.

Reserves capacity for at least additional more elements to be inserted in the given storage. The storage may reserve more space to avoid frequent reallocations. After calling reserve, capacity will be greater than or equal to self.len() + additional. Does nothing if capacity is already sufficient.

pub fn shrink_to_fit(&mut self)[src]

This is supported on crate feature quadtree only.

Shrinks the capacity of the storage as much as possible.

It will drop down as close as possible to the current length, though dynamically allocated storages may not always reallocate exactly as much as it is needed to store all elements and none more.

impl<B, L, S> Quadtree<B, L, usize, SparseStorage<Node<B, L, usize>, S>> where
    S: ListStorage<Element = SparseStorageSlot<Node<B, L, usize>>>, 
[src]

pub fn defragment(&mut self)[src]

This is supported on crate feature quadtree only.

Removes all holes from the sparse storage.

Under the hood, this uses defragment_and_fix. It’s not possible to defragment without fixing the indicies, as that might cause undefined behavior.

Example

use charcoal::quadtree::SparseVecQuadtree;

// Create a tree which explicitly uses sparse storage:
let mut tree = SparseVecQuadtree::new(0);
// This is already the default, but for the sake of this example we'll stay explicit.

// Add some elements for the holes to appear:
tree.root_mut().make_branch([
    1, 2, 3, 4,
]).unwrap(); // You can replace this with proper error handling
tree
    .root_mut()
    .nth_child_mut(0)
    .unwrap() // This too
    .make_branch([5, 6, 7, 8])
    .unwrap(); // And this

tree
    .root_mut()
    .nth_child_mut(0)
    .unwrap() // Same as above
    .try_remove_children()
    .unwrap(); // Same here

// We ended up creating 4 holes:
assert_eq!(tree.num_holes(), 4);
// Let's patch them:
tree.defragment();
// Now there are none:
assert_eq!(tree.num_holes(), 0);

pub fn num_holes(&self) -> usize[src]

This is supported on crate feature quadtree only.

Returns the number of holes in the storage. This operation returns immediately instead of looping through the entire storage, since the sparse storage automatically tracks the number of holes it creates and destroys.

Example

See the example in defragment.

pub fn is_dense(&self) -> bool[src]

This is supported on crate feature quadtree only.

Returns true if there are no holes in the storage, false otherwise. This operation returns immediately instead of looping through the entire storage, since the sparse storage automatically tracks the number of holes it creates and destroys.

Example

See the example in defragment.

Trait Implementations

impl<B: Clone, L: Clone, K: Clone, S: Clone> Clone for Quadtree<B, L, K, S> where
    S: Storage<Element = Node<B, L, K>, Key = K>,
    K: Clone + Debug + Eq
[src]

This is supported on crate feature quadtree only.

impl<B: Copy, L: Copy, K: Copy, S: Copy> Copy for Quadtree<B, L, K, S> where
    S: Storage<Element = Node<B, L, K>, Key = K>,
    K: Clone + Debug + Eq
[src]

This is supported on crate feature quadtree only.

impl<B: Debug, L: Debug, K: Debug, S: Debug> Debug for Quadtree<B, L, K, S> where
    S: Storage<Element = Node<B, L, K>, Key = K>,
    K: Clone + Debug + Eq
[src]

This is supported on crate feature quadtree only.

impl<B, L, K, S> Default for Quadtree<B, L, K, S> where
    L: Default,
    S: Storage<Element = Node<B, L, K>, Key = K>,
    K: Clone + Debug + Eq
[src]

This is supported on crate feature quadtree only.

impl<B: Eq, L: Eq, K: Eq, S: Eq> Eq for Quadtree<B, L, K, S> where
    S: Storage<Element = Node<B, L, K>, Key = K>,
    K: Clone + Debug + Eq
[src]

This is supported on crate feature quadtree only.

impl<B: Hash, L: Hash, K: Hash, S: Hash> Hash for Quadtree<B, L, K, S> where
    S: Storage<Element = Node<B, L, K>, Key = K>,
    K: Clone + Debug + Eq
[src]

This is supported on crate feature quadtree only.

impl<B: PartialEq, L: PartialEq, K: PartialEq, S: PartialEq> PartialEq<Quadtree<B, L, K, S>> for Quadtree<B, L, K, S> where
    S: Storage<Element = Node<B, L, K>, Key = K>,
    K: Clone + Debug + Eq
[src]

This is supported on crate feature quadtree only.

impl<B, L, K, S> StructuralEq for Quadtree<B, L, K, S> where
    S: Storage<Element = Node<B, L, K>, Key = K>,
    K: Clone + Debug + Eq
[src]

This is supported on crate feature quadtree only.

impl<B, L, K, S> StructuralPartialEq for Quadtree<B, L, K, S> where
    S: Storage<Element = Node<B, L, K>, Key = K>,
    K: Clone + Debug + Eq
[src]

This is supported on crate feature quadtree only.

impl<B, L, K, S> Traversable for Quadtree<B, L, K, S> where
    S: Storage<Element = Node<B, L, K>, Key = K>,
    K: Clone + Debug + Eq
[src]

This is supported on crate feature quadtree only.

type Leaf = L

The payload of the node if it’s a leaf node.

type Branch = B

The payload of the node if it’s a branch node.

type Cursor = K

The type for the cursor which will be used for keeping track of the traversed nodes. Read more

impl<B, L, K, S> TraversableMut for Quadtree<B, L, K, S> where
    S: Storage<Element = Node<B, L, K>, Key = K>,
    K: Clone + Debug + Eq
[src]

This is supported on crate feature quadtree only.

type PackedChildren = PackedChildren<L>

A container for the leaf children of a branch node.

Auto Trait Implementations

impl<B, L, K, S> RefUnwindSafe for Quadtree<B, L, K, S> where
    K: RefUnwindSafe,
    S: RefUnwindSafe

impl<B, L, K, S> Send for Quadtree<B, L, K, S> where
    K: Send,
    S: Send

impl<B, L, K, S> Sync for Quadtree<B, L, K, S> where
    K: Sync,
    S: Sync

impl<B, L, K, S> Unpin for Quadtree<B, L, K, S> where
    K: Unpin,
    S: Unpin

impl<B, L, K, S> UnwindSafe for Quadtree<B, L, K, S> where
    K: UnwindSafe,
    S: UnwindSafe

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> Slottable for T where
    T: Copy
[src]

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

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

The type returned in the event of a conversion error.