[][src]Struct generational_arena::Arena

pub struct Arena<T> { /* fields omitted */ }

The Arena allows inserting and removing elements that are referred to by Index.

See the module-level documentation for example usage and motivation.

Methods

impl<T> Arena<T>[src]

pub fn new() -> Arena<T>[src]

Constructs a new, empty Arena.

Examples

use generational_arena::Arena;

let mut arena = Arena::<usize>::new();

pub fn with_capacity(n: usize) -> Arena<T>[src]

Constructs a new, empty Arena<T> with the specified capacity.

The Arena<T> will be able to hold n elements without further allocation.

Examples

use generational_arena::Arena;

let mut arena = Arena::with_capacity(10);

// These insertions will not require further allocation.
for i in 0..10 {
    assert!(arena.try_insert(i).is_ok());
}

// But now we are at capacity, and there is no more room.
assert!(arena.try_insert(99).is_err());

pub fn clear(&mut self)[src]

Clear all the items inside the arena, but keep its allocation.

Examples

use generational_arena::Arena;

let mut arena = Arena::with_capacity(1);
arena.insert(42);
arena.insert(43);

arena.clear();

assert_eq!(arena.capacity(), 2);

pub fn try_insert(&mut self, value: T) -> Result<Index, T>[src]

Attempts to insert value into the arena using existing capacity.

This method will never allocate new capacity in the arena.

If insertion succeeds, then the value's index is returned. If insertion fails, then Err(value) is returned to give ownership of value back to the caller.

Examples

use generational_arena::Arena;

let mut arena = Arena::new();

match arena.try_insert(42) {
    Ok(idx) => {
        // Insertion succeeded.
        assert_eq!(arena[idx], 42);
    }
    Err(x) => {
        // Insertion failed.
        assert_eq!(x, 42);
    }
};

pub fn insert(&mut self, value: T) -> Index[src]

Insert value into the arena, allocating more capacity if necessary.

The value's associated index in the arena is returned.

Examples

use generational_arena::Arena;

let mut arena = Arena::new();

let idx = arena.insert(42);
assert_eq!(arena[idx], 42);

pub fn remove(&mut self, i: Index) -> Option<T>[src]

Remove the element at index i from the arena.

If the element at index i is still in the arena, then it is returned. If it is not in the arena, then None is returned.

Examples

use generational_arena::Arena;

let mut arena = Arena::new();
let idx = arena.insert(42);

assert_eq!(arena.remove(idx), Some(42));
assert_eq!(arena.remove(idx), None);

pub fn retain(&mut self, predicate: impl FnMut(Index, &mut T) -> bool)[src]

Retains only the elements specified by the predicate.

In other words, remove all indices such that predicate(index, &value) returns false.

Examples

use generational_arena::Arena;

let mut crew = Arena::new();
crew.extend(&["Jim Hawkins", "John Silver", "Alexander Smollett", "Israel Hands"]);
let pirates = ["John Silver", "Israel Hands"]; // too dangerous to keep them around
crew.retain(|_index, member| !pirates.contains(member));
let mut crew_members = crew.iter().map(|(_, member)| **member);
assert_eq!(crew_members.next(), Some("Jim Hawkins"));
assert_eq!(crew_members.next(), Some("Alexander Smollett"));
assert!(crew_members.next().is_none());

pub fn contains(&self, i: Index) -> bool[src]

Is the element at index i in the arena?

Returns true if the element at i is in the arena, false otherwise.

Examples

use generational_arena::Arena;

let mut arena = Arena::new();
let idx = arena.insert(42);

assert!(arena.contains(idx));
arena.remove(idx);
assert!(!arena.contains(idx));

pub fn get(&self, i: Index) -> Option<&T>[src]

Get a shared reference to the element at index i if it is in the arena.

If the element at index i is not in the arena, then None is returned.

Examples

use generational_arena::Arena;

let mut arena = Arena::new();
let idx = arena.insert(42);

assert_eq!(arena.get(idx), Some(&42));
arena.remove(idx);
assert!(arena.get(idx).is_none());

pub fn get_mut(&mut self, i: Index) -> Option<&mut T>[src]

Get an exclusive reference to the element at index i if it is in the arena.

If the element at index i is not in the arena, then None is returned.

Examples

use generational_arena::Arena;

let mut arena = Arena::new();
let idx = arena.insert(42);

*arena.get_mut(idx).unwrap() += 1;
assert_eq!(arena.remove(idx), Some(43));
assert!(arena.get_mut(idx).is_none());

pub fn get2_mut(
    &mut self,
    i1: Index,
    i2: Index
) -> (Option<&mut T>, Option<&mut T>)
[src]

Get a pair of exclusive references to the elements at index i1 and i2 if it is in the arena.

If the element at index i1 or i2 is not in the arena, then None is returned for this element.

Panics

Panics if i1 and i2 are pointing to the same item of the arena.

Examples

use generational_arena::Arena;

let mut arena = Arena::new();
let idx1 = arena.insert(0);
let idx2 = arena.insert(1);

{
    let (item1, item2) = arena.get2_mut(idx1, idx2);

    *item1.unwrap() = 3;
    *item2.unwrap() = 4;
}

assert_eq!(arena[idx1], 3);
assert_eq!(arena[idx2], 4);

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

Get the length of this arena.

The length is the number of elements the arena holds.

Examples

use generational_arena::Arena;

let mut arena = Arena::new();
assert_eq!(arena.len(), 0);

let idx = arena.insert(42);
assert_eq!(arena.len(), 1);

