Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion rules/algorithm/tgt_refcount.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,11 @@ fn f8<T1: PartialOrd + Clone + ByteRepr>(a0: Ptr<T1>, a1: Ptr<T1>) -> Ptr<T1> {
let count = a1.get_offset() - a0.get_offset();
let max_index = PtrValueIter::new(&a0, count)
.enumerate()
.max_by(|(_, val_a), (_, val_b)| {
.max_by(|(idx_a, val_a), (idx_b, val_b)| {
val_a
.partial_cmp(val_b)
.unwrap_or(std::cmp::Ordering::Equal)
.then_with(|| idx_b.cmp(idx_a))
})
.map(|(idx, _)| idx)
.unwrap_or(0);
Expand Down
6 changes: 5 additions & 1 deletion rules/algorithm/tgt_unsafe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,11 @@ unsafe fn f8<T1: PartialOrd>(a0: *mut T1, a1: *mut T1) -> *mut T1 {
std::slice::from_raw_parts(a0, count)
.iter()
.enumerate()
.max_by(|(_, x), (_, y)| x.partial_cmp(y).unwrap_or(std::cmp::Ordering::Equal))
.max_by(|(idx_a, x), (idx_b, y)| {
x.partial_cmp(y)
.unwrap_or(std::cmp::Ordering::Equal)
.then_with(|| idx_b.cmp(idx_a))
})
.map(|(i, _)| a0.add(i))
.unwrap_or(a0)
}
Expand Down
8 changes: 6 additions & 2 deletions rules/vector/tgt_refcount.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,9 @@ fn f42(a0: Ptr<u8>, a1: Ptr<u8>) -> Ptr<u8> {
let __count = a1.get_offset() - a0.get_offset();
let max_index = PtrValueIter::new(&a0, __count)
.enumerate()
.max_by_key(|&(_, val)| val)
.max_by(|&(idx_a, val_a), &(idx_b, val_b)| {
val_a.cmp(&val_b).then_with(|| idx_b.cmp(&idx_a))
})
.map(|(idx, _)| idx)
.unwrap_or(0);

Expand Down Expand Up @@ -312,7 +314,9 @@ fn f94(a0: Ptr<u8>, a1: Ptr<u8>) -> Ptr<u8> {
let __count = a1.get_offset() - a0.get_offset();
let max_index = PtrValueIter::new(&a0, __count)
.enumerate()
.max_by_key(|&(_, val)| val)
.max_by(|&(idx_a, val_a), &(idx_b, val_b)| {
val_a.cmp(&val_b).then_with(|| idx_b.cmp(&idx_a))
})
.map(|(idx, _)| idx)
.unwrap_or(0);

Expand Down
8 changes: 6 additions & 2 deletions rules/vector/tgt_unsafe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,9 @@ unsafe fn f41<T1>(a0: &mut Vec<T1>) -> *const T1 {
unsafe fn f42<T1: Ord>(a0: *const T1, a1: *const T1) -> *const T1 {
core::slice::from_raw_parts(a0, (a1).offset_from(a0) as usize)
.iter()
.max()
.enumerate()
.max_by(|(idx_a, val_a), (idx_b, val_b)| val_a.cmp(val_b).then_with(|| idx_b.cmp(idx_a)))
.map(|(_, value)| value)
.unwrap()
}

Expand Down Expand Up @@ -407,7 +409,9 @@ unsafe fn f93<T1>(a0: &mut Vec<T1>) -> *const T1 {
unsafe fn f94<T1: Ord>(a0: *const T1, a1: *const T1) -> *const T1 {
core::slice::from_raw_parts(a0, (a1).offset_from(a0) as usize)
.iter()
.max()
.enumerate()
.max_by(|(idx_a, val_a), (idx_b, val_b)| val_a.cmp(val_b).then_with(|| idx_b.cmp(idx_a)))
.map(|(_, value)| value)
.unwrap()
}

Expand Down
3 changes: 3 additions & 0 deletions tests/unit/max.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,8 @@ int main() {
int r4 = std::min(*p2, x3);
int r5 = std::max(30, 40);
assert(r1 + r2 + r3 + r4 + r5 == 56);
int values[] = {1, 7, 7, 3};
int *max = std::max_element(values, values + 4);
assert(max == values + 1);
return 0;
}
19 changes: 19 additions & 0 deletions tests/unit/out/refcount/max.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,25 @@ fn main_0() -> i32 {
.read())
};
assert!((((((r1 + r2) + r3) + r4) + r5) == 56));
let values: Value<Box<[i32]>> = Rc::new(RefCell::new(Box::new([1, 7, 7, 3])));
let mut max: Ptr<i32> = {
let count = (values.as_pointer() as Ptr<i32>)
.offset((4) as isize)
.get_offset()
- (values.as_pointer() as Ptr<i32>).get_offset();
let max_index = PtrValueIter::new(&(values.as_pointer() as Ptr<i32>), count)
.enumerate()
.max_by(|(idx_a, val_a), (idx_b, val_b)| {
val_a
.partial_cmp(val_b)
.unwrap_or(std::cmp::Ordering::Equal)
.then_with(|| idx_b.cmp(idx_a))
})
.map(|(idx, _)| idx)
.unwrap_or(0);
(values.as_pointer() as Ptr<i32>) + max_index
};
assert!(({ (max).clone() } == { (values.as_pointer() as Ptr::<i32>).offset((1) as isize) }));
return 0;
}
pub fn __cpp2rust_init_globals() {}
18 changes: 18 additions & 0 deletions tests/unit/out/unsafe/max.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,24 @@ unsafe fn main_0() -> i32 {
})
};
assert!(((((((r1) + (r2)) + (r3)) + (r4)) + (r5)) == (56)));
let mut values: [i32; 4] = [1, 7, 7, 3];
let mut max: *mut i32 = {
let count = values
.as_mut_ptr()
.offset((4) as isize)
.offset_from(values.as_mut_ptr()) as usize;
std::slice::from_raw_parts(values.as_mut_ptr(), count)
.iter()
.enumerate()
.max_by(|(idx_a, x), (idx_b, y)| {
x.partial_cmp(y)
.unwrap_or(std::cmp::Ordering::Equal)
.then_with(|| idx_b.cmp(idx_a))
})
.map(|(i, _)| values.as_mut_ptr().add(i))
.unwrap_or(values.as_mut_ptr())
};
assert!(((max) == (values.as_mut_ptr().offset((1) as isize))));
return 0;
}
pub unsafe fn __cpp2rust_init_globals() {}
Loading