-
-
Save sliminality/9be7f2106e1cb2aebac11aa3ffd5ac58 to your computer and use it in GitHub Desktop.
Rust code shared from the playground
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#![allow(dead_code)] | |
extern crate rand; | |
use rand::Rng; | |
use std::sync::{Arc, Mutex}; | |
// use std::time::Duration; | |
use std::thread; | |
use std::sync::mpsc; | |
fn partition(arr: &mut [i32]) -> usize { | |
let end = arr.len() - 1; | |
let pivot_i: usize = end / 2; | |
let pivot: i32 = arr[pivot_i]; | |
// println!(" Pivot is: {}", pivot); | |
arr.swap(pivot_i, end); | |
// println!(" Swapped to end: {:?}", arr); | |
let mut gt_i: usize = 0; | |
for i in 0..end { | |
// println!("{} <? {}", arr[i], pivot); | |
if arr[i] < pivot { | |
arr.swap(i, gt_i); | |
gt_i += 1; | |
// println!("gt: {}", gt_i); | |
} | |
} | |
arr.swap(end, gt_i); | |
// println!(" Now slice is {:?}", arr); | |
return gt_i; | |
} | |
fn quicksort(arr: &mut [i32]) { | |
let len: usize = arr.len(); | |
if len <= 2 { | |
if len == 2 && arr[0] > arr[1] { | |
arr.swap(0, 1); | |
} | |
return; | |
} | |
// println!("------START-----"); | |
// println!("qs_recur called with {:?}", arr); | |
let pivot: usize = partition(&mut arr[..]); | |
// println!("after partitioning {:?}", arr); | |
// println!("recursing left with {:?}", &arr[0..pivot]); | |
quicksort(&mut arr[0..pivot]); | |
// println!("...now {:?}", &arr[0..pivot]); | |
// println!("recursing right with {:?}", &arr[(pivot+1)..len]); | |
quicksort(&mut arr[pivot..len]); | |
// println!("...now {:?}", &arr[(pivot+1)..len]); | |
// println!("\nEnding with: {:?}, {:?}, {:?} for total {:?}", &arr[0..pivot], arr[pivot], &arr[(pivot+1)..len], &arr); | |
// println!("------END------") | |
} | |
fn sum() { | |
let nums: [i32; 16] = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]; | |
let mut parallel_sum: i32 = 0; | |
for i in 0..4 { | |
let start: usize = 4 * i; | |
let handle = thread::spawn(move || { | |
let mut partial: i32 = 0; | |
for j in start..(start + 4) { | |
partial += nums[j]; | |
} | |
partial | |
}); | |
parallel_sum += handle.join().unwrap(); | |
} | |
println!("Parallel sum is: {}", parallel_sum); | |
} | |
fn parallel_test() { | |
let mut unsorted = Vec::new(); | |
for _ in 0..10 { | |
let rng = rand::thread_rng().gen_range(-5000, 5000); | |
unsorted.push(rng); | |
} | |
let data: Arc<Mutex<Vec<i32>>> = Arc::new(Mutex::new(unsorted)); | |
println!("Data before: {:?}", data); | |
for i in 0..3 { | |
let data = data.clone(); | |
thread::spawn(move || { | |
let start = i * 3; | |
let mut data = data.lock().unwrap(); | |
for j in start..(start + 3) { | |
data[j] *= 10; | |
} | |
}); | |
} | |
// remember to sleep here | |
//https://doc.rust-lang.org/book/concurrency.html | |
println!("Data after : {:?}", data); | |
} | |
fn main() { | |
let data2 = Arc::new(Mutex::new(vec![0, 1, 2, 3, 4, 5,6, 7, 8, 9, 10, 11])); | |
let (sender, receiver) = mpsc::channel(); | |
for i in 0..10 { | |
let (data2, sender) = (data2.clone(), sender.clone()); | |
thread::spawn(move || { | |
let mut data2 = data2.lock().unwrap(); | |
data2[i] += 1; | |
sender.send(()).unwrap(); | |
}); | |
} | |
for _ in 0..10 { | |
receiver.recv().unwrap(); | |
} | |
println!("Data2 after : {:?}", data2); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment