Skip to content

Commit 08d0b70

Browse files
author
blake2-ppc
committed
extra: Simplify the bitv iterators using Repeat
1 parent b5cd81d commit 08d0b70

File tree

1 file changed

+19
-44
lines changed

1 file changed

+19
-44
lines changed

src/libextra/bitv.rs

+19-44
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
use std::cmp;
1515
use std::iterator::RandomAccessIterator;
16-
use std::iterator::{Invert, Enumerate};
16+
use std::iterator::{Invert, Enumerate, Repeat, Map, Zip};
1717
use std::num;
1818
use std::ops;
1919
use std::uint;
@@ -864,13 +864,12 @@ impl BitvSet {
864864
/// w1, w2) where the bit location is the number of bits offset so far,
865865
/// and w1/w2 are the words coming from the two vectors self, other.
866866
fn common_iter<'a>(&'a self, other: &'a BitvSet)
867-
-> MapE<(uint,&uint),(uint,uint,uint), &'a ~[uint],Enumerate<vec::VecIterator<'a,uint>>> {
868-
let min = num::min(self.bitv.storage.len(),
869-
other.bitv.storage.len());
870-
MapE{iter: self.bitv.storage.slice(0, min).iter().enumerate(),
871-
env: &other.bitv.storage,
872-
f: |(i, &w): (uint, &uint), o_store| (i * uint::bits, w, o_store[i])
873-
}
867+
-> Map<'static, ((uint, &'a uint), &'a ~[uint]), (uint, uint, uint),
868+
Zip<Enumerate<vec::VecIterator<'a, uint>>, Repeat<&'a ~[uint]>>> {
869+
let min = num::min(self.bitv.storage.len(), other.bitv.storage.len());
870+
self.bitv.storage.slice(0, min).iter().enumerate()
871+
.zip(Repeat::new(&other.bitv.storage))
872+
.transform(|((i, &w), o_store)| (i * uint::bits, w, o_store[i]))
874873
}
875874

876875
/// Visits each word in self or other that extends beyond the other. This
@@ -881,45 +880,21 @@ impl BitvSet {
881880
/// is true if the word comes from 'self', and false if it comes from
882881
/// 'other'.
883882
fn outlier_iter<'a>(&'a self, other: &'a BitvSet)
884-
-> MapE<(uint, &uint),(bool, uint, uint), uint, Enumerate<vec::VecIterator<'a, uint>>> {
885-
let len1 = self.bitv.storage.len();
886-
let len2 = other.bitv.storage.len();
887-
let min = num::min(len1, len2);
888-
889-
if min < len1 {
890-
MapE{iter: self.bitv.storage.slice(min, len1).iter().enumerate(),
891-
env: min,
892-
f: |(i, &w): (uint, &uint), min| (true, (i + min) * uint::bits, w)
893-
}
883+
-> Map<'static, ((uint, &'a uint), uint), (bool, uint, uint),
884+
Zip<Enumerate<vec::VecIterator<'a, uint>>, Repeat<uint>>> {
885+
let slen = self.bitv.storage.len();
886+
let olen = other.bitv.storage.len();
887+
888+
if olen < slen {
889+
self.bitv.storage.slice_from(olen).iter().enumerate()
890+
.zip(Repeat::new(olen))
891+
.transform(|((i, &w), min)| (true, (i + min) * uint::bits, w))
894892
} else {
895-
MapE{iter: other.bitv.storage.slice(min, len2).iter().enumerate(),
896-
env: min,
897-
f: |(i, &w): (uint, &uint), min| (false, (i + min) * uint::bits, w)
898-
}
899-
}
900-
}
901-
}
902-
903-
/// Like iterator::Map with explicit env capture
904-
struct MapE<A, B, Env, I> {
905-
priv env: Env,
906-
priv f: &'static fn(A, Env) -> B,
907-
priv iter: I,
908-
}
909-
910-
impl<'self, A, B, Env: Clone, I: Iterator<A>> Iterator<B> for MapE<A, B, Env, I> {
911-
#[inline]
912-
fn next(&mut self) -> Option<B> {
913-
match self.iter.next() {
914-
Some(elt) => Some((self.f)(elt, self.env.clone())),
915-
None => None
893+
other.bitv.storage.slice_from(slen).iter().enumerate()
894+
.zip(Repeat::new(slen))
895+
.transform(|((i, &w), min)| (false, (i + min) * uint::bits, w))
916896
}
917897
}
918-
919-
#[inline]
920-
fn size_hint(&self) -> (uint, Option<uint>) {
921-
self.iter.size_hint()
922-
}
923898
}
924899

925900
pub struct BitvSetIterator<'self> {

0 commit comments

Comments
 (0)