Skip to content

Commit 40c4516

Browse files
committed
Refactor BitV internals to not use macro, reduce duplication
1 parent 8c9bdda commit 40c4516

File tree

1 file changed

+22
-25
lines changed

1 file changed

+22
-25
lines changed

src/libcollections/bitv.rs

Lines changed: 22 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ use core::prelude::*;
6666
use core::cmp;
6767
use core::default::Default;
6868
use core::fmt;
69-
use core::iter::Take;
69+
use core::iter::{Chain, Enumerate, Repeat, Skip, Take};
7070
use core::iter;
7171
use core::ops::Index;
7272
use core::slice;
@@ -76,25 +76,22 @@ use std::hash;
7676
use {Collection, Mutable, Set, MutableSet, MutableSeq};
7777
use vec::Vec;
7878

79+
type MatchWords<'a> = Chain<MaskWords<'a>, Skip<Take<Enumerate<Repeat<uint>>>>>;
7980
// Take two BitV's, and return iterators of their words, where the shorter one
8081
// has been padded with 0's
81-
macro_rules! match_words(
82-
($a_expr:expr, $b_expr:expr) => ({
83-
let a = $a_expr;
84-
let b = $b_expr;
85-
let a_len = a.storage.len();
86-
let b_len = b.storage.len();
87-
88-
// have to uselessly pretend to pad the longer one for type matching
89-
if a_len < b_len {
90-
(a.mask_words(0).chain(iter::Repeat::new(0u).enumerate().take(b_len).skip(a_len)),
91-
b.mask_words(0).chain(iter::Repeat::new(0u).enumerate().take(0).skip(0)))
92-
} else {
93-
(a.mask_words(0).chain(iter::Repeat::new(0u).enumerate().take(0).skip(0)),
94-
b.mask_words(0).chain(iter::Repeat::new(0u).enumerate().take(a_len).skip(b_len)))
95-
}
96-
})
97-
)
82+
fn match_words <'a,'b>(a: &'a Bitv, b: &'b Bitv) -> (MatchWords<'a>, MatchWords<'b>) {
83+
let a_len = a.storage.len();
84+
let b_len = b.storage.len();
85+
86+
// have to uselessly pretend to pad the longer one for type matching
87+
if a_len < b_len {
88+
(a.mask_words(0).chain(Repeat::new(0u).enumerate().take(b_len).skip(a_len)),
89+
b.mask_words(0).chain(Repeat::new(0u).enumerate().take(0).skip(0)))
90+
} else {
91+
(a.mask_words(0).chain(Repeat::new(0u).enumerate().take(0).skip(0)),
92+
b.mask_words(0).chain(Repeat::new(0u).enumerate().take(a_len).skip(b_len)))
93+
}
94+
}
9895

9996
static TRUE: bool = true;
10097
static FALSE: bool = false;
@@ -1015,23 +1012,23 @@ impl Extendable<bool> for BitvSet {
10151012
impl PartialOrd for BitvSet {
10161013
#[inline]
10171014
fn partial_cmp(&self, other: &BitvSet) -> Option<Ordering> {
1018-
let (a_iter, b_iter) = match_words!(self.get_ref(), other.get_ref());
1015+
let (a_iter, b_iter) = match_words(self.get_ref(), other.get_ref());
10191016
iter::order::partial_cmp(a_iter, b_iter)
10201017
}
10211018
}
10221019

10231020
impl Ord for BitvSet {
10241021
#[inline]
10251022
fn cmp(&self, other: &BitvSet) -> Ordering {
1026-
let (a_iter, b_iter) = match_words!(self.get_ref(), other.get_ref());
1023+
let (a_iter, b_iter) = match_words(self.get_ref(), other.get_ref());
10271024
iter::order::cmp(a_iter, b_iter)
10281025
}
10291026
}
10301027

10311028
impl cmp::PartialEq for BitvSet {
10321029
#[inline]
10331030
fn eq(&self, other: &BitvSet) -> bool {
1034-
let (a_iter, b_iter) = match_words!(self.get_ref(), other.get_ref());
1031+
let (a_iter, b_iter) = match_words(self.get_ref(), other.get_ref());
10351032
iter::order::eq(a_iter, b_iter)
10361033
}
10371034
}
@@ -1192,10 +1189,10 @@ impl BitvSet {
11921189
self_bitv.reserve(other_bitv.capacity());
11931190

11941191
// virtually pad other with 0's for equal lengths
1195-
let self_len = self_bitv.storage.len();
1196-
let other_len = other_bitv.storage.len();
1197-
let mut other_words = other_bitv.mask_words(0)
1198-
.chain(iter::Repeat::new(0u).enumerate().take(self_len).skip(other_len));
1192+
let mut other_words = {
1193+
let (_, result) = match_words(self_bitv, other_bitv);
1194+
result
1195+
};
11991196

12001197
// Apply values found in other
12011198
for (i, w) in other_words {

0 commit comments

Comments
 (0)