Skip to content

Commit 961184f

Browse files
author
blake2-ppc
committed
dlist: Use inline on very small functions and iterator functions
1 parent 7681cf6 commit 961184f

File tree

1 file changed

+25
-2
lines changed

1 file changed

+25
-2
lines changed

src/libextra/dlist.rs

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,10 +101,12 @@ fn link_with_prev<T>(mut next: ~Node<T>, prev: Rawlink<Node<T>>) -> Link<T> {
101101

102102
impl<T> Container for DList<T> {
103103
/// O(1)
104+
#[inline]
104105
fn is_empty(&self) -> bool {
105106
self.list_head.is_none()
106107
}
107108
/// O(1)
109+
#[inline]
108110
fn len(&self) -> uint {
109111
self.length
110112
}
@@ -114,28 +116,33 @@ impl<T> Mutable for DList<T> {
114116
/// Remove all elements from the DList
115117
///
116118
/// O(N)
119+
#[inline]
117120
fn clear(&mut self) {
118121
*self = DList::new()
119122
}
120123
}
121124

122125
impl<T> Deque<T> for DList<T> {
123126
/// Provide a reference to the front element, or None if the list is empty
127+
#[inline]
124128
fn front<'a>(&'a self) -> Option<&'a T> {
125129
self.list_head.map(|head| &head.value)
126130
}
127131

128132
/// Provide a mutable reference to the front element, or None if the list is empty
133+
#[inline]
129134
fn front_mut<'a>(&'a mut self) -> Option<&'a mut T> {
130135
self.list_head.map_mut(|head| &mut head.value)
131136
}
132137

133138
/// Provide a reference to the back element, or None if the list is empty
139+
#[inline]
134140
fn back<'a>(&'a self) -> Option<&'a T> {
135141
self.list_tail.resolve_immut().map(|tail| &tail.value)
136142
}
137143

138144
/// Provide a mutable reference to the back element, or None if the list is empty
145+
#[inline]
139146
fn back_mut<'a>(&'a mut self) -> Option<&'a mut T> {
140147
self.list_tail.resolve().map_mut(|tail| &mut tail.value)
141148
}
@@ -158,7 +165,6 @@ impl<T> Deque<T> for DList<T> {
158165
/// Remove the last element and return it, or None if the list is empty
159166
///
160167
/// O(1)
161-
#[inline]
162168
fn pop_back(&mut self) -> Option<T> {
163169
match self.list_tail.resolve() {
164170
None => None,
@@ -250,6 +256,7 @@ impl<T> DList<T> {
250256
/// Add all elements from `other` to the beginning of the list
251257
///
252258
/// O(1)
259+
#[inline]
253260
pub fn prepend(&mut self, mut other: DList<T>) {
254261
util::swap(self, &mut other);
255262
self.append(other);
@@ -259,7 +266,6 @@ impl<T> DList<T> {
259266
/// or at the end.
260267
///
261268
/// O(N)
262-
#[inline]
263269
pub fn insert_when(&mut self, elt: T, f: &fn(&T, &T) -> bool) {
264270
{
265271
let mut it = self.mut_iter();
@@ -300,16 +306,19 @@ impl<T> DList<T> {
300306

301307

302308
/// Provide a forward iterator
309+
#[inline]
303310
pub fn iter<'a>(&'a self) -> DListIterator<'a, T> {
304311
DListIterator{nelem: self.len(), head: &self.list_head, tail: self.list_tail}
305312
}
306313

307314
/// Provide a reverse iterator
315+
#[inline]
308316
pub fn rev_iter<'a>(&'a self) -> InvertIterator<&'a T, DListIterator<'a, T>> {
309317
self.iter().invert()
310318
}
311319

312320
/// Provide a forward iterator with mutable references
321+
#[inline]
313322
pub fn mut_iter<'a>(&'a mut self) -> MutDListIterator<'a, T> {
314323
let head_raw = match self.list_head {
315324
Some(ref mut h) => Rawlink::some(*h),
@@ -323,18 +332,21 @@ impl<T> DList<T> {
323332
}
324333
}
325334
/// Provide a reverse iterator with mutable references
335+
#[inline]
326336
pub fn mut_rev_iter<'a>(&'a mut self) -> InvertIterator<&'a mut T,
327337
MutDListIterator<'a, T>> {
328338
self.mut_iter().invert()
329339
}
330340

331341

332342
/// Consume the list into an iterator yielding elements by value
343+
#[inline]
333344
pub fn consume_iter(self) -> ConsumeIterator<T> {
334345
ConsumeIterator{list: self}
335346
}
336347

337348
/// Consume the list into an iterator yielding elements by value, in reverse
349+
#[inline]
338350
pub fn consume_rev_iter(self) -> InvertIterator<T, ConsumeIterator<T>> {
339351
self.consume_iter().invert()
340352
}
@@ -344,6 +356,7 @@ impl<T: cmp::TotalOrd> DList<T> {
344356
/// Insert `elt` sorted in ascending order
345357
///
346358
/// O(N)
359+
#[inline]
347360
pub fn insert_ordered(&mut self, elt: T) {
348361
self.insert_when(elt, |a, b| a.cmp(b) != cmp::Less);
349362
}
@@ -365,12 +378,14 @@ impl<'self, A> Iterator<&'self A> for DListIterator<'self, A> {
365378
}
366379
}
367380

381+
#[inline]
368382
fn size_hint(&self) -> (uint, Option<uint>) {
369383
(self.nelem, Some(self.nelem))
370384
}
371385
}
372386

373387
impl<'self, A> DoubleEndedIterator<&'self A> for DListIterator<'self, A> {
388+
#[inline]
374389
fn next_back(&mut self) -> Option<&'self A> {
375390
if self.nelem == 0 {
376391
return None;
@@ -405,6 +420,7 @@ impl<'self, A> Iterator<&'self mut A> for MutDListIterator<'self, A> {
405420
}
406421
}
407422

423+
#[inline]
408424
fn size_hint(&self) -> (uint, Option<uint>) {
409425
(self.nelem, Some(self.nelem))
410426
}
@@ -457,6 +473,7 @@ impl<'self, A> ListInsertion<A> for MutDListIterator<'self, A> {
457473
}
458474
}
459475

476+
#[inline]
460477
fn peek_next<'a>(&'a mut self) -> Option<&'a mut A> {
461478
match self.head.resolve() {
462479
None => None,
@@ -466,13 +483,17 @@ impl<'self, A> ListInsertion<A> for MutDListIterator<'self, A> {
466483
}
467484

468485
impl<A> Iterator<A> for ConsumeIterator<A> {
486+
#[inline]
469487
fn next(&mut self) -> Option<A> { self.list.pop_front() }
488+
489+
#[inline]
470490
fn size_hint(&self) -> (uint, Option<uint>) {
471491
(self.list.length, Some(self.list.length))
472492
}
473493
}
474494

475495
impl<A> DoubleEndedIterator<A> for ConsumeIterator<A> {
496+
#[inline]
476497
fn next_back(&mut self) -> Option<A> { self.list.pop_back() }
477498
}
478499

@@ -489,6 +510,8 @@ impl<A: Eq> Eq for DList<A> {
489510
self.len() == other.len() &&
490511
self.iter().zip(other.iter()).all(|(a, b)| a.eq(b))
491512
}
513+
514+
#[inline]
492515
fn ne(&self, other: &DList<A>) -> bool {
493516
!self.eq(other)
494517
}

0 commit comments

Comments
 (0)