forked from rust-lang/rust
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rollup merge of rust-lang#86714 - iwahbe:add-linked-list-cursor-end-m…
…ethods, r=Amanieu Add linked list cursor end methods I add several methods to `LinkedList::CursorMut` and `LinkedList::Cursor`. These methods allow you to access/manipulate the ends of a list via the cursor. This is especially helpful when scanning through a list and reordering. For example: ```rust let mut c = ll.back_cursor_mut(); let mut moves = 10; while c.current().map(|x| x > 5).unwrap_or(false) { let n = c.remove_current(); c.push_front(n); if moves > 0 { break; } else { moves -= 1; } } ``` I encountered this problem working on my bachelors thesis doing graph index manipulation. While this problem can be avoided by splicing, it is awkward. I asked about the problem [here](https://internals.rust-lang.org/t/linked-list-cursurmut-missing-methods/14921/4) and it was suggested I write a PR. All methods added consist of ```rust Cursor::front(&self) -> Option<&T>; Cursor::back(&self) -> Option<&T>; CursorMut::front(&self) -> Option<&T>; CursorMut::back(&self) -> Option<&T>; CursorMut::front_mut(&mut self) -> Option<&mut T>; CursorMut::back_mut(&mut self) -> Option<&mut T>; CursorMut::push_front(&mut self, elt: T); CursorMut::push_back(&mut self, elt: T); CursorMut::pop_front(&mut self) -> Option<T>; CursorMut::pop_back(&mut self) -> Option<T>; ``` #### Design decisions: I tried to remain as consistent as possible with what was already present for linked lists. The methods `front`, `front_mut`, `back` and `back_mut` are identical to their `LinkedList` equivalents. I tried to make the `pop_front` and `pop_back` methods work the same way (vis a vis the "ghost" node) as `remove_current`. I thought this was the closest analog. `push_front` and `push_back` do not change the "current" node, even if it is the "ghost" node. I thought it was most intuitive to say that if you add to the list, current will never change. Any feedback would be welcome 😄
- Loading branch information
Showing
2 changed files
with
190 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters