-
Notifications
You must be signed in to change notification settings - Fork 14k
Vec::dedup_by optimization #82191
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Vec::dedup_by optimization #82191
Changes from 6 commits
1825810
c114894
2abab1f
afdbc9e
2285f11
96d6f22
b0092bc
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2102,6 +2102,132 @@ fn test_extend_from_within() { | |
| assert_eq!(v, ["a", "b", "c", "b", "c", "a", "b"]); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_vec_dedup_by() { | ||
| let mut vec: Vec<i32> = vec![1, -1, 2, 3, 1, -5, 5, -2, 2]; | ||
|
|
||
| vec.dedup_by(|a, b| a.abs() == b.abs()); | ||
|
|
||
| assert_eq!(vec, [1, 2, 3, 1, -5, -2]); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_vec_dedup_empty() { | ||
| let mut vec: Vec<i32> = Vec::new(); | ||
|
|
||
| vec.dedup(); | ||
|
|
||
| assert_eq!(vec, []); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_vec_dedup_one() { | ||
| let mut vec = vec![12i32]; | ||
|
|
||
| vec.dedup(); | ||
|
|
||
| assert_eq!(vec, [12]); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_vec_dedup_multiple_ident() { | ||
| let mut vec = vec![12, 12, 12, 12, 12, 11, 11, 11, 11, 11, 11]; | ||
|
|
||
| vec.dedup(); | ||
|
|
||
| assert_eq!(vec, [12, 11]); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_vec_dedup_partialeq() { | ||
| #[derive(Debug)] | ||
| struct Foo(i32, i32); | ||
|
|
||
| impl PartialEq for Foo { | ||
| fn eq(&self, other: &Foo) -> bool { | ||
| self.0 == other.0 | ||
| } | ||
| } | ||
|
|
||
| let mut vec = vec![Foo(0, 1), Foo(0, 5), Foo(1, 7), Foo(1, 9)]; | ||
|
|
||
| vec.dedup(); | ||
| assert_eq!(vec, [Foo(0, 1), Foo(1, 7)]); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_vec_dedup() { | ||
| let mut vec: Vec<bool> = Vec::with_capacity(8); | ||
| let mut template = vec.clone(); | ||
|
|
||
| for x in 0u8..255u8 { | ||
| vec.clear(); | ||
| template.clear(); | ||
|
|
||
| let iter = (0..8).map(move |bit| (x >> bit) & 1 == 1); | ||
| vec.extend(iter); | ||
| template.extend_from_slice(&vec); | ||
|
|
||
| let (dedup, _) = template.partition_dedup(); | ||
| vec.dedup(); | ||
|
|
||
| assert_eq!(vec, dedup); | ||
| } | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_vec_dedup_panicking() { | ||
| #[derive(Debug)] | ||
| struct Panic { | ||
| drop_counter: &'static AtomicU32, | ||
| value: bool, | ||
| index: usize, | ||
| } | ||
|
|
||
| impl PartialEq for Panic { | ||
| fn eq(&self, other: &Self) -> bool { | ||
| self.value == other.value | ||
| } | ||
| } | ||
|
|
||
| impl Drop for Panic { | ||
| fn drop(&mut self) { | ||
| let x = self.drop_counter.fetch_add(1, Ordering::SeqCst); | ||
| assert!(x != 4); | ||
| } | ||
| } | ||
|
|
||
| static DROP_COUNTER: AtomicU32 = AtomicU32::new(0); | ||
| let expected = [ | ||
| Panic { drop_counter: &DROP_COUNTER, value: false, index: 0 }, | ||
| Panic { drop_counter: &DROP_COUNTER, value: false, index: 5 }, | ||
| Panic { drop_counter: &DROP_COUNTER, value: true, index: 6 }, | ||
| Panic { drop_counter: &DROP_COUNTER, value: true, index: 7 }, | ||
| ]; | ||
| let mut vec = vec![ | ||
| Panic { drop_counter: &DROP_COUNTER, value: false, index: 0 }, | ||
| // these elements get deduplicated | ||
| Panic { drop_counter: &DROP_COUNTER, value: false, index: 1 }, | ||
| Panic { drop_counter: &DROP_COUNTER, value: false, index: 2 }, | ||
| Panic { drop_counter: &DROP_COUNTER, value: false, index: 3 }, | ||
| Panic { drop_counter: &DROP_COUNTER, value: false, index: 4 }, | ||
| // here it panics | ||
| Panic { drop_counter: &DROP_COUNTER, value: false, index: 5 }, | ||
| Panic { drop_counter: &DROP_COUNTER, value: true, index: 6 }, | ||
| Panic { drop_counter: &DROP_COUNTER, value: true, index: 7 }, | ||
| ]; | ||
|
|
||
| let _ = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| { | ||
| vec.dedup(); | ||
| })); | ||
|
|
||
| let ok = vec.iter().zip(expected.iter()).all(|(x, y)| x.index == y.index); | ||
|
|
||
| if !ok { | ||
| panic!("expected: {:?}\ngot: {:?}\n", expected, vec); | ||
| } | ||
| } | ||
|
|
||
| // Regression test for issue #82533 | ||
| #[test] | ||
| fn test_extend_from_within_panicing_clone() { | ||
|
|
@@ -2141,4 +2267,4 @@ fn test_extend_from_within_panicing_clone() { | |
| std::panic::catch_unwind(move || vec.extend_from_within(..)).unwrap_err(); | ||
|
|
||
| assert_eq!(count.load(Ordering::SeqCst), 4); | ||
| } | ||
| } | ||
|
||
Uh oh!
There was an error while loading. Please reload this page.