Replace all the arrays from the src values with the dst values in the tree
| 217 | |
| 218 | // Replace all the arrays from the src values with the dst values in the tree |
| 219 | void tree_replace( |
| 220 | nb::object& tree, |
| 221 | const std::vector<mx::array>& src, |
| 222 | const std::vector<mx::array>& dst) { |
| 223 | std::unordered_map<uintptr_t, mx::array> src_to_dst; |
| 224 | for (int i = 0; i < src.size(); ++i) { |
| 225 | src_to_dst.insert({src[i].id(), dst[i]}); |
| 226 | } |
| 227 | tree_visit_update(tree, [&](nb::handle node) { |
| 228 | auto arr = nb::cast<mx::array>(node); |
| 229 | if (auto it = src_to_dst.find(arr.id()); it != src_to_dst.end()) { |
| 230 | return nb::cast(it->second); |
| 231 | } |
| 232 | return nb::cast(arr); |
| 233 | }); |
| 234 | } |
| 235 | |
| 236 | std::vector<mx::array> tree_flatten(nb::handle tree, bool strict /* = true */) { |
| 237 | std::vector<mx::array> flat_tree; |