Compare commits

...
2 Commits
2 changed files with 73 additions and 49 deletions

No files matched your search

+8 -38
View File
@@ -108,44 +108,10 @@ pub fn lcs_gen_ms(str1: &str, str2: &str) -> Result<Vec<MonotonicSequence>, belp
Ok(db)
}
// #[allow(non_camel_case_types)]
// #[derive(Debug)]
// pub enum belper {
// DBE(DBError),
// None,
// }
// pub fn lcs_gen_dbai(str1: &str, str2: &str) -> Result<Vec<DB2>, belper> {
// let c1: Vec<char> = str1.chars().collect();
// let c1len = c1.len();
// let c2: Vec<char> = str2.chars().collect();
// let c2len = c2.len();
// let mut db: Vec<DB2> = vec![DB2::with_len(c2len as u32 + 1); c1len + 1];
// if c1len * c2len == 0 {
// // for i in &db {
// // println!("{i:?}")
// // }
// return Ok(db);
// }
// for i in 1..=c1len {
// for j in 1..=c2len {
// if c1[i - 1] == c2[j - 1] {
// let val = db[i - 1].get(j as u32 - 1).ok_or(belper::None)? + 1;
// db[i].push(val).map_err(|c| belper::DBE(c))?;
// } else {
// let val = db[i]
// .get(j as u32 - 1)
// .ok_or(belper::None)?
// .max(db[i - 1].get(j as _).ok_or(belper::None)?);
// db[i].push(val).map_err(|c| belper::DBE(c))?;
// }
// }
// }
// // for i in &db {
// // println!("{i:?}")
// // }
// Ok(db)
// }
#[allow(unused)]
pub fn lcs_traceback_vec(str1: &str, str2: &str, dp: Vec<Vec<u32>>) {
todo!()
}
#[cfg(test)]
mod test {
@@ -172,6 +138,7 @@ mod test {
println!("for test_wiki is: {:?}", duration);
}
// 30/45 micro how dropped to 8/15 im cooked
// 1.5/2
{
let start = Instant::now();
@@ -181,6 +148,7 @@ mod test {
println!("for test_db is: {:?}", duration);
}
// 22/30 micro
// 3.9/4.9
{
let start = Instant::now();
test_dbai();
@@ -189,8 +157,10 @@ mod test {
println!("for test_dbai is: {:?}", duration);
}
// 13/28 micro
// 3.5/5.8 // im SO COOKED
// success to me
// ABSOLUTE FAILURE
}
#[test]
+65 -11
View File
@@ -1,7 +1,7 @@
//! claude cooked
//! most of code is me? no most of code is claude
use std::cmp::Ordering;
use std::ops::Range;
#[derive(Default, Debug, Clone, PartialEq)]
pub struct MonotonicSequence {
@@ -57,14 +57,17 @@ impl MonotonicSequence {
/// `self.jumps[result of this function]` to get info
fn get_jump_index(&self, pos: usize) -> usize {
match self.jumps.binary_search_by(|(jump_point, _value)| {
if *jump_point <= pos {
Ordering::Less
} else {
Ordering::Greater
}
// if *jump_point <= pos {
// Ordering::Less
// } else {
// Ordering::Greater
// }
jump_point.cmp(&pos)
}) {
// we are literally At a jump point
// never happen point
Ok(index) => index,
// Below Zero
Err(0) => 0,
// debug_assert_ne!(index, 0); // confident
@@ -208,11 +211,11 @@ impl MonotonicSequence {
*ind += 1;
// if ind is self.length now then we need to pop
if *ind >= self.length {
let re_moved = self.jumps.pop(); // its just moved (+ 1) also its removed
let _re_moved = self.jumps.pop(); // its just moved (+ 1) also its removed
// get it
#[cfg(debug_assertions)]
{
if let Some(ind_and_val) = re_moved {
if let Some(ind_and_val) = _re_moved {
debug_assert_eq!(ind_and_val, (self.length, max))
}
}
@@ -283,9 +286,12 @@ impl MonotonicSequence {
}
pub fn validate(&self) -> bool {
// self.jumps.is_sorted() no
self.jumps.windows(2).all(|c| {
let [(lind, lv), (tind, tv)] = *c else {
unreachable!("windows is 2")
// unreachable!("windows is 2")
//1 or 0 items // also never reachable because of .all()
return true;
};
lind < tind && lv < tv
}) && self.jumps.iter().all(|&(ind, _)| ind < self.length)
@@ -428,17 +434,59 @@ impl MonotonicSequence {
}
pub fn propagate_to_end(&mut self, pos: usize, val: u32) -> Result<(), SequenceError> {
let len = self.len();
let Some(cur) = self.get(pos) else {
let Some(cur) = self.get(pos - 1) else {
return Ok(());
};
Self::can_set_mini(pos, val, (cur, cur + 1))?;
self.flatten_after(pos);
self.flatten_after(pos - 1);
if val == cur + 1 {
self.jumps.push((pos, val));
}
self.length = len;
Ok(())
}
pub fn range2(&self, item: u32) -> Option<Range<usize>> {
if self.is_empty() {
return None;
};
debug_assert!(self.validate());
let jump_index = (item - self.first().unwrap()) as usize;
let &(p, v) = self.jumps.get(jump_index)?;
assert_eq!(v, item);
let p2 = self.jumps.get(jump_index).map_or(self.len(), |(i, _)| *i);
return Some(p..p2);
}
// or you can do this:
pub fn range(&self, item: u32) -> Option<Range<usize>> {
debug_assert!(self.validate());
match self
.jumps
.binary_search_by(|(_indx, value)| value.cmp(&item))
{
Ok(f) => Some(Range {
start: self.jumps[f].0,
end: self
.jumps
.get(f + 1)
.map(|&(i, _)| i)
.unwrap_or(self.length),
}),
// Err(0) => None,
// Err(x) if x == self.len() => None,
// Range {
// start: self.jumps.first().map(|&(i, _)| i).unwrap_or(0),
// end: self.jumps.get(1).map(|&(i, _)| i).unwrap_or(self.length),
// },
Err(_) => None,
// no matching values
// Range {
// start: self.jumps[x - 1].0,
// end: self.jumps.get(x).map(|&(i, _)| i).unwrap_or(self.length),
// },
}
}
}
// ai gen testcases: not quite sure because short + not updated with new methods
@@ -580,4 +628,10 @@ mod tests {
Err(SequenceError::ValueTooHigh { .. })
));
}
#[test]
fn absolute_devastation() {
let c = [0, 0, 0, 1, 1, 1, 2, 2, 2, 3];
let mut db = MonotonicSequence::try_from_vec(&c).unwrap();
assert!(db.propagate_to_end(3, 2).is_err());
}
}