Skip to main content

GapBuffer

Struct GapBuffer 

Source
pub struct GapBuffer<T>(/* private fields */);
Expand description

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

GapBuffer<T> has methods similar to Vec.

Implementations§

Source§

impl<T> GapBuffer<T>

Source

pub const fn new() -> Self

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 gap_buf::GapBuffer;

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

pub fn with_capacity(capacity: usize) -> Self

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

§Examples
use gap_buf::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);
Source

pub const fn capacity(&self) -> usize

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

§Examples
use gap_buf::GapBuffer;

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

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

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 gap_buf::GapBuffer;

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

pub fn reserve_exact(&mut self, additional: usize)

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 gap_buf::GapBuffer;

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

pub fn shrink_to_fit(&mut self)

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

§Examples
use gap_buf::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);
Source

pub fn set_gap(&mut self, gap: usize)

Set gap offset of the GapBuffer<T>.

§Panics

Panics if index > len.

§Computational amount

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

Source

pub const fn gap(&self) -> usize

Return gap offset of the GapBuffer<T>.

Source

pub fn insert(&mut self, index: usize, element: 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()|

Source

pub fn insert_iter(&mut self, index: usize, iter: impl IntoIterator<Item = T>)

👎Deprecated: insert_iter renamed to insert_many.
Source

pub fn insert_many(&mut self, index: usize, iter: impl IntoIterator<Item = T>)

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.

Source

pub fn push_back(&mut self, value: T)

Appends an element to the back of a GapBuffer.

§Panics

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

Source

pub fn push_front(&mut self, value: T)

Prepends an element to the GapBuffer.

§Panics

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

Source

pub fn swap_remove(&mut self, index: usize) -> T

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 gap_buf::gap_buffer;

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]);
Source

pub fn remove(&mut self, index: usize) -> T

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 gap_buf::gap_buffer;

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]);
Source

pub fn clear(&mut self)

Clears the GapBuffer, removing all values.

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

Source

pub fn truncate(&mut self, len: usize)

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
use gap_buf::gap_buffer;

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

pub fn retain(&mut self, f: impl FnMut(&T) -> bool)

Retains only the elements specified by the predicate.

§Examples
use gap_buf::gap_buffer;

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

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

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

Source

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

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

Source

pub fn drain(&mut self, range: impl RangeBounds<usize>) -> Drain<'_, 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
use gap_buf::gap_buffer;

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);
Source

pub fn extract_if<F>( &mut self, range: impl RangeBounds<usize>, filter: F, ) -> ExtractIf<'_, T, F>
where F: FnMut(&mut T) -> bool,

Creates an extracting iterator that removes elements from the specified range in the GapBuffer based on a predicate

Note that this iterator will only remove elements that are consumed. If dropped, it will retain the remaining elements.

§Panics

Panics if the range is out of bounds.

§Examples
use gap_buf::gap_buffer;

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

let d : Vec<_> = buf.extract_if(.., |num| *num % 2 == 1).collect();
assert_eq!(buf, [2, 4]);
assert_eq!(d, [1, 3]);

buf.extract_if(.., |_| true);
assert_eq!(buf.is_empty(), false);
Source

pub fn splice<I: IntoIterator<Item = T>>( &mut self, range: impl RangeBounds<usize>, replace_with: I, ) -> Splice<'_, T, I::IntoIter>

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
use gap_buf::gap_buffer;

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]);
Source§

impl<T> GapBuffer<T>
where T: Clone,

Source

pub fn resize(&mut self, new_len: usize, value: T)

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

Methods from Deref<Target = Slice<T>>§

Source

pub fn len(&self) -> usize

Returns the number of elements in the GapBuffer.

Source

pub fn is_empty(&self) -> bool

Returns true if the GapBuffer contains no elements.

Source

pub fn get(&self, index: usize) -> Option<&T>

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

Source

pub fn get_mut(&mut self, index: usize) -> Option<&mut T>

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

Source

pub fn swap(&mut self, a: usize, b: usize)

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().

Source

pub fn range(&self, range: impl RangeBounds<usize>) -> Range<'_, T>

Return a immutable sub-range of this Slice.

§Panics

Panics if range is out of bounds.

§Examples
use gap_buf::gap_buffer;

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]);
Source

pub fn range_mut(&mut self, range: impl RangeBounds<usize>) -> RangeMut<'_, T>

Return a mutable sub-range of this Slice.

§Panics

Panics if range is out of bounds.

§Examples
use gap_buf::gap_buffer;

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]);
Source

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

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

§Examples
use gap_buf::gap_buffer;

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]);
Source

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

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

§Examples
use gap_buf::gap_buffer;

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]);
Source

