-
Notifications
You must be signed in to change notification settings - Fork 491
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
Storage container within RadiusResultSet #166
Comments
Fair point. |
A plan I had in mind was to introduce a new template parameter |
Exactly, I also thought of the same... |
I've noticed today it's possible to access the radius results from a C-like struct array with a bit of "cheating" as both
A small demonstration is shown below: /* radiusresult.cpp */
#include <iostream>
#include <type_traits>
#include <utility>
#include <vector>
typedef std::pair<int,double> RadiusResultItem;
struct RadiusResultItem_C {
int first;
double second;
};
int main(int argc, char const *argv[])
{
// both the std::pair struct and the C-style struct are standard layout
std::cout << "is_standard_layout<RadiusResultItem> " << std::is_standard_layout<RadiusResultItem>::value << '\n';
std::cout << "is_standard_layout<RadiusResultItem_C> " << std::is_standard_layout<RadiusResultItem_C>::value << '\n';
std::vector<RadiusResultItem> r(3);
r[0] = RadiusResultItem(1, 1.0);
r[1] = RadiusResultItem(2, 4.0);
r[2] = RadiusResultItem(3, 9.0);
// Cast the vector to a C-like struct
RadiusResultItem_C *rc;
rc = reinterpret_cast<RadiusResultItem_C *>(r.data());
std::cout << "r.size " << r.size() << '\n';
for (size_t i = 0; i < r.size(); i++) {
std::cout << "item[" << i << "]: " << rc[i].first << ' ' << rc[i].second << '\n';
}
return 0;
} $ g++ -Wall radiusresult.cpp
$ ./a.out
is_standard_layout<RadiusResultItem> 1
is_standard_layout<RadiusResultItem_C> 1
r.size 3
item[0]: 1 1
item[1]: 2 4
item[2]: 3 9 I'm not sure if it can be guaranteed that If not, the best one can do is document it clearly, and provide unit-tests to make sure, the layouts are compatible. I'm speaking here for mixed-language projects in C/Fortran/Python which would like to use nanoflann. |
Would it be possible to replace the
m_indices_dists
container withinRadiusResultSet
of typestd::vector<std::pair<IndexType, DistanceType>>
with something that interoperates better with C? Currently, any attempt to use nanoflann from a language other than C++ will probably have to make a copy of the results.The
KNNResultSet
is much easier to interoperate with since it simply uses two arrays as storage, i.e.One can easily pass a contiguous array from C, Python, or Fortran.
I understand the motivation for using
std::vector
because in the radius search we generally don't know the number of points that will be found in a query, hence the need for a dynamic container.One simple solution would be to simply replace
std::pair
with a struct:Then we can recover a C-interoperable array of structs via
m_indices_dists.data()
.The text was updated successfully, but these errors were encountered: