transfer_region refactor 1

Cleaning up difficult to reason about code
This commit is contained in:
Emilia Allison 2025-01-18 12:47:32 -05:00
parent 5e70a17a00
commit 5acae8ffde
Signed by: emilia
GPG Key ID: 05D5D1107E5100A1
1 changed files with 51 additions and 44 deletions

View File

@ -312,31 +312,16 @@ impl TransferRegion {
.div_euclid(il_source.1.unsigned_abs()),
);
let count = (
// How many times can we replicate?
if il_dest.0.unsigned_abs() == 0 {
1
} else {
(1..)
.position(|n| {
(n * number_used_src_wells.0 * il_dest.0.unsigned_abs())
.saturating_sub(il_dest.0.unsigned_abs())
+ 1
> d_dims.0
})
.unwrap() as u8
},
if il_dest.1.unsigned_abs() == 0 {
1
} else {
(1..)
.position(|n| {
(n * number_used_src_wells.1 * il_dest.1.unsigned_abs())
.saturating_sub(il_dest.1.unsigned_abs())
+ 1
> d_dims.1
})
.unwrap() as u8
},
Self::calculate_replication_count(
number_used_src_wells.0,
il_dest.0,
d_dims.0,
),
Self::calculate_replication_count(
number_used_src_wells.1,
il_dest.1,
d_dims.1,
),
);
let i = i
.saturating_sub(s_ul.row)
@ -354,27 +339,25 @@ impl TransferRegion {
Some(
possible_destination_wells
.into_iter()
.filter(|Well { row: x, .. }| {
x.checked_sub(d_ul.row).unwrap()
% row_modulus // Counter along x
== (il_dest.0.unsigned_abs() *i)
% row_modulus
})
.filter(|Well { col: y, .. }| {
y.checked_sub(d_ul.col).unwrap()
% column_modulus // Counter along u
== (il_dest.1.unsigned_abs() *j)
% column_modulus
})
.filter(|Well { row: x, col: y }| {
// How many times have we replicated? < How many are we allowed
// to replicate?
x.checked_sub(d_ul.row).unwrap().div_euclid(row_modulus)
// Compute row and column offsets once
let row_offset = x.checked_sub(d_ul.row).unwrap();
let col_offset = y.checked_sub(d_ul.col).unwrap();
// Row position check
let row_matches = row_offset % row_modulus
== (il_dest.0.unsigned_abs() * i) % row_modulus;
// Column position check
let col_matches = col_offset % column_modulus
== (il_dest.1.unsigned_abs() * j) % column_modulus;
// Replication limits check
let within_limits = row_offset.div_euclid(row_modulus)
< count.0
&& y.checked_sub(d_ul.col)
.unwrap()
.div_euclid(column_modulus)
< count.1
&& col_offset.div_euclid(column_modulus) < count.1;
row_matches && col_matches && within_limits
})
.collect(),
)
@ -429,6 +412,30 @@ impl TransferRegion {
}
}
fn calculate_replication_count(
number_of_source_wells: u8,
dest_interleave: i8,
dest_dimension: u8,
) -> u8 {
if dest_interleave == 0 {
return 1;
}
// Find the first position where the wells won't fit
// This tells us how many times we can replicate
(1..)
.position(|n| {
let wells_needed = {
// Calculate total wells needed for n replications
let total_wells = n * number_of_source_wells * dest_interleave.unsigned_abs();
// Adjust for interleaving offset and convert to actual position
total_wells.saturating_sub(dest_interleave.unsigned_abs()) + 1
};
wells_needed > dest_dimension
})
.unwrap() as u8
}
pub fn validate(&self) -> Result<(), &'static str> {
// Checks if the region does anything suspect
//