| 969 | // vactor and then copy the data over. |
| 970 | template <typename T> |
| 971 | class TensorVector { |
| 972 | public: |
| 973 | using value_type = T; |
| 974 | |
| 975 | const Tensor& tensor() { |
| 976 | if (!tensor_.has_value()) { |
| 977 | resize(0); |
| 978 | } |
| 979 | return *tensor_; |
| 980 | } |
| 981 | |
| 982 | int64 size() const { |
| 983 | return tensor_.has_value() ? tensor_->NumElements() : 0; |
| 984 | } |
| 985 | void resize(int64 new_size) { |
| 986 | DCHECK(!tensor_.has_value()); |
| 987 | tensor_ = Tensor(DataTypeToEnum<T>::v(), TensorShape({new_size})); |
| 988 | data_ = tensor_->flat<T>().data(); |
| 989 | } |
| 990 | T* data() { return data_; } |
| 991 | const T* data() const { return data_; } |
| 992 | |
| 993 | private: |
| 994 | // Use absl::optional to avoid calling the default constructor of Tensor |
| 995 | // unnecessarily. |
| 996 | absl::optional<Tensor> tensor_; |
| 997 | |
| 998 | // Cached pointer to the raw data inside the tensor. |
| 999 | T* data_ = nullptr; |
| 1000 | }; |
| 1001 | |
| 1002 | } // namespace |
| 1003 |
no test coverage detected