Skip to content

Commit

Permalink
Simplified some of the logic blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
antonmarsden committed Aug 18, 2023
1 parent 1447cdf commit e160ab1
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 47 deletions.
41 changes: 16 additions & 25 deletions src/ops.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use core::ops::{Index, IndexMut};
use core::cmp::Ordering;
use core::ptr;
use core::mem;

use crate::iter::*;
use crate::view::*;
Expand Down Expand Up @@ -232,7 +232,7 @@ pub trait TooDeeOpsMut<T> : TooDeeOps<T> + IndexMut<usize,Output=[T]> + IndexMu
/// ```
fn swap(&mut self, mut cell1: Coordinate, mut cell2: Coordinate) {
if cell1.1 > cell2.1 {
core::mem::swap(&mut cell1, &mut cell2);
mem::swap(&mut cell1, &mut cell2);
}
let num_cols = self.num_cols();
assert!(cell1.0 < num_cols && cell2.0 < num_cols);
Expand Down Expand Up @@ -272,14 +272,11 @@ pub trait TooDeeOpsMut<T> : TooDeeOps<T> + IndexMut<usize,Output=[T]> + IndexMu
/// assert_eq!(toodee[(0, 2)], 1);
/// ```
fn swap_rows(&mut self, mut r1: usize, mut r2: usize) {
match r1.cmp(&r2) {
Ordering::Less => {},
Ordering::Greater => {
core::mem::swap(&mut r1, &mut r2);
},
Ordering::Equal => {
return;
}
if r1 == r2 {
return;
}
if r2 < r1 {
mem::swap(&mut r1, &mut r2);
}
let mut iter = self.rows_mut();
let tmp = iter.nth(r1).unwrap();
Expand All @@ -305,21 +302,15 @@ pub trait TooDeeOpsMut<T> : TooDeeOps<T> + IndexMut<usize,Output=[T]> + IndexMu
let num_rows = self.num_rows();
assert!(r1 < num_rows);
assert!(r2 < num_rows);
assert!(r1 != r2);
match r1.cmp(&r2) {
Ordering::Less => {
let mut iter = self.rows_mut();
let tmp = iter.nth(r1).unwrap();
(tmp, iter.nth(r2-r1-1).unwrap())
},
Ordering::Greater => {
let mut iter = self.rows_mut();
let tmp = iter.nth(r2).unwrap();
(iter.nth(r1-r2-1).unwrap(), tmp)
},
Ordering::Equal => {
unreachable!("r1 != r2");
},
assert_ne!(r1, r2);
if r1 < r2 {
let mut iter = self.rows_mut();
let tmp = iter.nth(r1).unwrap();
(tmp, iter.nth(r2-r1-1).unwrap())
} else {
let mut iter = self.rows_mut();
let tmp = iter.nth(r2).unwrap();
(iter.nth(r1-r2-1).unwrap(), tmp)
}
}

Expand Down
16 changes: 5 additions & 11 deletions src/toodee.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ use core::iter::IntoIterator;
use core::ptr::{self, NonNull};
use core::mem;
use core::slice;
use core::cmp::Ordering;

extern crate alloc;

Expand Down Expand Up @@ -320,16 +319,11 @@ impl<T> TooDeeOpsMut<T> for TooDee<T> {
/// assert_eq!(toodee[(0, 2)], 1);
/// ```
fn swap_rows(&mut self, mut r1: usize, mut r2: usize) {
match r1.cmp(&r2) {
Ordering::Less => {},
Ordering::Greater => {
// force r1 < r2
mem::swap(&mut r1, &mut r2);
},
Ordering::Equal => {
// swapping a row with itself
return;
}
if r1 == r2 {
return;
}
if r2 < r1 {
mem::swap(&mut r1, &mut r2);
}
assert!(r2 < self.num_rows);
let num_cols = self.num_cols;
Expand Down
17 changes: 6 additions & 11 deletions src/view.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use core::fmt;
use core::fmt::{Formatter, Debug};
use core::ops::{Index, IndexMut, Range};
use core::cmp::Ordering;
use core::ptr;
use core::mem;

use crate::toodee::*;
use crate::ops::*;
Expand Down Expand Up @@ -431,16 +431,11 @@ impl<'a, T> TooDeeOpsMut<T> for TooDeeViewMut<'a, T> {
/// assert_eq!(toodee[(0, 2)], 1);
/// ```
fn swap_rows(&mut self, mut r1: usize, mut r2: usize) {
match r1.cmp(&r2) {
Ordering::Less => {}
Ordering::Greater => {
// force r1 < r2
core::mem::swap(&mut r1, &mut r2);
}
Ordering::Equal => {
// swapping a row with itself
return;
}
if r1 == r2 {
return;
}
if r2 < r1 {
mem::swap(&mut r1, &mut r2);
}
assert!(r2 < self.num_rows);
let num_cols = self.num_cols;
Expand Down

0 comments on commit e160ab1

Please sign in to comment.