Skip to content

Commit 7876b5b

Browse files
committed
Rename first*/last* BTree{Set,Map} methods to min*/max*
Note: `BTreeSet::{first,last}` are renamed to `{get_min,get_max}` to avoid conflicts with `Ord::{min,max}`.
1 parent 97cde9f commit 7876b5b

File tree

6 files changed

+139
-148
lines changed

6 files changed

+139
-148
lines changed

library/alloc/benches/btree/map.rs

+19-19
Original file line numberDiff line numberDiff line change
@@ -227,17 +227,17 @@ pub fn iteration_mut_100000(b: &mut Bencher) {
227227
bench_iteration_mut(b, 100000);
228228
}
229229

230-
fn bench_first_and_last_nightly(b: &mut Bencher, size: i32) {
230+
fn bench_min_and_max_nightly(b: &mut Bencher, size: i32) {
231231
let map: BTreeMap<_, _> = (0..size).map(|i| (i, i)).collect();
232232
b.iter(|| {
233233
for _ in 0..10 {
234-
black_box(map.first_key_value());
235-
black_box(map.last_key_value());
234+
black_box(map.min_key_value());
235+
black_box(map.max_key_value());
236236
}
237237
});
238238
}
239239

240-
fn bench_first_and_last_stable(b: &mut Bencher, size: i32) {
240+
fn bench_min_and_max_stable(b: &mut Bencher, size: i32) {
241241
let map: BTreeMap<_, _> = (0..size).map(|i| (i, i)).collect();
242242
b.iter(|| {
243243
for _ in 0..10 {
@@ -248,33 +248,33 @@ fn bench_first_and_last_stable(b: &mut Bencher, size: i32) {
248248
}
249249

250250
#[bench]
251-
pub fn first_and_last_0_nightly(b: &mut Bencher) {
252-
bench_first_and_last_nightly(b, 0);
251+
pub fn min_and_max_0_nightly(b: &mut Bencher) {
252+
bench_min_and_max_nightly(b, 0);
253253
}
254254

255255
#[bench]
256-
pub fn first_and_last_0_stable(b: &mut Bencher) {
257-
bench_first_and_last_stable(b, 0);
256+
pub fn min_and_max_0_stable(b: &mut Bencher) {
257+
bench_min_and_max_stable(b, 0);
258258
}
259259

260260
#[bench]
261-
pub fn first_and_last_100_nightly(b: &mut Bencher) {
262-
bench_first_and_last_nightly(b, 100);
261+
pub fn min_and_max_100_nightly(b: &mut Bencher) {
262+
bench_min_and_max_nightly(b, 100);
263263
}
264264

265265
#[bench]
266-
pub fn first_and_last_100_stable(b: &mut Bencher) {
267-
bench_first_and_last_stable(b, 100);
266+
pub fn min_and_max_100_stable(b: &mut Bencher) {
267+
bench_min_and_max_stable(b, 100);
268268
}
269269

270270
#[bench]
271-
pub fn first_and_last_10k_nightly(b: &mut Bencher) {
272-
bench_first_and_last_nightly(b, 10_000);
271+
pub fn min_and_max_10k_nightly(b: &mut Bencher) {
272+
bench_min_and_max_nightly(b, 10_000);
273273
}
274274

275275
#[bench]
276-
pub fn first_and_last_10k_stable(b: &mut Bencher) {
277-
bench_first_and_last_stable(b, 10_000);
276+
pub fn min_and_max_10k_stable(b: &mut Bencher) {
277+
bench_min_and_max_stable(b, 10_000);
278278
}
279279

280280
const BENCH_RANGE_SIZE: i32 = 145;
@@ -410,7 +410,7 @@ pub fn clone_slim_100_and_pop_all(b: &mut Bencher) {
410410
let src = slim_map(100);
411411
b.iter(|| {
412412
let mut map = src.clone();
413-
while map.pop_first().is_some() {}
413+
while map.pop_min().is_some() {}
414414
map
415415
});
416416
}
@@ -481,7 +481,7 @@ pub fn clone_slim_10k_and_pop_all(b: &mut Bencher) {
481481
let src = slim_map(10_000);
482482
b.iter(|| {
483483
let mut map = src.clone();
484-
while map.pop_first().is_some() {}
484+
while map.pop_min().is_some() {}
485485
map
486486
});
487487
}
@@ -552,7 +552,7 @@ pub fn clone_fat_val_100_and_pop_all(b: &mut Bencher) {
552552
let src = fat_val_map(100);
553553
b.iter(|| {
554554
let mut map = src.clone();
555-
while map.pop_first().is_some() {}
555+
while map.pop_min().is_some() {}
556556
map
557557
});
558558
}

library/alloc/benches/btree/set.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ pub fn clone_100_and_pop_all(b: &mut Bencher) {
9393
let src = slim_set(100);
9494
b.iter(|| {
9595
let mut set = src.clone();
96-
while set.pop_first().is_some() {}
96+
while set.pop_min().is_some() {}
9797
set
9898
});
9999
}
@@ -164,7 +164,7 @@ pub fn clone_10k_and_pop_all(b: &mut Bencher) {
164164
let src = slim_set(10_000);
165165
b.iter(|| {
166166
let mut set = src.clone();
167-
while set.pop_first().is_some() {}
167+
while set.pop_min().is_some() {}
168168
set
169169
});
170170
}

library/alloc/src/collections/btree/map.rs

+26-31
Original file line numberDiff line numberDiff line change
@@ -600,8 +600,7 @@ impl<K, V> BTreeMap<K, V> {
600600
}
601601
}
602602

603-
/// Returns the first key-value pair in the map.
604-
/// The key in this pair is the minimum key in the map.
603+
/// Returns the key-value pair with the minimum key in the map.
605604
///
606605
/// # Examples
607606
///
@@ -612,22 +611,21 @@ impl<K, V> BTreeMap<K, V> {
612611
/// use std::collections::BTreeMap;
613612
///
614613
/// let mut map = BTreeMap::new();
615-
/// assert_eq!(map.first_key_value(), None);
614+
/// assert_eq!(map.min_key_value(), None);
616615
/// map.insert(1, "b");
617616
/// map.insert(2, "a");
618-
/// assert_eq!(map.first_key_value(), Some((&1, &"b")));
617+
/// assert_eq!(map.min_key_value(), Some((&1, &"b")));
619618
/// ```
620619
#[unstable(feature = "map_first_last", issue = "62924")]
621-
pub fn first_key_value(&self) -> Option<(&K, &V)>
620+
pub fn min_key_value(&self) -> Option<(&K, &V)>
622621
where
623622
K: Ord,
624623
{
625624
let root_node = self.root.as_ref()?.reborrow();
626625
root_node.first_leaf_edge().right_kv().ok().map(Handle::into_kv)
627626
}
628627

629-
/// Returns the first entry in the map for in-place manipulation.
630-
/// The key of this entry is the minimum key in the map.
628+
/// Returns the entry with the minimum key in the map for in-place manipulation.
631629
///
632630
/// # Examples
633631
///
@@ -638,16 +636,16 @@ impl<K, V> BTreeMap<K, V> {
638636
/// let mut map = BTreeMap::new();
639637
/// map.insert(1, "a");
640638
/// map.insert(2, "b");
641-
/// if let Some(mut entry) = map.first_entry() {
639+
/// if let Some(mut entry) = map.min_entry() {
642640
/// if *entry.key() > 0 {
643-
/// entry.insert("first");
641+
/// entry.insert("min");
644642
/// }
645643
/// }
646-
/// assert_eq!(*map.get(&1).unwrap(), "first");
644+
/// assert_eq!(*map.get(&1).unwrap(), "min");
647645
/// assert_eq!(*map.get(&2).unwrap(), "b");
648646
/// ```
649647
#[unstable(feature = "map_first_last", issue = "62924")]
650-
pub fn first_entry(&mut self) -> Option<OccupiedEntry<'_, K, V>>
648+
pub fn min_entry(&mut self) -> Option<OccupiedEntry<'_, K, V>>
651649
where
652650
K: Ord,
653651
{
@@ -657,8 +655,7 @@ impl<K, V> BTreeMap<K, V> {
657655
Some(OccupiedEntry { handle: kv.forget_node_type(), dormant_map, _marker: PhantomData })
658656
}
659657

660-
/// Removes and returns the first element in the map.
661-
/// The key of this element is the minimum key that was in the map.
658+
/// Removes and returns the key-value pair with the minimum key in the map.
662659
///
663660
/// # Examples
664661
///
@@ -671,21 +668,21 @@ impl<K, V> BTreeMap<K, V> {
671668
/// let mut map = BTreeMap::new();
672669
/// map.insert(1, "a");
673670
/// map.insert(2, "b");
674-
/// while let Some((key, _val)) = map.pop_first() {
671+
/// while let Some((key, _val)) = map.pop_min() {
675672
/// assert!(map.iter().all(|(k, _v)| *k > key));
676673
/// }
677674
/// assert!(map.is_empty());
678675
/// ```
679676
#[unstable(feature = "map_first_last", issue = "62924")]
680-
pub fn pop_first(&mut self) -> Option<(K, V)>
677+
pub fn pop_min(&mut self) -> Option<(K, V)>
681678
where
682679
K: Ord,
683680
{
684-
self.first_entry().map(|entry| entry.remove_entry())
681+
self.min_entry().map(|entry| entry.remove_entry())
685682
}
686683

687-
/// Returns the last key-value pair in the map.
688-
/// The key in this pair is the maximum key in the map.
684+
/// Returns the key-value pair with the maximum key in the
685+
/// map. The key in this pair is the maximum key in the map.
689686
///
690687
/// # Examples
691688
///
@@ -698,19 +695,18 @@ impl<K, V> BTreeMap<K, V> {
698695
/// let mut map = BTreeMap::new();
699696
/// map.insert(1, "b");
700697
/// map.insert(2, "a");
701-
/// assert_eq!(map.last_key_value(), Some((&2, &"a")));
698+
/// assert_eq!(map.max_key_value(), Some((&2, &"a")));
702699
/// ```
703700
#[unstable(feature = "map_first_last", issue = "62924")]
704-
pub fn last_key_value(&self) -> Option<(&K, &V)>
701+
pub fn max_key_value(&self) -> Option<(&K, &V)>
705702
where
706703
K: Ord,
707704
{
708705
let root_node = self.root.as_ref()?.reborrow();
709706
root_node.last_leaf_edge().left_kv().ok().map(Handle::into_kv)
710707
}
711708

712-
/// Returns the last entry in the map for in-place manipulation.
713-
/// The key of this entry is the maximum key in the map.
709+
/// Returns the entry with the maximum key in the map for in-place manipulation.
714710
///
715711
/// # Examples
716712
///
@@ -721,16 +717,16 @@ impl<K, V> BTreeMap<K, V> {
721717
/// let mut map = BTreeMap::new();
722718
/// map.insert(1, "a");
723719
/// map.insert(2, "b");
724-
/// if let Some(mut entry) = map.last_entry() {
720+
/// if let Some(mut entry) = map.max_entry() {
725721
/// if *entry.key() > 0 {
726-
/// entry.insert("last");
722+
/// entry.insert("max");
727723
/// }
728724
/// }
729725
/// assert_eq!(*map.get(&1).unwrap(), "a");
730-
/// assert_eq!(*map.get(&2).unwrap(), "last");
726+
/// assert_eq!(*map.get(&2).unwrap(), "max");
731727
/// ```
732728
#[unstable(feature = "map_first_last", issue = "62924")]
733-
pub fn last_entry(&mut self) -> Option<OccupiedEntry<'_, K, V>>
729+
pub fn max_entry(&mut self) -> Option<OccupiedEntry<'_, K, V>>
734730
where
735731
K: Ord,
736732
{
@@ -740,8 +736,7 @@ impl<K, V> BTreeMap<K, V> {
740736
Some(OccupiedEntry { handle: kv.forget_node_type(), dormant_map, _marker: PhantomData })
741737
}
742738

743-
/// Removes and returns the last element in the map.
744-
/// The key of this element is the maximum key that was in the map.
739+
/// Removes and returns the key-value pair with the maximum key in the map.
745740
///
746741
/// # Examples
747742
///
@@ -754,17 +749,17 @@ impl<K, V> BTreeMap<K, V> {
754749
/// let mut map = BTreeMap::new();
755750
/// map.insert(1, "a");
756751
/// map.insert(2, "b");
757-
/// while let Some((key, _val)) = map.pop_last() {
752+
/// while let Some((key, _val)) = map.pop_max() {
758753
/// assert!(map.iter().all(|(k, _v)| *k < key));
759754
/// }
760755
/// assert!(map.is_empty());
761756
/// ```
762757
#[unstable(feature = "map_first_last", issue = "62924")]
763-
pub fn pop_last(&mut self) -> Option<(K, V)>
758+
pub fn pop_max(&mut self) -> Option<(K, V)>
764759
where
765760
K: Ord,
766761
{
767-
self.last_entry().map(|entry| entry.remove_entry())
762+
self.max_entry().map(|entry| entry.remove_entry())
768763
}
769764

770765
/// Returns `true` if the map contains a value for the specified key.

0 commit comments

Comments
 (0)