Encode the strings in 'array' into a contiguous buffer and return the base of the buffer. The caller takes ownership of the buffer.
| 241 | // Encode the strings in 'array' into a contiguous buffer and return the base of |
| 242 | // the buffer. The caller takes ownership of the buffer. |
| 243 | Status EncodePyBytesArray(PyArrayObject* array, tensorflow::int64 nelems, |
| 244 | size_t* size, void** buffer) { |
| 245 | // Compute bytes needed for encoding. |
| 246 | *size = 0; |
| 247 | TF_RETURN_IF_ERROR( |
| 248 | PyBytesArrayMap(array, [&size](const char* ptr, Py_ssize_t len) { |
| 249 | *size += sizeof(tensorflow::uint64) + |
| 250 | tensorflow::core::VarintLength(len) + len; |
| 251 | })); |
| 252 | // Encode all strings. |
| 253 | std::unique_ptr<char[]> base_ptr(new char[*size]); |
| 254 | char* base = base_ptr.get(); |
| 255 | char* data_start = base + sizeof(tensorflow::uint64) * nelems; |
| 256 | char* dst = data_start; // Where next string is encoded. |
| 257 | tensorflow::uint64* offsets = reinterpret_cast<tensorflow::uint64*>(base); |
| 258 | |
| 259 | TF_RETURN_IF_ERROR(PyBytesArrayMap( |
| 260 | array, [&data_start, &dst, &offsets](const char* ptr, Py_ssize_t len) { |
| 261 | *offsets = (dst - data_start); |
| 262 | offsets++; |
| 263 | dst = tensorflow::core::EncodeVarint64(dst, len); |
| 264 | memcpy(dst, ptr, len); |
| 265 | dst += len; |
| 266 | })); |
| 267 | CHECK_EQ(dst, base + *size); |
| 268 | *buffer = base_ptr.release(); |
| 269 | return Status::OK(); |
| 270 | } |
| 271 | |
| 272 | Status CopyTF_TensorStringsToPyArray(const TF_Tensor* src, uint64 nelems, |
| 273 | PyArrayObject* dst) { |
no test coverage detected