Make dlist::split use new DList::split_off.

This commit is contained in:
Matt Brubeck 2015-02-12 10:41:01 -08:00
parent 76a2653f8f
commit 04fb3a5267
2 changed files with 9 additions and 63 deletions

View file

@ -50,13 +50,13 @@ impl TextRunScanner {
let mut last_whitespace = true; let mut last_whitespace = true;
while !fragments.is_empty() { while !fragments.is_empty() {
// Create a clump. // Create a clump.
self.clump.append(&mut dlist::split(&mut fragments)); self.clump.append(&mut dlist::split_off_head(&mut fragments));
while !fragments.is_empty() && self.clump while !fragments.is_empty() && self.clump
.back() .back()
.unwrap() .unwrap()
.can_merge_with_fragment(fragments.front() .can_merge_with_fragment(fragments.front()
.unwrap()) { .unwrap()) {
self.clump.append(&mut dlist::split(&mut fragments)); self.clump.append(&mut dlist::split_off_head(&mut fragments));
} }
// Flush that clump to the list of fragments we're building up. // Flush that clump to the list of fragments we're building up.

View file

@ -6,69 +6,15 @@
use std::collections::DList; use std::collections::DList;
use std::mem; use std::mem;
use std::ptr;
struct RawDList<T> { /// Splits the head off a list in O(1) time, and returns the head.
length: uint, pub fn split_off_head<T>(list: &mut DList<T>) -> DList<T> {
head: *mut RawNode<T>, // FIXME: Work around https://github.com/rust-lang/rust/issues/22244
tail: *mut RawNode<T>, if list.len() == 1 {
} return mem::replace(list, DList::new());
#[allow(dead_code)]
struct RawNode<T> {
next: *mut RawNode<T>,
prev: *mut RawNode<T>,
value: T,
}
#[unsafe_destructor]
impl<T> Drop for RawDList<T> {
fn drop(&mut self) {
panic!("shouldn't happen")
}
}
/// Workaround for a missing method on Rust's `DList` type. Splits the head off a list in O(1)
/// time.
pub fn split<T>(list: &mut DList<T>) -> DList<T> {
let list = unsafe {
mem::transmute::<&mut DList<T>,&mut RawDList<T>>(list)
};
if list.length == 0 {
panic!("split_dlist(): empty list")
}
let head_node = mem::replace(&mut list.head, ptr::null_mut());
let head_list = RawDList {
length: 1,
head: head_node,
tail: head_node,
};
debug_assert!(list.head.is_null());
unsafe {
mem::swap(&mut (*head_list.head).next, &mut list.head);
debug_assert!((*head_list.head).next.is_null());
debug_assert!((*head_list.head).prev.is_null());
(*head_list.head).prev = ptr::null_mut();
}
list.length -= 1;
if list.length == 0 {
list.tail = ptr::null_mut()
} else {
if list.length == 1 {
list.tail = list.head
}
unsafe {
(*list.head).prev = ptr::null_mut()
}
}
unsafe {
mem::transmute::<RawDList<T>,DList<T>>(head_list)
} }
let tail = list.split_off(1);
mem::replace(list, tail)
} }
/// Prepends the items in the other list to this one, leaving the other list empty. /// Prepends the items in the other list to this one, leaving the other list empty.