JuliaInterop / libcxxwrap-julia

C++ library for backing CxxWrap.jl
Other
84 stars 43 forks source link

Have `jlcxx::Array` be a bit more like a Container #62

Open rgcv opened 4 years ago

rgcv commented 4 years ago

Specifically, as described in cppreference. A minor change such as typedefing the container's value_type already allows the usage of std::back_inserter.

As an example of such usage, here's a use case of mine - calling a function requiring an OutputIterator as argument:

mod.method("oifunc", [](jlcxx::ArrayRef<int> is) {
  jlcxx::Array<int> res;
  oifunc(is.begin(), is.end(), std::back_inserter(res));
  return res;
});

Otherwise, I'd probably need to do something like the following (if I still want to return a julia array, i.e. jlcxx::Array):

mod.method("oifunc", [](jlcxx::ArrayRef<int> is) {
  std::vector<int> res;
  jlcxx::Array<int> jlres;

  oifunc(is.begin(), is.end(), std::back_inserter(res));

  auto it = res.begin();
  while (it != res.end()) jlres.push_back(*it++);
  return jlres;
});

EDIT: Assuming the following definition:

template<typename InputIterator, typename OutputIterator>
oifunc(InputIterator begin, InputIterator end, OutputIterator out);

I'm not sure if making types like jlcxx::Array closer to STL-like spec is in the horizon, seeing one could just leverage the already in-place STL capabilities and just using jlcxx::apply_stl<T>(). Nonetheless, it would make usage more practical.

Addendum: Albeit slightly unrelated, I think this issue pairs nicely with #58.