Skip to content

Commit 8d40a43

Browse files
committed
make return an Option
1 parent 0576a40 commit 8d40a43

File tree

1 file changed

+35
-37
lines changed

1 file changed

+35
-37
lines changed

src/libcollections/bit.rs

+35-37
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ pub struct Bitv {
148148
impl Index<uint,bool> for Bitv {
149149
#[inline]
150150
fn index<'a>(&'a self, i: &uint) -> &'a bool {
151-
if self.get(*i) {
151+
if self.get(*i).expect("index out of bounds") {
152152
&TRUE
153153
} else {
154154
&FALSE
@@ -315,32 +315,30 @@ impl Bitv {
315315
bitv
316316
}
317317

318-
/// Retrieves the value at index `i`.
319-
///
320-
/// # Panics
321-
///
322-
/// Panics if `i` is out of bounds.
318+
/// Retrieves the value at index `i`, or `None` if the index is out of bounds.
323319
///
324320
/// # Example
325321
///
326322
/// ```
327323
/// use std::collections::Bitv;
328324
///
329325
/// let bv = Bitv::from_bytes(&[0b01100000]);
330-
/// assert_eq!(bv.get(0), false);
331-
/// assert_eq!(bv.get(1), true);
326+
/// assert_eq!(bv.get(0), Some(false));
327+
/// assert_eq!(bv.get(1), Some(true));
328+
/// assert_eq!(bv.get(100), None);
332329
///
333330
/// // Can also use array indexing
334331
/// assert_eq!(bv[1], true);
335332
/// ```
336333
#[inline]
337334
#[unstable = "panic semantics are likely to change in the future"]
338-
pub fn get(&self, i: uint) -> bool {
335+
pub fn get(&self, i: uint) -> Option<bool> {
339336
assert!(i < self.nbits);
340337
let w = i / u32::BITS;
341338
let b = i % u32::BITS;
342-
let x = self.storage[w] & (1 << b);
343-
x != 0
339+
self.storage.get(w).map(|block|
340+
(block & (1 << b)) != 0
341+
)
344342
}
345343

346344
/// Sets the value of a bit at an index `i`.
@@ -609,7 +607,7 @@ impl Bitv {
609607
if offset >= bitv.nbits {
610608
0
611609
} else {
612-
bitv.get(offset) as u8 << (7 - bit)
610+
bitv[offset] as u8 << (7 - bit)
613611
}
614612
}
615613

@@ -630,7 +628,7 @@ impl Bitv {
630628
/// Deprecated: Use `iter().collect()`.
631629
#[deprecated = "Use `iter().collect()`"]
632630
pub fn to_bools(&self) -> Vec<bool> {
633-
Vec::from_fn(self.nbits, |i| self.get(i))
631+
Vec::from_fn(self.nbits, |i| self[i])
634632
}
635633

636634
/// Compares a `Bitv` to a slice of `bool`s.
@@ -654,7 +652,7 @@ impl Bitv {
654652
assert_eq!(self.nbits, v.len());
655653
let mut i = 0;
656654
while i < self.nbits {
657-
if self.get(i) != v[i] { return false; }
655+
if self[i] != v[i] { return false; }
658656
i = i + 1;
659657
}
660658
true
@@ -837,7 +835,7 @@ impl Bitv {
837835
if self.is_empty() {
838836
None
839837
} else {
840-
let ret = self.get(self.nbits - 1);
838+
let ret = self[self.nbits - 1];
841839
// If we are unusing a whole word, make sure it is zeroed out
842840
self.nbits -= 1;
843841
if self.nbits % u32::BITS == 0 {
@@ -993,7 +991,7 @@ impl<'a> Iterator<bool> for Bits<'a> {
993991
if self.next_idx != self.end_idx {
994992
let idx = self.next_idx;
995993
self.next_idx += 1;
996-
Some(self.bitv.get(idx))
994+
Some(self.bitv[idx])
997995
} else {
998996
None
999997
}
@@ -1010,7 +1008,7 @@ impl<'a> DoubleEndedIterator<bool> for Bits<'a> {
10101008
fn next_back(&mut self) -> Option<bool> {
10111009
if self.next_idx != self.end_idx {
10121010
self.end_idx -= 1;
1013-
Some(self.bitv.get(self.end_idx))
1011+
Some(self.bitv[self.end_idx])
10141012
} else {
10151013
None
10161014
}
@@ -1030,7 +1028,7 @@ impl<'a> RandomAccessIterator<bool> for Bits<'a> {
10301028
if index >= self.indexable() {
10311029
None
10321030
} else {
1033-
Some(self.bitv.get(index))
1031+
Some(self.bitv[index])
10341032
}
10351033
}
10361034
}
@@ -1071,7 +1069,7 @@ impl<'a> RandomAccessIterator<bool> for Bits<'a> {
10711069
///
10721070
/// // Can convert back to a `Bitv`
10731071
/// let bv: Bitv = s.into_bitv();
1074-
/// assert!(bv.get(3));
1072+
/// assert!(bv[3]);
10751073
/// ```
10761074
#[deriving(Clone)]
10771075
pub struct BitvSet {
@@ -1260,8 +1258,8 @@ impl BitvSet {
12601258
/// s.insert(3);
12611259
///
12621260
/// let bv = s.into_bitv();
1263-
/// assert!(bv.get(0));
1264-
/// assert!(bv.get(3));
1261+
/// assert!(bv[0]);
1262+
/// assert!(bv[3]);
12651263
/// ```
12661264
#[inline]
12671265
pub fn into_bitv(self) -> Bitv {
@@ -1615,7 +1613,7 @@ impl BitvSet {
16151613
#[unstable = "matches collection reform specification, waiting for dust to settle"]
16161614
pub fn contains(&self, value: &uint) -> bool {
16171615
let bitv = &self.bitv;
1618-
*value < bitv.nbits && bitv.get(*value)
1616+
*value < bitv.nbits && bitv[*value]
16191617
}
16201618

16211619
/// Returns `true` if the set has no elements in common with `other`.
@@ -2173,9 +2171,9 @@ mod bitv_test {
21732171
b2.set(1, true);
21742172
b2.set(2, true);
21752173
assert!(b1.difference(&b2));
2176-
assert!(b1.get(0));
2177-
assert!(!b1.get(1));
2178-
assert!(!b1.get(2));
2174+
assert!(b1[0]);
2175+
assert!(!b1[1]);
2176+
assert!(!b1[2]);
21792177
}
21802178

21812179
#[test]
@@ -2187,9 +2185,9 @@ mod bitv_test {
21872185
b2.set(40, true);
21882186
b2.set(80, true);
21892187
assert!(b1.difference(&b2));
2190-
assert!(b1.get(0));
2191-
assert!(!b1.get(40));
2192-
assert!(!b1.get(80));
2188+
assert!(b1[0]);
2189+
assert!(!b1[40]);
2190+
assert!(!b1[80]);
21932191
}
21942192

21952193
#[test]
@@ -2287,16 +2285,16 @@ mod bitv_test {
22872285
fn test_bitv_push_pop() {
22882286
let mut s = Bitv::from_elem(5 * u32::BITS - 2, false);
22892287
assert_eq!(s.len(), 5 * u32::BITS - 2);
2290-
assert_eq!(s.get(5 * u32::BITS - 3), false);
2288+
assert_eq!(s[5 * u32::BITS - 3], false);
22912289
s.push(true);
22922290
s.push(true);
2293-
assert_eq!(s.get(5 * u32::BITS - 2), true);
2294-
assert_eq!(s.get(5 * u32::BITS - 1), true);
2291+
assert_eq!(s[5 * u32::BITS - 2], true);
2292+
assert_eq!(s[5 * u32::BITS - 1], true);
22952293
// Here the internal vector will need to be extended
22962294
s.push(false);
2297-
assert_eq!(s.get(5 * u32::BITS), false);
2295+
assert_eq!(s[5 * u32::BITS], false);
22982296
s.push(false);
2299-
assert_eq!(s.get(5 * u32::BITS + 1), false);
2297+
assert_eq!(s[5 * u32::BITS + 1], false);
23002298
assert_eq!(s.len(), 5 * u32::BITS + 2);
23012299
// Pop it all off
23022300
assert_eq!(s.pop(), Some(false));
@@ -2345,10 +2343,10 @@ mod bitv_test {
23452343
s.push(true);
23462344
s.push(false);
23472345
s.push(true);
2348-
assert_eq!(s.get(5 * u32::BITS - 1), true);
2349-
assert_eq!(s.get(5 * u32::BITS - 0), true);
2350-
assert_eq!(s.get(5 * u32::BITS + 1), false);
2351-
assert_eq!(s.get(5 * u32::BITS + 2), true);
2346+
assert_eq!(s[5 * u32::BITS - 1], true);
2347+
assert_eq!(s[5 * u32::BITS - 0], true);
2348+
assert_eq!(s[5 * u32::BITS + 1], false);
2349+
assert_eq!(s[5 * u32::BITS + 2], true);
23522350
}
23532351

23542352
#[test]

0 commit comments

Comments
 (0)