From e399db98047f82d7453a6e92458577104d9806d2 Mon Sep 17 00:00:00 2001 From: LinlyBoi Date: Tue, 9 May 2023 21:40:26 +0300 Subject: [PATCH] :D --- src/main.rs | 110 +++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 92 insertions(+), 18 deletions(-) diff --git a/src/main.rs b/src/main.rs index 4296e0b..8aaa08c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -52,6 +52,46 @@ fn sstf(head: i32, seek_distance: i32, mut requests: Vec) -> i32 { } } } +fn cscan(mut head: i32, disk_end: i32, mut requests: Vec, direction: &mut Direction) -> i32 { + requests.sort(); + requests.insert(0, head); + let split_index: Option; + match direction { + Direction::LEFT => { + requests.sort_by(|a, b| (head - a).cmp(&(head - *b))); + //split into two + + dbg!(&requests); + *direction = Direction::RIGHT; + } + Direction::RIGHT => { + requests.push(disk_end); + requests.sort_by(|a, b| (head - b).cmp(&(head - *a))); //this in theory does literall + //the opposite + *direction = Direction::LEFT; + } + } + split_index = split_vec(head, &mut requests); + + match split_index { + Some(foo) => { + let mut seek: i32 = 0; + let mut right_side = requests.split_off(foo); + match direction { + Direction::LEFT => right_side.push(0), + Direction::RIGHT => right_side.push(disk_end), + } + dbg!(&right_side); + seek = fcfs(head, seek, right_side.clone()); + requests.sort(); + dbg!(&requests); + seek = fcfs(0, seek, requests); + seek + } + //We are splitting based on an element we just pushed in (impossible case) + None => 0, + } +} //defining initial direction here fn scan(mut head: i32, disk_end: i32, mut requests: Vec, direction: &mut Direction) -> i32 { requests.sort(); @@ -67,7 +107,8 @@ fn scan(mut head: i32, disk_end: i32, mut requests: Vec, direction: &mut Di } Direction::RIGHT => { requests.push(disk_end); - requests.sort_by(|a, b| (head - b).cmp(&(head - *a))); + requests.sort_by(|a, b| (head - b).cmp(&(head - *a))); //this in theory does literall + //the opposite *direction = Direction::LEFT; } } @@ -77,28 +118,20 @@ fn scan(mut head: i32, disk_end: i32, mut requests: Vec, direction: &mut Di Some(foo) => { let mut seek: i32 = 0; let mut right_side = requests.split_off(foo); - right_side.push(0); + match direction { + Direction::LEFT => right_side.push(0), + Direction::RIGHT => right_side.push(disk_end), + } dbg!(&right_side); seek = fcfs(head, seek, right_side.clone()); requests.sort(); + requests.reverse(); dbg!(&requests); seek = fcfs(0, seek, requests); seek } - None => requests - .iter() - .map(|request| { - let seek: i32; - if *request < head { - seek = head + *request as i32; //TODO turn to function params - } else { - seek = request.abs_diff(head) as i32; //TODO turn to function params - } - - head = *request; - seek - }) - .sum(), + //We are splitting based on an element we just pushed in (impossible case) + None => 0, } } fn fscan( @@ -129,12 +162,47 @@ fn fscan( } dbg!(&q1, &q2); dbg!(requests.len()); - seek1 += scan(head, disk_end, q1.clone(), &mut direction); - seek2 += scan(head, disk_end, q2.clone(), &mut direction); + seek1 += cscan(head, disk_end, q1.clone(), &mut direction); + seek2 += cscan(head, disk_end, q2.clone(), &mut direction); } seek1 + seek2 } +fn nscan( + mut head: i32, + disk_end: i32, + mut requests: Vec, + mut direction: Direction, + capacity: usize, + q_num: usize, +) -> i32 { + let queues: Vec> = Vec::with_capacity(q_num); + for mut queue in queues.clone() { + for value in requests.iter().take(4) { + queue.push(*value); + } + queue.sort(); + for _i in 1..4 { + if requests.is_empty() { + break; + } + requests.remove(0); + } + } + flip(&direction); //flip once before the first iteration so it cancels out + queues + .iter() + .map(|queue| { + direction = flip(&direction); + match direction { + Direction::LEFT => head = queue[0], + Direction::RIGHT => head = queue[queue.len() - 1], + } + cscan(head, disk_end, queue.to_vec(), &mut direction) + }) + .sum() +} + fn split_vec(item: i32, items: &mut Vec) -> Option { for (i, &pooper) in items.clone().iter().enumerate() { if pooper == item { @@ -150,3 +218,9 @@ enum Direction { fn cycle(slice: &[T], start_pos: usize) -> impl Iterator { slice.iter().cycle().skip(start_pos).take(slice.len()) } +fn flip(direction: &Direction) -> Direction { + match direction { + Direction::LEFT => Direction::RIGHT, + Direction::RIGHT => Direction::LEFT, + } +}