1/* Copyright 2015 The TensorFlow Authors. All Rights Reserved.
2
3Licensed under the Apache License, Version 2.0 (the "License");
4you may not use this file except in compliance with the License.
5You may obtain a copy of the License at
6
7 http://www.apache.org/licenses/LICENSE-2.0
8
9Unless required by applicable law or agreed to in writing, software
10distributed under the License is distributed on an "AS IS" BASIS,
11WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12See the License for the specific language governing permissions and
13limitations under the License.
14==============================================================================*/
15
16#include "tensorflow/core/graph/edgeset.h"
17
18namespace tensorflow {
19
20std::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 gtl::FlatSet<const Edge*>;
41 s->insert(reinterpret_cast<const Edge**>(std::begin(ptrs_)),
42 reinterpret_cast<const Edge**>(std::end(ptrs_)));
43 ptrs_[0] = this;
44 ptrs_[1] = s;
45 // fall through.
46 }
47 auto p = s->insert(value);
48 ci.tree_iter_ = p.first;
49 return std::make_pair(ci, p.second);
50}
51
52EdgeSet::size_type EdgeSet::erase(key_type key) {
53 RegisterMutation();
54 auto s = get_set();
55 if (!s) {
56 for (int i = 0; i < kInline; i++) {
57 if (ptrs_[i] == key) {
58 size_t n = size();
59 ptrs_[i] = ptrs_[n - 1];
60 ptrs_[n - 1] = nullptr;
61 return 1;
62 }
63 }
64 return 0;
65 } else {
66 return s->erase(key);
67 }
68}
69
70} // namespace tensorflow
71