let _ = arena.insert(0);
assert_eq!(arena.len(), 2);

assert_eq!(arena.remove(idx), Some(42));
assert_eq!(arena.len(), 1);

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

Returns true if the arena contains no elements

Examples

use generational_arena::Arena;

let mut arena = Arena::new();
assert!(arena.is_empty());

let idx = arena.insert(42);
assert!(!arena.is_empty());

assert_eq!(arena.remove(idx), Some(42));
assert!(arena.is_empty());

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

Get the capacity of this arena.

The capacity is the maximum number of elements the arena can hold without further allocation, including however many it currently contains.

Examples

use generational_arena::Arena;

let mut arena = Arena::with_capacity(10);
assert_eq!(arena.capacity(), 10);

// `try_insert` does not allocate new capacity.
for i in 0..10 {
    assert!(arena.try_insert(1).is_ok());
    assert_eq!(arena.capacity(), 10);
}

// But `insert` will if the arena is already at capacity.
arena.insert(0);
assert!(arena.capacity() > 10);

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

Allocate space for additional_capacity more elements in the arena.

Panics

Panics if this causes the capacity to overflow.

Examples

use generational_arena::Arena;

let mut arena = Arena::with_capacity(10);
arena.reserve(5);
assert_eq!(arena.capacity(), 15);

Important traits for Iter<'a, T>
pub fn iter(&self) -> Iter<T>[src]

Iterate over shared references to the elements in this arena.

Yields pairs of (Index, &T) items.

Order of iteration is not defined.

Examples

use generational_arena::Arena;

let mut arena = Arena::new();
for i in 0..10 {
    arena.insert(i * i);
}

for (idx, value) in arena.iter() {
    println!("{} is at index {:?}", value, idx);
}

Important traits for IterMut<'a, T>
pub fn iter_mut(&mut self) -> IterMut<T>[src]

Iterate over exclusive references to the elements in this arena.

Yields pairs of (Index, &mut T) items.

Order of iteration is not defined.

Examples

use generational_arena::Arena;

let mut arena = Arena::new();
for i in 0..10 {
    arena.insert(i * i);
}

for (_idx, value) in arena.iter_mut() {
    *value += 5;
}

Important traits for Drain<'a, T>
pub fn drain(&mut self) -> Drain<T>[src]

Iterate over elements of the arena and remove them.

Yields pairs of (Index, T) items.

Order of iteration is not defined.

Note: All elements are removed even if the iterator is only partially consumed or not consumed at all.

Examples

use generational_arena::Arena;

let mut arena = Arena::new();
let idx_1 = arena.insert("hello");
let idx_2 = arena.insert("world");

assert!(arena.get(idx_1).is_some());
assert!(arena.get(idx_2).is_some());
for (idx, value) in arena.drain() {
    assert!((idx == idx_1 && value == "hello") || (idx == idx_2 && value == "world"));
}
assert!(arena.get(idx_1).is_none());
assert!(arena.get(idx_2).is_none());

pub fn get_unknown_gen(&self, i: usize) -> Option<(&T, Index)>[src]

Given a i of usize without generation, get a shared reference to the element and the matching Index of the entry behind i.

This method is useful when you know there might be an element at the position i, but don't know its generation or precise Index.

Use cases include using indexing such as Hierarchical BitMap Indexing or other kinds of bit-efficient indexing.

You should use the get method most of the time.

pub fn get_unknown_gen_mut(&mut self, i: usize) -> Option<(&mut T, Index)>[src]

Given a i of usize without generation, get an exclusive reference to the element and the matching Index of the entry behind i.

This method is useful when you know there might be an element at the position i, but don't know its generation or precise Index.

Use cases include using indexing such as Hierarchical BitMap Indexing or other kinds of bit-efficient indexing.

You should use the get_mut method most of the time.

Trait Implementations

impl<T: Debug> Debug for Arena<T>[src]

impl<T> Index<Index> for Arena<T>[src]

type Output = T

The returned type after indexing.

impl<T> IndexMut<Index> for Arena<T>[src]

impl<T> FromIterator<T> for Arena<T>[src]

impl<T> IntoIterator for Arena<T>[src]

type Item = T

The type of the elements being iterated over.

type IntoIter = IntoIter<T>

Which kind of iterator are we turning this into?

impl<'a, T> IntoIterator for &'a Arena<T>[src]

type Item = (Index, &'a T)

The type of the elements being iterated over.

type IntoIter = Iter<'a, T>

Which kind of iterator are we turning this into?

impl<'a, T> IntoIterator for &'a mut Arena<T>[src]

type Item = (Index, &'a mut T)

The type of the elements being iterated over.

type IntoIter = IterMut<'a, T>

Which kind of iterator are we turning this into?

impl<T> Extend<T> for Arena<T>[src]

impl<T: Clone> Clone for Arena<T>[src]

Auto Trait Implementations

impl<T> Unpin for Arena<T> where
    T: Unpin

impl<T> Send for Arena<T> where
    T: Send

impl<T> Sync for Arena<T> where
    T: Sync

impl<T> UnwindSafe for Arena<T> where
    T: UnwindSafe

impl<T> RefUnwindSafe for Arena<T> where
    T: RefUnwindSafe

Blanket Implementations

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

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<I> IntoIterator for I where
    I: Iterator
[src]

type Item = <I as Iterator>::Item

The type of the elements being iterated over.

type IntoIter = I

Which kind of iterator are we turning this into?

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

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.

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

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

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

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

type Owned = T

The resulting type after obtaining ownership.