Home | History | Annotate | Download | only in graph
      1 /* Copyright 2015 The TensorFlow Authors. All Rights Reserved.
      2 
      3 Licensed under the Apache License, Version 2.0 (the "License");
      4 you may not use this file except in compliance with the License.
      5 You may obtain a copy of the License at
      6 
      7     http://www.apache.org/licenses/LICENSE-2.0
      8 
      9 Unless required by applicable law or agreed to in writing, software
     10 distributed under the License is distributed on an "AS IS" BASIS,
     11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     12 See the License for the specific language governing permissions and
     13 limitations under the License.
     14 ==============================================================================*/
     15 
     16 #include "tensorflow/core/graph/tensor_id.h"
     17 
     18 #include <string>
     19 
     20 #include "tensorflow/core/lib/core/stringpiece.h"
     21 #include "tensorflow/core/lib/strings/str_util.h"
     22 
     23 namespace tensorflow {
     24 
     25 TensorId::TensorId(const SafeTensorId& id) : TensorId(id.first, id.second) {}
     26 
     27 SafeTensorId::SafeTensorId(const TensorId& id)
     28     : SafeTensorId(string(id.first), id.second) {}
     29 
     30 TensorId ParseTensorName(const string& name) {
     31   return ParseTensorName(StringPiece(name.data(), name.size()));
     32 }
     33 
     34 TensorId ParseTensorName(StringPiece name) {
     35   // Parse either a name, ^name, or name:digits.  To do so, we go backwards from
     36   // the end of the string, skipping over a run of digits.  If we hit a ':'
     37   // character, then we know we are in the 'name:digits' regime.  Otherwise, we
     38   // see if the name starts with '^', indicating a control edge. If we find
     39   // neither ':' nor '^' characters, the output index is implicitly 0, and the
     40   // whole name string forms the first part of the tensor name.
     41   const char* base = name.data();
     42   const char* p = base + name.size() - 1;
     43   unsigned int index = 0;
     44   unsigned int mul = 1;
     45   while (p > base && (*p >= '0' && *p <= '9')) {
     46     index += ((*p - '0') * mul);
     47     mul *= 10;
     48     p--;
     49   }
     50   TensorId id;
     51   if (p > base && *p == ':' && mul > 1) {
     52     id.first = StringPiece(base, p - base);
     53     id.second = index;
     54   } else if (str_util::StartsWith(name, "^")) {
     55     // Control edge
     56     id.first = StringPiece(base + 1);
     57     id.second = Graph::kControlSlot;
     58   } else {
     59     id.first = name;
     60     id.second = 0;
     61   }
     62   return id;
     63 }
     64 
     65 bool IsTensorIdControl(const TensorId& tensor_id) {
     66   return tensor_id.index() == Graph::kControlSlot;
     67 }
     68 
     69 }  // namespace tensorflow
     70