Struct gapbuf::GapBuffer[][src]

pub struct GapBuffer<T>(_);

Dynamic array that allows efficient insertion and deletion operations clustered near the same location.

GapBuffer<T> has methods similar to Vec.

Methods

impl<T> GapBuffer<T>
[src]

Constructs a new, empty GapBuffer<T>.

The gap buffer will not allocate until elements are pushed onto it.

Examples

let mut buf = GapBuffer::<i32>::new();

assert_eq!(buf.is_empty(), true);
assert_eq!(buf.len(), 0);
assert_eq!(buf.capacity(), 0);
use gapbuf::GapBuffer;

let mut buf = GapBuffer::new();
buf.push_back(5);

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

Examples

use gapbuf::GapBuffer;

let buf: GapBuffer<i32> = GapBuffer::with_capacity(5);
assert_eq!(buf.is_empty(), true);
assert_eq!(buf.len(), 0);
assert_eq!(buf.capacity(), 5);

Returns the number of elements the GapBuffer<T> can hold without reallocating.

Examples

use gapbuf::GapBuffer;

let buf: GapBuffer<i32> = GapBuffer::with_capacity(10);
assert_eq!(buf.capacity(), 10);

Reserves capacity for at least additional more elements to be inserted in the given GapBuffer<T>. The collection 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.

Panics

Panics if the new capacity overflows usize.

Examples

use gapbuf::GapBuffer;

let mut buf = GapBuffer::new();
buf.push_back(1);
buf.reserve(10);
assert!(buf.capacity() >= 11);

Reserves the minimum capacity for exactly additional more elements to be inserted in the given GapBuffer<T>. After calling reserve_exact, capacity will be greater than or equal to self.len() + additional. 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 usize.

Examples

use gapbuf::GapBuffer;

let mut buf = GapBuffer::new();
buf.push_back(1);
buf.reserve_exact(10);
assert!(buf.capacity() >= 11);

Shrinks the capacity of the GapBuffer<T> as much as possible.

Examples

use gapbuf::GapBuffer;

let mut buf = GapBuffer::new();
buf.push_back(1);

buf.reserve(10);
assert!(buf.capacity() >= 11);

buf.shrink_to_fit();
assert_eq!(buf.capacity(), 1);

Set gap offset of the GapBuffer<T>.

Panics

Panics if index > len.

Computational amount

O(n) , n = |self.gap() - gap|

Return gap offset of the GapBuffer<T>.

Inserts an element at position index within the GapBuffer<T>.

Panics

Panics if index > len.

Panics if the number of elements in the gap buffer overflows a usize.

Computational amount

O(n) , n = |index - self.gap()|

Inserts multiple elements at position index within the GapBuffer<T>.

Panics

Panics if index > len.

Panics if the number of elements in the gap buffer overflows a usize.

Appends an element to the back of a GapBuffer.

Panics

Panics if the number of elements in the gap buffer overflows a usize.

Prepends an element to the GapBuffer.

Panics

Panics if the number of elements in the gap buffer overflows a usize.

Removes an element from the GapBuffer and returns it.

The removed element is replaced by the near the gap.

Panics

Panics if index >= self.len().

Computational amount

O(1)

Examples

use gapbuf::GapBuffer;

let mut buf = gap_buffer![1, 2, 3, 4, 5];
buf.set_gap(5);
let value = buf.swap_remove(0);
assert_eq!(value, 1);
assert_eq!(buf, [5, 2, 3, 4]);

Removes an element from the GapBuffer and returns it.

Panics

Panics if index >= self.len().

Computational amount

O(n), n = |index - self.gap()|

Examples

use gapbuf::GapBuffer;

let mut buf = gap_buffer![1, 2, 3, 4, 5];
let value = buf.remove(0);
assert_eq!(value, 1);
assert_eq!(buf, [2, 3, 4, 5]);

Clears the GapBuffer, removing all values.

Note that this method has no effect on the allocated capacity of the GapBuffer.

Shortens the GapBuffer, keeping the first len elements and dropping the rest.

If len is greater than the GapBuffer's current length, this has no effect.

Note that this method has no effect on the allocated capacity of the vector.

Examples

let mut buf = gap_buffer![1, 2, 3, 4];
buf.truncate(2);
assert_eq!(buf, [1, 2]);

Retains only the elements specified by the predicate.

Examples

let mut buf = gap_buffer![1, 2, 3, 4];
buf.retain(|&x| x%2 == 0);
assert_eq!(buf, [2, 4]);

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

Removes the last element and returns it, or None if the GapBuffer is empty.

Important traits for Drain<'a, T>

Creates a draining iterator that removes the specified range in the GapBuffer and yields the removed items.

Note 1: The element range is removed even if the iterator is only partially consumed or not consumed at all. Note 2: It is unspecified how many elements are removed from the GapBuffer if the Drain value is leaked.

Panics

Panics if the range is out of bounds.

Examples

let mut buf = gap_buffer![1, 2, 3, 4];

let d : Vec<_> = buf.drain(1..3).collect();
assert_eq!(buf, [1, 4]);
assert_eq!(d, [2, 3]);

buf.drain(..);
assert_eq!(buf.is_empty(), true);

Important traits for Splice<'a, T, I>

