Struct std::collections::RingBufExperimental [-]  [+] [src]

pub struct RingBuf<T> {
    // some fields omitted
}

RingBuf is a circular buffer, which can be used as a double-ended queue efficiently.

Methods

impl<T> RingBuf<T>

fn new() -> RingBuf<T>

Creates an empty RingBuf.

fn with_capacity(n: uint) -> RingBuf<T>

Creates an empty RingBuf with space for at least n elements.

fn get(&self, i: uint) -> Option<&T>

Retrieves an element in the RingBuf by index.

Examples

fn main() { use std::collections::RingBuf; let mut buf = RingBuf::new(); buf.push_back(3i); buf.push_back(4); buf.push_back(5); assert_eq!(buf.get(1).unwrap(), &4); }
use std::collections::RingBuf;

let mut buf = RingBuf::new();
buf.push_back(3i);
buf.push_back(4);
buf.push_back(5);
assert_eq!(buf.get(1).unwrap(), &4);

fn get_mut(&mut self, i: uint) -> Option<&mut T>

Retrieves an element in the RingBuf mutably by index.

Examples

fn main() { use std::collections::RingBuf; let mut buf = RingBuf::new(); buf.push_back(3i); buf.push_back(4); buf.push_back(5); match buf.get_mut(1) { None => {} Some(elem) => { *elem = 7; } } assert_eq!(buf[1], 7); }
use std::collections::RingBuf;

let mut buf = RingBuf::new();
buf.push_back(3i);
buf.push_back(4);
buf.push_back(5);
match buf.get_mut(1) {
    None => {}
    Some(elem) => {
        *elem = 7;
    }
}

assert_eq!(buf[1], 7);

fn swap(&mut self, i: uint, j: uint)

Swaps elements at indices i and j.

i and j may be equal.

Fails if there is no element with either index.

Examples

fn main() { use std::collections::RingBuf; let mut buf = RingBuf::new(); buf.push_back(3i); buf.push_back(4); buf.push_back(5); buf.swap(0, 2); assert_eq!(buf[0], 5); assert_eq!(buf[2], 3); }
use std::collections::RingBuf;

let mut buf = RingBuf::new();
buf.push_back(3i);
buf.push_back(4);
buf.push_back(5);
buf.swap(0, 2);
assert_eq!(buf[0], 5);
assert_eq!(buf[2], 3);

fn capacity(&self) -> uint

Returns the number of elements the RingBuf can hold without reallocating.

Examples

fn main() { use std::collections::RingBuf; let buf: RingBuf<int> = RingBuf::with_capacity(10); assert!(buf.capacity() >= 10); }
use std::collections::RingBuf;

let buf: RingBuf<int> = RingBuf::with_capacity(10);
assert!(buf.capacity() >= 10);

fn reserve_exact(&mut self, additional: uint)

Reserves the minimum capacity for exactly additional more elements to be inserted in the given RingBuf. Does nothing if the capacity is already sufficient.

Note that the allocator may give the collection more space than it requests. Therefore capacity can not be relied upon to be precisely minimal. Prefer reserve if future insertions are expected.

Panics

Panics if the new capacity overflows uint.

Examples

fn main() { use std::collections::RingBuf; let mut buf: RingBuf<int> = vec![1].into_iter().collect(); buf.reserve_exact(10); assert!(buf.capacity() >= 11); }
use std::collections::RingBuf;

let mut buf: RingBuf<int> = vec![1].into_iter().collect();
buf.reserve_exact(10);
assert!(buf.capacity() >= 11);

fn reserve(&mut self, additional: uint)

Reserves capacity for at least additional more elements to be inserted in the given Ringbuf. The collection may reserve more space to avoid frequent reallocations.

Panics

Panics if the new capacity overflows uint.

Examples

fn main() { use std::collections::RingBuf; let mut buf: RingBuf<int> = vec![1].into_iter().collect(); buf.reserve(10); assert!(buf.capacity() >= 11); }
use std::collections::RingBuf;

