aboutsummaryrefslogtreecommitdiff
path: root/src/utils.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/utils.rs')
-rw-r--r--src/utils.rs14
1 files changed, 14 insertions, 0 deletions
diff --git a/src/utils.rs b/src/utils.rs
index 0d141d1..1be3c39 100644
--- a/src/utils.rs
+++ b/src/utils.rs
@@ -670,3 +670,17 @@ pub(crate) fn select_other<T: PartialEq>(pair: (T, T), elt: T) -> T {
pair.0
}
}
+
+/// Gets mutable references to two distinct elements from a slice.
+#[inline(always)]
+pub fn get2_mut<T>(vec: &mut [T], i1: usize, i2: usize) -> (&mut T, &mut T) {
+ assert_ne!(i1, i2, "Cannot mutably index the same index twice.");
+
+ if i1 < i2 {
+ let (xs, ys) = vec.split_at_mut(i2);
+ (&mut xs[i1], &mut ys[0])
+ } else {
+ let (xs, ys) = vec.split_at_mut(i1);
+ (&mut ys[0], &mut xs[i2])
+ }
+}