Struct IntervalTree

Source
pub struct IntervalTree<T> { /* private fields */ }
Expand description

An interval tree implementation specialized for VMM resource management.

Implementations§

Source§

impl<T> IntervalTree<T>

Source

pub fn new() -> Self

Construct a default empty IntervalTree object.

§Examples
extern crate dbs_allocator;

let tree = dbs_allocator::IntervalTree::<u64>::new();
Source

pub fn is_empty(&self) -> bool

Check whether the interval tree is empty.

Source

pub fn get(&self, key: &Range) -> Option<NodeState<&T>>

Get the data item associated with the key, or return None if no match found.

§Examples
extern crate dbs_allocator;
use dbs_allocator::{IntervalTree, NodeState, Range};

let mut tree = dbs_allocator::IntervalTree::<u64>::new();
assert!(tree.is_empty());
assert_eq!(tree.get(&Range::new(0x101u64, 0x101u64)), None);
tree.insert(Range::new(0x100u64, 0x100u64), Some(1));
tree.insert(Range::new(0x200u64, 0x2ffu64), None);
assert!(!tree.is_empty());
assert_eq!(
    tree.get(&Range::new(0x100u64, 0x100u64)),
    Some(NodeState::Valued(&1))
);
assert_eq!(
    tree.get(&Range::new(0x200u64, 0x2ffu64)),
    Some(NodeState::Free)
);
assert_eq!(tree.get(&Range::new(0x101u64, 0x101u64)), None);
assert_eq!(tree.get(&Range::new(0x100u64, 0x101u64)), None);
Source

pub fn get_superset(&self, key: &Range) -> Option<(&Range, NodeState<&T>)>

Get a shared reference to the node fully covering the entire key range.

§Examples
extern crate dbs_allocator;
use dbs_allocator::{IntervalTree, NodeState, Range};

let mut tree = IntervalTree::<u64>::new();
tree.insert(Range::new(0x100u32, 0x100u32), Some(1));
tree.insert(Range::new(0x200u32, 0x2ffu32), None);
assert_eq!(
    tree.get_superset(&Range::new(0x100u32, 0x100u32)),
    Some((&Range::new(0x100u32, 0x100u32), NodeState::Valued(&1)))
);
assert_eq!(
    tree.get_superset(&Range::new(0x210u32, 0x210u32)),
    Some((&Range::new(0x200u32, 0x2ffu32), NodeState::Free))
);
assert_eq!(
    tree.get_superset(&Range::new(0x2ffu32, 0x2ffu32)),
    Some((&Range::new(0x200u32, 0x2ffu32), NodeState::Free))
);
Source

pub fn get_superset_mut( &mut self, key: &Range, ) -> Option<(&Range, NodeState<&mut T>)>

Get a mutable reference to the node fully covering the entire key range.

§Examples
extern crate dbs_allocator;
use dbs_allocator::{IntervalTree, NodeState, Range};

let mut tree = IntervalTree::<u64>::new();
tree.insert(Range::new(0x100u32, 0x100u32), Some(1));
tree.insert(Range::new(0x200u32, 0x2ffu32), None);
assert_eq!(
    tree.get_superset_mut(&Range::new(0x100u32, 0x100u32)),
    Some((&Range::new(0x100u32, 0x100u32), NodeState::Valued(&mut 1)))
);
assert_eq!(
    tree.get_superset_mut(&Range::new(0x210u32, 0x210u32)),
    Some((&Range::new(0x200u32, 0x2ffu32), NodeState::Free))
);
assert_eq!(
    tree.get_superset_mut(&Range::new(0x2ffu32, 0x2ffu32)),
    Some((&Range::new(0x200u32, 0x2ffu32), NodeState::Free))
);
Source

pub fn get_by_id<U>(&self, id: U) -> Option<&T>
where u64: From<U>,

Get a shared reference to the value associated with the id.

§Examples
extern crate dbs_allocator;
use dbs_allocator::{IntervalTree, NodeState, Range};

let mut tree = IntervalTree::<u32>::new();
tree.insert(Range::new(0x100u16, 0x100u16), Some(1));
tree.insert(Range::new(0x200u16, 0x2ffu16), None);
assert_eq!(tree.get_by_id(0x100u16), Some(&1));
assert_eq!(tree.get_by_id(0x210u32), None);
assert_eq!(tree.get_by_id(0x2ffu64), None);
Source

pub fn get_by_id_mut<U>(&mut self, id: U) -> Option<&mut T>
where u64: From<U>,

Get a mutable reference to the value associated with the id.