Creates a splicing iterator that replaces the specified range in the GapBuffer with the given replace_with iterator and yields the removed items. replace_with does not need to be the same length as range.

The element range is removed even if the iterator is not consumed until the end.

This is optimal if the length of range is equal to the length of replace_with. Otherwise, call GapBuffer::set_gap internally.

Examples

let mut b = gap_buffer![1, 2, 3, 4];
let r : Vec<_> = b.splice(1..3, vec![7, 8, 9]).collect();

assert_eq!(b, [1, 7, 8, 9, 4]);
assert_eq!(r, [2, 3]);

impl<T> GapBuffer<T> where
    T: Clone
[src]

Resize the GapBuffer<T> in-place so that len is equal to new_len.

Methods from Deref<Target = Slice<T>>

Returns the number of elements in the GapBuffer.

Returns true if the GapBuffer contains no elements.

Returns a reference to an element at index or None if out of bounds.

Returns a mutable reference to an element at index or None if out of bounds.

Swaps two elements in the GapBuffer.

Arguments

  • a - The index of the first element
  • b - The index of the second element

Panics

Panics if a >= self.len() or b >= self.len().

Return a immutable sub-range of this Slice.

Panics

Panics if range is out of bounds.

Examples

let buf = gap_buffer![1, 2, 3, 4, 5];

let r1 = buf.range(1..);
assert_eq!(r1, [2, 3, 4, 5]);

let r2 = r1.range(1..3);
assert_eq!(r2, [3, 4]);

Return a mutable sub-range of this Slice.

Panics

Panics if range is out of bounds.

Examples

let mut buf = gap_buffer![1, 2, 3, 4, 5];
{
    let mut r = buf.range_mut(1..);
    assert_eq!(r, [2, 3, 4, 5]);
    r[0] = 0;
}
assert_eq!(buf, [1, 0, 3, 4, 5]);

Returns a pair of slices. First slice is before gap. Second slice is after gap.

Examples

let mut buf = gap_buffer![1, 2, 3, 4, 5];
buf.set_gap(2);
let (s1, s2) = buf.as_slices();
assert_eq!(s1, [1, 2]);
assert_eq!(s2, [3, 4, 5]);

Returns a pair of slices. First slice is before gap. Second slice is after gap.

Examples

let mut buf = gap_buffer![1, 2, 3, 4, 5];
buf.set_gap(2);
{
    let (mut s1, mut s2) = buf.as_mut_slices();
    s1[0] = 10;
    s2[0] = 11;
}
assert_eq!(buf, [10, 2, 11, 4, 5]);

Returns an iterator over the Slice.

Returns an iterator that allows modifying each value.

Trait Implementations

impl<T: Hash> Hash for GapBuffer<T>
[src]

Feeds this value into the given [Hasher]. Read more

Feeds a slice of this type into the given [Hasher]. Read more

impl<T> Drop for GapBuffer<T>
[src]

Executes the destructor for this type. Read more

impl<T> Deref for GapBuffer<T>
[src]

The resulting type after dereferencing.

Dereferences the value.

impl<T> DerefMut for GapBuffer<T>
[src]

Mutably dereferences the value.

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

Creates a value from an iterator. Read more

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

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

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

Extends a collection with the contents of an iterator. Read more

impl<'a, T: 'a + Copy> Extend<&'a T> for GapBuffer<T>
[src]

Extends a collection with the contents of an iterator. Read more

impl<T> Default for GapBuffer<T>
[src]

Returns the "default value" for a type. Read more

impl<T> Index<usize> for GapBuffer<T>
[src]

The returned type after indexing.

Performs the indexing (container[index]) operation.

impl<T> IndexMut<usize> for GapBuffer<T>
[src]

Performs the mutable indexing (container[index]) operation.

impl<T> Debug for GapBuffer<T> where
    T: Debug
[src]

Formats the value using the given formatter. Read more

impl<T, S> PartialEq<S> for GapBuffer<T> where
    T: PartialEq,
    S: ?Sized,
    &'b S: IntoIterator<Item = &'b T>, 
[src]

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

impl<T: Eq> Eq for GapBuffer<T>
[src]

impl<T, S> PartialOrd<S> for GapBuffer<T> where
    T: PartialOrd,
    S: ?Sized,
    &'b S: IntoIterator<Item = &'b T>, 
[src]

This method returns an ordering between self and other values if one exists. Read more

This method tests less than (for self and other) and is used by the < operator. Read more

This method tests less than or equal to (for self and other) and is used by the <= operator. Read more

This method tests greater than (for self and other) and is used by the > operator. Read more

This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more

impl<T: Ord> Ord for GapBuffer<T>
[src]

This method returns an Ordering between self and other. Read more

Compares and returns the maximum of two values. Read more

Compares and returns the minimum of two values. Read more

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

The type of the elements being iterated over.

Which kind of iterator are we turning this into?

Important traits for IntoIter<T>

Creates an iterator from a value. Read more

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

The type of the elements being iterated over.

Which kind of iterator are we turning this into?

Creates an iterator from a value. Read more

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

The type of the elements being iterated over.

Which kind of iterator are we turning this into?

Creates an iterator from a value. Read more

Auto Trait Implementations

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

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