small addition SUNK COST FALLACY

This commit is contained in:
lda committed 2025-01-22 21:10:24 +07:00
1 parent cc939b19f5
commit f874a15317
2 files changed
+62 -8

No files matched your search

+4
View File
@@ -172,6 +172,7 @@ mod test {
println!("for test_wiki is: {:?}", duration); println!("for test_wiki is: {:?}", duration);
} }
// 30/45 micro how dropped to 8/15 im cooked // 30/45 micro how dropped to 8/15 im cooked
// 1.5/2
{ {
let start = Instant::now(); let start = Instant::now();
@@ -181,6 +182,7 @@ mod test {
println!("for test_db is: {:?}", duration); println!("for test_db is: {:?}", duration);
} }
// 22/30 micro // 22/30 micro
// 3.9/4.9
{ {
let start = Instant::now(); let start = Instant::now();
test_dbai(); test_dbai();
@@ -189,8 +191,10 @@ mod test {
println!("for test_dbai is: {:?}", duration); println!("for test_dbai is: {:?}", duration);
} }
// 13/28 micro // 13/28 micro
// 3.5/5.8 // im SO COOKED
// success to me // success to me
// ABSOLUTE FAILURE
} }
#[test] #[test]
+58 -8
View File
@@ -1,7 +1,7 @@
//! claude cooked //! claude cooked
//! most of code is me? no most of code is claude //! most of code is me? no most of code is claude
use std::cmp::Ordering; use std::ops::Range;
#[derive(Default, Debug, Clone, PartialEq)] #[derive(Default, Debug, Clone, PartialEq)]
pub struct MonotonicSequence { pub struct MonotonicSequence {
@@ -57,14 +57,17 @@ impl MonotonicSequence {
/// `self.jumps[result of this function]` to get info /// `self.jumps[result of this function]` to get info
fn get_jump_index(&self, pos: usize) -> usize { fn get_jump_index(&self, pos: usize) -> usize {
match self.jumps.binary_search_by(|(jump_point, _value)| { match self.jumps.binary_search_by(|(jump_point, _value)| {
if *jump_point <= pos { // if *jump_point <= pos {
Ordering::Less // Ordering::Less
} else { // } else {
Ordering::Greater // Ordering::Greater
} // }
jump_point.cmp(&pos)
}) { }) {
// we are literally At a jump point // we are literally At a jump point
// never happen point
Ok(index) => index, Ok(index) => index,
// Below Zero // Below Zero
Err(0) => 0, Err(0) => 0,
// debug_assert_ne!(index, 0); // confident // debug_assert_ne!(index, 0); // confident
@@ -428,17 +431,58 @@ impl MonotonicSequence {
} }
pub fn propagate_to_end(&mut self, pos: usize, val: u32) -> Result<(), SequenceError> { pub fn propagate_to_end(&mut self, pos: usize, val: u32) -> Result<(), SequenceError> {
let len = self.len(); let len = self.len();
let Some(cur) = self.get(pos) else { let Some(cur) = self.get(pos - 1) else {
return Ok(()); return Ok(());
}; };
Self::can_set_mini(pos, val, (cur, cur + 1))?; Self::can_set_mini(pos, val, (cur, cur + 1))?;
self.flatten_after(pos); self.flatten_after(pos - 1);
if val == cur + 1 { if val == cur + 1 {
self.jumps.push((pos, val)); self.jumps.push((pos, val));
} }
self.length = len; self.length = len;
Ok(()) 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 // ai gen testcases: not quite sure because short + not updated with new methods
@@ -580,4 +624,10 @@ mod tests {
Err(SequenceError::ValueTooHigh { .. }) 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());
}
} }