mirror of
https://github.com/rust-lang/rust.git
synced 2025-05-14 02:49:40 +00:00
Rollup merge of #64975 - crgl:clone-from-linked-list, r=bluss
Implement Clone::clone_from for LinkedList See #28481. This represents a substantial speedup when the list sizes are comparable, and shouldn't ever be significantly worse. Technically split_off is doing an unnecessary search, but the code is hopefully cleaner as a result. I'm happy to rework anything that needs to be changed as well!
This commit is contained in:
commit
c4f8fd2bf5
@ -1197,6 +1197,19 @@ impl<T: Clone> Clone for LinkedList<T> {
|
||||
fn clone(&self) -> Self {
|
||||
self.iter().cloned().collect()
|
||||
}
|
||||
|
||||
fn clone_from(&mut self, other: &Self) {
|
||||
let mut iter_other = other.iter();
|
||||
if self.len() > other.len() {
|
||||
self.split_off(other.len());
|
||||
}
|
||||
for (elem, elem_other) in self.iter_mut().zip(&mut iter_other) {
|
||||
elem.clone_from(elem_other);
|
||||
}
|
||||
if !iter_other.is_empty() {
|
||||
self.extend(iter_other.cloned());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[stable(feature = "rust1", since = "1.0.0")]
|
||||
|
@ -110,6 +110,49 @@ fn test_append() {
|
||||
check_links(&n);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_clone_from() {
|
||||
// Short cloned from long
|
||||
{
|
||||
let v = vec![1, 2, 3, 4, 5];
|
||||
let u = vec![8, 7, 6, 2, 3, 4, 5];
|
||||
let mut m = list_from(&v);
|
||||
let n = list_from(&u);
|
||||
m.clone_from(&n);
|
||||
check_links(&m);
|
||||
assert_eq!(m, n);
|
||||
for elt in u {
|
||||
assert_eq!(m.pop_front(), Some(elt))
|
||||
}
|
||||
}
|
||||
// Long cloned from short
|
||||
{
|
||||
let v = vec![1, 2, 3, 4, 5];
|
||||
let u = vec![6, 7, 8];
|
||||
let mut m = list_from(&v);
|
||||
let n = list_from(&u);
|
||||
m.clone_from(&n);
|
||||
check_links(&m);
|
||||
assert_eq!(m, n);
|
||||
for elt in u {
|
||||
assert_eq!(m.pop_front(), Some(elt))
|
||||
}
|
||||
}
|
||||
// Two equal length lists
|
||||
{
|
||||
let v = vec![1, 2, 3, 4, 5];
|
||||
let u = vec![9, 8, 1, 2, 3];
|
||||
let mut m = list_from(&v);
|
||||
let n = list_from(&u);
|
||||
m.clone_from(&n);
|
||||
check_links(&m);
|
||||
assert_eq!(m, n);
|
||||
for elt in u {
|
||||
assert_eq!(m.pop_front(), Some(elt))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_insert_prev() {
|
||||
let mut m = list_from(&[0, 2, 4, 6, 8]);
|
||||
|
Loading…
Reference in New Issue
Block a user