143
143
#![ allow( missing_docs) ]
144
144
#![ stable( feature = "rust1" , since = "1.0.0" ) ]
145
145
146
+ use core:: alloc:: Allocator ;
146
147
use core:: fmt;
147
148
use core:: iter:: { FromIterator , FusedIterator , InPlaceIterable , SourceIter , TrustedLen } ;
148
149
use core:: mem:: { self , swap, ManuallyDrop } ;
149
150
use core:: ops:: { Deref , DerefMut } ;
150
151
use core:: ptr;
151
152
153
+ use crate :: alloc:: Global ;
152
154
use crate :: collections:: TryReserveError ;
153
155
use crate :: slice;
154
156
use crate :: vec:: { self , AsVecIntoIter , Vec } ;
@@ -265,8 +267,11 @@ mod tests;
265
267
/// [peek\_mut]: BinaryHeap::peek_mut
266
268
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
267
269
#[ 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 > ,
270
275
}
271
276
272
277
/// Structure wrapping a mutable reference to the greatest item on a
@@ -277,20 +282,24 @@ pub struct BinaryHeap<T> {
277
282
///
278
283
/// [`peek_mut`]: BinaryHeap::peek_mut
279
284
#[ 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 > ,
282
291
sift : bool ,
283
292
}
284
293
285
294
#[ 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 > {
287
296
fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
288
297
f. debug_tuple ( "PeekMut" ) . field ( & self . heap . data [ 0 ] ) . finish ( )
289
298
}
290
299
}
291
300
292
301
#[ 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 > {
294
303
fn drop ( & mut self ) {
295
304
if self . sift {
296
305
// SAFETY: PeekMut is only instantiated for non-empty heaps.
@@ -300,7 +309,7 @@ impl<T: Ord> Drop for PeekMut<'_, T> {
300
309
}
301
310
302
311
#[ 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 > {
304
313
type Target = T ;
305
314
fn deref ( & self ) -> & T {
306
315
debug_assert ! ( !self . heap. is_empty( ) ) ;
@@ -310,7 +319,7 @@ impl<T: Ord> Deref for PeekMut<'_, T> {
310
319
}
311
320
312
321
#[ 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 > {
314
323
fn deref_mut ( & mut self ) -> & mut T {
315
324
debug_assert ! ( !self . heap. is_empty( ) ) ;
316
325
self . sift = true ;
@@ -330,7 +339,7 @@ impl<'a, T: Ord> PeekMut<'a, T> {
330
339
}
331
340
332
341
#[ 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 > {
334
343
fn clone ( & self ) -> Self {
335
344
BinaryHeap { data : self . data . clone ( ) }
336
345
}
@@ -350,13 +359,13 @@ impl<T: Ord> Default for BinaryHeap<T> {
350
359
}
351
360
352
361
#[ 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 > {
354
363
fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
355
364
f. debug_list ( ) . entries ( self . iter ( ) ) . finish ( )
356
365
}
357
366
}
358
367
359
- impl < T : Ord > BinaryHeap < T > {
368
+ impl < T : Ord > BinaryHeap < T , Global > {
360
369
/// Creates an empty `BinaryHeap` as a max-heap.
361
370
///
362
371
/// # Examples
@@ -394,6 +403,52 @@ impl<T: Ord> BinaryHeap<T> {
394
403
pub fn with_capacity ( capacity : usize ) -> BinaryHeap < T > {
395
404
BinaryHeap { data : Vec :: with_capacity ( capacity) }
396
405
}
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
+ }
397
452
398
453
/// Returns a mutable reference to the greatest item in the binary heap, or
399
454
/// `None` if it is empty.
@@ -425,7 +480,7 @@ impl<T: Ord> BinaryHeap<T> {
425
480
/// If the item is modified then the worst case time complexity is *O*(log(*n*)),
426
481
/// otherwise it's *O*(1).
427
482
#[ 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 > > {
429
484
if self . is_empty ( ) { None } else { Some ( PeekMut { heap : self , sift : false } ) }
430
485
}
431
486
@@ -520,7 +575,7 @@ impl<T: Ord> BinaryHeap<T> {
520
575
/// ```
521
576
#[ must_use = "`self` will be dropped if the result is not used" ]
522
577
#[ 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 > {
524
579
let mut end = self . len ( ) ;
525
580
while end > 1 {
526
581
end -= 1 ;
@@ -778,7 +833,7 @@ impl<T: Ord> BinaryHeap<T> {
778
833
/// ```
779
834
#[ inline]
780
835
#[ 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 > {
782
837
DrainSorted { inner : self }
783
838
}
784
839
@@ -821,7 +876,7 @@ impl<T: Ord> BinaryHeap<T> {
821
876
}
822
877
}
823
878
824
- impl < T > BinaryHeap < T > {
879
+ impl < T , A : Allocator > BinaryHeap < T , A > {
825
880
/// Returns an iterator visiting all values in the underlying vector, in
826
881
/// arbitrary order.
827
882
///
@@ -858,7 +913,7 @@ impl<T> BinaryHeap<T> {
858
913
/// assert_eq!(heap.into_iter_sorted().take(2).collect::<Vec<_>>(), [5, 4]);
859
914
/// ```
860
915
#[ 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 > {
862
917
IntoIterSorted { inner : self }
863
918
}
864
919
@@ -1124,7 +1179,7 @@ impl<T> BinaryHeap<T> {
1124
1179
/// ```
1125
1180
#[ must_use = "`self` will be dropped if the result is not used" ]
1126
1181
#[ 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 > {
1128
1183
self . into ( )
1129
1184
}
1130
1185
@@ -1195,7 +1250,7 @@ impl<T> BinaryHeap<T> {
1195
1250
/// ```
1196
1251
#[ inline]
1197
1252
#[ stable( feature = "drain" , since = "1.6.0" ) ]
1198
- pub fn drain ( & mut self ) -> Drain < ' _ , T > {
1253
+ pub fn drain ( & mut self ) -> Drain < ' _ , T , A > {
1199
1254
Drain { iter : self . data . drain ( ..) }
1200
1255
}
1201
1256
@@ -1438,12 +1493,15 @@ unsafe impl<I> AsVecIntoIter for IntoIter<I> {
1438
1493
#[ must_use = "iterators are lazy and do nothing unless consumed" ]
1439
1494
#[ unstable( feature = "binary_heap_into_iter_sorted" , issue = "59278" ) ]
1440
1495
#[ 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 > ,
1443
1501
}
1444
1502
1445
1503
#[ 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 > {
1447
1505
type Item = T ;
1448
1506
1449
1507
#[ inline]
@@ -1459,13 +1517,13 @@ impl<T: Ord> Iterator for IntoIterSorted<T> {
1459
1517
}
1460
1518
1461
1519
#[ 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 > { }
1463
1521
1464
1522
#[ 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 > { }
1466
1524
1467
1525
#[ 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 > { }
1469
1527
1470
1528
/// A draining iterator over the elements of a `BinaryHeap`.
1471
1529
///
@@ -1475,12 +1533,16 @@ unsafe impl<T: Ord> TrustedLen for IntoIterSorted<T> {}
1475
1533
/// [`drain`]: BinaryHeap::drain
1476
1534
#[ stable( feature = "drain" , since = "1.6.0" ) ]
1477
1535
#[ 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 > ,
1480
1542
}
1481
1543
1482
1544
#[ 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 > {
1484
1546
type Item = T ;
1485
1547
1486
1548
#[ inline]
@@ -1495,22 +1557,22 @@ impl<T> Iterator for Drain<'_, T> {
1495
1557
}
1496
1558
1497
1559
#[ 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 > {
1499
1561
#[ inline]
1500
1562
fn next_back ( & mut self ) -> Option < T > {
1501
1563
self . iter . next_back ( )
1502
1564
}
1503
1565
}
1504
1566
1505
1567
#[ 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 > {
1507
1569
fn is_empty ( & self ) -> bool {
1508
1570
self . iter . is_empty ( )
1509
1571
}
1510
1572
}
1511
1573
1512
1574
#[ 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 > { }
1514
1576
1515
1577
/// A draining iterator over the elements of a `BinaryHeap`.
1516
1578
///
@@ -1520,17 +1582,21 @@ impl<T> FusedIterator for Drain<'_, T> {}
1520
1582
/// [`drain_sorted`]: BinaryHeap::drain_sorted
1521
1583
#[ unstable( feature = "binary_heap_drain_sorted" , issue = "59278" ) ]
1522
1584
#[ 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 > ,
1525
1591
}
1526
1592
1527
1593
#[ 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 > {
1529
1595
/// Removes heap elements in heap order.
1530
1596
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 > ) ;
1532
1598
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 > {
1534
1600
fn drop ( & mut self ) {
1535
1601
while self . 0 . inner . pop ( ) . is_some ( ) { }
1536
1602
}
@@ -1545,7 +1611,7 @@ impl<'a, T: Ord> Drop for DrainSorted<'a, T> {
1545
1611
}
1546
1612
1547
1613
#[ 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 > {
1549
1615
type Item = T ;
1550
1616
1551
1617
#[ inline]
@@ -1561,20 +1627,20 @@ impl<T: Ord> Iterator for DrainSorted<'_, T> {
1561
1627
}
1562
1628
1563
1629
#[ 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 > { }
1565
1631
1566
1632
#[ 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 > { }
1568
1634
1569
1635
#[ 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 > { }
1571
1637
1572
1638
#[ 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 > {
1574
1640
/// Converts a `Vec<T>` into a `BinaryHeap<T>`.
1575
1641
///
1576
1642
/// 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 > {
1578
1644
let mut heap = BinaryHeap { data : vec } ;
1579
1645
heap. rebuild ( ) ;
1580
1646
heap
@@ -1598,12 +1664,12 @@ impl<T: Ord, const N: usize> From<[T; N]> for BinaryHeap<T> {
1598
1664
}
1599
1665
1600
1666
#[ 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 > {
1602
1668
/// Converts a `BinaryHeap<T>` into a `Vec<T>`.
1603
1669
///
1604
1670
/// This conversion requires no data movement or allocation, and has
1605
1671
/// constant time complexity.
1606
- fn from ( heap : BinaryHeap < T > ) -> Vec < T > {
1672
+ fn from ( heap : BinaryHeap < T , A > ) -> Vec < T , A > {
1607
1673
heap. data
1608
1674
}
1609
1675
}
@@ -1644,7 +1710,7 @@ impl<T> IntoIterator for BinaryHeap<T> {
1644
1710
}
1645
1711
1646
1712
#[ 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 > {
1648
1714
type Item = & ' a T ;
1649
1715
type IntoIter = Iter < ' a , T > ;
1650
1716
@@ -1691,7 +1757,7 @@ impl<T: Ord> SpecExtend<BinaryHeap<T>> for BinaryHeap<T> {
1691
1757
}
1692
1758
}
1693
1759
1694
- impl < T : Ord > BinaryHeap < T > {
1760
+ impl < T : Ord , A : Allocator > BinaryHeap < T , A > {
1695
1761
fn extend_desugared < I : IntoIterator < Item = T > > ( & mut self , iter : I ) {
1696
1762
let iterator = iter. into_iter ( ) ;
1697
1763
let ( lower, _) = iterator. size_hint ( ) ;
0 commit comments