Skip to content

Commit d5cf994

Browse files
Copilotnunoplopes
andauthored
Preserve first max_element result
Co-authored-by: nunoplopes <2998477+nunoplopes@users.noreply.github.com>
1 parent 195831c commit d5cf994

4 files changed

Lines changed: 19 additions & 6 deletions

File tree

‎rules/algorithm/tgt_refcount.rs‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,11 @@ fn f8<T1: PartialOrd + Clone + ByteRepr>(a0: Ptr<T1>, a1: Ptr<T1>) -> Ptr<T1> {
4747
let count = a1.get_offset() - a0.get_offset();
4848
let max_index = PtrValueIter::new(&a0, count)
4949
.enumerate()
50-
.max_by(|(_, val_a), (_, val_b)| {
50+
.max_by(|(idx_a, val_a), (idx_b, val_b)| {
5151
val_a
5252
.partial_cmp(val_b)
5353
.unwrap_or(std::cmp::Ordering::Equal)
54+
.then_with(|| idx_b.cmp(idx_a))
5455
})
5556
.map(|(idx, _)| idx)
5657
.unwrap_or(0);

‎rules/algorithm/tgt_unsafe.rs‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,11 @@ unsafe fn f8<T1: PartialOrd>(a0: *mut T1, a1: *mut T1) -> *mut T1 {
4949
std::slice::from_raw_parts(a0, count)
5050
.iter()
5151
.enumerate()
52-
.max_by(|(_, x), (_, y)| x.partial_cmp(y).unwrap_or(std::cmp::Ordering::Equal))
52+
.max_by(|(idx_a, x), (idx_b, y)| {
53+
x.partial_cmp(y)
54+
.unwrap_or(std::cmp::Ordering::Equal)
55+
.then_with(|| idx_b.cmp(idx_a))
56+
})
5357
.map(|(i, _)| a0.add(i))
5458
.unwrap_or(a0)
5559
}

‎rules/vector/tgt_refcount.rs‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,9 @@ fn f42(a0: Ptr<u8>, a1: Ptr<u8>) -> Ptr<u8> {
152152
let __count = a1.get_offset() - a0.get_offset();
153153
let max_index = PtrValueIter::new(&a0, __count)
154154
.enumerate()
155-
.max_by_key(|&(_, val)| val)
155+
.max_by(|&(idx_a, val_a), &(idx_b, val_b)| {
156+
val_a.cmp(&val_b).then_with(|| idx_b.cmp(&idx_a))
157+
})
156158
.map(|(idx, _)| idx)
157159
.unwrap_or(0);
158160

@@ -312,7 +314,9 @@ fn f94(a0: Ptr<u8>, a1: Ptr<u8>) -> Ptr<u8> {
312314
let __count = a1.get_offset() - a0.get_offset();
313315
let max_index = PtrValueIter::new(&a0, __count)
314316
.enumerate()
315-
.max_by_key(|&(_, val)| val)
317+
.max_by(|&(idx_a, val_a), &(idx_b, val_b)| {
318+
val_a.cmp(&val_b).then_with(|| idx_b.cmp(&idx_a))
319+
})
316320
.map(|(idx, _)| idx)
317321
.unwrap_or(0);
318322

‎rules/vector/tgt_unsafe.rs‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,9 @@ unsafe fn f41<T1>(a0: &mut Vec<T1>) -> *const T1 {
184184
unsafe fn f42<T1: Ord>(a0: *const T1, a1: *const T1) -> *const T1 {
185185
core::slice::from_raw_parts(a0, (a1).offset_from(a0) as usize)
186186
.iter()
187-
.max()
187+
.enumerate()
188+
.max_by(|(idx_a, val_a), (idx_b, val_b)| val_a.cmp(val_b).then_with(|| idx_b.cmp(idx_a)))
189+
.map(|(_, value)| value)
188190
.unwrap()
189191
}
190192

@@ -407,7 +409,9 @@ unsafe fn f93<T1>(a0: &mut Vec<T1>) -> *const T1 {
407409
unsafe fn f94<T1: Ord>(a0: *const T1, a1: *const T1) -> *const T1 {
408410
core::slice::from_raw_parts(a0, (a1).offset_from(a0) as usize)
409411
.iter()
410-
.max()
412+
.enumerate()
413+
.max_by(|(idx_a, val_a), (idx_b, val_b)| val_a.cmp(val_b).then_with(|| idx_b.cmp(idx_a)))
414+
.map(|(_, value)| value)
411415
.unwrap()
412416
}
413417

0 commit comments

Comments
 (0)