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()), .div_euclid(il_source.1.unsigned_abs()),
); );
let count = ( let count = (
// How many times can we replicate? Self::calculate_replication_count(
if il_dest.0.unsigned_abs() == 0 { number_used_src_wells.0,
1 il_dest.0,
} else { d_dims.0,
(1..) ),
.position(|n| { Self::calculate_replication_count(
(n * number_used_src_wells.0 * il_dest.0.unsigned_abs()) number_used_src_wells.1,
.saturating_sub(il_dest.0.unsigned_abs()) il_dest.1,
+ 1 d_dims.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
},
); );
let i = i let i = i
.saturating_sub(s_ul.row) .saturating_sub(s_ul.row)
@ -354,27 +339,25 @@ impl TransferRegion {
Some( Some(
possible_destination_wells possible_destination_wells
.into_iter() .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 }| { .filter(|Well { row: x, col: y }| {
// How many times have we replicated? < How many are we allowed // Compute row and column offsets once
// to replicate? let row_offset = x.checked_sub(d_ul.row).unwrap();
x.checked_sub(d_ul.row).unwrap().div_euclid(row_modulus) 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 < count.0
&& y.checked_sub(d_ul.col) && col_offset.div_euclid(column_modulus) < count.1;
.unwrap()
.div_euclid(column_modulus) row_matches && col_matches && within_limits
< count.1
}) })
.collect(), .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> { pub fn validate(&self) -> Result<(), &'static str> {
// Checks if the region does anything suspect // Checks if the region does anything suspect
// //