|
151 | 151 | #![allow(missing_docs)]
|
152 | 152 | #![stable(feature = "rust1", since = "1.0.0")]
|
153 | 153 |
|
| 154 | +use core::ops::{Drop, Deref, DerefMut}; |
154 | 155 | use core::iter::FromIterator;
|
155 | 156 | use core::mem::swap;
|
156 | 157 | use core::mem::size_of;
|
@@ -218,6 +219,37 @@ pub struct BinaryHeap<T> {
|
218 | 219 | data: Vec<T>,
|
219 | 220 | }
|
220 | 221 |
|
| 222 | +/// A container object that represents the result of the [`peek_mut()`] method |
| 223 | +/// on `BinaryHeap`. See its documentation for details. |
| 224 | +/// |
| 225 | +/// [`peek_mut()`]: struct.BinaryHeap.html#method.peek_mut |
| 226 | +#[unstable(feature = "binary_heap_peek_mut", issue = "34392")] |
| 227 | +pub struct PeekMut<'a, T: 'a + Ord> { |
| 228 | + heap: &'a mut BinaryHeap<T> |
| 229 | +} |
| 230 | + |
| 231 | +#[unstable(feature = "binary_heap_peek_mut", issue = "34392")] |
| 232 | +impl<'a, T: Ord> Drop for PeekMut<'a, T> { |
| 233 | + fn drop(&mut self) { |
| 234 | + self.heap.sift_down(0); |
| 235 | + } |
| 236 | +} |
| 237 | + |
| 238 | +#[unstable(feature = "binary_heap_peek_mut", issue = "34392")] |
| 239 | +impl<'a, T: Ord> Deref for PeekMut<'a, T> { |
| 240 | + type Target = T; |
| 241 | + fn deref(&self) -> &T { |
| 242 | + &self.heap.data[0] |
| 243 | + } |
| 244 | +} |
| 245 | + |
| 246 | +#[unstable(feature = "binary_heap_peek_mut", issue = "34392")] |
| 247 | +impl<'a, T: Ord> DerefMut for PeekMut<'a, T> { |
| 248 | + fn deref_mut(&mut self) -> &mut T { |
| 249 | + &mut self.heap.data[0] |
| 250 | + } |
| 251 | +} |
| 252 | + |
221 | 253 | #[stable(feature = "rust1", since = "1.0.0")]
|
222 | 254 | impl<T: Clone> Clone for BinaryHeap<T> {
|
223 | 255 | fn clone(&self) -> Self {
|
@@ -323,6 +355,42 @@ impl<T: Ord> BinaryHeap<T> {
|
323 | 355 | self.data.get(0)
|
324 | 356 | }
|
325 | 357 |
|
| 358 | + /// Returns a mutable reference to the greatest item in the binary heap, or |
| 359 | + /// `None` if it is empty. |
| 360 | + /// |
| 361 | + /// Note: If the `PeekMut` value is leaked, the heap may be in an |
| 362 | + /// inconsistent state. |
| 363 | + /// |
| 364 | + /// # Examples |
| 365 | + /// |
| 366 | + /// Basic usage: |
| 367 | + /// |
| 368 | + /// ``` |
| 369 | + /// #![feature(binary_heap_peek_mut)] |
| 370 | + /// use std::collections::BinaryHeap; |
| 371 | + /// let mut heap = BinaryHeap::new(); |
| 372 | + /// assert!(heap.peek_mut().is_none()); |
| 373 | + /// |
| 374 | + /// heap.push(1); |
| 375 | + /// heap.push(5); |
| 376 | + /// heap.push(2); |
| 377 | + /// { |
| 378 | + /// let mut val = heap.peek_mut().unwrap(); |
| 379 | + /// *val = 0; |
| 380 | + /// } |
| 381 | + /// assert_eq!(heap.peek(), Some(&2)); |
| 382 | + /// ``` |
| 383 | + #[unstable(feature = "binary_heap_peek_mut", issue = "34392")] |
| 384 | + pub fn peek_mut(&mut self) -> Option<PeekMut<T>> { |
| 385 | + if self.is_empty() { |
| 386 | + None |
| 387 | + } else { |
| 388 | + Some(PeekMut { |
| 389 | + heap: self |
| 390 | + }) |
| 391 | + } |
| 392 | + } |
| 393 | + |
326 | 394 | /// Returns the number of elements the binary heap can hold without reallocating.
|
327 | 395 | ///
|
328 | 396 | /// # Examples
|
|
0 commit comments