Skip to content

Instantly share code, notes, and snippets.

@xaberus
Last active November 3, 2018 07:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xaberus/cfb69f20460b5a605a1a2f77fc48ed36 to your computer and use it in GitHub Desktop.
Save xaberus/cfb69f20460b5a605a1a2f77fc48ed36 to your computer and use it in GitHub Desktop.
pub struct LinkedList<T> {
...
marker: PhantomData<Box<T>>,
}
impl<T> LinkedList<T> {
/* other list methods go before */
/// Provides a cursor with immutable references to elements in the list.
/// The cursor starts ether with an empty element before the head or after the tail
/// as specified by the `position` argument.
pub fn cursor(&self, position: StartPosition) -> Cursor<T> {...}
/// Provides a cursor with immutable references to elements in the list starting with
/// head of the list, i.e. the first valid element or the empty element before the head.
pub fn head(&self) -> Cursor<T> {...}
/// Provides a cursor with immutable references to elements in the list starting with
/// tail of the list, i.e. the last valid element or the empty element after the tail.
pub fn tail(&self) -> Cursor<T> {...}
/// Provides a cursor with mutable references to elements and mutable access to the list.
/// The cursor starts ether with an empty element before the head or after the tail
/// as specified by the `position` argument.
pub fn cursor_mut(&mut self, position: StartPosition) -> CursorMut<T> {...}
/// Provides a cursor with mutable references to elements and mutable access to the list
/// starting with head of the list, i.e. the first valid element or the empty element
/// before the head.
pub fn head_mut(&mut self) -> CursorMut<T> {...}
/// Provides a cursor with mutable references to elements and mutable access to the list
/// starting with tail of the list, i.e. the last valid element or the empty element
/// after the tail.
pub fn tail_mut(&mut self) -> CursorMut<T> {...}
/* other list methods go after */
}
pub enum StartPosition {
BeforeHead,
AfterTail,
}
/// An immutable view into a `LinkedList` that can be moved back and forth
pub struct Cursor<'list, T: 'list> {
list: &'list LinkedList<T>,
...
}
impl<'list, T> Cursor<'list, T> {
/// Return cursor position where `None` is the empty element before the head,
/// `Some(0)..Some(list.len())` are valid element positions and `Some(list.len())`
/// is the empty element after the tail.
pub fn pos(&self) -> Option<usize> {...}
/// Move to the subsequent element of the list if it exists or the empty
/// element after the tail.
pub fn move_next(&mut self) {...}
/// Move to the previous element of the list if it exists or the empty
/// element before the head.
pub fn move_prev(&mut self) {...}
/// Get the current element
pub fn current(&self) -> Option<&'list T> {...}
/// Get the following element
pub fn peek_after(&self) -> Option<&'list T> {...}
/// Get the previous element
pub fn peek_before(&self) -> Option<&'list T> {...}
}
/// A mutable view into a `LinkedList` that can be used to edit the collection
pub struct CursorMut<'list, T: 'list> {
list: &'list mut LinkedList<T>,
...
}
impl<'list, T> CursorMut<'list, T> {
/// Return cursor position where `None` is the empty element before the head,
/// `Some(0)..Some(list.len())` are valid element positions and `Some(list.len())`
/// is the empty element after the tail.
pub fn pos(&self) -> Option<usize> {...}
/// Move to the subsequent element of the list if it exists or the empty
/// element after the tail.
pub fn move_next(&mut self) {...}
/// Move to the previous element of the list if it exists or the empty
/// element before the head.
pub fn move_prev(&mut self) {...}
/// Get the current element
pub fn current(&mut self) -> Option<&mut T> {...}
/// Get the next element
pub fn peek_after(&mut self) -> Option<&mut T> {...}
/// Get the previous element
pub fn peek_before(&self) -> Option<&mut T> {...}
/// Get an immutable cursor at the current element
pub fn as_cursor(&self) -> Cursor<T> {...}
/// Insert `element` after the cursor
///
/// This function will move the cursor so `peek_after` will point to the new element.
pub fn insert_after(&mut self, element: T) {...}
/// Insert `element` before the cursor
///
/// This function will move the cursor so `peek_before` will point to the new element.
pub fn insert_before(&mut self, element: T) {...}
/// Insert `list` between the current element and the next
///
/// This function will move the cursor so `peek_after` will point to the head of the inserted
/// `list`.
pub fn insert_list_after(&mut self, mut list: LinkedList<T>) {...}
/// Insert `list` between the previous element and current
///
/// This function will move the cursor so `peek_before` will point to the tail of the inserted
/// `list`.
pub fn insert_list_before(&mut self, mut list: LinkedList<T>) {...}
/// Remove and return the element following the cursor
///
/// This function will not consume the element pointed to by the cursor. If you want to drain
/// the list you should start with an empty element.
pub fn pop_after(&mut self) -> Option<T> {...}
/// Remove and return the element before the cursor
///
/// This function will not consume the element pointed to by the cursor. If you want to drain
/// the list you should start with an empty element.
pub fn pop_before(&mut self) -> Option<T> {...}
/// Split the list in two after the current element
/// The returned list consists of all elements following but **not including**
/// the current one.
pub fn split_after(self) -> LinkedList<T> {...}
/// Split the list in two before the current element
/// The returned list consists of all elements following and **including**
/// the current one.
pub fn split_before(self) -> LinkedList<T> {...}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment