project_note
Clone
https://git.tahinli.com/Tahinli/project_note.git
0 watching·0 forks·Rust·3.6 MiB·c49f5e7·Tahinli·1 month ago·2 commits
| 1 | use std::{fmt::Debug, time::Instant}; |
| 2 | |
| 3 | pub fn minify_error<T, F, N>(site_num: u8, actions: Vec<T>, f: F, normalize: N) |
| 4 | where |
| 5 | F: Fn(u8, &mut [T]), |
| 6 | N: Fn(u8, &mut [T]) -> Vec<T>, |
| 7 | T: Clone + Debug, |
| 8 | { |
| 9 | std::panic::set_hook(Box::new(|_info| { |
| 10 | // ignore panic output |
| 11 | // println!("{:?}", _info); |
| 12 | })); |
| 13 | |
| 14 | let f_ref: *const _ = &f; |
| 15 | let f_ref: usize = f_ref as usize; |
| 16 | #[allow(clippy::redundant_clone)] |
| 17 | let mut actions_clone = actions.clone(); |
| 18 | let action_ref: usize = (&mut actions_clone) as *mut _ as usize; |
| 19 | #[allow(clippy::blocks_in_conditions)] |
| 20 | if std::panic::catch_unwind(|| { |
| 21 | // SAFETY: test |
| 22 | let f = unsafe { &*(f_ref as *const F) }; |
| 23 | // SAFETY: test |
| 24 | let actions_ref = unsafe { &mut *(action_ref as *mut Vec<T>) }; |
| 25 | f(site_num, actions_ref); |
| 26 | }) |
| 27 | .is_ok() |
| 28 | { |
| 29 | println!("No Error Found"); |
| 30 | return; |
| 31 | } |
| 32 | |
| 33 | let mut minified = actions.clone(); |
| 34 | let mut candidates = Vec::new(); |
| 35 | for i in 0..actions.len() { |
| 36 | let mut new = actions.clone(); |
| 37 | new.remove(i); |
| 38 | candidates.push(new); |
| 39 | } |
| 40 | |
| 41 | println!("Minifying..."); |
| 42 | let start = Instant::now(); |
| 43 | while let Some(candidate) = candidates.pop() { |
| 44 | let f_ref: *const _ = &f; |
| 45 | let f_ref: usize = f_ref as usize; |
| 46 | let mut actions_clone = candidate.clone(); |
| 47 | let action_ref: usize = (&mut actions_clone) as *mut _ as usize; |
| 48 | #[allow(clippy::blocks_in_conditions)] |
| 49 | if std::panic::catch_unwind(|| { |
| 50 | // SAFETY: test |
| 51 | let f = unsafe { &*(f_ref as *const F) }; |
| 52 | // SAFETY: test |
| 53 | let actions_ref = unsafe { &mut *(action_ref as *mut Vec<T>) }; |
| 54 | f(site_num, actions_ref); |
| 55 | }) |
| 56 | .is_err() |
| 57 | { |
| 58 | for i in 0..candidate.len() { |
| 59 | let mut new = candidate.clone(); |
| 60 | new.remove(i); |
| 61 | candidates.push(new); |
| 62 | } |
| 63 | if candidate.len() < minified.len() { |
| 64 | minified = candidate; |
| 65 | println!("New min len={}", minified.len()); |
| 66 | } |
| 67 | if candidates.len() > 40 { |
| 68 | candidates.drain(0..30); |
| 69 | } |
| 70 | } |
| 71 | if start.elapsed().as_secs() > 10 && minified.len() <= 4 { |
| 72 | break; |
| 73 | } |
| 74 | if start.elapsed().as_secs() > 60 { |
| 75 | break; |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | let minified = normalize(site_num, &mut minified); |
| 80 | println!( |
| 81 | "Old Length {}, New Length {}", |
| 82 | actions.len(), |
| 83 | minified.len() |
| 84 | ); |
| 85 | dbg!(&minified); |
| 86 | if actions.len() > minified.len() { |
| 87 | minify_error(site_num, minified, f, normalize); |
| 88 | } |
| 89 | } |