dlist: Use inline on very small functions and iterator functions

This commit is contained in:
blake2-ppc 2013-07-14 23:03:54 +02:00
parent 7681cf62e3
commit 961184f852

View File

@ -101,10 +101,12 @@ fn link_with_prev<T>(mut next: ~Node<T>, prev: Rawlink<Node<T>>) -> Link<T> {
impl<T> Container for DList<T> { impl<T> Container for DList<T> {
/// O(1) /// O(1)
#[inline]
fn is_empty(&self) -> bool { fn is_empty(&self) -> bool {
self.list_head.is_none() self.list_head.is_none()
} }
/// O(1) /// O(1)
#[inline]
fn len(&self) -> uint { fn len(&self) -> uint {
self.length self.length
} }
@ -114,6 +116,7 @@ impl<T> Mutable for DList<T> {
/// Remove all elements from the DList /// Remove all elements from the DList
/// ///
/// O(N) /// O(N)
#[inline]
fn clear(&mut self) { fn clear(&mut self) {
*self = DList::new() *self = DList::new()
} }
@ -121,21 +124,25 @@ impl<T> Mutable for DList<T> {
impl<T> Deque<T> for DList<T> { impl<T> Deque<T> for DList<T> {
/// Provide a reference to the front element, or None if the list is empty /// Provide a reference to the front element, or None if the list is empty
#[inline]
fn front<'a>(&'a self) -> Option<&'a T> { fn front<'a>(&'a self) -> Option<&'a T> {
self.list_head.map(|head| &head.value) self.list_head.map(|head| &head.value)
} }
/// Provide a mutable reference to the front element, or None if the list is empty /// Provide a mutable reference to the front element, or None if the list is empty
#[inline]
fn front_mut<'a>(&'a mut self) -> Option<&'a mut T> { fn front_mut<'a>(&'a mut self) -> Option<&'a mut T> {
self.list_head.map_mut(|head| &mut head.value) self.list_head.map_mut(|head| &mut head.value)
} }
/// Provide a reference to the back element, or None if the list is empty /// Provide a reference to the back element, or None if the list is empty
#[inline]
fn back<'a>(&'a self) -> Option<&'a T> { fn back<'a>(&'a self) -> Option<&'a T> {
self.list_tail.resolve_immut().map(|tail| &tail.value) self.list_tail.resolve_immut().map(|tail| &tail.value)
} }
/// Provide a mutable reference to the back element, or None if the list is empty /// Provide a mutable reference to the back element, or None if the list is empty
#[inline]
fn back_mut<'a>(&'a mut self) -> Option<&'a mut T> { fn back_mut<'a>(&'a mut self) -> Option<&'a mut T> {
self.list_tail.resolve().map_mut(|tail| &mut tail.value) self.list_tail.resolve().map_mut(|tail| &mut tail.value)
} }
@ -158,7 +165,6 @@ impl<T> Deque<T> for DList<T> {
/// Remove the last element and return it, or None if the list is empty /// Remove the last element and return it, or None if the list is empty
/// ///
/// O(1) /// O(1)
#[inline]
fn pop_back(&mut self) -> Option<T> { fn pop_back(&mut self) -> Option<T> {
match self.list_tail.resolve() { match self.list_tail.resolve() {
None => None, None => None,
@ -250,6 +256,7 @@ impl<T> DList<T> {
/// Add all elements from `other` to the beginning of the list /// Add all elements from `other` to the beginning of the list
/// ///
/// O(1) /// O(1)
#[inline]
pub fn prepend(&mut self, mut other: DList<T>) { pub fn prepend(&mut self, mut other: DList<T>) {
util::swap(self, &mut other); util::swap(self, &mut other);
self.append(other); self.append(other);
@ -259,7 +266,6 @@ impl<T> DList<T> {
/// or at the end. /// or at the end.
/// ///
/// O(N) /// O(N)
#[inline]
pub fn insert_when(&mut self, elt: T, f: &fn(&T, &T) -> bool) { pub fn insert_when(&mut self, elt: T, f: &fn(&T, &T) -> bool) {
{ {
let mut it = self.mut_iter(); let mut it = self.mut_iter();
@ -300,16 +306,19 @@ impl<T> DList<T> {
/// Provide a forward iterator /// Provide a forward iterator
#[inline]
pub fn iter<'a>(&'a self) -> DListIterator<'a, T> { pub fn iter<'a>(&'a self) -> DListIterator<'a, T> {
DListIterator{nelem: self.len(), head: &self.list_head, tail: self.list_tail} DListIterator{nelem: self.len(), head: &self.list_head, tail: self.list_tail}
} }
/// Provide a reverse iterator /// Provide a reverse iterator
#[inline]
pub fn rev_iter<'a>(&'a self) -> InvertIterator<&'a T, DListIterator<'a, T>> { pub fn rev_iter<'a>(&'a self) -> InvertIterator<&'a T, DListIterator<'a, T>> {
self.iter().invert() self.iter().invert()
} }
/// Provide a forward iterator with mutable references /// Provide a forward iterator with mutable references
#[inline]
pub fn mut_iter<'a>(&'a mut self) -> MutDListIterator<'a, T> { pub fn mut_iter<'a>(&'a mut self) -> MutDListIterator<'a, T> {
let head_raw = match self.list_head { let head_raw = match self.list_head {
Some(ref mut h) => Rawlink::some(*h), Some(ref mut h) => Rawlink::some(*h),
@ -323,6 +332,7 @@ impl<T> DList<T> {
} }
} }
/// Provide a reverse iterator with mutable references /// Provide a reverse iterator with mutable references
#[inline]
pub fn mut_rev_iter<'a>(&'a mut self) -> InvertIterator<&'a mut T, pub fn mut_rev_iter<'a>(&'a mut self) -> InvertIterator<&'a mut T,
MutDListIterator<'a, T>> { MutDListIterator<'a, T>> {
self.mut_iter().invert() self.mut_iter().invert()
@ -330,11 +340,13 @@ impl<T> DList<T> {
/// Consume the list into an iterator yielding elements by value /// Consume the list into an iterator yielding elements by value
#[inline]
pub fn consume_iter(self) -> ConsumeIterator<T> { pub fn consume_iter(self) -> ConsumeIterator<T> {
ConsumeIterator{list: self} ConsumeIterator{list: self}
} }
/// Consume the list into an iterator yielding elements by value, in reverse /// Consume the list into an iterator yielding elements by value, in reverse
#[inline]
pub fn consume_rev_iter(self) -> InvertIterator<T, ConsumeIterator<T>> { pub fn consume_rev_iter(self) -> InvertIterator<T, ConsumeIterator<T>> {
self.consume_iter().invert() self.consume_iter().invert()
} }
@ -344,6 +356,7 @@ impl<T: cmp::TotalOrd> DList<T> {
/// Insert `elt` sorted in ascending order /// Insert `elt` sorted in ascending order
/// ///
/// O(N) /// O(N)
#[inline]
pub fn insert_ordered(&mut self, elt: T) { pub fn insert_ordered(&mut self, elt: T) {
self.insert_when(elt, |a, b| a.cmp(b) != cmp::Less); self.insert_when(elt, |a, b| a.cmp(b) != cmp::Less);
} }
@ -365,12 +378,14 @@ impl<'self, A> Iterator<&'self A> for DListIterator<'self, A> {
} }
} }
#[inline]
fn size_hint(&self) -> (uint, Option<uint>) { fn size_hint(&self) -> (uint, Option<uint>) {
(self.nelem, Some(self.nelem)) (self.nelem, Some(self.nelem))
} }
} }
impl<'self, A> DoubleEndedIterator<&'self A> for DListIterator<'self, A> { impl<'self, A> DoubleEndedIterator<&'self A> for DListIterator<'self, A> {
#[inline]
fn next_back(&mut self) -> Option<&'self A> { fn next_back(&mut self) -> Option<&'self A> {
if self.nelem == 0 { if self.nelem == 0 {
return None; return None;
@ -405,6 +420,7 @@ impl<'self, A> Iterator<&'self mut A> for MutDListIterator<'self, A> {
} }
} }
#[inline]
fn size_hint(&self) -> (uint, Option<uint>) { fn size_hint(&self) -> (uint, Option<uint>) {
(self.nelem, Some(self.nelem)) (self.nelem, Some(self.nelem))
} }
@ -457,6 +473,7 @@ impl<'self, A> ListInsertion<A> for MutDListIterator<'self, A> {
} }
} }
#[inline]
fn peek_next<'a>(&'a mut self) -> Option<&'a mut A> { fn peek_next<'a>(&'a mut self) -> Option<&'a mut A> {
match self.head.resolve() { match self.head.resolve() {
None => None, None => None,
@ -466,13 +483,17 @@ impl<'self, A> ListInsertion<A> for MutDListIterator<'self, A> {
} }
impl<A> Iterator<A> for ConsumeIterator<A> { impl<A> Iterator<A> for ConsumeIterator<A> {
#[inline]
fn next(&mut self) -> Option<A> { self.list.pop_front() } fn next(&mut self) -> Option<A> { self.list.pop_front() }
#[inline]
fn size_hint(&self) -> (uint, Option<uint>) { fn size_hint(&self) -> (uint, Option<uint>) {
(self.list.length, Some(self.list.length)) (self.list.length, Some(self.list.length))
} }
} }
impl<A> DoubleEndedIterator<A> for ConsumeIterator<A> { impl<A> DoubleEndedIterator<A> for ConsumeIterator<A> {
#[inline]
fn next_back(&mut self) -> Option<A> { self.list.pop_back() } fn next_back(&mut self) -> Option<A> { self.list.pop_back() }
} }
@ -489,6 +510,8 @@ impl<A: Eq> Eq for DList<A> {
self.len() == other.len() && self.len() == other.len() &&
self.iter().zip(other.iter()).all(|(a, b)| a.eq(b)) self.iter().zip(other.iter()).all(|(a, b)| a.eq(b))
} }
#[inline]
fn ne(&self, other: &DList<A>) -> bool { fn ne(&self, other: &DList<A>) -> bool {
!self.eq(other) !self.eq(other)
} }