• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright 2019-2021 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 #ifndef MINDSPORE_CORE_UTILS_OVERLOAD_H_
18 #define MINDSPORE_CORE_UTILS_OVERLOAD_H_
19 
20 #include <list>
21 #include <utility>
22 #include <vector>
23 #include <iostream>
24 #include <map>
25 #include <memory>
26 #include <string>
27 #include "utils/hash_map.h"
28 
29 namespace mindspore {
30 constexpr size_t kThresholdToDisplayIndex = 10;
31 template <typename T>
32 std::ostream &operator<<(std::ostream &os, const std::vector<T> &v) {
33   os << "[const vector]{";
34   const bool display_index = (v.size() > kThresholdToDisplayIndex);
35   size_t last_index = v.size() - 1;
36   for (size_t i = 0; i < v.size(); ++i) {
37     if (display_index) {
38       os << "[" << i << "]:{";
39     }
40     os << v[i];
41     if (display_index) {
42       os << "}";
43     }
44     if (i != last_index) {
45       os << ", ";
46     }
47   }
48   os << "}";
49   return os;
50 }
51 
52 template <typename T>
53 std::ostream &operator<<(std::ostream &os, const std::list<T> &v) {
54   os << "[const list]{";
55   const bool display_index = (v.size() > kThresholdToDisplayIndex);
56   size_t i = 0;
57   size_t last_index = v.size() - 1;
58   for (auto it = v.begin(); it != v.end(); ++it, ++i) {
59     if (display_index) {
60       os << "[" << i << "]:{";
61     }
62     os << *it;
63     if (display_index) {
64       os << "}";
65     }
66     if (i != last_index) {
67       os << ", ";
68     }
69   }
70   os << "}";
71   return os;
72 }
73 
74 template <typename T>
75 std::ostream &operator<<(std::ostream &os, const std::initializer_list<T> &v) {
76   os << "[const initializer_list]{";
77   const bool display_index = (v.size() > kThresholdToDisplayIndex);
78   size_t i = 0;
79   size_t last_index = v.size() - 1;
80   for (auto it = v.begin(); it != v.end(); ++it, ++i) {
81     if (display_index) {
82       os << "[" << i << "]:{";
83     }
84     os << *it;
85     if (display_index) {
86       os << "}";
87     }
88     if (i != last_index) {
89       os << ", ";
90     }
91   }
92   os << "}";
93   return os;
94 }
95 
96 template <typename T>
97 bool operator==(const std::initializer_list<T> &lhs, const std::initializer_list<T> &rhs) {
98   if (lhs.size() != rhs.size()) {
99     return false;
100   }
101   auto lit = lhs.begin();
102   auto rit = rhs.begin();
103   while (lit != lhs.end()) {
104     if (!(*lit == *rit)) {
105       return false;
106     }
107     lit++;
108     rit++;
109   }
110   return true;
111 }
112 
113 template <typename T1, typename T2>
114 std::ostream &operator<<(std::ostream &os, const std::pair<T1, T2> &pair) {
115   os << "[const pair]";
116 
117   return os;
118 }
119 
120 template <typename T1, typename T2, typename T3>
121 std::ostream &operator<<(std::ostream &os, const mindspore::HashMap<T1, T2, T3> &) {
122   os << "[const hash_map]";
123   return os;
124 }
125 
126 template <typename T1, typename T2, typename T3>
127 std::ostream &operator<<(std::ostream &os, const std::map<T1, T2, T3> &map) {
128   os << "[const map]";
129   return os;
130 }
131 
132 template <typename T>
ToString(const std::vector<T> & vec)133 std::string ToString(const std::vector<T> &vec) {
134   std::ostringstream buffer;
135 
136   buffer << vec;
137   return buffer.str();
138 }
139 
140 template <typename T1, typename T2>
ToString(const mindspore::HashMap<T1,T2> & map)141 std::string ToString(const mindspore::HashMap<T1, T2> &map) {
142   std::ostringstream buffer;
143 
144   buffer << map;
145   return buffer.str();
146 }
147 
148 template <typename T1, typename T2>
ToString(const std::map<T1,T2> & map)149 std::string ToString(const std::map<T1, T2> &map) {
150   std::ostringstream buffer;
151 
152   buffer << map;
153   return buffer.str();
154 }
155 }  // namespace mindspore
156 
157 #endif  // MINDSPORE_CORE_UTILS_OVERLOAD_H_
158