1 /**
2 * Copyright 2019-2020 Huawei Technologies Co., Ltd
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include "base/base_ref.h"
18
19 namespace mindspore {
ConstIteratorCast(std::vector<BaseRef> * v,const const_iterator iter)20 iterator ConstIteratorCast(std::vector<BaseRef> *v, const const_iterator iter) {
21 return std::next(v->begin(), std::distance(v->cbegin(), iter));
22 }
23
BaseRef(const BaseRef & other)24 BaseRef::BaseRef(const BaseRef &other) : Base(other), m_ptr(other.m_ptr) {
25 if (!m_ptr) {
26 m_ptr = other.copy();
27 }
28 }
29
operator ==(const BaseRef & other) const30 bool BaseRef::operator==(const BaseRef &other) const {
31 if (m_ptr == other.m_ptr) {
32 return true;
33 }
34 if (m_ptr == nullptr && other.m_ptr == nullptr) {
35 return *this == other;
36 }
37 if (m_ptr == nullptr || other.m_ptr == nullptr) {
38 return false;
39 }
40 if (type() != other.type()) {
41 MS_LOG(DEBUG) << "Type mismatch";
42 return false;
43 }
44 if (m_ptr->isa<Value>()) {
45 return *(m_ptr->cast<ValuePtr>()) == *(other.m_ptr->cast<ValuePtr>());
46 }
47
48 // for noderef equal
49 if (m_ptr->isa<BaseRef>()) {
50 return *std::static_pointer_cast<BaseRef>(m_ptr) == *std::static_pointer_cast<BaseRef>(other.m_ptr);
51 }
52
53 // for node equal
54 return *m_ptr == *other.m_ptr;
55 }
56
57 // left reference
operator =(const BaseRef & other)58 BaseRef &BaseRef::operator=(const BaseRef &other) {
59 if ((m_ptr != nullptr && m_ptr == other.m_ptr) || this == &other) {
60 return *this;
61 }
62 m_ptr = other.copy();
63 return *this;
64 }
65
66 // right reference
operator =(BaseRef && other)67 BaseRef &BaseRef::operator=(BaseRef &&other) {
68 if ((m_ptr != nullptr && m_ptr == other.m_ptr) || this == &other) {
69 return *this;
70 }
71 m_ptr = other.copy();
72 other.m_ptr = nullptr;
73 return *this;
74 }
75
ToString() const76 std::string BaseRef::ToString() const {
77 if (m_ptr != nullptr) {
78 return std::string(m_ptr->type_name()) + std::string(" value:") + m_ptr->ToString();
79 }
80 return std::string();
81 }
82
type() const83 uint32_t BaseRef::type() const {
84 if (m_ptr != nullptr) {
85 return m_ptr->tid();
86 }
87 return tid();
88 }
89
90 // left reference
operator =(const SetRef & other)91 SetRef &SetRef::operator=(const SetRef &other) {
92 if (elements_ == other.elements_ || this == &other) {
93 return *this;
94 }
95 elements_ = other.elements_;
96 return *this;
97 }
98
ToString() const99 std::string SetRef::ToString() const {
100 std::ostringstream buffer;
101 bool begin = true;
102 buffer << "set[";
103 for (auto &attr : elements_) {
104 if (!begin) {
105 buffer << ", ";
106 } else {
107 begin = false;
108 }
109 buffer << attr.ToString();
110 }
111 buffer << "]";
112 return buffer.str();
113 }
114
115 // left reference
operator =(const VectorRef & other)116 VectorRef &VectorRef::operator=(const VectorRef &other) {
117 if (elements_ == other.elements_ || this == &other) {
118 return *this;
119 }
120 elements_ = other.elements_;
121 return *this;
122 }
123
ToString() const124 std::string VectorRef::ToString() const {
125 std::ostringstream buffer;
126 bool begin = true;
127 buffer << "vector[";
128 for (auto &attr : elements_) {
129 if (!begin) {
130 buffer << ", ";
131 } else {
132 begin = false;
133 }
134 buffer << attr.ToString();
135 }
136 buffer << "]";
137 return buffer.str();
138 }
139
operator ==(const BaseRef & other) const140 bool VectorRef::operator==(const BaseRef &other) const {
141 if (!utils::isa<VectorRef>(other)) {
142 return false;
143 }
144 return *this == utils::cast<VectorRef>(other);
145 }
146
operator ==(const VectorRef & other) const147 bool VectorRef::operator==(const VectorRef &other) const {
148 if (elements_.size() != other.elements_.size()) {
149 return false;
150 }
151 for (size_t i = 0; i < elements_.size(); ++i) {
152 if (elements_[i] != other.elements_[i]) {
153 return false;
154 }
155 }
156 return true;
157 }
158
operator ==(const BaseRef & other) const159 bool SetRef::operator==(const BaseRef &other) const {
160 if (!utils::isa<SetRef>(other)) {
161 return false;
162 }
163 return *this == utils::cast<SetRef>(other);
164 }
165
operator ==(const SetRef & other) const166 bool SetRef::operator==(const SetRef &other) const {
167 if (elements_.size() != other.elements_.size()) {
168 return false;
169 }
170 auto iter = elements_.begin();
171 auto oth_iter = other.elements_.begin();
172 for (; iter != elements_.end(); iter++, oth_iter++) {
173 if (*iter != *oth_iter) {
174 return false;
175 }
176 }
177 return true;
178 }
179
operator ==(const BaseRef & other) const180 bool RunFunctionRef::operator==(const BaseRef &other) const {
181 if (!utils::isa<RunFunctionRef>(other)) {
182 return false;
183 }
184 return *this == utils::cast<RunFunctionRef>(other);
185 }
186
operator ==(const RunFunctionRef & other) const187 bool RunFunctionRef::operator==(const RunFunctionRef &other) const { return func_ == other.func_; }
188 } // namespace mindspore
189