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

A doubly-linked list that uses handles to refer to elements that exist within a vector. This allows for O(1) insertion and removal of elements from the list, and O(1) access to elements by handle.

Implementations§

Creates a new, empty LinkedVector.

Creates a new, empty LinkedVector with the specified capacity.

Moves all elements from other into self, leaving other empty. This operation completes in O(n) time where n is the length of other.

use linked_vector::*;
let mut lv1 = LinkedVector::new();
let mut lv2 = LinkedVector::from([1, 2, 3]);
 
lv1.append(&mut lv2);
 
assert_eq!(lv1.into_iter().collect::<Vec<_>>(), vec![1, 2, 3]);
assert_eq!(lv2.len(), 0);

Gives a reference to the back element, or None if the list is empty. This operation completes in O(1) time.

use linked_vector::*;
let mut lv = LinkedVector::from([1, 2, 3]);
assert_eq!(lv.back(), Some(&3));

Gives a mutable reference to the element back element, or None if the list is empty. This operation completes in O(1) time.

use linked_vector::*;
let mut lv = LinkedVector::from([1, 2, 3]);
 
*lv.back_mut().unwrap() = 42;
 
assert_eq!(lv.back_mut(), Some(&mut 42));

Returns the total number of elements the vector can hold without reallocating.

Removes all elements from the list.

Consumes the LinkedVector and produces a new one that has all its nodes placed contiguously in sequential order at the front of the internal vector. Where performance is critical and the cost of a compacting operation is infrequent and acceptible, compacting the vector may give a gain in performance for certain use cases. All handles from the old vector will not be native to the new compacted vector. compact() completes in O(n) time.

Returns true if the list contains an element with the given value. This operation completes in O(n) time where n is the length of the list.

Creates a cursor that can be used to traverse the list starting at the given node. This operation completes in O(1) time.

use linked_vector::*;
let lv = LinkedVector::from([1, 2, 3, 4, 5, 6, 7, 8, 9]);
let h3 = lv.handle(2).unwrap();
let mut cursor = lv.cursor(h3);
 
cursor.forward(3);
 
assert_eq!(*cursor, 6);

Creates a cursor that holds a mutable reference to the LinkedVector that can be used to traverse the list starting at the given node. If the vector is empty, None is returned. This operation completes in O(1) time.

use linked_vector::*;
let mut lv = LinkedVector::from([1, 2, 3, 4, 5, 6]);
let mut cursor = lv.cursor_mut(lv.front_node().unwrap());
 
cursor.forward(3);
 
assert_eq!(*cursor, 4);
 
*cursor = 42;
 
assert_eq!(lv.to_vec(), vec![1, 2, 3, 42, 5, 6]);

Returns a Cursor starting at the front element, or None if the list is empty. If the vector is empty, None is returned. This operation completes in O(1) time.

Returns a Cursor starting at the back element, or None if the list is empty. This operation completes in O(1) time.

Gives a reference to the element at the front of the vector, or None if the list is empty. This operation completes in O(1) time.

Gives a mutable Cursor starting at the front of the vector, or None if the list is empty. This operation completes in O(1) time.

Gives a reference to the element at the front of the vector, or None if the list is empty. This operation completes in O(1) time.

Gives a mutable reference to the element at the front of the vector, or None if the list is empty. This operation completes in O(1) time.

Returns a handle to the first node in the list, or None if the list is empty. This operation completes in O(1) time.

use linked_vector::*;
let mut lv = LinkedVector::from([1, 2, 3]);
let hnode = lv.push_front(42);
 
assert_eq!(lv.front_node(), Some(hnode));
assert_eq!(lv.front(), Some(&42));

Returns a handle to the last node in the list, or None if the list is empty. This operation completes in O(1) time.

Provides a reference to the element indicated by the given handle, or None if the handle is invalid. With the “optionless-accesors” feature, this method returns its reference directly - no Option, see usage notes. This operation completes in O(1) time.

use linked_vector::*;
let mut lv = LinkedVector::from([1, 2, 3]);
let hnode = lv.push_front(42);
 
assert_eq!(lv.get(hnode), Some(&42));

Provides a mutable reference to the element indicated by the given handle, or None if the handle is invalid. With the “optionless-accessors” feature enabled, this method returns its reference directly - no Option, see usage notes. This operation completes in O(1) time.

use linked_vector::*;
let mut lv = LinkedVector::new();
let hnode = lv.push_front(0);
 
*lv.get_mut(hnode).unwrap() = 42;
 
assert_eq!(lv[hnode], 42);