pub fn iter(&self) -> Iter<'_, T>

Returns an iterator over the Slice.

Source

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

Returns an iterator that allows modifying each value.

Trait Implementations§

Source§

impl<T: Clone> Clone for GapBuffer<T>

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<T> Debug for GapBuffer<T>
where T: Debug,

Source§

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

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

impl<T> Default for GapBuffer<T>

Source§

fn default() -> Self

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

impl<T> Deref for GapBuffer<T>

Source§

type Target = Slice<T>

The resulting type after dereferencing.
Source§

fn deref(&self) -> &Self::Target

Dereferences the value.
Source§

impl<T> DerefMut for GapBuffer<T>

Source§

fn deref_mut(&mut self) -> &mut Self::Target

Mutably dereferences the value.
Source§

impl<T> Drop for GapBuffer<T>

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

impl<'gb, T: 'gb + Copy> Extend<&'gb T> for GapBuffer<T>

Source§

fn extend<I: IntoIterator<Item = &'gb T>>(&mut self, iter: I)

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

fn extend_one(&mut self, item: A)

🔬This is a nightly-only experimental API. (extend_one)
Extends a collection with exactly one element.
Source§

fn extend_reserve(&mut self, additional: usize)

🔬This is a nightly-only experimental API. (extend_one)
Reserves capacity in a collection for the given number of additional elements. Read more
Source§

impl<T> Extend<T> for GapBuffer<T>

Source§

fn extend<I: IntoIterator<Item = T>>(&mut self, iter: I)

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

fn extend_one(&mut self, item: A)

🔬This is a nightly-only experimental API. (extend_one)
Extends a collection with exactly one element.
Source§

fn extend_reserve(&mut self, additional: usize)

🔬This is a nightly-only experimental API. (extend_one)
Reserves capacity in a collection for the given number of additional elements. Read more
Source§

impl<T> From<Vec<T>> for GapBuffer<T>

Source§

fn from(value: Vec<T>) -> Self

An O(1) conversion from a Vec<T>.

Source§

impl<T> FromIterator<T> for GapBuffer<T>

Source§

fn from_iter<S: IntoIterator<Item = T>>(s: S) -> GapBuffer<T>

Creates a value from an iterator. Read more
Source§

impl<T: Hash> Hash for GapBuffer<T>

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl<T> Index<usize> for GapBuffer<T>

Source§

type Output = T

The returned type after indexing.
Source§

fn index(&self, index: usize) -> &T

Performs the indexing (container[index]) operation. Read more
Source§

impl<T> IndexMut<usize> for GapBuffer<T>

Source§

fn index_mut(&mut self, index: usize) -> &mut T

Performs the mutable indexing (container[index]) operation. Read more
Source§

impl<'gb, T> IntoIterator for &'gb GapBuffer<T>

Source§

type Item = &'gb T

The type of the elements being iterated over.
Source§

type IntoIter = Chain<Iter<'gb, T>, Iter<'gb, T>>

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> Iter<'gb, T>

Creates an iterator from a value. Read more
Source§

impl<'gb, T> IntoIterator for &'gb mut GapBuffer<T>

Source§

type Item = &'gb mut T

The type of the elements being iterated over.
Source§

type IntoIter = Chain<IterMut<'gb, T>, IterMut<'gb, T>>

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> IterMut<'gb, T>

Creates an iterator from a value. Read more
Source§

impl<T> IntoIterator for GapBuffer<T>

Source§

type Item = T

The type of the elements being iterated over.
Source§

type IntoIter = IntoIter<T>

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> IntoIter<T>

Creates an iterator from a value. Read more
Source§

impl<T: Ord> Ord for GapBuffer<T>

Source§

fn cmp(&self, other: &Self) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

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

Source§

fn eq(&self, other: &S) -> 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, S> PartialOrd<S> for GapBuffer<T>
where T: PartialOrd, S: ?Sized, for<'b> &'b S: IntoIterator<Item = &'b T>,

Source§

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

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

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

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · Source§

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

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · Source§

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

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · Source§

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

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl<T: Eq> Eq for GapBuffer<T>

Auto Trait Implementations§

§

impl<T> Freeze for GapBuffer<T>

§

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

§

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

§

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

§

impl<T> Unpin for GapBuffer<T>

§

impl<T> UnsafeUnpin for GapBuffer<T>

§

impl<T> UnwindSafe for GapBuffer<T>
where T: RefUnwindSafe,

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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<P, T> Receiver for P
where P: Deref<Target = T> + ?Sized, T: ?Sized,

Source§

type Target = T

🔬This is a nightly-only experimental API. (arbitrary_self_types)
The target type on which the method may be called.
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.