Skip to content

Commit 4f33302

Browse files
committed
auto merge of #7790 : blake2-ppc/rust/dlist-ringbuf-small-changes, r=thestinger
Implement size_hint for the ringbuf iterators. Do small cleanups in dlist, use Option's .map and .map_mut properly, and put inline on all the small methods.
2 parents 0cb1ac0 + 961184f commit 4f33302

File tree

2 files changed

+44
-15
lines changed

2 files changed

+44
-15
lines changed

src/libextra/dlist.rs

+29-15
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,39 +116,35 @@ 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> {
125-
self.list_head.chain_ref(|x| Some(&x.value))
129+
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> {
130-
match self.list_head {
131-
None => None,
132-
Some(ref mut head) => Some(&mut head.value),
133-
}
135+
self.list_head.map_mut(|head| &mut head.value)
134136
}
135137

136138
/// Provide a reference to the back element, or None if the list is empty
139+
#[inline]
137140
fn back<'a>(&'a self) -> Option<&'a T> {
138-
match self.list_tail.resolve_immut() {
139-
None => None,
140-
Some(tail) => Some(&tail.value),
141-
}
141+
self.list_tail.resolve_immut().map(|tail| &tail.value)
142142
}
143143

144144
/// Provide a mutable reference to the back element, or None if the list is empty
145+
#[inline]
145146
fn back_mut<'a>(&'a mut self) -> Option<&'a mut T> {
146-
match self.list_tail.resolve() {
147-
None => None,
148-
Some(tail) => Some(&mut tail.value),
149-
}
147+
self.list_tail.resolve().map_mut(|tail| &mut tail.value)
150148
}
151149

152150
/// Add an element last in the list
@@ -167,7 +165,6 @@ impl<T> Deque<T> for DList<T> {
167165
/// Remove the last element and return it, or None if the list is empty
168166
///
169167
/// O(1)
170-
#[inline]
171168
fn pop_back(&mut self) -> Option<T> {
172169
match self.list_tail.resolve() {
173170
None => None,
@@ -259,6 +256,7 @@ impl<T> DList<T> {
259256
/// Add all elements from `other` to the beginning of the list
260257
///
261258
/// O(1)
259+
#[inline]
262260
pub fn prepend(&mut self, mut other: DList<T>) {
263261
util::swap(self, &mut other);
264262
self.append(other);
@@ -268,7 +266,6 @@ impl<T> DList<T> {
268266
/// or at the end.
269267
///
270268
/// O(N)
271-
#[inline]
272269
pub fn insert_when(&mut self, elt: T, f: &fn(&T, &T) -> bool) {
273270
{
274271
let mut it = self.mut_iter();
@@ -309,16 +306,19 @@ impl<T> DList<T> {
309306

310307

311308
/// Provide a forward iterator
309+
#[inline]
312310
pub fn iter<'a>(&'a self) -> DListIterator<'a, T> {
313311
DListIterator{nelem: self.len(), head: &self.list_head, tail: self.list_tail}
314312
}
315313

316314
/// Provide a reverse iterator
315+
#[inline]
317316
pub fn rev_iter<'a>(&'a self) -> InvertIterator<&'a T, DListIterator<'a, T>> {
318317
self.iter().invert()
319318
}
320319

321320
/// Provide a forward iterator with mutable references
321+
#[inline]
322322
pub fn mut_iter<'a>(&'a mut self) -> MutDListIterator<'a, T> {
323323
let head_raw = match self.list_head {
324324
Some(ref mut h) => Rawlink::some(*h),
@@ -332,18 +332,21 @@ impl<T> DList<T> {
332332
}
333333
}
334334
/// Provide a reverse iterator with mutable references
335+
#[inline]
335336
pub fn mut_rev_iter<'a>(&'a mut self) -> InvertIterator<&'a mut T,
336337
MutDListIterator<'a, T>> {
337338
self.mut_iter().invert()
338339
}
339340

340341

341342
/// Consume the list into an iterator yielding elements by value
343+
#[inline]
342344
pub fn consume_iter(self) -> ConsumeIterator<T> {
343345
ConsumeIterator{list: self}
344346
}
345347

346348
/// Consume the list into an iterator yielding elements by value, in reverse
349+
#[inline]
347350
pub fn consume_rev_iter(self) -> InvertIterator<T, ConsumeIterator<T>> {
348351
self.consume_iter().invert()
349352
}
@@ -353,6 +356,7 @@ impl<T: cmp::TotalOrd> DList<T> {
353356
/// Insert `elt` sorted in ascending order
354357
///
355358
/// O(N)
359+
#[inline]
356360
pub fn insert_ordered(&mut self, elt: T) {
357361
self.insert_when(elt, |a, b| a.cmp(b) != cmp::Less);
358362
}
@@ -374,12 +378,14 @@ impl<'self, A> Iterator<&'self A> for DListIterator<'self, A> {
374378
}
375379
}
376380

381+
#[inline]
377382
fn size_hint(&self) -> (uint, Option<uint>) {
378383
(self.nelem, Some(self.nelem))
379384
}
380385
}
381386

382387
impl<'self, A> DoubleEndedIterator<&'self A> for DListIterator<'self, A> {
388+
#[inline]
383389
fn next_back(&mut self) -> Option<&'self A> {
384390
if self.nelem == 0 {
385391
return None;
@@ -414,6 +420,7 @@ impl<'self, A> Iterator<&'self mut A> for MutDListIterator<'self, A> {
414420
}
415421
}
416422

