From 354b234d02f35a017d4d28ae133b200a5534db2d Mon Sep 17 00:00:00 2001 From: Dikshant Date: Wed, 31 Jul 2024 11:44:05 +0530 Subject: [PATCH] added hpx::sort Signed-off-by: Dikshant --- hpx-sys/include/wrapper.h | 12 ++++++++++++ hpx-sys/src/lib.rs | 19 +++++++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/hpx-sys/include/wrapper.h b/hpx-sys/include/wrapper.h index f8746c5..abcf994 100644 --- a/hpx-sys/include/wrapper.h +++ b/hpx-sys/include/wrapper.h @@ -136,3 +136,15 @@ inline int64_t hpx_find(const rust::Vec& src, int32_t value) { } return -1; } + +inline void hpx_sort(rust::Vec& src) { + std::vector cpp_vec(src.begin(), src.end()); + + hpx::sort(hpx::execution::par, cpp_vec.begin(), cpp_vec.end()); + + src.clear(); + src.reserve(cpp_vec.size()); + for (const auto& item : cpp_vec) { + src.push_back(item); + } +} diff --git a/hpx-sys/src/lib.rs b/hpx-sys/src/lib.rs index 1ee4a6a..5b45748 100644 --- a/hpx-sys/src/lib.rs +++ b/hpx-sys/src/lib.rs @@ -27,6 +27,7 @@ pub mod ffi { fn hpx_equal(slice1: &[i32], slice2: &[i32]) -> bool; fn hpx_fill(src: &mut Vec, value: i32); // will only work for linear vectors fn hpx_find(src: &Vec, value: i32) -> i64; + fn hpx_sort(src: &mut Vec); } } @@ -347,4 +348,22 @@ mod tests { assert_eq!(result, 0); } } + + #[test] + #[serial] + fn test_hpx_sort() { + let (argc, mut argv) = create_c_args(&["test_hpx_sort"]); + + let hpx_main = |_argc: i32, _argv: *mut *mut c_char| -> i32 { + let mut src = vec![5, 2, 8, 1, 9, 3, 7, 6, 4]; + ffi::hpx_sort(&mut src); + assert_eq!(src, vec![1, 2, 3, 4, 5, 6, 7, 8, 9]); + ffi::finalize() + }; + + unsafe { + let result = ffi::init(hpx_main, argc, argv.as_mut_ptr()); + assert_eq!(result, 0); + } + } }