Skip to content

Commit

Permalink
added hpx::partial_sort
Browse files Browse the repository at this point in the history
Signed-off-by: Dikshant <dikshant.073@gmail.com>
  • Loading branch information
pingu-73 committed Aug 1, 2024
1 parent 779b4d8 commit 1ea64f2
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 0 deletions.
32 changes: 32 additions & 0 deletions hpx-sys/include/wrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -180,3 +180,35 @@ inline void hpx_merge(const rust::Vec<int32_t>& src1,
dest.push_back(item);
}
}

inline void hpx_partial_sort(rust::Vec<int32_t>& src, size_t last) {
std::vector<int32_t> cpp_vec(src.begin(), src.end());

hpx::partial_sort(hpx::execution::par,
cpp_vec.begin(),
cpp_vec.begin() + last,
cpp_vec.end());

src.clear();
src.reserve(cpp_vec.size());
for (const auto& item : cpp_vec) {
src.push_back(item);
}
}

inline void hpx_partial_sort_comp(rust::Vec<int32_t>& src, size_t last,
rust::Fn<bool(int32_t, int32_t)> comp) {
std::vector<int32_t> cpp_vec(src.begin(), src.end());

hpx::partial_sort(hpx::execution::par,
cpp_vec.begin(),
cpp_vec.begin() + last,
cpp_vec.end(),
[&](int32_t a, int32_t b) { return comp(a, b); });

src.clear();
src.reserve(cpp_vec.size());
for (const auto& item : cpp_vec) {
src.push_back(item);
}
}
62 changes: 62 additions & 0 deletions hpx-sys/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ pub mod ffi {
fn hpx_sort(src: &mut Vec<i32>);
fn hpx_sort_comp(src: &mut Vec<i32>, comp: fn(i32, i32) -> bool);
fn hpx_merge(src1: &Vec<i32>, src2: &Vec<i32>, dest: &mut Vec<i32>);
fn hpx_partial_sort(src: &mut Vec<i32>, last: usize);
fn hpx_partial_sort_comp(src: &mut Vec<i32>, last: usize, comp: fn(i32, i32) -> bool);
}
}

Expand Down Expand Up @@ -421,4 +423,64 @@ mod tests {
assert_eq!(result, 0);
}
}

#[test]
#[serial]
fn test_hpx_partial_sort() {
let (argc, mut argv) = create_c_args(&["test_hpx_partial_sort"]);

let hpx_main = |_argc: i32, _argv: *mut *mut c_char| -> i32 {
let mut vec = vec![5, 2, 8, 1, 9, 3, 7, 6, 4];
let last = 4;
println!("Before partial sort: {:?}", vec);

ffi::hpx_partial_sort(&mut vec, last);
println!("After partial sort: {:?}", vec);

// If first -> last elements are sorted
assert!(vec[..last].windows(2).all(|w| w[0] <= w[1]));

// If ele of sorted part <= ele of unsorted part
assert!(vec[..last]
.iter()
.all(|&x| vec[last..].iter().all(|&y| x <= y)));

ffi::finalize()
};

unsafe {
let result = ffi::init(hpx_main, argc, argv.as_mut_ptr());
assert_eq!(result, 0);
}
}

#[test]
#[serial]
fn test_hpx_partial_sort_comp() {
let (argc, mut argv) = create_c_args(&["test_hpx_partial_sort_comp"]);

let hpx_main = |_argc: i32, _argv: *mut *mut c_char| -> i32 {
let mut vec = vec![5, 2, 8, 1, 9, 3, 7, 6, 4];
let last = 4;
println!("Before partial sort: {:?}", vec);

ffi::hpx_partial_sort_comp(&mut vec, last, |a, b| b < a);
println!("After partial sort: {:?}", vec);

// If first -> last elements are sorted dec
assert!(vec[..last].windows(2).all(|w| w[0] >= w[1]));

// If ele of sorted part >= ele of unsorted part
assert!(vec[..last]
.iter()
.all(|&x| vec[last..].iter().all(|&y| x >= y)));

ffi::finalize()
};

unsafe {
let result = ffi::init(hpx_main, argc, argv.as_mut_ptr());
assert_eq!(result, 0);
}
}
}

0 comments on commit 1ea64f2

Please sign in to comment.