Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor functions to Traits for all types #28

Merged
merged 3 commits into from
Feb 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 21 additions & 23 deletions csx3/src/merge_lazy.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,41 +6,39 @@ Need to perform "shallow" or lazy merge, that is,
## Lazy merge operation
* Swapping of references instead of the actual data (light operation)
* Ordering logic per iteration
```rust
use csx3::utils::VirtualSlice;
```rust,noplayground
use csx3::merge::Merge;

let (s1, s2) = (&mut [5,6,7], &mut[1,2,3,4]);
let mut vs = VirtualSlice::new();

vs.attach(s1); // attach to s1
vs.merge_shallow(s2); // attach to s2 and do shallow merge with s1
let mut vs = s1.merge_shallow(s2); // attach to s2 and do shallow merge with s1

vs.iter() // ordered access of attached slices
.enumerate() // [&1, &2, &3, &4, &5, &6, &7]
vs.iter() // ordered access of attached slices
.enumerate() // [&1, &2, &3, &4, &5, &6, &7]
.for_each(|(i,x)|
assert_eq(*x,i+1)
);

assert_eq!(s1, &[5,6,7]); // while s1 & s2 are unaffected
assert_eq!(s1, &[5,6,7]); // while s1 & s2 are unaffected
assert_eq!(s2, &[1,2,3,4]);
```
## Deferred Mutability; Superimpose order
* Straight swapping of data referenced (could end up a heavy heap operation)
* No ordering logic per iteration
```rust
use csx3::utils::VirtualSlice;
```rust,noplayground
use csx3::merge::Merge;

let (s1, s2) = (&mut [5,6,7], &mut[1,2,3,4]);
let mut vs = VirtualSlice::new();

vs.attach(s1); // attach to s1
vs.merge_shallow(s2); // attach to s2 and do shallow merge with s1
// vs == &[&1,&2,&3,&4,&5,&6,&7]
// s1 == &[5,6,7]
// s2 == &[1,2,3,4]
vs.impose_shallow_merge(); // superimpose order mask

assert_eq!(s1, &[1,2,3]);
assert_eq!(s2, &[4,5,6,7]);
let s1 = &mut [5,6,7];
let s2 = &mut [1,2,3,4];

let mut mask = s1.merge_virtual(s2); // mask mutably borrows s1 & s2

mask.iter() // iterate over merged contents
.enumerate() // while s1 and s2 are unaffected
.for_each(|(i,x)| assert_eq!(*x,i+1) );

mask.superimpose_shallow_merge(); // mutate the order back to s1 and s2
// and drop mutable references
assert_eq!(s1, &[1,2,3]);
assert_eq!(s2, &[4,5,6,7]);
```
35 changes: 18 additions & 17 deletions src/bin/main.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
use csx3::sort::{merge::*, quick::quick_sort, count::CountSort};
use csx3::trees::*;
use csx3::linkedlists::*;
use csx3::select::*;
use std::env::args;
use std::str::FromStr;
use csx3::random_sequence;
use csx3::{
sort::{
merge::*,
quick::QuickSort,
count::CountSort
},
merge::Merge,
select::Select,
trees::*,
linkedlists::*,
random_sequence
};

type MyType = i8;

Expand All @@ -30,19 +37,13 @@ fn main() {

println!("Random Selection");
let mut arr: Vec<MyType> = list.iter().copied().collect();
println!("1st order stat= {:?}", r_selection(&mut arr, 1));
println!("2nd order stat= {:?}", r_selection(&mut arr, 2));
println!("3rd order stat= {:?}", r_selection(&mut arr, 3));
println!("1st order stat= {:?}", arr.r_selection(1));
println!("2nd order stat= {:?}", arr.r_selection(2));
println!("3rd order stat= {:?}", arr.r_selection(3));

println!("Deterministic Selection");
let mut arr: Vec<MyType> = list.iter().copied().collect();
println!("1st order stat= {:?}", d_selection(&mut arr, 1));
println!("2nd order stat= {:?}", d_selection(&mut arr, 2));
println!("3rd order stat= {:?}", d_selection(&mut arr, 3));

println!("MergeSort Immut: {:?}", mergesort(&v));
println!("MergeSort Mut : ({}, {:?})", mergesort_mut(&mut v, merge_mut), v);
quick_sort(&mut v);
println!("MergeSort Immut: {:?}", v.mergesort());
println!("MergeSort Mut : ({}, {:?})", v.mergesort_mut(Merge::merge_mut), v);
v.quick_sort();
println!("Quick Sort : {:?}", v);

let mut arr: Vec<MyType> = list.iter().copied().collect();
Expand Down
7 changes: 5 additions & 2 deletions src/linkedlists/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
use std::fmt::Debug;
use crate::sort::merge::{merge_mut_adjacent, mergesort_mut};
use crate::{
sort::merge::MergeSort,
merge::Merge
};

/// I could have done it differently
/// but I wanted to see how far I could go with this simple enum structure
Expand Down Expand Up @@ -108,7 +111,7 @@ impl<T> List<T>
}
pub fn sort_with_count(&self) -> (usize, Vec<T>){
let mut s = self.iter().copied().collect::<Vec<T>>();
(mergesort_mut(&mut s[..], merge_mut_adjacent), s)
(s.mergesort_mut(Merge::merge_mut_adjacent), s)
}
}

Expand Down
Loading