testing returned good

This commit is contained in:
lda committed 2025-01-20 22:26:10 +07:00
1 parent b3c0933cb5
commit 9799c5af2f
2 files changed
+39 -15

No files matched your search

+12 -12
View File
@@ -33,11 +33,11 @@ impl DB {
"the inner vector elements denote at what indexes the DB::vec(&self) vec value jumps up by one, and so any inner elements should not exceed self.len(), which some of them do"); "the inner vector elements denote at what indexes the DB::vec(&self) vec value jumps up by one, and so any inner elements should not exceed self.len(), which some of them do");
} }
fn verify(&self) { pub fn verify(&self) {
self.verify_len(); self.verify_len();
self.verify_increasing(); self.verify_increasing();
} }
fn new() -> Self { pub fn new() -> Self {
Default::default() Default::default()
} }
pub fn with_len(len: u32) -> Self { pub fn with_len(len: u32) -> Self {
@@ -46,7 +46,7 @@ impl DB {
..Default::default() ..Default::default()
} }
} }
fn push(&mut self, val: u32) { pub fn push(&mut self, val: u32) {
let last = self.last().unwrap_or_default(); let last = self.last().unwrap_or_default();
if val == last + 1 { if val == last + 1 {
self.inner.push(self.len()); self.inner.push(self.len());
@@ -64,7 +64,7 @@ impl DB {
None None
} }
} }
fn pop(&mut self) -> Option<u32> { pub fn pop(&mut self) -> Option<u32> {
let returnvalue = self.last(); let returnvalue = self.last();
if let Some(_) = returnvalue { if let Some(_) = returnvalue {
if let Some(bro) = self.inner.last() { if let Some(bro) = self.inner.last() {
@@ -90,13 +90,13 @@ impl DB {
// panic!("index {ind} >= len {selflen} which is ugly") // panic!("index {ind} >= len {selflen} which is ugly")
return None; return None;
} }
dbg!(&self.inner) (&self.inner)
.iter() .iter()
.rposition(|f| ind >= *f) // imagine got 7 > 4, and 4 is at index 2, so the value of 7 is 2 .rposition(|f| ind >= *f) // imagine got 7 > 4, and 4 is at index 2, so the value of 7 is 2
.map(|c| c as u32 + 1) // hack .map(|c| c as u32 + 1) // hack
.or(Some(0)) .or(Some(0))
} }
fn range_of(&self, ele: u32) -> Option<Range<u32>> { pub fn range_of(&self, ele: u32) -> Option<Range<u32>> {
if ele == 0 { if ele == 0 {
Some(0..*self.inner.get(0).unwrap_or(&self.len())) Some(0..*self.inner.get(0).unwrap_or(&self.len()))
} else if ele > self.last().unwrap_or_default() { } else if ele > self.last().unwrap_or_default() {
@@ -116,7 +116,7 @@ impl DB {
/// output: (two numbers that can be, can be the same), (should be the other number; should be appending) /// output: (two numbers that can be, can be the same), (should be the other number; should be appending)
/// ///
/// must check for in len() range by yourself /// must check for in len() range by yourself
fn available_values(&self, ind: u32) -> (Option<[u32; 2]>, Option<bool>) { pub fn available_values(&self, ind: u32) -> (Option<[u32; 2]>, Option<bool>) {
// let mut last_ind: u32 = 0; // let mut last_ind: u32 = 0;
for (&cur_ind, cur_val) in // ([0].iter()).chain for (&cur_ind, cur_val) in // ([0].iter()).chain
(self.inner.iter()).chain([self.len()].iter()).zip(1u32..) (self.inner.iter()).chain([self.len()].iter()).zip(1u32..)
@@ -192,13 +192,13 @@ impl DB {
/// override everything from ind on, that is if ind / val is good /// override everything from ind on, that is if ind / val is good
pub fn propagate_to_end(&mut self, ind: u32, val: u32) { pub fn propagate_to_end(&mut self, ind: u32, val: u32) {
println!("idn = {ind}, vala{val}"); // println!("idn = {ind}, vala{val}");
if ind >= self.len() { if ind >= self.len() {
panic!("uh oh out of bounds: {ind} >={}", self.len()) panic!("uh oh out of bounds: {ind} >={}", self.len())
} }
if self.len() > 0 { if self.len() > 0 {
if matches!(self.last(), Some(x) if dbg!(x) > 0) { if matches!(self.last(), Some(x) if (x) > 0) {
match dbg!(self.get(ind)) { match self.get(ind) {
Some(vecv) if vecv == val => { Some(vecv) if vecv == val => {
self.inner.truncate(val as usize); self.inner.truncate(val as usize);
} }
@@ -219,7 +219,7 @@ impl DB {
self.inner.push(ind); self.inner.push(ind);
} }
} }
println!("{:?}", self.inner) // println!("{:?}", self.inner)
} }
} }
pub fn vec(&self) -> Vec<u32> { pub fn vec(&self) -> Vec<u32> {
@@ -235,7 +235,7 @@ impl DB {
.flatten() .flatten()
.collect() .collect()
} }
fn try_from_vec(v: Vec<u32>) -> Option<Self> { pub fn try_from_vec(v: Vec<u32>) -> Option<Self> {
// let mut count = (0..).into_iter(); // let mut count = (0..).into_iter();
let len = v.len() as _; let len = v.len() as _;
let mut inner: Vec<u32> = vec![]; let mut inner: Vec<u32> = vec![];
+27 -3
View File
@@ -29,7 +29,7 @@ pub fn lcs_gen(str1: &str, str2: &str) -> Vec<Vec<u32>> {
db db
} }
pub fn lcs_gen_DB(str1: &str, str2: &str) -> Vec<DB> { pub fn lcs_gen_db(str1: &str, str2: &str) -> Vec<DB> {
let c1: Vec<char> = str1.chars().collect(); let c1: Vec<char> = str1.chars().collect();
let c1len = c1.len(); let c1len = c1.len();
let c2: Vec<char> = str2.chars().collect(); let c2: Vec<char> = str2.chars().collect();
@@ -65,13 +65,37 @@ pub fn lcs_gen_DB(str1: &str, str2: &str) -> Vec<DB> {
#[cfg(test)] #[cfg(test)]
mod test { mod test {
use super::{lcs_gen, lcs_gen_DB, DB}; use super::{lcs_gen, lcs_gen_db, DB};
#[test] #[test]
fn test_normal_case() { fn test_normal_case() {
lcs_gen("asjhdgasdhj", "gajhdgashjd"); lcs_gen("asjhdgasdhj", "gajhdgashjd");
// lcs_gen("", ""); // lcs_gen("", "");
} }
#[test]
fn time() {
use std::time::Instant;
{
let start = Instant::now();
test_wiki();
let duration = start.elapsed();
println!("for test_wiki is: {:?}", duration);
}
// 30/45 micro
{
let start = Instant::now();
test_db();
let duration = start.elapsed();
println!("for test_db is: {:?}", duration);
}
// 22 micro
// success to me
}
#[test] #[test]
fn test_wiki() { fn test_wiki() {
// 0 1 2 3 4 5 6 7 // 0 1 2 3 4 5 6 7
@@ -109,7 +133,7 @@ mod test {
vec![0, 1, 1, 2, 3, 3, 3, 4], vec![0, 1, 1, 2, 3, 3, 3, 4],
vec![0, 1, 2, 2, 3, 3, 3, 4], vec![0, 1, 2, 2, 3, 3, 3, 4],
]; ];
let c: Vec<Vec<u32>> = lcs_gen_DB("XMJYAUZ", "MZJAWXU") let c: Vec<Vec<u32>> = lcs_gen_db("XMJYAUZ", "MZJAWXU")
.iter() .iter()
.map(DB::vec) .map(DB::vec)
.collect(); .collect();