From f48bf131dd58ff60d8f1b763af4bee7a03c9f9b0 Mon Sep 17 00:00:00 2001 From: matexianjun Date: Tue, 3 Jan 2023 16:57:22 +0800 Subject: [PATCH] =?UTF-8?q?=E6=A0=BC=E5=BC=8F=E5=8C=96=EF=BC=8Cy=E4=B8=8D?= =?UTF-8?q?=E5=8A=A8=E7=82=B9=E7=AE=97=E5=AD=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- rust/src/implements/mod.rs | 4 +-- rust/src/main.rs | 54 ++++++++++++++++++++++++++++++---- rust/tests/integration_test.rs | 1 + 3 files changed, 52 insertions(+), 7 deletions(-) diff --git a/rust/src/implements/mod.rs b/rust/src/implements/mod.rs index 337b8fe..70352b8 100644 --- a/rust/src/implements/mod.rs +++ b/rust/src/implements/mod.rs @@ -12,8 +12,8 @@ mod q0006; mod q0007; mod q0008; mod q0009; -mod q0012; mod q0011; +mod q0012; mod q0013; mod q0014; mod q0020; @@ -51,9 +51,9 @@ mod q0101; mod q0102; mod q0104; mod q0108; -mod q0112; mod q0110; mod q0111; +mod q0112; mod q0118; mod q0119; mod q0121; diff --git a/rust/src/main.rs b/rust/src/main.rs index f9c9968..18aae97 100644 --- a/rust/src/main.rs +++ b/rust/src/main.rs @@ -1,3 +1,4 @@ +#![allow(dead_code)] //! 目前此文件用于临时测试、将来将提供所有题目的入口 const MASK_LOOKUP: [[u128; 5]; 5] = [ [0x00000000ffff0000000000000000ffff_u128, 0x000000000000000000000000ffff0000_u128, 0x000000000000ffff0000000000000000_u128, 0x00000000000000000000ffff00000000_u128, 0x0000000000000000ffff000000000000_u128], @@ -24,20 +25,63 @@ pub fn re_three(mut a: u128) -> (u32, u32, u32) { ((a >> 64) as u32, (a >> 32) as u32, a as u32) } -fn main() { +pub fn test_speed() { use std::time::Instant; let t1 = Instant::now(); let t = 10000000; - for _ in 0..t { - three(0, 0xffffffffu32, 0); + for i in 0..t { + three(i, 0xffffffffu32, i); } println!("{}", t1.elapsed().as_millis()); - /*let contents = std::fs::read_to_string("F:\\MyProject\\my_leetcode\\rust\\src\\implements\\mod.rs").expect("Something went wrong reading the file"); +} + +struct Func<'a, A, F>(&'a dyn Fn(Func<'a, A, F>, A) -> F); + +impl<'a, A, F> Clone for Func<'a, A, F> { + fn clone(&self) -> Self { + Self(self.0) + } +} + +impl<'a, A, F> Copy for Func<'a, A, F> {} + +impl<'a, A, F> Func<'a, A, F> { + fn call(&self, f: Func<'a, A, F>, x: A) -> F { + (self.0)(f, x) + } +} + +fn y(g: impl Fn(&dyn Fn(A) -> R, A) -> R) -> impl Fn(A) -> R { + move |x: A| { + (|f: Func, x| f.call(f, x))( + Func(&|f: Func, x: A| g(&|x: A| f.call(f, x), x)), + x, + ) + } +} + +fn test_y() { + let fact = y(|f: &dyn Fn(usize) -> usize, x: usize| match x { + 0usize => 1usize, + 1usize => 1usize, + _ => f(x - 2) + f(x - 1), + }); + println!("{}", fact(10usize)); +} + +fn reformat() { + let contents = std::fs::read_to_string("F:\\MyProject\\my_leetcode\\rust\\src\\implements\\mod.rs").expect("Something went wrong reading the file"); let mut arr = contents.split("\r\n").collect::>(); arr.sort_unstable(); for item in arr { println!("{}", item); - }*/ + } +} + +fn main() { + test_speed(); + test_y(); + reformat(); //println!("{:0128b}", a); } diff --git a/rust/tests/integration_test.rs b/rust/tests/integration_test.rs index e69de29..8b13789 100644 --- a/rust/tests/integration_test.rs +++ b/rust/tests/integration_test.rs @@ -0,0 +1 @@ +