Skip to content

Commit b46e925

Browse files
Copilotkalwalt
andauthored
refactor(core): extract Scalar::channel_or_default, rename verbose test
Agent-Logs-Url: https://github.com/webarkit/purecv/sessions/40d61898-25f4-4bc4-989c-0d33763dac83 Co-authored-by: kalwalt <1275858+kalwalt@users.noreply.github.com>
1 parent e6f6367 commit b46e925

1 file changed

Lines changed: 18 additions & 3 deletions

File tree

src/core/types.rs

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,21 @@ impl<T: Copy + Default> From<T> for Scalar<T> {
256256
}
257257
}
258258

259+
impl<T: Copy + Default> Scalar<T> {
260+
/// Returns channel `i` when `i < 4`, otherwise `T::default()`.
261+
///
262+
/// Used by `VecN + Scalar` / `VecN - Scalar` to broadcast scalar channels
263+
/// onto vectors of arbitrary length without bounds-checking the caller side.
264+
#[inline]
265+
pub fn channel_or_default(&self, i: usize) -> T {
266+
if i < 4 {
267+
self.v[i]
268+
} else {
269+
T::default()
270+
}
271+
}
272+
}
273+
259274
/// Per-channel addition: `result[c] = self[c] + rhs[c]`.
260275
impl<T: Copy + Default + Add<Output = T>> Add for Scalar<T> {
261276
type Output = Self;
@@ -674,7 +689,7 @@ impl<T: Copy + Default + Add<Output = T>, const N: usize> Add<Scalar<T>> for Vec
674689
type Output = Self;
675690
fn add(self, rhs: Scalar<T>) -> Self {
676691
Self {
677-
val: std::array::from_fn(|i| self.val[i] + if i < 4 { rhs.v[i] } else { T::default() }),
692+
val: std::array::from_fn(|i| self.val[i] + rhs.channel_or_default(i)),
678693
}
679694
}
680695
}
@@ -685,7 +700,7 @@ impl<T: Copy + Default + Sub<Output = T>, const N: usize> Sub<Scalar<T>> for Vec
685700
type Output = Self;
686701
fn sub(self, rhs: Scalar<T>) -> Self {
687702
Self {
688-
val: std::array::from_fn(|i| self.val[i] - if i < 4 { rhs.v[i] } else { T::default() }),
703+
val: std::array::from_fn(|i| self.val[i] - rhs.channel_or_default(i)),
689704
}
690705
}
691706
}
@@ -900,7 +915,7 @@ mod vecn_tests {
900915
}
901916

902917
#[test]
903-
fn test_add_scalar_6_extra_channels_zero() {
918+
fn test_add_scalar_vec6_zero_pads_extra_channels() {
904919
// Channels 4 and 5 of the Vec get scalar's default (0) added.
905920
let v = Vec6f::new(1.0_f32, 2.0, 3.0, 4.0, 5.0, 6.0);
906921
let s = Scalar::new(10.0_f32, 10.0, 10.0, 10.0);

0 commit comments

Comments
 (0)