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/edgeset.h"
     17 
     18 namespace tensorflow {
     19 
     20 std::pair<EdgeSet::const_iterator, bool> EdgeSet::insert(value_type value) {
     21   RegisterMutation();
     22   const_iterator ci;
     23   ci.Init(this);
     24   auto s = get_set();
     25   if (!s) {
     26     for (int i = 0; i < kInline; i++) {
     27       if (ptrs_[i] == value) {
     28         ci.array_iter_ = &ptrs_[i];
     29         return std::make_pair(ci, false);
     30       }
     31     }
     32     for (int i = 0; i < kInline; i++) {
     33       if (ptrs_[i] == nullptr) {
     34         ptrs_[i] = value;
     35         ci.array_iter_ = &ptrs_[i];
     36         return std::make_pair(ci, true);
     37       }
     38     }
     39     // array is full. convert to set.
     40     s = new std::set<const Edge*>;
     41     for (int i = 0; i < kInline; i++) {
     42       s->insert(static_cast<const Edge*>(ptrs_[i]));
     43     }
     44     ptrs_[0] = this;
     45     ptrs_[1] = s;
     46     // fall through.
     47   }
     48   auto p = s->insert(value);
     49   ci.tree_iter_ = p.first;
     50   return std::make_pair(ci, p.second);
     51 }
     52 
     53 EdgeSet::size_type EdgeSet::erase(key_type key) {
     54   RegisterMutation();
     55   auto s = get_set();
     56   if (!s) {
     57     for (int i = 0; i < kInline; i++) {
     58       if (ptrs_[i] == key) {
     59         size_t n = size();
     60         ptrs_[i] = ptrs_[n - 1];
     61         ptrs_[n - 1] = nullptr;
     62         return 1;
     63       }
     64     }
     65     return 0;
     66   } else {
     67     return s->erase(key);
     68   }
     69 }
     70 
     71 }  // namespace tensorflow
     72