Struct nekolib::ds::removable_heap::RemovableHeap
source · pub struct RemovableHeap<T> { /* private fields */ }
Expand description
削除可能ヒープ。
Idea
ヒープを二つ使う。
Implementation notes
peek()
を &self
にするため、remove()
と pop()
の直後はとりあえず
force()
している。
Examples
use nekolib::ds::RemovableHeap;
let mut heap = RemovableHeap::new();
heap.push(2);
heap.push(1);
heap.push(3);
heap.push(0);
assert_eq!(heap.peek(), Some(&3)); // {0, 1, 2, 3}
heap.remove(2); // {0, 1, 3}
assert_eq!(heap.pop(), Some(3)); // {0, 1}
assert_eq!(heap.peek(), Some(&1));
heap.remove(1); // {0}
assert_eq!(heap.peek(), Some(&0));
assert_eq!(heap.pop(), Some(0)); // {}
assert!(heap.is_empty());
削除する要素がヒープに入っていないとき、こまる。
use nekolib::ds::RemovableHeap;
let mut heap = RemovableHeap::new();
heap.push(1);
heap.remove(2);
heap.push(2);
heap.push(3); // {1, 2, 3}?
assert_eq!(heap.pop(), Some(3)); // seems good?
assert_ne!(heap.peek(), Some(&2)); // but...
assert_eq!(heap.peek(), Some(&1));
未来の push 操作を打ち消すというわけでもない。
use nekolib::ds::RemovableHeap;
let mut heap = RemovableHeap::new();
heap.push(1);
heap.push(2);
heap.remove(100);
heap.remove(3);
heap.push(101);
heap.push(3); // {1, 2, 101}?
assert_eq!(heap.pop(), Some(101)); // seems good, so far
assert_ne!(heap.peek(), Some(&2)); // but...
assert_eq!(heap.peek(), Some(&3));
Implementations§
Trait Implementations§
source§impl<T: Clone> Clone for RemovableHeap<T>
impl<T: Clone> Clone for RemovableHeap<T>
source§fn clone(&self) -> RemovableHeap<T>
fn clone(&self) -> RemovableHeap<T>
Returns a copy of the value. Read more
1.0.0 · source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source
. Read moreAuto Trait Implementations§
impl<T> RefUnwindSafe for RemovableHeap<T>where T: RefUnwindSafe,
impl<T> Send for RemovableHeap<T>where T: Send,
impl<T> Sync for RemovableHeap<T>where T: Sync,
impl<T> Unpin for RemovableHeap<T>where T: Unpin,
impl<T> UnwindSafe for RemovableHeap<T>where T: UnwindSafe,
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more