fixup! fix Mat impl Index<Range>
This commit is contained in:
parent
e6bf7ec12b
commit
13f5d0c2eb
|
@ -64,6 +64,14 @@ impl Mat {
|
|||
true
|
||||
}
|
||||
}
|
||||
|
||||
fn to_array_range(&self, r: core::ops::Range<i16>) -> core::ops::Range<usize> {
|
||||
let y1 = core::cmp::max(r.end, 0);
|
||||
let y1 = core::cmp::min(y1 as usize, self.0.len());
|
||||
let y0 = core::cmp::max(r.start, 0);
|
||||
let y0 = core::cmp::min(y0 as usize, y1);
|
||||
y0..y1
|
||||
}
|
||||
}
|
||||
|
||||
impl core::ops::Index<i16> for Mat {
|
||||
|
@ -77,12 +85,10 @@ impl core::ops::Index<i16> for Mat {
|
|||
}
|
||||
}
|
||||
|
||||
impl core::ops::Index<core::ops::RangeTo<i16>> for Mat {
|
||||
impl core::ops::Index<core::ops::Range<i16>> for Mat {
|
||||
type Output = [u16];
|
||||
fn index(&self, r: core::ops::RangeTo<i16>) -> &[u16] {
|
||||
let y = core::cmp::max(r.end, 0);
|
||||
let y = core::cmp::min(y as usize, self.0.len());
|
||||
&self.0[..y]
|
||||
fn index(&self, r: core::ops::Range<i16>) -> &[u16] {
|
||||
&self.0[self.to_array_range(r)]
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -310,6 +316,31 @@ mod test {
|
|||
assert_eq!(M2.cols(), 10);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_index() {
|
||||
assert_eq!(Mat::EMPTY[0], EMPTY_ROW);
|
||||
assert_eq!(Mat::EMPTY[1], EMPTY_ROW);
|
||||
assert_eq!(Mat::EMPTY[-1], FULL_ROW);
|
||||
//
|
||||
let m1_0 = 0b1011110011 | EMPTY_ROW;
|
||||
let m1_1 = 0b0100001000 | EMPTY_ROW;
|
||||
assert_eq!(M1[0], m1_0, "{:b}", M1[0]);
|
||||
assert_eq!(M1[1], m1_1, "{:b}", M1[1]);
|
||||
assert_eq!(M1[2], EMPTY_ROW);
|
||||
assert_eq!(M1[-1], FULL_ROW);
|
||||
assert_eq!(M1[0..2], [m1_0, m1_1]);
|
||||
//
|
||||
let m2_0 = 0b1111101111 | EMPTY_ROW;
|
||||
let m2_1 = 0b1100000001 | EMPTY_ROW;
|
||||
let m2_2 = 0b1000000000 | EMPTY_ROW;
|
||||
let m2_3 = 0b1000000000 | EMPTY_ROW;
|
||||
assert_eq!(M2[0..4], [m2_0, m2_1, m2_2, m2_3]);
|
||||
assert_eq!(M2[0..1], [m2_0]);
|
||||
assert_eq!(M2[2..4], [m2_2, m2_3]);
|
||||
assert_eq!(M2[2..5], M2[2..4]);
|
||||
assert_eq!(M2[-1..3], M2[0..3]);
|
||||
}
|
||||
|
||||
fn occ(m: &Mat, y: i16, xs: RangeInclusive<i16>) -> Vec<bool> {
|
||||
xs.map(|x| m.get(x, y)).collect()
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue