| 329 | // the literal. For array-shaped ShapeIndexes, this data structure holds the |
| 330 | // pointer to the memory allocated for the array data. |
| 331 | class Piece { |
| 332 | public: |
| 333 | // Returns the buffer holding the array data for this piece as an array |
| 334 | // slice. This piece must be array-shaped. |
| 335 | template <typename NativeT> |
| 336 | absl::Span<const NativeT> data() const; |
| 337 | template <typename NativeT> |
| 338 | absl::Span<NativeT> data(); |
| 339 | |
| 340 | // Returns the buffer holding the array data for this piece as a void*. This |
| 341 | // piece must be array-shaped. |
| 342 | void* untyped_data(); |
| 343 | const void* untyped_data() const; |
| 344 | |
| 345 | // Gets or sets an element in the array at the given index. The multi_index |
| 346 | // is CHECKed against the dimension sizes of the array. This piece must be |
| 347 | // array-shaped. |
| 348 | template <typename NativeT> |
| 349 | NativeT Get(absl::Span<const int64> index) const; |
| 350 | template <typename NativeT> |
| 351 | void Set(absl::Span<const int64> index, NativeT value); |
| 352 | |
| 353 | // Gets/sets the buffer holding the array data. |
| 354 | char* buffer() const { return buffer_; } |
| 355 | void set_buffer(char* buffer) { buffer_ = buffer; } |
| 356 | |
| 357 | // Gets or sets the subshape of this piece. This reference points to a |
| 358 | // subshape within the shape in the containing Literal (Literal::shape_). |
| 359 | const Shape& subshape() const { return *subshape_; } |
| 360 | void set_subshape(const Shape* subshape) { subshape_ = subshape; } |
| 361 | |
| 362 | // Returns the size in bytes of the buffer holding the array data. |
| 363 | int64 size_bytes() const { return ShapeUtil::ByteSizeOf(subshape()); } |
| 364 | |
| 365 | // Returns the number of elements in this piece's array. |
| 366 | int64 element_count() const { return ShapeUtil::ElementsIn(subshape()); } |
| 367 | |
| 368 | // Returns the child piece at 'index' of this piece. |
| 369 | Piece& child(int64 index) { return children_[index]; } |
| 370 | |
| 371 | // Adds a child piece to this piece's children. |
| 372 | void emplace_back(Piece child_piece) { |
| 373 | children_.emplace_back(std::move(child_piece)); |
| 374 | } |
| 375 | |
| 376 | // Returns the size of children pieces of this piece. |
| 377 | int64 children_size() { return children_.size(); } |
| 378 | |
| 379 | // Visitor functions that recursively traverses the piece and calls the |
| 380 | // given function at each child piece. The function has the type: |
| 381 | // void (const ShapeIndex& index, const Piece& piece) |
| 382 | template <typename Fn> |
| 383 | void ForEachSubpiece(const Fn& func) const { |
| 384 | ShapeIndex index; |
| 385 | return ForEachHelper( |
| 386 | [&func](const ShapeIndex& index, const Piece& piece) { |
| 387 | func(index, piece); |
| 388 | return Status::OK(); |
no outgoing calls
no test coverage detected