| 165 | } |
| 166 | |
| 167 | void Array::reshape(const std::vector<int64_t>& shape) { |
| 168 | std::vector<int64_t> new_shape = shape; |
| 169 | int64_t new_size = 1; |
| 170 | int infer_dim = -1; |
| 171 | for (int dim = 0; dim < new_shape.size(); dim++) { |
| 172 | if (new_shape[dim] < 0) { |
| 173 | if (infer_dim >= 0) { |
| 174 | throw std::runtime_error("Array: can infer only one dimension"); |
| 175 | } else { |
| 176 | infer_dim = dim; |
| 177 | } |
| 178 | } else { |
| 179 | new_size *= new_shape[dim]; |
| 180 | } |
| 181 | } |
| 182 | int64_t old_size = size(); |
| 183 | if (infer_dim >= 0) { |
| 184 | if (old_size % new_size == 0) { |
| 185 | new_shape[infer_dim] = old_size / new_size; |
| 186 | new_size *= new_shape[infer_dim]; |
| 187 | } else { |
| 188 | throw std::runtime_error( |
| 189 | "Array: cannot infer dimension: incompatible shape provided"); |
| 190 | } |
| 191 | } |
| 192 | if (old_size != new_size) { |
| 193 | throw std::runtime_error("Array: incompatible shape provided"); |
| 194 | } |
| 195 | shape_ = new_shape; |
| 196 | } |
| 197 | |
| 198 | Array::~Array() {} |
| 199 | |