Skip to content

Commit

Permalink
removed redundant copy from find, fill, sort_comp
Browse files Browse the repository at this point in the history
Signed-off-by: Dikshant <[email protected]>
  • Loading branch information
pingu-73 committed Aug 20, 2024
1 parent e08516a commit eea1569
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 30 deletions.
34 changes: 8 additions & 26 deletions hpx-sys/include/wrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -93,28 +93,18 @@ inline bool hpx_equal(rust::Slice<const int32_t> src, rust::Slice<const int32_t>
);
}

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

hpx::fill(hpx::execution::par, cpp_vec.begin(), cpp_vec.end(), value);

src.clear();
src.reserve(cpp_vec.size());
for (const auto& item : cpp_vec) {
src.push_back(item);
}
inline void hpx_fill(rust::Slice<int32_t> src, int32_t value) {
hpx::fill(hpx::execution::par, src.begin(), src.end(), value);
}

inline int64_t hpx_find(const rust::Vec<int32_t>& src, int32_t value) {
std::vector<int32_t> cpp_vec(src.begin(), src.end());

inline int64_t hpx_find(rust::Slice<const int32_t> src, int32_t value) {
auto result = hpx::find(hpx::execution::par,
cpp_vec.begin(),
cpp_vec.end(),
src.begin(),
src.end(),
value);

if (result != cpp_vec.end()) {
return static_cast<int64_t>(std::distance(cpp_vec.begin(), result));
if (result != src.end()) {
return static_cast<int64_t>(std::distance(src.begin(), result));
}
return -1;
}
Expand All @@ -124,16 +114,8 @@ inline void hpx_sort(rust::Slice<int32_t> src) {
}

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

hpx::sort(hpx::execution::par, cpp_vec.begin(), cpp_vec.end(),
hpx::sort(hpx::execution::par, src.begin(), src.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);
}
}

inline void hpx_merge(const rust::Vec<int32_t>& src1,
Expand Down
12 changes: 8 additions & 4 deletions hpx-sys/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ pub mod ffi {
fn hpx_count_if(src: &Vec<i32>, pred: fn(i32) -> bool) -> i64;
fn hpx_ends_with(src: &[i32], dest: &[i32]) -> bool;
fn hpx_equal(slice1: &[i32], slice2: &[i32]) -> bool;
fn hpx_fill(src: &mut Vec<i32>, value: i32); // will only work for linear vectors
fn hpx_find(src: &Vec<i32>, value: i32) -> i64;
fn hpx_fill(src: &mut [i32], value: i32); // will only work for linear vectors
fn hpx_find(src: &[i32], value: i32) -> i64;
fn hpx_sort(src: &mut [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>);
Expand Down Expand Up @@ -73,8 +73,8 @@ pub fn count(vec: &Vec<i32>, value: i32) -> i64 {
ffi::hpx_count(vec, value)
}

pub fn find(vec: &Vec<i32>, value: i32) -> Option<usize> {
match ffi::hpx_find(vec, value) {
pub fn find(slice: &[i32], value: i32) -> Option<usize> {
match ffi::hpx_find(slice, value) {
-1 => None,
index => Some(index as usize),
}
Expand Down Expand Up @@ -325,6 +325,10 @@ mod tests {
ffi::hpx_fill(&mut v2, 7);
assert!(v2.iter().all(|&x| x == 7));

let mut v3: Vec<i32> = Vec::new();
ffi::hpx_fill(&mut v3, 100);
assert!(v3.is_empty());

ffi::finalize()
};

Expand Down

0 comments on commit eea1569

Please sign in to comment.