143143#![ allow( missing_docs) ]
144144#![ stable( feature = "rust1" , since = "1.0.0" ) ]
145145
146+ use core:: alloc:: Allocator ;
146147use core:: fmt;
147148use core:: iter:: { FromIterator , FusedIterator , InPlaceIterable , SourceIter , TrustedLen } ;
148149use core:: mem:: { self , swap, ManuallyDrop } ;
149150use core:: ops:: { Deref , DerefMut } ;
150151use core:: ptr;
151152
153+ use crate :: alloc:: Global ;
152154use crate :: collections:: TryReserveError ;
153155use crate :: slice;
154156use crate :: vec:: { self , AsVecIntoIter , Vec } ;
@@ -265,8 +267,11 @@ mod tests;
265267/// [peek\_mut]: BinaryHeap::peek_mut
266268#[ stable( feature = "rust1" , since = "1.0.0" ) ]
267269#[ cfg_attr( not( test) , rustc_diagnostic_item = "BinaryHeap" ) ]
268- pub struct BinaryHeap < T > {
269- data : Vec < T > ,
270+ pub struct BinaryHeap <
271+ T ,
272+ #[ unstable( feature = "allocator_api" , issue = "32838" ) ] A : Allocator = Global ,
273+ > {
274+ data : Vec < T , A > ,
270275}
271276
272277/// Structure wrapping a mutable reference to the greatest item on a
@@ -277,20 +282,24 @@ pub struct BinaryHeap<T> {
277282///
278283/// [`peek_mut`]: BinaryHeap::peek_mut
279284#[ stable( feature = "binary_heap_peek_mut" , since = "1.12.0" ) ]
280- pub struct PeekMut < ' a , T : ' a + Ord > {
281- heap : & ' a mut BinaryHeap < T > ,
285+ pub struct PeekMut <
286+ ' a ,
287+ T : ' a + Ord ,
288+ #[ unstable( feature = "allocator_api" , issue = "32838" ) ] A : Allocator + ' a = Global ,
289+ > {
290+ heap : & ' a mut BinaryHeap < T , A > ,
282291 sift : bool ,
283292}
284293
285294#[ stable( feature = "collection_debug" , since = "1.17.0" ) ]
286- impl < T : Ord + fmt:: Debug > fmt:: Debug for PeekMut < ' _ , T > {
295+ impl < T : Ord + fmt:: Debug , A : Allocator > fmt:: Debug for PeekMut < ' _ , T , A > {
287296 fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
288297 f. debug_tuple ( "PeekMut" ) . field ( & self . heap . data [ 0 ] ) . finish ( )
289298 }
290299}
291300
292301#[ stable( feature = "binary_heap_peek_mut" , since = "1.12.0" ) ]
293- impl < T : Ord > Drop for PeekMut < ' _ , T > {
302+ impl < T : Ord , A : Allocator > Drop for PeekMut < ' _ , T , A > {
294303 fn drop ( & mut self ) {
295304 if self . sift {
296305 // SAFETY: PeekMut is only instantiated for non-empty heaps.
@@ -300,7 +309,7 @@ impl<T: Ord> Drop for PeekMut<'_, T> {
300309}
301310
302311#[ stable( feature = "binary_heap_peek_mut" , since = "1.12.0" ) ]
303- impl < T : Ord > Deref for PeekMut < ' _ , T > {
312+ impl < T : Ord , A : Allocator > Deref for PeekMut < ' _ , T , A > {
304313 type Target = T ;
305314 fn deref ( & self ) -> & T {
306315 debug_assert ! ( !self . heap. is_empty( ) ) ;
@@ -310,7 +319,7 @@ impl<T: Ord> Deref for PeekMut<'_, T> {
310319}
311320
312321#[ stable( feature = "binary_heap_peek_mut" , since = "1.12.0" ) ]
313- impl < T : Ord > DerefMut for PeekMut < ' _ , T > {
322+ impl < T : Ord , A : Allocator > DerefMut for PeekMut < ' _ , T , A > {
314323 fn deref_mut ( & mut self ) -> & mut T {
315324 debug_assert ! ( !self . heap. is_empty( ) ) ;
316325 self . sift = true ;
@@ -330,7 +339,7 @@ impl<'a, T: Ord> PeekMut<'a, T> {
330339}
331340
332341#[ stable( feature = "rust1" , since = "1.0.0" ) ]
333- impl < T : Clone > Clone for BinaryHeap < T > {
342+ impl < T : Clone , A : Allocator + Clone > Clone for BinaryHeap < T , A > {
334343 fn clone ( & self ) -> Self {
335344 BinaryHeap { data : self . data . clone ( ) }
336345 }
@@ -350,13 +359,13 @@ impl<T: Ord> Default for BinaryHeap<T> {
350359}
351360
352361#[ stable( feature = "binaryheap_debug" , since = "1.4.0" ) ]
353- impl < T : fmt:: Debug > fmt:: Debug for BinaryHeap < T > {
362+ impl < T : fmt:: Debug , A : Allocator > fmt:: Debug for BinaryHeap < T , A > {
354363 fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
355364 f. debug_list ( ) . entries ( self . iter ( ) ) . finish ( )
356365 }
357366}
358367
359- impl < T : Ord > BinaryHeap < T > {
368+ impl < T : Ord > BinaryHeap < T , Global > {
360369 /// Creates an empty `BinaryHeap` as a max-heap.
361370 ///
362371 /// # Examples
@@ -394,6 +403,52 @@ impl<T: Ord> BinaryHeap<T> {
394403 pub fn with_capacity ( capacity : usize ) -> BinaryHeap < T > {
395404 BinaryHeap { data : Vec :: with_capacity ( capacity) }
396405 }
406+ }
407+
408+ impl < T : Ord , A : Allocator > BinaryHeap < T , A > {
409+ /// Creates an empty `BinaryHeap` as a max-heap, using `A` as allocator.
410+ ///
411+ /// # Examples
412+ ///
413+ /// Basic usage:
414+ ///
415+ /// ```
416+ /// #![feature(allocator_api)]
417+ ///
418+ /// use std::alloc::System;
419+ /// use std::collections::BinaryHeap;
420+ /// let mut heap = BinaryHeap::new_in(System);
421+ /// heap.push(4);
422+ /// ```
423+ #[ unstable( feature = "allocator_api" , issue = "32838" ) ]
424+ #[ must_use]
425+ pub fn new_in ( alloc : A ) -> BinaryHeap < T , A > {
426+ BinaryHeap { data : Vec :: new_in ( alloc) }
427+ }
428+
429+ /// Creates an empty `BinaryHeap` with at least the specified capacity, using `A` as allocator.
430+ ///
431+ /// The binary heap will be able to hold at least `capacity` elements without
432+ /// reallocating. This method is allowed to allocate for more elements than
433+ /// `capacity`. If `capacity` is 0, the binary heap will not allocate.
434+ ///
435+ /// # Examples
436+ ///
437+ /// Basic usage:
438+ ///
439+ /// ```
440+ /// #![feature(allocator_api)]
441+ ///
442+ /// use std::alloc::System;
443+ /// use std::collections::BinaryHeap;
444+ /// let mut heap = BinaryHeap::with_capacity_in(10, System);
445+ /// heap.push(4);
446+ /// ```
447+ #[ unstable( feature = "allocator_api" , issue = "32838" ) ]
448+ #[ must_use]
449+ pub fn with_capacity_in ( capacity : usize , alloc : A ) -> BinaryHeap < T , A > {
450+ BinaryHeap { data : Vec :: with_capacity_in ( capacity, alloc) }
451+ }
397452
398453 /// Returns a mutable reference to the greatest item in the binary heap, or
399454 /// `None` if it is empty.
@@ -425,7 +480,7 @@ impl<T: Ord> BinaryHeap<T> {
425480 /// If the item is modified then the worst case time complexity is *O*(log(*n*)),
426481 /// otherwise it's *O*(1).
427482 #[ stable( feature = "binary_heap_peek_mut" , since = "1.12.0" ) ]
428- pub fn peek_mut ( & mut self ) -> Option < PeekMut < ' _ , T > > {
483+ pub fn peek_mut ( & mut self ) -> Option < PeekMut < ' _ , T , A > > {
429484 if self . is_empty ( ) { None } else { Some ( PeekMut { heap : self , sift : false } ) }
430485 }
431486
@@ -520,7 +575,7 @@ impl<T: Ord> BinaryHeap<T> {
520575 /// ```
521576 #[ must_use = "`self` will be dropped if the result is not used" ]
522577 #[ stable( feature = "binary_heap_extras_15" , since = "1.5.0" ) ]
523- pub fn into_sorted_vec ( mut self ) -> Vec < T > {
578+ pub fn into_sorted_vec ( mut self ) -> Vec < T , A > {
524579 let mut end = self . len ( ) ;
525580 while end > 1 {
526581 end -= 1 ;
@@ -778,7 +833,7 @@ impl<T: Ord> BinaryHeap<T> {
778833 /// ```
779834 #[ inline]
780835 #[ unstable( feature = "binary_heap_drain_sorted" , issue = "59278" ) ]
781- pub fn drain_sorted ( & mut self ) -> DrainSorted < ' _ , T > {
836+ pub fn drain_sorted ( & mut self ) -> DrainSorted < ' _ , T , A > {
782837 DrainSorted { inner : self }
783838 }
784839
@@ -821,7 +876,7 @@ impl<T: Ord> BinaryHeap<T> {
821876 }
822877}
823878
824- impl < T > BinaryHeap < T > {
879+ impl < T , A : Allocator > BinaryHeap < T , A > {
825880 /// Returns an iterator visiting all values in the underlying vector, in
826881 /// arbitrary order.
827882 ///
@@ -858,7 +913,7 @@ impl<T> BinaryHeap<T> {
858913 /// assert_eq!(heap.into_iter_sorted().take(2).collect::<Vec<_>>(), [5, 4]);
859914 /// ```
860915 #[ unstable( feature = "binary_heap_into_iter_sorted" , issue = "59278" ) ]
861- pub fn into_iter_sorted ( self ) -> IntoIterSorted < T > {
916+ pub fn into_iter_sorted ( self ) -> IntoIterSorted < T , A > {
862917 IntoIterSorted { inner : self }
863918 }
864919
@@ -1124,7 +1179,7 @@ impl<T> BinaryHeap<T> {
11241179 /// ```
11251180 #[ must_use = "`self` will be dropped if the result is not used" ]
11261181 #[ stable( feature = "binary_heap_extras_15" , since = "1.5.0" ) ]
1127- pub fn into_vec ( self ) -> Vec < T > {
1182+ pub fn into_vec ( self ) -> Vec < T , A > {
11281183 self . into ( )
11291184 }
11301185
@@ -1195,7 +1250,7 @@ impl<T> BinaryHeap<T> {
11951250 /// ```
11961251 #[ inline]
11971252 #[ stable( feature = "drain" , since = "1.6.0" ) ]
1198- pub fn drain ( & mut self ) -> Drain < ' _ , T > {
1253+ pub fn drain ( & mut self ) -> Drain < ' _ , T , A > {
11991254 Drain { iter : self . data . drain ( ..) }
12001255 }
12011256
@@ -1438,12 +1493,15 @@ unsafe impl<I> AsVecIntoIter for IntoIter<I> {
14381493#[ must_use = "iterators are lazy and do nothing unless consumed" ]
14391494#[ unstable( feature = "binary_heap_into_iter_sorted" , issue = "59278" ) ]
14401495#[ derive( Clone , Debug ) ]
1441- pub struct IntoIterSorted < T > {
1442- inner : BinaryHeap < T > ,
1496+ pub struct IntoIterSorted <
1497+ T ,
1498+ #[ unstable( feature = "allocator_api" , issue = "32838" ) ] A : Allocator = Global ,
1499+ > {
1500+ inner : BinaryHeap < T , A > ,
14431501}
14441502
14451503#[ unstable( feature = "binary_heap_into_iter_sorted" , issue = "59278" ) ]
1446- impl < T : Ord > Iterator for IntoIterSorted < T > {
1504+ impl < T : Ord , A : Allocator > Iterator for IntoIterSorted < T , A > {
14471505 type Item = T ;
14481506
14491507 #[ inline]
@@ -1459,13 +1517,13 @@ impl<T: Ord> Iterator for IntoIterSorted<T> {
14591517}
14601518
14611519#[ unstable( feature = "binary_heap_into_iter_sorted" , issue = "59278" ) ]
1462- impl < T : Ord > ExactSizeIterator for IntoIterSorted < T > { }
1520+ impl < T : Ord , A : Allocator > ExactSizeIterator for IntoIterSorted < T , A > { }
14631521
14641522#[ unstable( feature = "binary_heap_into_iter_sorted" , issue = "59278" ) ]
1465- impl < T : Ord > FusedIterator for IntoIterSorted < T > { }
1523+ impl < T : Ord , A : Allocator > FusedIterator for IntoIterSorted < T , A > { }
14661524
14671525#[ unstable( feature = "trusted_len" , issue = "37572" ) ]
1468- unsafe impl < T : Ord > TrustedLen for IntoIterSorted < T > { }
1526+ unsafe impl < T : Ord , A : Allocator > TrustedLen for IntoIterSorted < T , A > { }
14691527
14701528/// A draining iterator over the elements of a `BinaryHeap`.
14711529///
@@ -1475,12 +1533,16 @@ unsafe impl<T: Ord> TrustedLen for IntoIterSorted<T> {}
14751533/// [`drain`]: BinaryHeap::drain
14761534#[ stable( feature = "drain" , since = "1.6.0" ) ]
14771535#[ derive( Debug ) ]
1478- pub struct Drain < ' a , T : ' a > {
1479- iter : vec:: Drain < ' a , T > ,
1536+ pub struct Drain <
1537+ ' a ,
1538+ T : ' a ,
1539+ #[ unstable( feature = "allocator_api" , issue = "32838" ) ] A : Allocator + ' a = Global ,
1540+ > {
1541+ iter : vec:: Drain < ' a , T , A > ,
14801542}
14811543
14821544#[ stable( feature = "drain" , since = "1.6.0" ) ]
1483- impl < T > Iterator for Drain < ' _ , T > {
1545+ impl < ' a , T , A : Allocator + ' a > Iterator for Drain < ' a , T , A > {
14841546 type Item = T ;
14851547
14861548 #[ inline]
@@ -1495,22 +1557,22 @@ impl<T> Iterator for Drain<'_, T> {
14951557}
14961558
14971559#[ stable( feature = "drain" , since = "1.6.0" ) ]
1498- impl < T > DoubleEndedIterator for Drain < ' _ , T > {
1560+ impl < ' a , T , A : Allocator + ' a > DoubleEndedIterator for Drain < ' a , T , A > {
14991561 #[ inline]
15001562 fn next_back ( & mut self ) -> Option < T > {
15011563 self . iter . next_back ( )
15021564 }
15031565}
15041566
15051567#[ stable( feature = "drain" , since = "1.6.0" ) ]
1506- impl < T > ExactSizeIterator for Drain < ' _ , T > {
1568+ impl < ' a , T , A : Allocator + ' a > ExactSizeIterator for Drain < ' a , T , A > {
15071569 fn is_empty ( & self ) -> bool {
15081570 self . iter . is_empty ( )
15091571 }
15101572}
15111573
15121574#[ stable( feature = "fused" , since = "1.26.0" ) ]
1513- impl < T > FusedIterator for Drain < ' _ , T > { }
1575+ impl < ' a , T , A : Allocator + ' a > FusedIterator for Drain < ' a , T , A > { }
15141576
15151577/// A draining iterator over the elements of a `BinaryHeap`.
15161578///
@@ -1520,17 +1582,21 @@ impl<T> FusedIterator for Drain<'_, T> {}
15201582/// [`drain_sorted`]: BinaryHeap::drain_sorted
15211583#[ unstable( feature = "binary_heap_drain_sorted" , issue = "59278" ) ]
15221584#[ derive( Debug ) ]
1523- pub struct DrainSorted < ' a , T : Ord > {
1524- inner : & ' a mut BinaryHeap < T > ,
1585+ pub struct DrainSorted <
1586+ ' a ,
1587+ T : Ord ,
1588+ #[ unstable( feature = "allocator_api" , issue = "32838" ) ] A : Allocator + ' a = Global ,
1589+ > {
1590+ inner : & ' a mut BinaryHeap < T , A > ,
15251591}
15261592
15271593#[ unstable( feature = "binary_heap_drain_sorted" , issue = "59278" ) ]
1528- impl < ' a , T : Ord > Drop for DrainSorted < ' a , T > {
1594+ impl < ' a , T : Ord , A : Allocator + ' a > Drop for DrainSorted < ' a , T , A > {
15291595 /// Removes heap elements in heap order.
15301596 fn drop ( & mut self ) {
1531- struct DropGuard < ' r , ' a , T : Ord > ( & ' r mut DrainSorted < ' a , T > ) ;
1597+ struct DropGuard < ' r , ' a , T : Ord , A : Allocator + ' a > ( & ' r mut DrainSorted < ' a , T , A > ) ;
15321598
1533- impl < ' r , ' a , T : Ord > Drop for DropGuard < ' r , ' a , T > {
1599+ impl < ' r , ' a , T : Ord , A : Allocator + ' a > Drop for DropGuard < ' r , ' a , T , A > {
15341600 fn drop ( & mut self ) {
15351601 while self . 0 . inner . pop ( ) . is_some ( ) { }
15361602 }
@@ -1545,7 +1611,7 @@ impl<'a, T: Ord> Drop for DrainSorted<'a, T> {
15451611}
15461612
15471613#[ unstable( feature = "binary_heap_drain_sorted" , issue = "59278" ) ]
1548- impl < T : Ord > Iterator for DrainSorted < ' _ , T > {
1614+ impl < T : Ord , A : Allocator > Iterator for DrainSorted < ' _ , T , A > {
15491615 type Item = T ;
15501616
15511617 #[ inline]
@@ -1561,20 +1627,20 @@ impl<T: Ord> Iterator for DrainSorted<'_, T> {
15611627}
15621628
15631629#[ unstable( feature = "binary_heap_drain_sorted" , issue = "59278" ) ]
1564- impl < T : Ord > ExactSizeIterator for DrainSorted < ' _ , T > { }
1630+ impl < T : Ord , A : Allocator > ExactSizeIterator for DrainSorted < ' _ , T , A > { }
15651631
15661632#[ unstable( feature = "binary_heap_drain_sorted" , issue = "59278" ) ]
1567- impl < T : Ord > FusedIterator for DrainSorted < ' _ , T > { }
1633+ impl < T : Ord , A : Allocator > FusedIterator for DrainSorted < ' _ , T , A > { }
15681634
15691635#[ unstable( feature = "trusted_len" , issue = "37572" ) ]
1570- unsafe impl < T : Ord > TrustedLen for DrainSorted < ' _ , T > { }
1636+ unsafe impl < T : Ord , A : Allocator > TrustedLen for DrainSorted < ' _ , T , A > { }
15711637
15721638#[ stable( feature = "binary_heap_extras_15" , since = "1.5.0" ) ]
1573- impl < T : Ord > From < Vec < T > > for BinaryHeap < T > {
1639+ impl < T : Ord , A : Allocator > From < Vec < T , A > > for BinaryHeap < T , A > {
15741640 /// Converts a `Vec<T>` into a `BinaryHeap<T>`.
15751641 ///
15761642 /// This conversion happens in-place, and has *O*(*n*) time complexity.
1577- fn from ( vec : Vec < T > ) -> BinaryHeap < T > {
1643+ fn from ( vec : Vec < T , A > ) -> BinaryHeap < T , A > {
15781644 let mut heap = BinaryHeap { data : vec } ;
15791645 heap. rebuild ( ) ;
15801646 heap
@@ -1598,12 +1664,12 @@ impl<T: Ord, const N: usize> From<[T; N]> for BinaryHeap<T> {
15981664}
15991665
16001666#[ stable( feature = "binary_heap_extras_15" , since = "1.5.0" ) ]
1601- impl < T > From < BinaryHeap < T > > for Vec < T > {
1667+ impl < T , A : Allocator > From < BinaryHeap < T , A > > for Vec < T , A > {
16021668 /// Converts a `BinaryHeap<T>` into a `Vec<T>`.
16031669 ///
16041670 /// This conversion requires no data movement or allocation, and has
16051671 /// constant time complexity.
1606- fn from ( heap : BinaryHeap < T > ) -> Vec < T > {
1672+ fn from ( heap : BinaryHeap < T , A > ) -> Vec < T , A > {
16071673 heap. data
16081674 }
16091675}
@@ -1644,7 +1710,7 @@ impl<T> IntoIterator for BinaryHeap<T> {
16441710}
16451711
16461712#[ stable( feature = "rust1" , since = "1.0.0" ) ]
1647- impl < ' a , T > IntoIterator for & ' a BinaryHeap < T > {
1713+ impl < ' a , T , A : Allocator + ' a > IntoIterator for & ' a BinaryHeap < T , A > {
16481714 type Item = & ' a T ;
16491715 type IntoIter = Iter < ' a , T > ;
16501716
@@ -1691,7 +1757,7 @@ impl<T: Ord> SpecExtend<BinaryHeap<T>> for BinaryHeap<T> {
16911757 }
16921758}
16931759
1694- impl < T : Ord > BinaryHeap < T > {
1760+ impl < T : Ord , A : Allocator > BinaryHeap < T , A > {
16951761 fn extend_desugared < I : IntoIterator < Item = T > > ( & mut self , iter : I ) {
16961762 let iterator = iter. into_iter ( ) ;
16971763 let ( lower, _) = iterator. size_hint ( ) ;
0 commit comments