Struct fwdlist::Cursor
[−]
[src]
pub struct Cursor<'a, T: 'a> { /* fields omitted */ }
A cursor to navigate the list and reshape it.
Conceptually, a cursor moves between nodes, think the cursor of your text editor.
Cursor by example
use fwdlist::List; let mut q: List<_> = (0..5).collect(); let mut c = q.cursor();
So given the list [0,1,2,3,4]
, the cursor c
, represented by |
initially points to the first node:
|0 1 2 3 4
After advancing the cursor once c.next()
:
0|1 2 3 4
And after Advancing the cursor 4 times c.nth(4)
:
0 1 2 3 4|
Modifying the structure of the list
A cursor let you modify the list after its position (the tail
). A cursor
is really an abstraction of a mutable pointer to the next node of the list.
With a cursor, you can truncate the list, insert and removes nodes, etc.
Methods
impl<'a, T> Cursor<'a, T>
[src]
fn value(&self) -> Option<&T>
A read-only reference to the following node's value.
Return None
if the cursor is past the end of the list.
fn value_mut(&mut self) -> Option<&mut T>
A mutable reference to the following node's value.
Return None
if the cursor is past the end of the list.
fn next(&mut self) -> bool
Move the cursor past the following node. Returns true
on success,
false
if the cursor is already at the end of the list.
fn len(&self) -> usize
The lengths of the tail.
fn position(&self) -> usize
The position from the beginning of the list.
fn checkpoint(&mut self) -> Cursor<T>
Returns a copy of the cursor, freezing self
while the copy is alive.
fn nth(&mut self, nth: usize) -> usize
Move forward by nth
nodes in O(min(nth, self.len)).
Returns the number of nodes skipped, which could be less than nth
if
there is not enough remaining nodes.
fn last(&mut self) -> usize
Move forward before the last node of the list in O(self.len - 1).
Returns the number of nodes skipped, which could be less than nth
if
there is not enough remaining nodes.
fn end(&mut self) -> usize
Move the cursor forward after the end of the list in O(self.len).
fn insert(&mut self, v: T) -> &mut T
Create a new node containing the value v
and insert it at the current
location in O(1).
fn remove(&mut self) -> Option<T>
Remove the following node and return the contained value in O(1).
Return None
if the cursor is past the end of the list.
fn truncate(&mut self) -> List<T>
Truncate the list after the cursor, returning the tail in O(1).
fn splice(&mut self, other: &mut List<T>)
Insert the list other
after the cursor.
- In O(other.len()) if self.len > 0
- O(1) if self.len == 0
fn split(&mut self, after: usize) -> List<T>
Split the list after nth
and return the tail in O(min(at, self.len)).
This is the same as:
{ let mut c = c.checkpoint(); c.nth(after); c.truncate() }
fn remove_n(&mut self, count: usize) -> List<T>
Remove count
nodes after the cursor in O(min(count, self.len)).
Return the removed list.
Trait Implementations
impl<'a, T> IntoIterator for Cursor<'a, T>
[src]
type Item = &'a mut Cursor<'a, T>
The type of the elements being iterated over.
type IntoIter = CursorIntoIter<'a, T>
Which kind of iterator are we turning this into?
fn into_iter(self) -> Self::IntoIter
Creates an iterator from a value. Read more