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