Every permutation is a product of one or more cycles. Iterate over the cycles in the permutation, and convert each of those into a product of transpositions (swaps): https://en.wikipedia.org/wiki/Cyclic_permutation
| 400 | // in the permutation, and convert each of those into a product of |
| 401 | // transpositions (swaps): https://en.wikipedia.org/wiki/Cyclic_permutation |
| 402 | void PermuteNodesInPlace(GraphDef* graph, std::vector<int>* permutation, |
| 403 | bool invert_permutation) { |
| 404 | CHECK_EQ(graph->node_size(), permutation->size()); |
| 405 | std::vector<int> inv_perm(permutation->size(), 0); |
| 406 | if (invert_permutation) { |
| 407 | for (size_t n = 0; n < permutation->size(); ++n) { |
| 408 | inv_perm[(*permutation)[n]] = n; |
| 409 | } |
| 410 | permutation->swap(inv_perm); |
| 411 | } |
| 412 | for (std::size_t n = 0; n + 1 < permutation->size(); ++n) { |
| 413 | while (n != (*permutation)[n]) { |
| 414 | std::size_t r = (*permutation)[n]; |
| 415 | graph->mutable_node()->SwapElements(n, r); |
| 416 | std::swap((*permutation)[n], (*permutation)[r]); |
| 417 | } |
| 418 | } |
| 419 | } |
| 420 | |
| 421 | void DedupControlInputs(NodeDef* node) { |
| 422 | std::unordered_set<string> inputs; |
no test coverage detected