Returns the handle to the node at the given index, or None if the index is out of bounds. If index > self.len / 2, the search starts from the end of the list. This operation performs in O(n / 2) time worst case.

use linked_vector::*;
let mut lv = LinkedVector::new();
let h1 = lv.push_front(1);
let h2 = lv.push_front(2);
let h3 = lv.push_front(3);
 
assert_eq!(lv.handle(1), Some(h2));
assert_eq!(lv.handle(3), None);
assert_eq!(lv.handle(2), Some(h1));

Returns an iterator over the handles of the vector. The handles will reflect the order of the linked list. This operation completes in O(1) time.

use linked_vector::*;
let mut lv = LinkedVector::new();
 
let h1 = lv.push_back(42);
let h2 = lv.push_back(43);
let h3 = lv.push_back(44);
 
let iter = lv.handles();
 
assert_eq!(iter.collect::<Vec<_>>(), vec![h1, h2, h3]);

Inserts a new element at the position indicated by the handle, node. Returns a handle to the newly inserted element. This operation completes in O(1) time.

use linked_vector::*;
let mut lv = LinkedVector::new();
 
let h1 = lv.push_back(42);
let h2 = lv.insert(h1, 43);
 
assert_eq!(lv.next_node(h2), Some(h1));
assert_eq!(lv[h1], 42);

Inserts a new element after the one indicated by the handle, node. Returns a handle to the newly inserted element. This operation completes in O(1) time.

use linked_vector::*;
let mut lv = LinkedVector::new();
 
let h1 = lv.push_back(42);
let h2 = lv.insert_after(h1, 43);
 
assert_eq!(lv.next_node(h1), Some(h2));
assert_eq!(lv[h2], 43);

Returns true if the list contains no elements.

Returns an iterator over the elements of the list.

Returns an iterator over the elements of the list. Renders mutable references to the elements.

use linked_vector::*;
let mut lv = LinkedVector::from([1, 2, 3]);
 
lv.iter_mut().for_each(|x| *x += 1);
 
assert_eq!(lv, LinkedVector::from([2, 3, 4]));

Returns the length of the list.

Returns a handle to the next node in the list, or None if the given handle is the last node in the list. This operation completes in O(1)

use linked_vector::*;
let mut lv = LinkedVector::new();
 
let h1 = lv.push_back(42);
let h2 = lv.push_back(43);
 
assert_eq!(lv.next_node(h1), Some(h2));

Returns a reference to the next element’s value in the list, or None if the given handle is the last node in the list. This operation completes in O(1) time.

Returns a mutable reference to the next element’s value in the list, or None if the given handle is the last node in the list. This operation completes in O(1) time.

Returns a handle to the previous node in the list, or None if the given handle is the first node in the list. This operation completes in O(1) time.

use linked_vector::*;
let mut lv = LinkedVector::new();
 
let h1 = lv.push_back(42);
let h2 = lv.push_back(43);
 
assert_eq!(lv.prev_node(h2), Some(h1));

Returns a reference to the previous element’s value in the list, or None if the given handle is the first node in the list. This operation completes in O(1) time.

Returns a mutable reference to the previous element’s value in the list, or None if the given handle is the first node in the list. This operation completes in O(1) time.

Pops the last element of the vector. Returns None if the vector is empty. This operation completes in O(1) time.

use linked_vector::*;
let mut lv = LinkedVector::from([1, 2, 3]);
 
assert_eq!(lv.pop_back(), Some(3));

Pops the first element of the vector. Returns None if the vector is empty. This operation completes in O(1) time.

use linked_vector::*;
let mut lv = LinkedVector::from([1, 2, 3]);
 
assert_eq!(lv.pop_front(), Some(1));

Pushes a new element to the back of the list. Returns a handle to the newly inserted element. This operation completes in O(1) time.

use linked_vector::*;
let mut lv = LinkedVector::new();
 
let h1 = lv.push_back(42);
let h2 = lv.push_back(43);
 
assert_eq!(lv.next_node(h1), Some(h2));

Pushes a new element to the front of the list. Returns a handle to the newly inserted element. This operation completes in O(1) time.

use linked_vector::*;
let mut lv = LinkedVector::from([1, 2, 3]);
 
let h1 = lv.front_node().unwrap();
let h2 = lv.push_front(42);
 
assert_eq!(lv.next_node(h2), Some(h1));

Removes the element indicated by the handle, node. Returns the element if the handle is valid, or None otherwise. This operation completes in O(1) time. With the "optionless-accessors" feature enabled, this method returns its value directly, not wrapped in an Option, see usage notes.

