| 299 | } |
| 300 | |
| 301 | std::shared_ptr<Array> slice( |
| 302 | const std::shared_ptr<const Array>& arr, |
| 303 | int index0) { |
| 304 | // build a new shape dropping the first dimension |
| 305 | auto new_shape = std::vector<int64_t>(); |
| 306 | auto current_shape = arr->shape(); |
| 307 | |
| 308 | for (int i = 1; i < current_shape.size(); i++) { |
| 309 | new_shape.push_back(current_shape[i]); |
| 310 | } |
| 311 | |
| 312 | int64_t slice_size = (new_shape.size() > 0); |
| 313 | for (auto dim : new_shape) { |
| 314 | slice_size *= dim; |
| 315 | } |
| 316 | |
| 317 | // return a slice of this at the given index |
| 318 | auto item_size = arr->itemsize(); |
| 319 | auto new_pointer = ((char*)arr->data() + index0 * item_size * slice_size); |
| 320 | std::shared_ptr<void> new_data((void*)new_pointer, [](void*) {}); |
| 321 | return std::make_shared<Array>(arr->type(), new_shape, new_data); |
| 322 | } |
| 323 | |
| 324 | void copy( |
| 325 | const std::shared_ptr<Array>& dst, |