py::object MapNestedImpl()

in rlmeta/cc/nested_utils.cc [47:78]


py::object MapNestedImpl(Function func, const py::object& obj) {
  if (py::isinstance<py::tuple>(obj)) {
    const py::tuple src = py::reinterpret_borrow<py::tuple>(obj);
    const int64_t n = src.size();
    py::tuple dst(n);
    for (int64_t i = 0; i < n; ++i) {
      dst[i] = MapNestedImpl(func, src[i]);
    }
    return std::move(dst);
  }

  if (py::isinstance<py::list>(obj)) {
    const py::list src = py::reinterpret_borrow<py::list>(obj);
    const int64_t n = src.size();
    py::list dst(n);
    for (int64_t i = 0; i < n; ++i) {
      dst[i] = MapNestedImpl(func, src[i]);
    }
    return std::move(dst);
  }

  if (py::isinstance<py::dict>(obj)) {
    const py::dict src = py::reinterpret_borrow<py::dict>(obj);
    py::dict dst;
    for (const auto [k, v] : src) {
      dst[k] = MapNestedImpl(func, py::reinterpret_borrow<py::object>(v));
    }
    return std::move(dst);
  }

  return func(obj);
}