use linked_vector::*;
let mut lv = LinkedVector::from([1, 2, 3]);
let handles = lv.handles().collect::<Vec<_>>();
 
lv.remove(handles[1]);
 
assert_eq!(lv, LinkedVector::from([1, 3]));

Sorts the elemements in place in ascending order. Previously held handles will still be valid and reference the same elements (with the same values) as before. Only the next and prev fields of the nodes are modified in the list. Uses Rust’s stable sort internally and requires some auxiliary memory for a temporary handle list.

use linked_vector::*;
let mut lv = LinkedVector::new();
let h1 = lv.push_back(3);
let h2 = lv.push_back(2);
let h3 = lv.push_back(1);
 
lv.extend([7, 11, 4, 6, 8, 13, 12, 9, 14, 5, 10]);
 
lv.sort();
 
assert_eq!(lv.to_vec(), (1..15).collect::<Vec<_>>());
assert_eq!(lv[h1], 3);
assert_eq!(lv[h2], 2);
assert_eq!(lv[h3], 1);

Sorts the elemements in place using the provided comparison function. See sort() for more details.

use linked_vector::*;
let mut lv = LinkedVector::from([1, 2, 3, 4, 5]);
 
lv.sort_by(|a, b| b.cmp(a));
 
assert_eq!(lv.to_vec(), vec![5, 4, 3, 2, 1]);

Sorts the elemements in place in using the provided key extraction function. See sort() for more details.

use linked_vector::*;
let mut lv = LinkedVector::from([1, 2, 3, 4, 5]);
 
lv.sort_by_key(|k| -k);
 
assert_eq!(lv.to_vec(), vec![5, 4, 3, 2, 1]);

Sorts the elemements in place in ascending order. Previously held handles will still be valid and reference the same elements (with the same values) as before. Only the next and prev fields of the nodes are modified in the list. Uses Rust’s unstable sort internally and requires some auxiliary memory for a temporary handle list.

use linked_vector::*;
let mut lv = LinkedVector::from([5, 4, 3, 2, 1, 0]);
 
lv.sort_unstable();
 
assert_eq!(lv.to_vec(), vec![0, 1, 2, 3, 4, 5]);

Sorts the elemements in place using the provided comparison function. See sort_unstable() for more details.

use linked_vector::*;
let mut lv = LinkedVector::from([1, 2, 3, 4, 5]);
 
lv.sort_unstable_by(|a, b| b.cmp(a));
 
assert_eq!(lv.to_vec(), vec![5, 4, 3, 2, 1]);

Sorts the elemements in place in using the provided key extraction function. See sort_unstable() for more details.

use linked_vector::*;
let mut lv = LinkedVector::from([1, 2, 3, 4, 5]);
 
lv.sort_unstable_by_key(|k| -k);
 
assert_eq!(lv.to_vec(), vec![5, 4, 3, 2, 1]);

Returns a vector containing the elements of the list. This operation completes in O(n) time.

use linked_vector::*;
let lv = LinkedVector::from([1, 2, 3]);
 
assert_eq!(lv.to_vec(), vec![1, 2, 3]);

Trait Implementations§

Returns a copy of the value. Read more
Performs copy-assignment from source. Read more
Formats the value using the given formatter. Read more

Renders the default value for an HNode. This will internally be set to BAD_HANDLE which is a handle that is invalid.

Extends a collection with the contents of an iterator. Read more
🔬This is a nightly-only experimental API. (extend_one)
Extends a collection with exactly one element.
🔬This is a nightly-only experimental API. (extend_one)
Reserves capacity in a collection for the given number of additional elements. Read more
Extends a collection with the contents of an iterator. Read more
🔬This is a nightly-only experimental API. (extend_one)
Extends a collection with exactly one element.
🔬This is a nightly-only experimental API. (extend_one)
Reserves capacity in a collection for the given number of additional elements. Read more
Converts to this type from the input type.
Creates a value from an iterator. Read more
Feeds this value into the given Hasher. Read more
Feeds a slice of this type into the given Hasher. Read more
The returned type after indexing.
Performs the indexing (container[index]) operation. Read more
The returned type after indexing.
Performs the indexing (container[index]) operation. Read more
Performs the mutable indexing (container[index]) operation. Read more
Performs the mutable indexing (container[index]) operation. Read more
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
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
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
This method tests for self and other values to be equal, and is used by ==. Read more
This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason. Read more

Auto Trait Implementations§

Blanket Implementations§

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The resulting type after obtaining ownership.
Creates owned data from borrowed data, usually by cloning. Read more
Uses borrowed data to replace owned data, usually by cloning. Read more
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.