-
Notifications
You must be signed in to change notification settings - Fork 55
Implement SimpleArray member functions for searching part 2
#535
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
pchsu-hsupc
wants to merge
1
commit into
solvcon:master
Choose a base branch
from
pchsu-hsupc:issue519_feat2
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -292,6 +292,11 @@ class SimpleArrayMixinSearch | |
| } | ||
| return max_index; | ||
| } | ||
|
|
||
| SimpleArray<uint64_t> argwhere() const; | ||
|
|
||
| SimpleArray<uint64_t> argwhere(std::function<bool(value_type const &)> const & condition) const; | ||
| A where(std::function<bool(value_type const &)> const & condition, A const & other) const; | ||
| }; /* end class SimpleArrayMixinSearch */ | ||
|
|
||
| } /* end namespace detail */ | ||
|
|
@@ -1032,6 +1037,69 @@ A detail::SimpleArrayMixinSort<A, T>::take_along_axis_simd(SimpleArray<I> const | |
| return ret; | ||
| } | ||
|
|
||
| template <typename A, typename T> | ||
| SimpleArray<uint64_t> detail::SimpleArrayMixinSearch<A, T>::argwhere() const | ||
| { | ||
| auto default_condition = [](value_type const & x) | ||
| { | ||
| return x != value_type(); | ||
| }; | ||
| return this->argwhere(default_condition); | ||
| } | ||
|
|
||
| template <typename A, typename T> | ||
| SimpleArray<uint64_t> detail::SimpleArrayMixinSearch<A, T>::argwhere(std::function<bool(value_type const &)> const & condition) const | ||
| { | ||
| auto athis = static_cast<A const *>(this); | ||
| uint64_t const array_size = athis->size(); | ||
| uint64_t const array_dim = athis->ndim(); | ||
| std::vector<uint64_t> indices; | ||
| for (uint64_t i = 0; i < array_size; ++i) | ||
| { | ||
| if (condition(athis->data(i))) | ||
| { | ||
| indices.push_back(i); | ||
| } | ||
| } | ||
|
|
||
| SimpleArray<uint64_t> coordinates(std::vector<size_t>{indices.size(), array_dim}); | ||
| auto coord = coordinates.begin(); | ||
|
|
||
| std::vector<uint64_t> product_of_dims(array_dim, 1); | ||
| for (size_t i = 1; i < array_dim; ++i) product_of_dims[i] = product_of_dims[i - 1] * athis->shape(i); | ||
| for (auto const & index : indices) | ||
| { | ||
| uint64_t remaining_index = index; | ||
| for (int i = array_dim - 1; i >= 0; --i) | ||
| { | ||
| *coord = remaining_index / product_of_dims[i]; | ||
| remaining_index = remaining_index % product_of_dims[i]; | ||
| ++coord; | ||
| } | ||
| } | ||
| return coordinates; | ||
| } | ||
|
|
||
| template <typename A, typename T> | ||
| A detail::SimpleArrayMixinSearch<A, T>::where(std::function<bool(value_type const &)> const & condition, A const & other) const | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| { | ||
| auto athis = static_cast<A const *>(this); | ||
| uint64_t const array_size = athis->size(); | ||
| A ret(athis->shape()); | ||
| for (uint64_t i = 0; i < array_size; ++i) | ||
| { | ||
| if (condition(athis->data(i))) | ||
| { | ||
| ret.data(i) = athis->data(i); | ||
| } | ||
| else | ||
| { | ||
| ret.data(i) = other.data(i); | ||
| } | ||
| } | ||
| return ret; | ||
| } | ||
|
|
||
| template <typename S> | ||
| using is_simple_array = std::is_same< | ||
| std::remove_reference_t<S>, | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -313,9 +313,23 @@ class MODMESH_PYTHON_WRAPPER_VISIBILITY WrapSimpleArray | |
| (*this) | ||
| .def("argmin", &wrapped_type::argmin) | ||
| .def("argmax", &wrapped_type::argmax) | ||
| .def( | ||
| "argwhere", | ||
| [](wrapped_type const & self, std::function<bool(value_type const &)> const & condition) | ||
| { | ||
| if (!condition) | ||
| { | ||
| return py::cast(self.argwhere()); | ||
| } | ||
| return py::cast(self.argwhere(condition)); | ||
| }, | ||
| py::arg("condition") = py::none()) | ||
| .def( | ||
| "where", | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Numpy's where() says that when only condition is provided, |
||
| [](wrapped_type const & self, std::function<bool(value_type const &)> const & condition, wrapped_type const & other) | ||
| { return py::cast(self.where(condition, other)); }) | ||
| // | ||
| ; | ||
|
|
||
| return *this; | ||
| } | ||
| }; /* end class WrapSimpleArray */ | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
std::vectorto store the indices of elements that satisfy the condition.coordinatesand filling the dimensions's information.