let mut buf: RingBuf<int> = vec![1].into_iter().collect();
buf.reserve(10);
assert!(buf.capacity() >= 11);

fn iter(&self) -> Iter<T>

Returns a front-to-back iterator.

Examples

fn main() { use std::collections::RingBuf; let mut buf = RingBuf::new(); buf.push_back(5i); buf.push_back(3); buf.push_back(4); let b: &[_] = &[&5, &3, &4]; assert_eq!(buf.iter().collect::<Vec<&int>>().as_slice(), b); }
use std::collections::RingBuf;

let mut buf = RingBuf::new();
buf.push_back(5i);
buf.push_back(3);
buf.push_back(4);
let b: &[_] = &[&5, &3, &4];
assert_eq!(buf.iter().collect::<Vec<&int>>().as_slice(), b);

fn iter_mut(&'a mut self) -> IterMut<'a, T>

Returns a front-to-back iterator that returns mutable references.

Examples

fn main() { use std::collections::RingBuf; let mut buf = RingBuf::new(); buf.push_back(5i); buf.push_back(3); buf.push_back(4); for num in buf.iter_mut() { *num = *num - 2; } let b: &[_] = &[&mut 3, &mut 1, &mut 2]; assert_eq!(buf.iter_mut().collect::<Vec<&mut int>>()[], b); }
use std::collections::RingBuf;

let mut buf = RingBuf::new();
buf.push_back(5i);
buf.push_back(3);
buf.push_back(4);
for num in buf.iter_mut() {
    *num = *num - 2;
}
let b: &[_] = &[&mut 3, &mut 1, &mut 2];
assert_eq!(buf.iter_mut().collect::<Vec<&mut int>>()[], b);

fn into_iter(self) -> IntoIter<T>

Consumes the list into an iterator yielding elements by value.

fn as_slices(&'a self) -> (&'a [T], &'a [T])

Returns a pair of slices which contain, in order, the contents of the RingBuf.

fn as_mut_slices(&'a mut self) -> (&'a mut [T], &'a mut [T])

Returns a pair of slices which contain, in order, the contents of the RingBuf.

fn len(&self) -> uint

Returns the number of elements in the RingBuf.

Examples

fn main() { use std::collections::RingBuf; let mut v = RingBuf::new(); assert_eq!(v.len(), 0); v.push_back(1i); assert_eq!(v.len(), 1); }
use std::collections::RingBuf;

let mut v = RingBuf::new();
assert_eq!(v.len(), 0);
v.push_back(1i);
assert_eq!(v.len(), 1);

fn is_empty(&self) -> bool

Returns true if the buffer contains no elements

Examples

fn main() { use std::collections::RingBuf; let mut v = RingBuf::new(); assert!(v.is_empty()); v.push_front(1i); assert!(!v.is_empty()); }
use std::collections::RingBuf;

let mut v = RingBuf::new();
assert!(v.is_empty());
v.push_front(1i);
assert!(!v.is_empty());

fn drain(&'a mut self) -> Drain<'a, T>

Creates a draining iterator that clears the RingBuf and iterates over the removed items from start to end.

Examples

fn main() { use std::collections::RingBuf; let mut v = RingBuf::new(); v.push_back(1i); assert_eq!(v.drain().next(), Some(1)); assert!(v.is_empty()); }
use std::collections::RingBuf;

let mut v = RingBuf::new();
v.push_back(1i);
assert_eq!(v.drain().next(), Some(1));
assert!(v.is_empty());

fn clear(&mut self)

Clears the buffer, removing all values.

Examples

fn main() { use std::collections::RingBuf; let mut v = RingBuf::new(); v.push_back(1i); v.clear(); assert!(v.is_empty()); }
use std::collections::RingBuf;

let mut v = RingBuf::new();
v.push_back(1i);
v.clear();
assert!(v.is_empty());

fn front(&self) -> Option<&T>

Provides a reference to the front element, or None if the sequence is empty.

Examples

fn main() { use std::collections::RingBuf; let mut d = RingBuf::new(); assert_eq!(d.front(), None); d.push_back(1i); d.push_back(2i); assert_eq!(d.front(), Some(&1i)); }
use std::collections::RingBuf;

let mut d = RingBuf::new();
assert_eq!(d.front(), None);

d.push_back(1i);
d.push_back(2i);
assert_eq!(d.front(), Some(&1i));

fn front_mut(&mut self) -> Option<&mut T>

Provides a mutable reference to the front element, or None if the sequence is empty.

Examples

fn main() { use std::collections::RingBuf; let mut d = RingBuf::new(); assert_eq!(d.front_mut(), None); d.push_back(1i); d.push_back(2i); match d.front_mut() { Some(x) => *x = 9i, None => (), } assert_eq!(d.front(), Some(&9i)); }
use std::collections::RingBuf;

let mut d = RingBuf::new();
assert_eq!(d.front_mut(), None);

d.push_back(1i);
d.push_back(2i);
match d.front_mut() {
    Some(x) => *x = 9i,
    None => (),
}
assert_eq!(d.front(), Some(&9i));

fn back(&self) -> Option<&T>

Provides a reference to the back element, or None if the sequence is empty.

Examples

fn main() { use std::collections::RingBuf; let mut d = RingBuf::new(); assert_eq!(d.back(), None); d.push_back(1i); d.push_back(2i); assert_eq!(d.back(), Some(&2i)); }
use std::collections::RingBuf;

let mut d = RingBuf::new();
assert_eq!(d.back(), None);

d.push_back(1i);
d.push_back(2i);
assert_eq!(d.back(), Some(&2i));

fn back_mut(&mut self) -> Option<&mut T>

Provides a mutable reference to the back element, or None if the sequence is empty.

Examples

fn main() { use std::collections::RingBuf; let mut d = RingBuf::new(); assert_eq!(d.back(), None); d.push_back(1i); d.push_back(2i); match d.back_mut() { Some(x) => *x = 9i, None => (), } assert_eq!(d.back(), Some(&9i)); }
use std::collections::RingBuf;

let mut d = RingBuf::new();
assert_eq!(d.back(), None);

d.push_back(1i);
d.push_back(2i);
match d.back_mut() {
    Some(x) => *x = 9i,
    None => (),
}
assert_eq!(d.back(), Some(&9i));

fn pop_front(&mut self) -> Option<T>

Removes the first element and returns it, or None if the sequence is empty.

Examples

fn main() { use std::collections::RingBuf; let mut d = RingBuf::new(); d.push_back(1i); d.push_back(2i); assert_eq!(d.pop_front(), Some(1i)); assert_eq!(d.pop_front(), Some(2i)); assert_eq!(d.pop_front(), None); }
use std::collections::RingBuf;

let mut d = RingBuf::new();
d.push_back(1i);
d.push_back(2i);

assert_eq!(d.pop_front(), Some(1i));
assert_eq!(d.pop_front(), Some(2i));
assert_eq!(d.pop_front(), None);

fn push_front(&mut self, t: T)

Inserts an element first in the sequence.

Examples

fn main() { use std::collections::RingBuf; let mut d = RingBuf::new(); d.push_front(1i); d.push_front(2i); assert_eq!(d.front(), Some(&2i)); }
use std::collections::RingBuf;

let mut d = RingBuf::new();
d.push_front(1i);
d.push_front(2i);
assert_eq!(d.front(), Some(&2i));

fn push(&mut self, t: T)

Deprecated: Renamed to push_back.

fn push_back(&mut self, t: T)

Appends an element to the back of a buffer

Examples

fn main() { use std::collections::RingBuf; let mut buf = RingBuf::new(); buf.push_back(1i); buf.push_back(3); assert_eq!(3, *buf.back().unwrap()); }
use std::collections::RingBuf;

let mut buf = RingBuf::new();
buf.push_back(1i);
buf.push_back(3);
assert_eq!(3, *buf.back().unwrap());

fn pop(&mut self) -> Option<T>

Deprecated: Renamed to pop_back.

fn pop_back(&mut self) -> Option<T>

Removes the last element from a buffer and returns it, or None if it is empty.

Examples

fn main() { use std::collections::RingBuf; let mut buf = RingBuf::new(); assert_eq!(buf.pop_back(), None); buf.push_back(1i); buf.push_back(3); assert_eq!(buf.pop_back(), Some(3)); }
use std::collections::RingBuf;

let mut buf = RingBuf::new();
assert_eq!(buf.pop_back(), None);
buf.push_back(1i);
buf.push_back(3);
assert_eq!(buf.pop_back(), Some(3));

fn insert(&mut self, i: uint, t: T)

Inserts an element at position i within the ringbuf. Whichever end is closer to the insertion point will be moved to make room, and all the affected elements will be moved to new positions.

Panics

Panics if i is greater than ringbuf's length

Example

fn main() { use std::collections::RingBuf; let mut buf = RingBuf::new(); buf.push_back(10i); buf.push_back(12); buf.insert(1,11); assert_eq!(Some(&11), buf.get(1)); }
use std::collections::RingBuf;

let mut buf = RingBuf::new();
buf.push_back(10i);
buf.push_back(12);
buf.insert(1,11);
assert_eq!(Some(&11), buf.get(1));

fn remove(&mut self, i: uint) -> Option<T>

Removes and returns the element at position i from the ringbuf. Whichever end is closer to the removal point will be moved to make room, and all the affected elements will be moved to new positions. Returns None if i is out of bounds.

Example

fn main() { use std::collections::RingBuf; let mut buf = RingBuf::new(); buf.push_back(5i); buf.push_back(10i); buf.push_back(12i); buf.push_back(15); buf.remove(2); assert_eq!(Some(&15), buf.get(2)); }
use std::collections::RingBuf;

let mut buf = RingBuf::new();
buf.push_back(5i);
buf.push_back(10i);
buf.push_back(12i);
buf.push_back(15);
buf.remove(2);
assert_eq!(Some(&15), buf.get(2));

Trait Implementations

impl<T> Send for RingBuf<T>

impl<T> Sync for RingBuf<T>

impl<T: Clone> Clone for RingBuf<T>

fn clone(&self) -> RingBuf<T>

fn clone_from(&mut self, &RingBuf<T>)

impl<T> Drop for RingBuf<T>

fn drop(&mut self)

impl<T> Default for RingBuf<T>

fn default() -> RingBuf<T>

impl<A: PartialEq<A>> PartialEq<RingBuf<A>> for RingBuf<A>

fn eq(&self, other: &RingBuf<A>) -> bool

fn ne(&self, &RingBuf<A>) -> bool

impl<A: Eq> Eq for RingBuf<A>

fn assert_receiver_is_total_eq(&self)

impl<A: PartialOrd<A>> PartialOrd<RingBuf<A>> for RingBuf<A>

fn partial_cmp(&self, other: &RingBuf<A>) -> Option<Ordering>

fn lt(&self, &RingBuf<A>) -> bool

fn le(&self, &RingBuf<A>) -> bool

fn gt(&self, &RingBuf<A>) -> bool

fn ge(&self, &RingBuf<A>) -> bool

impl<A: Ord> Ord for RingBuf<A>

fn cmp(&self, other: &RingBuf<A>) -> Ordering

impl<S: Writer, A: Hash<S>> Hash<S> for RingBuf<A>

fn hash(&self, state: &mut S)

impl<A> Index<uint, A> for RingBuf<A>

fn index(&'a self, i: &uint) -> &'a A

impl<A> IndexMut<uint, A> for RingBuf<A>

fn index_mut(&'a mut self, i: &uint) -> &'a mut A

impl<A> FromIterator<A> for RingBuf<A>

fn from_iter<T: Iterator<A>>(iterator: T) -> RingBuf<A>

impl<A> Extend<A> for RingBuf<A>

fn extend<T: Iterator<A>>(&mut self, iterator: T)

impl<T: Show> Show for RingBuf<T>

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