| 268 | } |
| 269 | |
| 270 | Status TensorSlice::SliceTensorShape(const TensorShape& shape, |
| 271 | TensorShape* result_shape) const { |
| 272 | result_shape->Clear(); |
| 273 | // Mismatching ranks: we can't apply the slice at all. |
| 274 | if (shape.dims() != dims()) { |
| 275 | return errors::Internal("Mismatching ranks: shape = ", shape.DebugString(), |
| 276 | ", slice = ", DebugString()); |
| 277 | } |
| 278 | for (int d = 0; d < dims(); ++d) { |
| 279 | if (IsFullAt(d)) { |
| 280 | result_shape->AddDim(shape.dim_size(d)); |
| 281 | } else { |
| 282 | // Check if the extent applies to the dimension |
| 283 | if (end(d) <= shape.dim_size(d)) { |
| 284 | // Yes: the end is within the range of the dim -- we adjust the result |
| 285 | // shape so that its size along this dimension is the length of the |
| 286 | // slice. |
| 287 | result_shape->AddDim(length(d)); |
| 288 | } else { |
| 289 | // The extent doesn't apply to the dimension |
| 290 | result_shape->Clear(); |
| 291 | return errors::Internal("Extent in dimension ", d, |
| 292 | " out of bounds: shape = ", shape.DebugString(), |
| 293 | ", slice = ", DebugString()); |
| 294 | } |
| 295 | } |
| 296 | } |
| 297 | // If we are here, we have successfully applied the shape. |
| 298 | return Status::OK(); |
| 299 | } |
| 300 | |
| 301 | const int64 TensorSlice::kFullExtent = -1; |
| 302 | |