| 91 | }; |
| 92 | |
| 93 | static bool compute_common_dim(std::vector<std::size_t>& cd_dims, |
| 94 | common_dim_state& state1, |
| 95 | common_dim_state& state2) |
| 96 | { |
| 97 | assert(state1.get() < state2.get()); |
| 98 | auto d2 = state2.get(); |
| 99 | auto dims = state1.dims_for(d2); |
| 100 | auto naxes = distance(dims); |
| 101 | |
| 102 | if(naxes == 0) |
| 103 | return false; |
| 104 | |
| 105 | // Check if state1 has a remainder from previous split |
| 106 | bool has_remainder = (state1.rem != 1); |
| 107 | |
| 108 | // Compute the product of dimensions, adjusting for remainder if needed |
| 109 | auto n = elements(dims); |
| 110 | if(has_remainder and naxes > 0) |
| 111 | { |
| 112 | n = n / *dims.begin() * (*dims.begin() / state1.rem); |
| 113 | } |
| 114 | |
| 115 | // If not divisible then we can't compute a common dim |
| 116 | if((d2 % n) != 0) |
| 117 | return false; |
| 118 | |
| 119 | auto rem = d2 / n; |
| 120 | auto start_pos = cd_dims.size(); |
| 121 | |
| 122 | // Add axes mappings |
| 123 | if(has_remainder) |
| 124 | { |
| 125 | // state1: dimension was split, keep axes together |
| 126 | state1.add_axes(naxes, start_pos); |
| 127 | // state2: axes should include the previous remainder dimension |
| 128 | state2.add_axes(rem == 1 ? naxes : naxes + 1, start_pos - 1); |
| 129 | } |
| 130 | else |
| 131 | { |
| 132 | // state1: separate axes for each dimension |
| 133 | state1.add_multi_axes(naxes, start_pos); |
| 134 | // state2: normal axes mapping |
| 135 | state2.add_axes(rem == 1 ? naxes : naxes + 1, start_pos); |
| 136 | } |
| 137 | |
| 138 | // Add dimensions to cd_dims |
| 139 | if(has_remainder and naxes > 0) |
| 140 | { |
| 141 | // Adjust the first dimension by dividing by the remainder |
| 142 | cd_dims.push_back(*dims.begin() / state1.rem); |
| 143 | cd_dims.insert(cd_dims.end(), std::next(dims.begin()), dims.end()); |
| 144 | } |
| 145 | else |
| 146 | { |
| 147 | cd_dims.insert(cd_dims.end(), dims.begin(), dims.end()); |
| 148 | } |
| 149 | |
| 150 | // Add remainder dimension if needed |
no test coverage detected