How to assign 2 variables simultaneously in Rust? - rust
I'm currently teaching myself Rust with a roguelike tutorial, and I'm attempting to get a key press to move a character diagonally which would mean player_x -=1, player_y -= 1 for up left.
No matter which way I try to arrange the code, I keep getting error messages from the compiler. I couldn't find any example of this anywhere in the documentation or on GitHub.
Key { code: Escape, .. } => return true, // exit game
// movement keys
Key { code: Up, .. } => *player_y -= 1,
Key { code: Down, .. } => *player_y += 1,
Key { code: Left, .. } => *player_x -= 1,
Key { code: Right, .. } => *player_x += 1,
Key { printable: 'k', .. } => *player_y -= 1,
Key { printable: 'k', .. } => *player_x -= 1,
_ => {}
You can kind of see what I'm trying to do here, but this throws an error message saying the pattern is unreachable, how would I fix this code?
Here's the full code, it's fairly small, not sure what would be alone necessary to compile:
extern crate tcod;
extern crate input;
use tcod::console::*;
use tcod::colors;
// actual size of the window
const SCREEN_WIDTH: i32 = 80;
const SCREEN_HEIGHT: i32 = 50;
const LIMIT_FPS: i32 = 20; // 20 frames-per-second maximum
fn handle_keys(root: &mut Root, player_x: &mut i32, player_y: &mut i32) -> bool {
use tcod::input::Key;
use tcod::input::KeyCode::*;
let key = root.wait_for_keypress(true);
match key {
Key { code: Enter, alt: true, .. } => {
// Alt+Enter: toggle fullscreen
let fullscreen = root.is_fullscreen();
root.set_fullscreen(!fullscreen);
}
Key { code: Escape, .. } => return true, // exit game
// movement keys
Key { code: Up, .. } => *player_y -= 1,
Key { code: Down, .. } => *player_y += 1,
Key { code: Left, .. } => *player_x -= 1,
Key { code: Right, .. } => *player_x += 1,
Key { printable: 'k', ..} => *player_y -= 1,
Key { printable: 'k', ..} => *player_x -= 1,
_ => {},
}
false
}
fn main() {
let mut root = Root::initializer()
.font("terminal8x8_gs_tc.png", FontLayout::Tcod)
.font_type(FontType::Greyscale)
.size(SCREEN_WIDTH, SCREEN_HEIGHT)
.title("Rust/libtcod tutorial")
.init();
tcod::system::set_fps(LIMIT_FPS);
let mut player_x = SCREEN_WIDTH / 2;
let mut player_y = SCREEN_HEIGHT / 2;
while !root.window_closed() {
root.set_default_foreground(colors::WHITE);
root.put_char(player_x, player_y, '#', BackgroundFlag::None);
root.flush();
root.put_char(player_x, player_y, ' ', BackgroundFlag::None);
// handle keys and exit game if needed
let exit = handle_keys(&mut root, &mut player_x, &mut player_y);
if exit {
break
}
}
}
How to assign 2 variables simultaneously in Rust?
You cannot. You can bind two variables at once:
let (a, b) = (1, 2);
If you aren't trying to create new bindings, you need to have two assignment statements:
let mut a = 1;
let mut b = 2;
a = 3;
b = 4;
In your case, for a match statement, you need to introduce a block:
let key = 42;
let mut a = 1;
let mut b = 2;
match key {
0 => {
a += 1;
b -= 1;
}
_ => {
a -= 10;
b *= 100;
}
}
You could also have the match expression evaluate to a tuple, which you then create new bindings for and apply them afterwards:
let key = 42;
let mut x = 1;
let mut y = 2;
let (d_x, d_y) = match key {
0 => (1, -1),
_ => (10, 10),
};
x += d_x;
y += d_y;
I strongly recommend reading The Rust Programming Language instead of trying to learn Rust by intuition or trial and error. It has an entire chapter on the match statement.
See also:
How to swap two variables?
The second printable: 'k' is unreachable, since the first will match instead. What you want is doing both assignments in the same arm of the match, like this:
Key { printable: 'k', .. } => {
*player_y -= 1;
*player_x -= 1;
}
Related
Very Slow and Choppy Camera Controller in Rust
I am currently working on simple Rendering in Rust using the OpenGL Wrapper Glium. I have a simple 3D model and a Camera ready set up and started working on a fitting camera controller. I'm using device_query to check for keyboard input and if it's a Specific Keyboard input I just change the x and y coordinates. This works, however it works really bad, inconstantly and choppy. My Event loop: event_loop.run(move |event, _, control_flow| { let next_frame_time = std::time::Instant::now() + std::time::Duration::from_nanos(16_666_667); ####### This is where I change the coordinates ######## x = keyboardinput::keyboard_input_x(x); y = keyboardinput::keyboard_input_y(y); *control_flow = glutin::event_loop::ControlFlow::WaitUntil(next_frame_time); match event { glutin::event::Event::WindowEvent { event, .. } => match event { glutin::event::WindowEvent::CloseRequested => { *control_flow = glutin::event_loop::ControlFlow::Exit; return; } _ => return, }, glutin::event::Event::NewEvents(cause) => match cause { glutin::event::StartCause::ResumeTimeReached { .. } => (), glutin::event::StartCause::Init => (), _ => return, }, _ => return, } This is my Keyboard input rs file: use device_query::{DeviceQuery, DeviceState, Keycode}; pub fn keyboard_input_x(mut x: f32) -> f32 { let device_state = DeviceState::new(); let keys: Vec<Keycode> = device_state.get_keys(); for key in keys.iter() { println!("Pressed key: {:?}", key); if key == &Keycode::W { x -= 0.03; return x; } if key == &Keycode::S { x += 0.03; return x; } } return x; } pub fn keyboard_input_y(mut y: f32) -> f32 { let device_state = DeviceState::new(); let keys: Vec<Keycode> = device_state.get_keys(); for key in keys.iter() { println!("Pressed key: {:?}", key); if key == &Keycode::Space { y += 0.03; return y; } if key == &Keycode::LControl { y -= 0.03; return y; } } return y; } What can I do the make the camera as smooth as possible?
Instead of directly updating your x/y position in the input handler, create two variables holding your x velocity and y velocity (xv/yv), and then in your input handler do something like this: bool space_pressed = false; for key in keys.iter() { if key == &Keycode::Space { space_pressed = true; } } if space_pressed { yv = 1.0; // Move up. } else { yv = 0.0; // Don't move. } And in your main loop where you compute each frame, count the amount of time since the last frame in seconds (often called delta-t or dt), and integrate your x velocity and y velocity to a new position: // Update camera position. x += xv * dt; y += yv * dt; Now when you hold space you will move up at a constant velocity, rather than unpredictably and choppy.
Tic Tac Toe - Minimax
I'm trying to build a tic-tac-toe game using minimax algorithm with rust. And I'm stuck. I tried to write a rust code based on the psudeo code on the wikipedia page. https://en.wikipedia.org/wiki/Minimax. However, it didn't work. Ai always makes the first possible move. I would be glad if you could help me. In main.rs fn main() { let mut g = Game::new(); while g.game_state() == Game_State::Continuous { g.print(); println!("{}", minimax(&g)); if g.turn == Player::Cross { g.take_input(); } else { g = best_move(&g); } } g.print(); if let Game_State::Win(Player::None) = g.game_state() { println!("Draw"); } else { g.print_winner(); } } In ai.rs pub fn child_nodes(game: &Game) -> Vec<Game> { let mut children: Vec<Game> = Vec::new(); for r in 0..3 { for c in 0..3 { if game.grid[r][c] == Player::None { let mut child = game.clone(); child.grid[r][c] = game.turn; child.turn = reverse_player(child.turn); children.push(child); } } } return children; } pub fn minimax(game: &Game) -> isize { match game.game_state() { Game_State::Win(winner) => to_scor(winner), Game_State::Continuous => { use std::cmp::{min, max}; let children_vec = child_nodes(&game); let mut score: isize; if game.turn == Player::Cross { score = -2; for i in &children_vec { score = max(score, minimax(i)); } } else { score = 2; for i in &children_vec { score = min(score, minimax(i)); } } return score; } } } pub fn best_move(game: &Game) -> Game { let children = child_nodes(game); let mut values: Vec<isize> = Vec::new(); for i in 0..children.len() { values.push(minimax(&children[i])); } let mut index: usize = 0; let iter = values.iter().enumerate(); if game.turn == Player::Cross { if let Option::Some(t) = iter.max() { index = t.0; } } else if game.turn == Player::Circle { if let Option::Some(t) = iter.min() { index = t.0; } } let best_pos = children[index]; best_pos } pub fn to_scor(x: Player) -> isize { match x { Player::Cross => 1, Player::Circle => -1, Player::None => 0 } }
.enumerate() returns an iterator over tuples, and .max() and .min() on an iterator of tuples will compare the tuples - that is, (1, x) is always considered to be less than (2, y) for any values of x and y. This can be demonstrated with this snippet: fn main() { let v = vec![3, 1, 2, 5, 3, 6, 7, 2]; println!("{:?}", v.iter().enumerate().min()); println!("{:?}", v.iter().enumerate().max()); } which prints: Some((0, 3)) Some((7, 2)) which are just the first and last elements of the list (and not the minimum or maximum elements). However, as shown here, you can use max_by to use your own function to compare the tuples.
Severe performance degredation over time in multi-threading: what am I missing?
In my application a method runs quickly once started but begins to continuously degrade in performance upon nearing completion, this seems to be even irrelevant of the amount of work (the number of iterations of a function each thread has to perform). Once it reaches near the end it slows to an incredibly slow pace compared to earlier (worth noting this is not just a result of fewer threads remaining incomplete, it seems even each thread slows down). I cannot figure out why this occurs, so I'm asking. What am I doing wrong? An overview of CPU usage: A slideshow of the problem Worth noting that CPU temperature remains low throughout. This stage varies with however much work is set, more work produces a better appearance with all threads constantly near 100%. Still, at this moment this appears good. Here we see the continued performance of earlier, Here we see it start to degrade. I do not know why this occurs. After some period of chaos most of the threads have finished their work and the remaining threads continue, at this point although it seems they are at 100% they in actually perform their remaining workload very slowly. I cannot understand why this occurs. Printing progress I have written a multi-threaded random_search (documentation link) function for optimization. Most of the complexity in this function comes from printing data passing data between threads, this supports giving outputs showing progress like: 2300 565 (24.57%) 00:00:11 / 00:00:47 [25.600657363049734] { [563.0ns, 561.3ms, 125.0ns, 110.0ns] [2.0µs, 361.8ms, 374.0ns, 405.0ns] [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1] } I have been trying to use this output to figure out whats gone wrong, but I have no idea. This output describes: The total number of iterations 2300. The total number of current iterations 565. The time running 00:00:11 (mm:ss:ms). The estimated time remaining 00:00:47 (mm:ss:ms). The current best value [25.600657363049734]. The most recently measured times between execution positions (effectively time taken for thread to go from some line, to another line (defined specifically with update_execution_position in code below) [563.0ns, 561.3ms, 125.0ns, 110.0ns]. The averages times between execution positions (this is average across entire runtime rather than since last measured) [2.0µs, 361.8ms, 374.0ns, 405.0ns]. The execution positions of threads (0 is when a thread is completed, rest represent a thread having hit some line, which triggered this setting, but yet to hit next line which changes it, effectively being between 2 positions) [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1] The random_search code: Given I have tested implementations with the other methods in my library grid_search and simulated_annealing it would suggest to me the problem does not atleast entirely reside in random_search.rs. random_search.rs: pub fn random_search< A: 'static + Send + Sync, T: 'static + Copy + Send + Sync + Default + SampleUniform + PartialOrd, const N: usize, >( // Generics ranges: [Range<T>; N], f: fn(&[T; N], Option<Arc<A>>) -> f64, evaluation_data: Option<Arc<A>>, polling: Option<Polling>, // Specifics iterations: u64, ) -> [T; N] { // Gets cpu data let cpus = num_cpus::get() as u64; let search_cpus = cpus - 1; // 1 cpu is used for polling, this one. let remainder = iterations % search_cpus; let per = iterations / search_cpus; let ranges_arc = Arc::new(ranges); let (best_value, best_params) = search( // Generics ranges_arc.clone(), f, evaluation_data.clone(), // Since we are doing this on the same thread, we don't need to use these Arc::new(AtomicU64::new(Default::default())), Arc::new(Mutex::new(Default::default())), Arc::new(AtomicBool::new(false)), Arc::new(AtomicU8::new(0)), Arc::new([ Mutex::new((Duration::new(0, 0), 0)), Mutex::new((Duration::new(0, 0), 0)), Mutex::new((Duration::new(0, 0), 0)), Mutex::new((Duration::new(0, 0), 0)), ]), // Specifics remainder, ); let thread_exit = Arc::new(AtomicBool::new(false)); // (handles,(counters,thread_bests)) let (handles, links): (Vec<_>, Vec<_>) = (0..search_cpus) .map(|_| { let ranges_clone = ranges_arc.clone(); let counter = Arc::new(AtomicU64::new(0)); let thread_best = Arc::new(Mutex::new(f64::MAX)); let thread_execution_position = Arc::new(AtomicU8::new(0)); let thread_execution_time = Arc::new([ Mutex::new((Duration::new(0, 0), 0)), Mutex::new((Duration::new(0, 0), 0)), Mutex::new((Duration::new(0, 0), 0)), Mutex::new((Duration::new(0, 0), 0)), ]); let counter_clone = counter.clone(); let thread_best_clone = thread_best.clone(); let thread_exit_clone = thread_exit.clone(); let evaluation_data_clone = evaluation_data.clone(); let thread_execution_position_clone = thread_execution_position.clone(); let thread_execution_time_clone = thread_execution_time.clone(); ( thread::spawn(move || { search( // Generics ranges_clone, f, evaluation_data_clone, counter_clone, thread_best_clone, thread_exit_clone, thread_execution_position_clone, thread_execution_time_clone, // Specifics per, ) }), ( counter, ( thread_best, (thread_execution_position, thread_execution_time), ), ), ) }) .unzip(); let (counters, links): (Vec<Arc<AtomicU64>>, Vec<_>) = links.into_iter().unzip(); let (thread_bests, links): (Vec<Arc<Mutex<f64>>>, Vec<_>) = links.into_iter().unzip(); let (thread_execution_positions, thread_execution_times) = links.into_iter().unzip(); if let Some(poll_data) = polling { poll( poll_data, counters, remainder, iterations, thread_bests, thread_exit, thread_execution_positions, thread_execution_times, ); } let joins: Vec<_> = handles.into_iter().map(|h| h.join().unwrap()).collect(); let (_, best_params) = joins .into_iter() .fold((best_value, best_params), |(bv, bp), (v, p)| { if v < bv { (v, p) } else { (bv, bp) } }); return best_params; fn search< A: 'static + Send + Sync, T: 'static + Copy + Send + Sync + Default + SampleUniform + PartialOrd, const N: usize, >( // Generics ranges: Arc<[Range<T>; N]>, f: fn(&[T; N], Option<Arc<A>>) -> f64, evaluation_data: Option<Arc<A>>, counter: Arc<AtomicU64>, best: Arc<Mutex<f64>>, thread_exit: Arc<AtomicBool>, thread_execution_position: Arc<AtomicU8>, thread_execution_times: Arc<[Mutex<(Duration, u64)>; 4]>, // Specifics iterations: u64, ) -> (f64, [T; N]) { let mut execution_position_timer = Instant::now(); let mut rng = thread_rng(); let mut params = [Default::default(); N]; let mut best_value = f64::MAX; let mut best_params = [Default::default(); N]; for _ in 0..iterations { // Gen random values for (range, param) in ranges.iter().zip(params.iter_mut()) { *param = rng.gen_range(range.clone()); } // Update execution position execution_position_timer = update_execution_position( 1, execution_position_timer, &thread_execution_position, &thread_execution_times, ); // Run function let new_value = f(¶ms, evaluation_data.clone()); // Update execution position execution_position_timer = update_execution_position( 2, execution_position_timer, &thread_execution_position, &thread_execution_times, ); // Check best if new_value < best_value { best_value = new_value; best_params = params; *best.lock().unwrap() = best_value; } // Update execution position execution_position_timer = update_execution_position( 3, execution_position_timer, &thread_execution_position, &thread_execution_times, ); counter.fetch_add(1, Ordering::SeqCst); // Update execution position execution_position_timer = update_execution_position( 4, execution_position_timer, &thread_execution_position, &thread_execution_times, ); if thread_exit.load(Ordering::SeqCst) { break; } } // Update execution position // 0 represents ended state thread_execution_position.store(0, Ordering::SeqCst); return (best_value, best_params); } } util.rs: pub fn update_execution_position<const N: usize>( i: usize, execution_position_timer: Instant, thread_execution_position: &Arc<AtomicU8>, thread_execution_times: &Arc<[Mutex<(Duration, u64)>; N]>, ) -> Instant { { let mut data = thread_execution_times[i - 1].lock().unwrap(); data.0 += execution_position_timer.elapsed(); data.1 += 1; } thread_execution_position.store(i as u8, Ordering::SeqCst); Instant::now() } pub struct Polling { pub poll_rate: u64, pub printing: bool, pub early_exit_minimum: Option<f64>, pub thread_execution_reporting: bool, } impl Polling { const DEFAULT_POLL_RATE: u64 = 10; pub fn new(printing: bool, early_exit_minimum: Option<f64>) -> Self { Self { poll_rate: Polling::DEFAULT_POLL_RATE, printing, early_exit_minimum, thread_execution_reporting: false, } } } pub fn poll<const N: usize>( data: Polling, // Current count of each thread. counters: Vec<Arc<AtomicU64>>, offset: u64, // Final total iterations. iterations: u64, // Best values of each thread. thread_bests: Vec<Arc<Mutex<f64>>>, // Early exit switch. thread_exit: Arc<AtomicBool>, // Current positions of execution of each thread. thread_execution_positions: Vec<Arc<AtomicU8>>, // Current average times between execution positions for each thread thread_execution_times: Vec<Arc<[Mutex<(Duration, u64)>; N]>>, ) { let start = Instant::now(); let mut stdout = stdout(); let mut count = offset + counters .iter() .map(|c| c.load(Ordering::SeqCst)) .sum::<u64>(); if data.printing { println!("{:20}", iterations); } let mut poll_time = Instant::now(); let mut held_best: f64 = f64::MAX; let mut held_average_execution_times: [(Duration, u64); N] = vec![(Duration::new(0, 0), 0); N].try_into().unwrap(); let mut held_recent_execution_times: [Duration; N] = vec![Duration::new(0, 0); N].try_into().unwrap(); while count < iterations { if data.printing { // loop { let percent = count as f32 / iterations as f32; // If count == 0, give 00... for remaining time as placeholder let remaining_time_estimate = if count == 0 { Duration::new(0, 0) } else { start.elapsed().div_f32(percent) }; print!( "\r{:20} ({:.2}%) {} / {} [{}] {}\t", count, 100. * percent, print_duration(start.elapsed(), 0..3), print_duration(remaining_time_estimate, 0..3), if held_best == f64::MAX { String::from("?") } else { format!("{}", held_best) }, if data.thread_execution_reporting { let (average_execution_times, recent_execution_times): ( Vec<String>, Vec<String>, ) = (0..thread_execution_times[0].len()) .map(|i| { let (mut sum, mut num) = (Duration::new(0, 0), 0); for n in 0..thread_execution_times.len() { { let mut data = thread_execution_times[n][i].lock().unwrap(); sum += data.0; held_average_execution_times[i].0 += data.0; num += data.1; held_average_execution_times[i].1 += data.1; *data = (Duration::new(0, 0), 0); } } if num > 0 { held_recent_execution_times[i] = sum.div_f64(num as f64); } ( if held_average_execution_times[i].1 > 0 { format!( "{:.1?}", held_average_execution_times[i] .0 .div_f64(held_average_execution_times[i].1 as f64) ) } else { String::from("?") }, if held_recent_execution_times[i] > Duration::new(0, 0) { format!("{:.1?}", held_recent_execution_times[i]) } else { String::from("?") }, ) }) .unzip(); let execution_positions: Vec<u8> = thread_execution_positions .iter() .map(|pos| pos.load(Ordering::SeqCst)) .collect(); format!( "{{ [{}] [{}] {:.?} }}", recent_execution_times.join(", "), average_execution_times.join(", "), execution_positions ) } else { String::from("") } ); stdout.flush().unwrap(); } // Updates best and does early exiting match (data.early_exit_minimum, data.printing) { (Some(early_exit), true) => { for thread_best in thread_bests.iter() { let thread_best_temp = *thread_best.lock().unwrap(); if thread_best_temp < held_best { held_best = thread_best_temp; if thread_best_temp <= early_exit { thread_exit.store(true, Ordering::SeqCst); println!(); return; } } } } (None, true) => { for thread_best in thread_bests.iter() { let thread_best_temp = *thread_best.lock().unwrap(); if thread_best_temp < held_best { held_best = thread_best_temp; } } } (Some(early_exit), false) => { for thread_best in thread_bests.iter() { if *thread_best.lock().unwrap() <= early_exit { thread_exit.store(true, Ordering::SeqCst); return; } } } (None, false) => {} } thread::sleep(saturating_sub( Duration::from_millis(data.poll_rate), poll_time.elapsed(), )); poll_time = Instant::now(); count = offset + counters .iter() .map(|c| c.load(Ordering::SeqCst)) .sum::<u64>(); } if data.printing { println!( "\r{:20} (100.00%) {} / {} [{}] {}\t", count, print_duration(start.elapsed(), 0..3), print_duration(start.elapsed(), 0..3), held_best, if data.thread_execution_reporting { let (average_execution_times, recent_execution_times): (Vec<String>, Vec<String>) = (0..thread_execution_times[0].len()) .map(|i| { let (mut sum, mut num) = (Duration::new(0, 0), 0); for n in 0..thread_execution_times.len() { { let mut data = thread_execution_times[n][i].lock().unwrap(); sum += data.0; held_average_execution_times[i].0 += data.0; num += data.1; held_average_execution_times[i].1 += data.1; *data = (Duration::new(0, 0), 0); } } if num > 0 { held_recent_execution_times[i] = sum.div_f64(num as f64); } ( if held_average_execution_times[i].1 > 0 { format!( "{:.1?}", held_average_execution_times[i] .0 .div_f64(held_average_execution_times[i].1 as f64) ) } else { String::from("?") }, if held_recent_execution_times[i] > Duration::new(0, 0) { format!("{:.1?}", held_recent_execution_times[i]) } else { String::from("?") }, ) }) .unzip(); let execution_positions: Vec<u8> = thread_execution_positions .iter() .map(|pos| pos.load(Ordering::SeqCst)) .collect(); format!( "{{ [{}] [{}] {:.?} }}", recent_execution_times.join(", "), average_execution_times.join(", "), execution_positions ) } else { String::from("") } ); stdout.flush().unwrap(); } } // Since `Duration::saturating_sub` is unstable this is an alternative. fn saturating_sub(a: Duration, b: Duration) -> Duration { if let Some(dur) = a.checked_sub(b) { dur } else { Duration::new(0, 0) } } main.rs use std::{cmp,sync::Arc}; type Image = Vec<Vec<Pixel>>; #[derive(Clone)] pub struct Pixel { pub luma: u8, } impl From<&u8> for Pixel { fn from(x: &u8) -> Pixel { Pixel { luma: *x } } } fn main() { // Setup // ------------------------------------------- fn open_image(path: &str) -> Image { let example = image::open(path).unwrap().to_rgb8(); let dims = example.dimensions(); let size = (dims.0 as usize, dims.1 as usize); let example_vec = example.into_raw(); // Binarizes image let img_vec = from_raw(&example_vec, size); img_vec } println!("Started ..."); let example: Image = open_image("example.jpg"); let target: Image = open_image("target.jpg"); // let first_image = Some(Arc::new((examples[0].clone(), targets[0].clone()))); println!("Opened..."); let image = Some(Arc::new((example, target))); // Running the optimization // ------------------------------------------- println!("Started opt..."); let best = simple_optimization::random_search( [0..255, 0..255, 0..255, 1..255, 1..255], eval_one, image, Some(simple_optimization::Polling { poll_rate: 100, printing: true, early_exit_minimum: None, thread_execution_reporting: true, }), 2300, ); println!("{:.?}", best); // [34, 220, 43, 253, 168] assert!(false); fn eval_one(arr: &[u8; 5], opt: Option<Arc<(Image, Image)>>) -> f64 { let bin_params = ( arr[0] as u8, arr[1] as u8, arr[2] as u8, arr[3] as usize, arr[4] as usize, ); let arc = opt.unwrap(); // Gets average mean-squared-error let binary_pixels = binarize_buffer(arc.0.clone(), bin_params); mse(binary_pixels, &arc.1) } // Mean-squared-error fn mse(prediction: Image, target: &Image) -> f64 { let n = target.len() * target[0].len(); prediction .iter() .flatten() .zip(target.iter().flatten()) .map(|(p, t)| difference(p, t).powf(2.)) .sum::<f64>() / (2. * n as f64) } #[rustfmt::skip] fn difference(p: &Pixel, t: &Pixel) -> f64 { p.luma as f64 - t.luma as f64 } } pub fn from_raw(raw: &[u8], (_i_size, j_size): (usize, usize)) -> Vec<Vec<Pixel>> { (0..raw.len()) .step_by(j_size) .map(|index| { raw[index..index + j_size] .iter() .map(Pixel::from) .collect::<Vec<Pixel>>() }) .collect() } pub fn binarize_buffer( mut img: Vec<Vec<Pixel>>, (_, _, local_luma_boundary, local_field_reach, local_field_size): (u8, u8, u8, usize, usize), ) -> Vec<Vec<Pixel>> { let (i_size, j_size) = (img.len(), img[0].len()); let i_chunks = (i_size as f32 / local_field_size as f32).ceil() as usize; let j_chunks = (j_size as f32 / local_field_size as f32).ceil() as usize; let mut local_luma: Vec<Vec<u8>> = vec![vec![u8::default(); j_chunks]; i_chunks]; // Gets average luma in local fields // O((s+r)^2*(n/s)*(m/s)) : s = local field size, r = local field reach for (i_chunk, i) in (0..i_size).step_by(local_field_size).enumerate() { let i_range = zero_checked_sub(i, local_field_reach) ..cmp::min(i + local_field_size + local_field_reach, i_size); let i_range_length = i_range.end - i_range.start; for (j_chunk, j) in (0..j_size).step_by(local_field_size).enumerate() { let j_range = zero_checked_sub(j, local_field_reach) ..cmp::min(j + local_field_size + local_field_reach, j_size); let j_range_length = j_range.end - j_range.start; let total: u32 = i_range .clone() .map(|i_range_indx| { img[i_range_indx][j_range.clone()] .iter() .map(|p| p.luma as u32) .sum::<u32>() }) .sum(); local_luma[i_chunk][j_chunk] = (total / (i_range_length * j_range_length) as u32) as u8; } } // Apply binarization // O(nm) for i in 0..i_size { let i_group: usize = i / local_field_size; // == floor(i as f32 / local_field_size as f32) as usize for j in 0..j_size { let j_group: usize = j / local_field_size; // Local average boundaries // -------------------------------- if let Some(local) = local_luma[i_group][j_group].checked_sub(local_luma_boundary) { if img[i][j].luma < local { img[i][j].luma = 0; continue; } } if let Some(local) = local_luma[i_group][j_group].checked_add(local_luma_boundary) { if img[i][j].luma > local { img[i][j].luma = 255; continue; } } // White is the negative (false/0) colour in our binarization, thus this is our else case img[i][j].luma = 255; } } img } #[rustfmt::skip] fn zero_checked_sub(a: usize, b: usize) -> usize { if a > b { a - b } else { 0 } } Project zip (in case you'd rather not spend time setting it up). Else, here are the images being used as /target.jpg and /example.jpg (it shouldn't matter it being specifically these images, any should work): And Cargo.toml dependencies: [dependencies] rand = "0.8.4" itertools = "0.10.1" # izip! num_cpus = "1.13.0" # Multi-threading print_duration = "1.0.0" # Printing progress num = "0.4.0" # Generics rand_distr = "0.4.1" # Normal distribution image = "0.23.14" serde = { version="1.0.118", features=["derive"] } serde_json = "1.0.50" I do feel rather reluctant to post such a large question and inevitably require people to read a few hundred lines (especially given the project doesn't work in a playground), but I'm really lost here and can see no other way to communicate the whole area of the problem. Apologies for this. As noted, I have tried for a while to figure out what is happening here, but I have come up short, any help would be really appreciate.
Some basic debugging (aka println! everywhere) shows that your performance problem is not related to the multithreading at all. It just happens randomly, and when there are 24 threads doing their job, the fact that one is randomly stalling is not noticeable, but when there is only one or two threads left, they stand out as slow. But where is this performance bottleneck? Well, you are stating it yourself in the code: in binary_buffer you say: // Gets average luma in local fields // O((s+r)^2*(n/s)*(m/s)) : s = local field size, r = local field reach The values of s and r seem to be random values between 0 and 255, while n is the length of a image row, in bytes 3984 * 3 = 11952, and m is the number of rows 2271. Now, most of the times that O() is around a few millions, quite manageable. But if s happens to be small and r big, such as (3, 200) then the number of computations blows up to over 1e11! Fortunately I think you can define the ranges of those values in the original call to random_search so a bit of tweaking there should send you back to reasonable complexity. Changing the ranges to: [0..255, 0..255, 0..255, 1..255, 20..255], // ^ here seems to do the trick for me. PS: These lines at the beginning of binary_buffer were key to discover this: let o = (i_size / local_field_size) * (j_size / local_field_size) * (local_field_size + local_field_reach).pow(2); println!("\nO() = {}", o);
Is there a way to update a string in place in rust?
You can also consider this as, is it possible to URLify a string in place in rust? For example, Problem statement: Replace whitespace with %20 Assumption: String will have enough capacity left to accommodate new characters. Input: Hello how are you Output: Hello%20how%20are%20you I know there are ways to do this if we don't have to do this "in place". I am solving a problem that explicitly states that you have to update in place. If there isn't any safe way to do this, is there any particular reason behind that? [Edit] I was able to solve this using unsafe approach, but would appreciate a better approach than this. More idiomatic approach if there is. fn space_20(sentence: &mut String) { if !sentence.is_ascii() { panic!("Invalid string"); } let chars: Vec<usize> = sentence.char_indices().filter(|(_, ch)| ch.is_whitespace()).map(|(idx, _)| idx ).collect(); let char_count = chars.len(); if char_count == 0 { return; } let sentence_len = sentence.len(); sentence.push_str(&"*".repeat(char_count*2)); // filling string with * so that bytes array becomes of required size. unsafe { let bytes = sentence.as_bytes_mut(); let mut final_idx = sentence_len + (char_count * 2) - 1; let mut i = sentence_len - 1; let mut char_ptr = char_count - 1; loop { if i != chars[char_ptr] { bytes[final_idx] = bytes[i]; if final_idx == 0 { // all elements are filled. println!("all elements are filled."); break; } final_idx -= 1; } else { bytes[final_idx] = '0' as u8; bytes[final_idx - 1] = '2' as u8; bytes[final_idx - 2] = '%' as u8; // final_idx is of type usize cannot be less than 0. if final_idx < 3 { println!("all elements are filled at start."); break; } final_idx -= 3; // char_ptr is of type usize cannot be less than 0. if char_ptr > 0 { char_ptr -= 1; } } if i == 0 { // all elements are parsed. println!("all elements are parsed."); break; } i -= 1; } } } fn main() { let mut sentence = String::with_capacity(1000); sentence.push_str(" hello, how are you?"); // sentence.push_str("hello, how are you?"); // sentence.push_str(" hello, how are you? "); // sentence.push_str(" "); // sentence.push_str("abcd"); space_20(&mut sentence); println!("{}", sentence); }
An O(n) solution that neither uses unsafe nor allocates (provided that the string has enough capacity), using std::mem::take: fn urlify_spaces(text: &mut String) { const SPACE_REPLACEMENT: &[u8] = b"%20"; // operating on bytes for simplicity let mut buffer = std::mem::take(text).into_bytes(); let old_len = buffer.len(); let space_count = buffer.iter().filter(|&&byte| byte == b' ').count(); let new_len = buffer.len() + (SPACE_REPLACEMENT.len() - 1) * space_count; buffer.resize(new_len, b'\0'); let mut write_pos = new_len; for read_pos in (0..old_len).rev() { let byte = buffer[read_pos]; if byte == b' ' { write_pos -= SPACE_REPLACEMENT.len(); buffer[write_pos..write_pos + SPACE_REPLACEMENT.len()] .copy_from_slice(SPACE_REPLACEMENT); } else { write_pos -= 1; buffer[write_pos] = byte; } } *text = String::from_utf8(buffer).expect("invalid UTF-8 during URL-ification"); } (playground) Basically, it calculates the final length of the string, sets up a reading pointer and a writing pointer, and translates the string from right to left. Since "%20" has more characters than " ", the writing pointer never catches up with the reading pointer.
Is it possible to do this without unsafe? Yes like this: fn main() { let mut my_string = String::from("Hello how are you"); let mut insert_positions = Vec::new(); let mut char_counter = 0; for c in my_string.chars() { if c == ' ' { insert_positions.push(char_counter); char_counter += 2; // Because we will insert two extra chars here later. } char_counter += 1; } for p in insert_positions.iter() { my_string.remove(*p); my_string.insert(*p, '0'); my_string.insert(*p, '2'); my_string.insert(*p, '%'); } println!("{}", my_string); } Here is the Playground. But should you do it? As discussed for example here on Reddit this is almost always not the recommended way of doing this, because both remove and insert are O(n) operations as noted in the documentation. Edit A slightly better version: fn main() { let mut my_string = String::from("Hello how are you"); let mut insert_positions = Vec::new(); let mut char_counter = 0; for c in my_string.chars() { if c == ' ' { insert_positions.push(char_counter); char_counter += 2; // Because we will insert two extra chars here later. } char_counter += 1; } for p in insert_positions.iter() { my_string.remove(*p); my_string.insert_str(*p, "%20"); } println!("{}", my_string); } and the corresponding Playground.
How do I get a substring between two patterns in Rust?
I want to create a substring in Rust. It starts with an occurrence of a string and ends at the end of the string minus four characters or at a certain character. My first approach was string[string.find("pattern").unwrap()..string.len()-5] That is wrong because Rust's strings are valid UTF-8 and thus byte and not char based. My second approach is correct but too verbose: let start_bytes = string.find("pattern").unwrap(); let mut char_byte_counter = 0; let result = line.chars() .skip_while(|c| { char_byte_counter += c.len_utf8(); return start_bytes > char_byte_counter; }) .take_while(|c| *c != '<') .collect::<String>(); Are there simpler ways to create substrings? Is there any part of the standard library I did not find?
I don't remember a built-in library function in other languages that works exactly the way you want (give me the substring between two patterns, or between the first and the end if the second does not exist). I think you would have to write some custom logic anyway. The closest equivalent to a "substring" function is slicing. However (as you found out) it works with bytes, not with unicode characters, so you will have to be careful with indices. In "Löwe", the 'e' is at (byte) index 4, not 3 (playground). But you can still use it in your case, because you are not working with indices directly (using find instead to... find the index you need for you) Here's how you could do it with slicing (bonus, you don't need to re-allocate other Strings): // adding some unicode to check that everything works // also ouside of ASCII let line = "asdfapatterndf1老虎23<12"; let start_bytes = line.find("pattern").unwrap_or(0); //index where "pattern" starts // or beginning of line if // "pattern" not found let end_bytes = line.find("<").unwrap_or(line.len()); //index where "<" is found // or end of line let result = &line[start_bytes..end_bytes]; //slicing line, returns patterndf1老虎23
Try using something like the following method: //Return result in &str or empty &str if not found fn between<'a>(source: &'a str, start: &'a str, end: &'a str) -> &'a str { let start_position = source.find(start); if start_position.is_some() { let start_position = start_position.unwrap() + start.len(); let source = &source[start_position..]; let end_position = source.find(end).unwrap_or_default(); return &source[..end_position]; } return ""; }
This method approximate to O(n) with char and grapheme in mind. It works, but I'm not sure if there are any bugs. fn between(str: &String, start: String, end: String, limit_one:bool, ignore_case: bool) -> Vec<String> { let mut result:Vec<String> = vec![]; let mut starts = start.graphemes(true); let mut ends = end.graphemes(true); let sc = start.graphemes(true).count(); let ec = end.graphemes(true).count(); let mut m = 0; let mut started:bool = false; let mut temp = String::from(""); let mut temp2 = String::from(""); for c in str.graphemes(true) { if started == false { let opt = starts.next(); match opt { Some(d) => { if (ignore_case && c.to_uppercase().cmp(&d.to_uppercase()) == std::cmp::Ordering::Equal) || c == d { m += 1; if m == sc { started = true; starts = start.graphemes(true); } } else { m = 0; starts = start.graphemes(true); } }, None => { starts = start.graphemes(true); let opt = starts.next(); match opt { Some(e) => { if (ignore_case && c.to_uppercase().cmp(&e.to_uppercase()) == std::cmp::Ordering::Equal) || c == e { m += 1; if m == sc { started = true; starts = start.graphemes(true); } } }, None => {} } } } } else if started == true { let opt = ends.next(); match opt { Some(e) => { if (ignore_case && c.to_uppercase().cmp(&e.to_uppercase()) == std::cmp::Ordering::Equal) || c == e { m += 1; temp2.push_str(e); } else { temp.push_str(&temp2.to_string()); temp2 = String::from("") ; temp.push_str(c); ends = end.graphemes(true); } }, None => { ends = end.graphemes(true); let opt = ends.next(); match opt { Some(e) => { if (ignore_case && c.to_uppercase().cmp(&e.to_uppercase()) == std::cmp::Ordering::Equal) || c == e { m += 1; temp2.push_str(e); } else { temp.push_str(&temp2.to_string()); temp2 = String::from("") ; temp.push_str(c); ends = end.graphemes(true); } }, None => { } } } } if temp2.graphemes(true).count() == end.graphemes(true).count() { temp2 = String::from("") ; result.push(temp); if limit_one == true { return result; } started = false; temp = String::from("") ; } } } return result; }