@@ -9,6 +9,7 @@ use std::hash::BuildHasher;
9
9
use std:: hash:: Hasher ;
10
10
use std:: collections:: hash_map:: RandomState ;
11
11
use std:: borrow:: Borrow ;
12
+ use std:: ops:: RangeFull ;
12
13
13
14
use std:: cmp:: { max, Ordering } ;
14
15
use std:: fmt;
@@ -400,18 +401,6 @@ impl<K, V, S> OrderMap<K, V, S>
400
401
pub fn capacity ( & self ) -> usize {
401
402
usable_capacity ( self . raw_capacity ( ) )
402
403
}
403
-
404
- /// Clears the `OrderMap`, returning all key-value pairs as a `Drain`.
405
- /// Keeps the allocated memory for reuse.
406
- pub fn drain ( & mut self ) -> Drain < K , V > {
407
- for i in & mut self . indices {
408
- * i = Pos :: none ( ) ;
409
- }
410
-
411
- Drain {
412
- inner : self . entries . drain ( ..) ,
413
- }
414
- }
415
404
}
416
405
417
406
/// Trait for the "size class". Either u32 or u64 depending on the index
@@ -991,6 +980,17 @@ impl<K, V, S> OrderMap<K, V, S>
991
980
self . entries . sort_by ( move |a, b| cmp ( & a. key , & a. value , & b. key , & b. value ) ) ;
992
981
self . into_iter ( )
993
982
}
983
+ /// Clears the `OrderMap`, returning all key-value pairs as a drain iterator.
984
+ /// Keeps the allocated memory for reuse.
985
+ pub fn drain ( & mut self , range : RangeFull ) -> Drain < K , V > {
986
+ for i in & mut self . indices {
987
+ * i = Pos :: none ( ) ;
988
+ }
989
+
990
+ Drain {
991
+ inner : self . entries . drain ( range) ,
992
+ }
993
+ }
994
994
}
995
995
996
996
impl < K , V , S > OrderMap < K , V , S > {
@@ -1517,6 +1517,9 @@ impl<'a, K, V> Iterator for Drain<'a, K, V> {
1517
1517
fn next ( & mut self ) -> Option < Self :: Item > {
1518
1518
self . inner . next ( ) . map ( |bucket| ( bucket. key , bucket. value ) )
1519
1519
}
1520
+ fn size_hint ( & self ) -> ( usize , Option < usize > ) {
1521
+ self . inner . size_hint ( )
1522
+ }
1520
1523
}
1521
1524
1522
1525
#[ cfg( test) ]
@@ -1713,39 +1716,4 @@ mod tests {
1713
1716
assert_ne ! ( map_a, map_c) ;
1714
1717
assert_ne ! ( map_c, map_a) ;
1715
1718
}
1716
-
1717
-
1718
- #[ test]
1719
- fn drain_basic ( ) {
1720
- let mut map_a = OrderMap :: new ( ) ;
1721
- map_a. insert ( 1 , "1" ) ;
1722
- map_a. insert ( 2 , "2" ) ;
1723
- let entries: Vec < ( u32 , & str ) > = map_a. drain ( ) . collect ( ) ;
1724
- assert ! ( map_a. is_empty( ) ) ;
1725
- assert_eq ! ( entries. len( ) , 2 ) ;
1726
- assert ! ( map_a. is_empty( ) ) ;
1727
-
1728
- map_a. insert ( 3 , "3" ) ;
1729
- assert ! ( !map_a. is_empty( ) ) ;
1730
- assert_eq ! ( map_a. len( ) , 1 ) ;
1731
- assert ! ( map_a. get( & 3 ) . is_some( ) ) ;
1732
- assert ! ( map_a. get( & 1 ) . is_none( ) ) ;
1733
- }
1734
-
1735
- #[ test]
1736
- fn drain_after_resize ( ) {
1737
- let mut map_a = OrderMap :: with_capacity ( 2 ) ;
1738
- map_a. insert ( 1 , "1" ) ;
1739
- map_a. insert ( 2 , "2" ) ;
1740
- map_a. insert ( 3 , "3" ) ;
1741
- let entries: Vec < ( u32 , & str ) > = map_a. drain ( ) . collect ( ) ;
1742
- assert ! ( map_a. is_empty( ) ) ;
1743
- assert_eq ! ( entries. len( ) , 3 ) ;
1744
-
1745
- map_a. insert ( 4 , "4" ) ;
1746
- assert ! ( !map_a. is_empty( ) ) ;
1747
- assert_eq ! ( map_a. len( ) , 1 ) ;
1748
- assert ! ( map_a. get( & 4 ) . is_some( ) ) ;
1749
- assert ! ( map_a. get( & 1 ) . is_none( ) ) ;
1750
- }
1751
1719
}
0 commit comments