Skip to content

Commit e750212

Browse files
Googlercopybara-github
authored andcommitted
Migrate from std::string to tensorflow::tstring.
Note that during the transition period tstring is typedef'ed to std::string. See: tensorflow/community#91 PiperOrigin-RevId: 281163678 Change-Id: I20ac40a58b48113c76103011c5061283d434558b
1 parent 3f4c82a commit e750212

File tree

6 files changed

+24
-21
lines changed

6 files changed

+24
-21
lines changed

tensorflow_compression/cc/kernels/range_coder.cc

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,13 @@ limitations under the License.
3030

3131
namespace tensorflow_compression {
3232
using tensorflow::int32;
33-
using tensorflow::string;
33+
using tensorflow::tstring;
3434
using tensorflow::uint32;
3535
using tensorflow::uint64;
3636
using tensorflow::uint8;
3737

3838
void RangeEncoder::Encode(int32 lower, int32 upper, int precision,
39-
string* sink) {
39+
tstring* sink) {
4040
// Input requirement: 0 < precision < 16.
4141
DCHECK_GT(precision, 0);
4242
DCHECK_LE(precision, 16);
@@ -265,7 +265,7 @@ void RangeEncoder::Encode(int32 lower, int32 upper, int precision,
265265
}
266266
}
267267

268-
void RangeEncoder::Finalize(string* sink) {
268+
void RangeEncoder::Finalize(tstring* sink) {
269269
// Finalize the encode by writing out any number in the interval
270270
// [base, base + size).
271271
//
@@ -300,7 +300,7 @@ void RangeEncoder::Finalize(string* sink) {
300300
delay_ = 0;
301301
}
302302

303-
RangeDecoder::RangeDecoder(const string& source)
303+
RangeDecoder::RangeDecoder(const tstring& source)
304304
: current_(source.begin()), end_(source.end()) {
305305
Read16BitValue();
306306
Read16BitValue();

tensorflow_compression/cc/kernels/range_coder.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,12 @@ class RangeEncoder {
4545
// REQUIRES: 0 <= lower < upper <= 2^precision.
4646
// REQUIRES: 0 < precision <= 16.
4747
void Encode(tensorflow::int32 lower, tensorflow::int32 upper, int precision,
48-
tensorflow::string* sink);
48+
tensorflow::tstring* sink);
4949

5050
// The encode may contain some under-determined values from previous encoding.
5151
// After Encode() calls, Finalize() must be called. Otherwise the encoded
5252
// string may not be decoded.
53-
void Finalize(tensorflow::string* sink);
53+
void Finalize(tensorflow::tstring* sink);
5454

5555
private:
5656
tensorflow::uint32 base_ = 0;
@@ -63,7 +63,7 @@ class RangeDecoder {
6363
public:
6464
// Holds a reference to `source`. The caller has to make sure that `source`
6565
// outlives the decoder object.
66-
explicit RangeDecoder(const tensorflow::string& source);
66+
explicit RangeDecoder(const tensorflow::tstring& source);
6767

6868
// Decodes a character from `source` using CDF. The size of `cdf` should be
6969
// one more than the number of the character in the alphabet.
@@ -96,8 +96,8 @@ class RangeDecoder {
9696
std::numeric_limits<tensorflow::uint32>::max();
9797
tensorflow::uint32 value_ = 0;
9898

99-
tensorflow::string::const_iterator current_;
100-
const tensorflow::string::const_iterator end_;
99+
tensorflow::tstring::const_iterator current_;
100+
const tensorflow::tstring::const_iterator end_;
101101
};
102102

103103
} // namespace tensorflow_compression

tensorflow_compression/cc/kernels/range_coder_test.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ void RangeEncodeDecodeTest(int precision, random::SimplePhilox* gen) {
6868
}
6969

7070
RangeEncoder encoder;
71-
string encoded;
71+
tensorflow::tstring encoded;
7272
double ideal_length = 0.0;
7373
for (uint8 x : data) {
7474
encoder.Encode(cdf[x], cdf[x + 1], precision, &encoded);
@@ -109,7 +109,7 @@ TEST(RangeCoderTest, Precision12To16) {
109109
TEST(RangeCoderTest, FinalizeState0) {
110110
constexpr int kPrecision = 2;
111111

112-
string output;
112+
tensorflow::tstring output;
113113
RangeEncoder encoder;
114114
encoder.Encode(0, 2, kPrecision, &output);
115115
encoder.Finalize(&output);

tensorflow_compression/cc/kernels/range_coding_kernels.cc

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ using tensorflow::string;
4949
using tensorflow::Tensor;
5050
using tensorflow::TensorShape;
5151
using tensorflow::TensorShapeUtils;
52+
using tensorflow::tstring;
5253
using tensorflow::TTypes;
5354
using tensorflow::uint32;
5455
using tensorflow::uint64;
@@ -204,7 +205,7 @@ class RangeEncodeOp : public OpKernel {
204205
Tensor* output_tensor;
205206
OP_REQUIRES_OK(context,
206207
context->allocate_output(0, TensorShape{}, &output_tensor));
207-
string* output = &output_tensor->scalar<string>()();
208+
tstring* output = &output_tensor->scalar<tstring>()();
208209

209210
switch (data_shape.size()) {
210211
#define RANGE_ENCODE_CASE(dims) \
@@ -235,7 +236,7 @@ class RangeEncodeOp : public OpKernel {
235236
absl::Span<const int64> data_shape,
236237
TTypes<int32>::ConstMatrix cdf,
237238
absl::Span<const int64> cdf_shape,
238-
string* output) const {
239+
tstring* output) const {
239240
const int64 data_size = data.size();
240241
const int64 cdf_size = cdf.size();
241242
const int64 chip_size = cdf.dimension(1);
@@ -313,7 +314,7 @@ class RangeDecodeOp : public OpKernel {
313314
OP_REQUIRES_OK(
314315
context, MergeAxes(output_shape, cdf.shape(), &data_shape, &cdf_shape));
315316

316-
const string& encoded = encoded_tensor.scalar<string>()();
317+
const tstring& encoded = encoded_tensor.scalar<tstring>()();
317318

318319
Tensor* output;
319320
OP_REQUIRES_OK(context, context->allocate_output(0, output_shape, &output));
@@ -347,7 +348,7 @@ class RangeDecodeOp : public OpKernel {
347348
absl::Span<const int64> output_shape,
348349
TTypes<int32>::ConstMatrix cdf,
349350
absl::Span<const int64> cdf_shape,
350-
const string& encoded) const {
351+
const tstring& encoded) const {
351352
BroadcastRange<int16, int32, N> view{output.data(), output_shape,
352353
cdf.data(), cdf_shape};
353354

tensorflow_compression/cc/kernels/range_coding_kernels_test.cc

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ using tensorflow::Status;
5858
using tensorflow::Tensor;
5959
using tensorflow::TensorShape;
6060
using tensorflow::TensorShapeUtils;
61+
using tensorflow::tstring;
6162
using tensorflow::TTypes;
6263

6364
int LogUniform(random::SimplePhilox* gen, uint32 n) {
@@ -470,12 +471,12 @@ TEST_F(RangeCoderOpsTest, EncoderDebug) {
470471
TEST_F(RangeCoderOpsTest, DecoderDebug) {
471472
RangeEncoder encoder;
472473

473-
string encoded_string;
474+
tstring encoded_string;
474475
encoder.Encode(16, 18, 5, &encoded_string);
475476
encoder.Finalize(&encoded_string);
476477

477478
Tensor encoded(DT_STRING, {});
478-
encoded.scalar<string>()().swap(encoded_string);
479+
encoded.scalar<tstring>()() = std::move(encoded_string);
479480

480481
Tensor shape(DT_INT32, {0});
481482

tensorflow_compression/cc/kernels/unbounded_index_range_coding_kernels.cc

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ using tensorflow::string;
4949
using tensorflow::Tensor;
5050
using tensorflow::TensorShape;
5151
using tensorflow::TensorShapeUtils;
52+
using tensorflow::tstring;
5253
using tensorflow::TTypes;
5354
using tensorflow::uint32;
5455
using tensorflow::uint64;
@@ -181,15 +182,15 @@ class UnboundedIndexRangeEncodeOp : public OpKernel {
181182

182183
RangeEncodeImpl(data.flat<int32>(), index.flat<int32>(),
183184
cdf.matrix<int32>(), cdf_size.vec<int32>(),
184-
offset.vec<int32>(), &output->flat<string>()(0));
185+
offset.vec<int32>(), &output->flat<tstring>()(0));
185186
}
186187

187188
private:
188189
void RangeEncodeImpl(TTypes<int32>::ConstFlat data,
189190
TTypes<int32>::ConstFlat index,
190191
TTypes<int32>::ConstMatrix cdf,
191192
TTypes<int32>::ConstVec cdf_size,
192-
TTypes<int32>::ConstVec offset, string* output) const {
193+
TTypes<int32>::ConstVec offset, tstring* output) const {
193194
RangeEncoder encoder;
194195

195196
DCHECK_GE(cdf.dimension(1), 2);
@@ -300,7 +301,7 @@ class UnboundedIndexRangeDecodeOp : public OpKernel {
300301
OP_REQUIRES_OK(
301302
context, RangeDecodeImpl(output->flat<int32>(), index.flat<int32>(),
302303
cdf.matrix<int32>(), cdf_size.vec<int32>(),
303-
offset.vec<int32>(), encoded.flat<string>()));
304+
offset.vec<int32>(), encoded.flat<tstring>()));
304305
}
305306

306307
private:
@@ -309,7 +310,7 @@ class UnboundedIndexRangeDecodeOp : public OpKernel {
309310
TTypes<int32>::ConstMatrix cdf,
310311
TTypes<int32>::ConstVec cdf_size,
311312
TTypes<int32>::ConstVec offset,
312-
TTypes<string>::ConstFlat encoded) const {
313+
TTypes<tstring>::ConstFlat encoded) const {
313314
RangeDecoder decoder(encoded(0));
314315

315316
DCHECK_GE(cdf.dimension(1), 2);

0 commit comments

Comments
 (0)