§Examples
extern crate dbs_allocator;
use dbs_allocator::{IntervalTree, NodeState, Range};

let mut tree = IntervalTree::<u32>::new();
tree.insert(Range::new(0x100u16, 0x100u16), Some(1));
tree.insert(Range::new(0x200u16, 0x2ffu16), None);
assert_eq!(tree.get_by_id_mut(0x100u16), Some(&mut 1));
assert_eq!(tree.get_by_id_mut(0x210u32), None);
assert_eq!(tree.get_by_id_mut(0x2ffu64), None);
Source

pub fn insert(&mut self, key: Range, data: Option<T>)

Insert the (key, data) pair into the interval tree, panic if intersects with existing nodes.

§Examples
extern crate dbs_allocator;
use dbs_allocator::{IntervalTree, NodeState, Range};

let mut tree = IntervalTree::<u64>::new();
tree.insert(Range::new(0x100u32, 0x100u32), Some(1));
tree.insert(Range::new(0x200u32, 0x2ffu32), None);
assert_eq!(
    tree.get(&Range::new(0x100u64, 0x100u64)),
    Some(NodeState::Valued(&1))
);
assert_eq!(
    tree.get(&Range::new(0x200u64, 0x2ffu64)),
    Some(NodeState::Free)
);
Source

pub fn update(&mut self, key: &Range, data: T) -> Option<T>

Update an existing entry and return the old value.

§Examples
extern crate dbs_allocator;
use dbs_allocator::{Constraint, IntervalTree, Range};

let mut tree = IntervalTree::<u64>::new();
tree.insert(Range::new(0x100u64, 0x100u64), None);
tree.insert(Range::new(0x200u64, 0x2ffu64), None);

let constraint = Constraint::new(2u32);
let key = tree.allocate(&constraint);
assert_eq!(key, Some(Range::new(0x200u64, 0x201u64)));
let old = tree.update(&Range::new(0x200u64, 0x201u64), 2);
assert_eq!(old, None);
let old = tree.update(&Range::new(0x200u64, 0x201u64), 3);
assert_eq!(old, Some(2));
Source

pub fn delete(&mut self, key: &Range) -> Option<T>

Remove the key from the tree and return the associated data.

§Examples
extern crate dbs_allocator;
use dbs_allocator::{IntervalTree, Range};

let mut tree = IntervalTree::<u64>::new();
tree.insert(Range::new(0x100u64, 0x100u64), Some(1));
tree.insert(Range::new(0x200u64, 0x2ffu64), None);
let old = tree.delete(&Range::new(0x100u64, 0x100u64));
assert_eq!(old, Some(1));
let old = tree.delete(&Range::new(0x200u64, 0x2ffu64));
assert_eq!(old, None);
Source

pub fn allocate(&mut self, constraint: &Constraint) -> Option<Range>

Allocate a resource range according the allocation constraints.

§Examples
extern crate dbs_allocator;
use dbs_allocator::{Constraint, IntervalTree, Range};

let mut tree = IntervalTree::<u64>::new();
tree.insert(Range::new(0x100u64, 0x100u64), None);
tree.insert(Range::new(0x200u64, 0x2ffu64), None);

let constraint = Constraint::new(2u8);
let key = tree.allocate(&constraint);
assert_eq!(key, Some(Range::new(0x200u64, 0x201u64)));
tree.update(&Range::new(0x200u64, 0x201u64), 2);
Source

pub fn free(&mut self, key: &Range) -> Option<T>

Free an allocated range and return the associated data.

Trait Implementations§

Source§

impl<T: Debug> Debug for IntervalTree<T>

Source§

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

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

impl<T: Default> Default for IntervalTree<T>

Source§

fn default() -> IntervalTree<T>

Returns the “default value” for a type. Read more
Source§

impl<T: PartialEq> PartialEq for IntervalTree<T>

Source§

fn eq(&self, other: &IntervalTree<T>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

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

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<T: Eq> Eq for IntervalTree<T>

Source§

impl<T> StructuralPartialEq for IntervalTree<T>

Auto Trait Implementations§

§

impl<T> Freeze for IntervalTree<T>

§

impl<T> RefUnwindSafe for IntervalTree<T>
where T: RefUnwindSafe,

§

impl<T> Send for IntervalTree<T>
where T: Send,

§

impl<T> Sync for IntervalTree<T>
where T: Sync,

§

impl<T> Unpin for IntervalTree<T>

§

impl<T> UnwindSafe for IntervalTree<T>
where T: UnwindSafe,

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> 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, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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.