-
Notifications
You must be signed in to change notification settings - Fork 294
Open
Description
For example:
pub fn split_before(&mut self) -> LinkedList<T> {
// We have this:
//
// list.front -> A <-> B <- list.back
// ^
// cur
//
//
// And we want to produce this:
//
// list.front -> A <-> B <- list.back
// ^
// cur
//
//
// return.front -> None <- return.back
//
if let Some(cur) = self.cur {
unsafe {
let old_len = self.list.len; // 2
let old_idx = self.index.unwrap(); // 0
let prev = (*cur.as_ptr()).front; // None
let new_len = old_len - old_idx; // 2
let new_front = self.cur;
let new_back = self.list.back;
let new_idx = Some(0);
let output_len = old_len - new_len; // 0
// oops...
let output_front = self.list.front; // -> A
let output_back = prev; // None
if let Some(prev) = prev {
(*cur.as_ptr()).front = None;
(*prev.as_ptr()).back = None;
}
self.list.len = new_len;
self.list.front = new_front;
self.list.back = new_back;
self.index = new_idx;
LinkedList {
front: output_front, // -> A
back: output_back, // None
len: output_len, // 0
_boo: PhantomData,
}
}
} else {
std::mem::replace(self.list, LinkedList::new())
}
}
necrashter, chikaku, FailFish and overnap
Metadata
Metadata
Assignees
Labels
No labels