423+
#[inline]
417424
fn size_hint(&self) -> (uint, Option<uint>) {
418425
(self.nelem, Some(self.nelem))
419426
}
@@ -466,6 +473,7 @@ impl<'self, A> ListInsertion<A> for MutDListIterator<'self, A> {
466473
}
467474
}
468475

476+
#[inline]
469477
fn peek_next<'a>(&'a mut self) -> Option<&'a mut A> {
470478
match self.head.resolve() {
471479
None => None,
@@ -475,13 +483,17 @@ impl<'self, A> ListInsertion<A> for MutDListIterator<'self, A> {
475483
}
476484

477485
impl<A> Iterator<A> for ConsumeIterator<A> {
486+
#[inline]
478487
fn next(&mut self) -> Option<A> { self.list.pop_front() }
488+
489+
#[inline]
479490
fn size_hint(&self) -> (uint, Option<uint>) {
480491
(self.list.length, Some(self.list.length))
481492
}
482493
}
483494

484495
impl<A> DoubleEndedIterator<A> for ConsumeIterator<A> {
496+
#[inline]
485497
fn next_back(&mut self) -> Option<A> { self.list.pop_back() }
486498
}
487499

@@ -498,6 +510,8 @@ impl<A: Eq> Eq for DList<A> {
498510
self.len() == other.len() &&
499511
self.iter().zip(other.iter()).all(|(a, b)| a.eq(b))
500512
}
513+
514+
#[inline]
501515
fn ne(&self, other: &DList<A>) -> bool {
502516
!self.eq(other)
503517
}

src/libextra/ringbuf.rs

+15
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,11 @@ macro_rules! iterator {
214214
self.nelts -= 1;
215215
Some(self.elts[raw_index]. $getter ())
216216
}
217+
218+
#[inline]
219+
fn size_hint(&self) -> (uint, Option<uint>) {
220+
(self.nelts, Some(self.nelts))
221+
}
217222
}
218223
}
219224
}
@@ -578,6 +583,7 @@ mod tests {
578583
fn test_iter() {
579584
let mut d = RingBuf::new();
580585
assert_eq!(d.iter().next(), None);
586+
assert_eq!(d.iter().size_hint(), (0, Some(0)));
581587

582588
for int::range(0,5) |i| {
583589
d.push_back(i);
@@ -588,6 +594,15 @@ mod tests {
588594
d.push_front(i);
589595
}
590596
assert_eq!(d.iter().collect::<~[&int]>(), ~[&8,&7,&6,&0,&1,&2,&3,&4]);
597+
598+
let mut it = d.iter();
599+
let mut len = d.len();
600+
loop {
601+
match it.next() {
602+
None => break,
603+
_ => { len -= 1; assert_eq!(it.size_hint(), (len, Some(len))) }
604+
}
605+
}
591606
}
592607

593608
#[test]

0 commit comments

Comments
 (0)