| 644 | |
| 645 | template <typename T> |
| 646 | void ShapeTree<T>::CopySubtreeFrom(const ShapeTree<T>& other, |
| 647 | const ShapeIndex& source_base_index, |
| 648 | const ShapeIndex& target_base_index) { |
| 649 | CHECK(ShapeUtil::Compatible( |
| 650 | ShapeUtil::GetSubshape(shape(), target_base_index), |
| 651 | ShapeUtil::GetSubshape(other.shape(), source_base_index))); |
| 652 | ForEachMutableElement([this, &other, &source_base_index, &target_base_index]( |
| 653 | const ShapeIndex& index, T* data) { |
| 654 | // Copy the data element only if index is in the |
| 655 | // subtree rooted at target_base_index. |
| 656 | for (int i = 0; i < target_base_index.size(); ++i) { |
| 657 | if (i >= index.size() || index[i] != target_base_index[i]) { |
| 658 | return; |
| 659 | } |
| 660 | } |
| 661 | // Construct source element index to copy from. |
| 662 | ShapeIndex source_index = source_base_index; |
| 663 | for (int i = target_base_index.size(); i < index.size(); ++i) { |
| 664 | source_index.push_back(index[i]); |
| 665 | } |
| 666 | *data = other.element(source_index); |
| 667 | }); |
| 668 | } |
| 669 | |
| 670 | template <typename T> |
| 671 | StatusOr<ShapeTree<T>> ShapeTree<T>::SubShapeTree( |