1 /**
2 * Copyright 2019-2022 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_ptr<Value>()) == *(other.m_ptr->cast_ptr<Value>());
46 }
47
48 // for noderef equal
49 if (m_ptr->isa<BaseRef>()) {
50 return *(m_ptr->cast_ptr<BaseRef>()) == *(other.m_ptr->cast_ptr<BaseRef>());
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
SetRef(const SetRef & other)91 SetRef::SetRef(const SetRef &other) : elements_(other.elements_) {}
92
operator =(const SetRef & other)93 SetRef &SetRef::operator=(const SetRef &other) {
94 if (elements_ == other.elements_ || this == &other) {
95 return *this;
96 }
97 elements_ = other.elements_;
98 return *this;
99 }
100
ToString() const101 std::string SetRef::ToString() const {
102 std::ostringstream buffer;
103 bool begin = true;
104 buffer << "set[";
105 for (auto &attr : elements_) {
106 if (!begin) {
107 buffer << ", ";
108 } else {
109 begin = false;
110 }
111 buffer << attr.ToString();
112 }
113 buffer << "]";
114 return buffer.str();
115 }
116
117 // left reference
VectorRef(const VectorRef & other)118 VectorRef::VectorRef(const VectorRef &other) : elements_(other.elements_) {}
119
operator =(const VectorRef & other)120 VectorRef &VectorRef::operator=(const VectorRef &other) {
121 if (elements_ == other.elements_ || this == &other) {
122 return *this;
123 }
124 elements_ = other.elements_;
125 return *this;
126 }
127
ToString() const128 std::string VectorRef::ToString() const {
129 std::ostringstream buffer;
130 bool begin = true;
131 buffer << "vector[";
132 for (auto &attr : elements_) {
133 if (!begin) {
134 buffer << ", ";
135 } else {
136 begin = false;
137 }
138 buffer << attr.ToString();
139 }
140 buffer << "]";
141 return buffer.str();
142 }
143
operator ==(const BaseRef & other) const144 bool VectorRef::operator==(const BaseRef &other) const {
145 if (!utils::isa<VectorRef>(other)) {
146 return false;
147 }
148 return *this == utils::cast<VectorRef>(other);
149 }
150
operator ==(const VectorRef & other) const151 bool VectorRef::operator==(const VectorRef &other) const {
152 if (elements_.size() != other.elements_.size()) {
153 return false;
154 }
155 for (size_t i = 0; i < elements_.size(); ++i) {
156 if (elements_[i] != other.elements_[i]) {
157 return false;
158 }
159 }
160 return true;
161 }
162
operator ==(const BaseRef & other) const163 bool SetRef::operator==(const BaseRef &other) const {
164 if (!utils::isa<SetRef>(other)) {
165 return false;
166 }
167 return *this == utils::cast<SetRef>(other);
168 }
169
operator ==(const SetRef & other) const170 bool SetRef::operator==(const SetRef &other) const {
171 if (elements_.size() != other.elements_.size()) {
172 return false;
173 }
174 auto iter = elements_.begin();
175 auto oth_iter = other.elements_.begin();
176 for (; iter != elements_.end(); iter++, oth_iter++) {
177 if (*iter != *oth_iter) {
178 return false;
179 }
180 }
181 return true;
182 }
183
operator ==(const BaseRef & other) const184 bool RunFunctionRef::operator==(const BaseRef &other) const {
185 if (!utils::isa<RunFunctionRef>(other)) {
186 return false;
187 }
188 return *this == utils::cast<RunFunctionRef>(other);
189 }
190
operator ==(const RunFunctionRef & other) const191 bool RunFunctionRef::operator==(const RunFunctionRef &other) const { return func_ == other.func_; }
192 } // namespace mindspore
193