Skip to content

Commit

Permalink
Make return array of argsort into uint64_t and wrap argsort
Browse files Browse the repository at this point in the history
In order to make the datatype of return value of SimpleArray::argsort
compatible to the wrapped python datatypes, the return value is changed
to SimpleArray<uint64_t>.
Function argsort is also wrapped in this commit.
  • Loading branch information
KHLee529 committed Jan 22, 2025
1 parent 94d78d6 commit 00bc2b6
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 6 deletions.
12 changes: 6 additions & 6 deletions cpp/modmesh/buffer/SimpleArray.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -595,28 +595,28 @@ class SimpleArray
std::sort(begin(), end());
}

SimpleArray<size_t> argsort(void)
SimpleArray<uint64_t> argsort(void)
{
if (ndim() != 1){
throw std::runtime_error("SimpleArray: Sorting is only supported in 1D array.");
}

SimpleArray<size_t> ret(shape());
SimpleArray<uint64_t> ret(shape());

{ // Return array initialization
size_t cnt = 0;
std::for_each(ret.begin(), ret.end(), [&cnt](size_t &v){v = cnt++;});
uint64_t cnt = 0;
std::for_each(ret.begin(), ret.end(), [&cnt](uint64_t &v){v = cnt++;});
}

value_type const *buf = body();
auto cmp = [buf](size_t a, size_t b) {
auto cmp = [buf](uint64_t a, uint64_t b) {
return buf[a] < buf[b];
};
std::sort(ret.begin(), ret.end(), cmp);
return ret;
}

void apply_argsort(SimpleArray<size_t> const &sorted_args)
void apply_argsort(SimpleArray<uint64_t> const &sorted_args)
{
if (ndim() != 1 || sorted_args.ndim() != 1){
throw std::runtime_error("SimpleArray: Sorting is only supported in 1D array.");
Expand Down
4 changes: 4 additions & 0 deletions cpp/modmesh/buffer/pymod/wrap_SimpleArray.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,10 @@ class MODMESH_PYTHON_WRAPPER_VISIBILITY WrapSimpleArray
"sort",
[](wrapped_type & self)
{ self.sort(); })
.def(
"argsort",
[](wrapped_type & self)
{ return pybind11::cast(self.argsort()); })
.def_property_readonly("has_ghost", &wrapped_type::has_ghost)
.def_property("nghost", &wrapped_type::nghost, &wrapped_type::set_nghost)
.def_property_readonly("nbody", &wrapped_type::nbody)
Expand Down

0 comments on commit 00bc2b6

Please sign in to comment.