Skip to content

allow DList to split_at len. fixes #22244 #22247

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

Merged
merged 1 commit into from
Feb 16, 2015
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 20 additions & 1 deletion src/libcollections/dlist.rs
Original file line number Diff line number Diff line change
Expand Up @@ -560,7 +560,12 @@ impl<T> DList<T> {
/// Splits the list into two at the given index. Returns everything after the given index,
/// including the index.
///
/// # Panics
///
/// Panics if `at > len`.
///
/// This operation should compute in O(n) time.
///
/// # Examples
///
/// ```
Expand All @@ -580,9 +585,11 @@ impl<T> DList<T> {
#[stable(feature = "rust1", since = "1.0.0")]
pub fn split_off(&mut self, at: usize) -> DList<T> {
let len = self.len();
assert!(at < len, "Cannot split off at a nonexistent index");
assert!(at <= len, "Cannot split off at a nonexistent index");
if at == 0 {
return mem::replace(self, DList::new());
} else if at == len {
return DList::new();
}

// Below, we iterate towards the `i-1`th node, either from the start or the end,
Expand Down Expand Up @@ -1116,6 +1123,18 @@ mod tests {
}
}

// no-op on the last index
{
let mut m = DList::new();
m.push_back(1);

let p = m.split_off(1);
assert_eq!(m.len(), 1);
assert_eq!(p.len(), 0);
assert_eq!(m.back(), Some(&1));
assert_eq!(m.front(), Some(&1));
}

}

#[test]
Expand Down