Set the corresponding value from line or tokens based on 'index' into the tensor 't'. The value is transformed to the given data type 'dtype'.
| 193 | // Set the corresponding value from line or tokens based on 'index' into the |
| 194 | // tensor 't'. The value is transformed to the given data type 'dtype'. |
| 195 | Status SetValue(const string& line, const std::vector<string>& tokens, |
| 196 | int64 index, Tensor* tensor) { |
| 197 | if (index == kLineNumber) { |
| 198 | tensor->flat<int64>()(0) = next_id_; |
| 199 | return Status::OK(); |
| 200 | } |
| 201 | const string& token = (index == kWholeLine) ? line : tokens[index]; |
| 202 | const DataType& dtype = tensor->dtype(); |
| 203 | switch (dtype) { |
| 204 | case DT_INT32: { |
| 205 | int32 value; |
| 206 | if (!strings::safe_strto32(token.c_str(), &value)) { |
| 207 | valid_ = false; |
| 208 | return errors::InvalidArgument("Field ", token, " in line ", next_id_, |
| 209 | " is not a valid int32."); |
| 210 | } |
| 211 | tensor->flat<int32>()(0) = value; |
| 212 | } break; |
| 213 | case DT_INT64: { |
| 214 | int64 value; |
| 215 | if (!strings::safe_strto64(token.c_str(), &value)) { |
| 216 | valid_ = false; |
| 217 | return errors::InvalidArgument("Field ", token, " in line ", next_id_, |
| 218 | " is not a valid int64."); |
| 219 | } |
| 220 | tensor->flat<int64>()(0) = value; |
| 221 | } break; |
| 222 | case DT_FLOAT: { |
| 223 | float value; |
| 224 | if (!strings::safe_strtof(token.c_str(), &value)) { |
| 225 | valid_ = false; |
| 226 | return errors::InvalidArgument("Field ", token, " in line ", next_id_, |
| 227 | " is not a valid float."); |
| 228 | } |
| 229 | tensor->flat<float>()(0) = value; |
| 230 | } break; |
| 231 | case DT_DOUBLE: { |
| 232 | double value; |
| 233 | if (!strings::safe_strtod(token.c_str(), &value)) { |
| 234 | valid_ = false; |
| 235 | return errors::InvalidArgument("Field ", token, " in line ", next_id_, |
| 236 | " is not a valid double."); |
| 237 | } |
| 238 | tensor->flat<double>()(0) = value; |
| 239 | } break; |
| 240 | case DT_STRING: |
| 241 | tensor->flat<tstring>()(0) = token; |
| 242 | break; |
| 243 | default: |
| 244 | valid_ = false; |
| 245 | return errors::InvalidArgument("Data type ", DataTypeString(dtype), |
| 246 | " not supported."); |
| 247 | } |
| 248 | return Status::OK(); |
| 249 | } |
| 250 | |
| 251 | TF_DISALLOW_COPY_AND_ASSIGN(TextFileLineIterator); |
| 252 | }; |
nothing calls this directly
no test coverage detected