• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright 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 #ifndef MINDSPORE_INCLUDE_API_DUAL_ABI_HELPER_H_
17 #define MINDSPORE_INCLUDE_API_DUAL_ABI_HELPER_H_
18 
19 #include <algorithm>
20 #include <iterator>
21 #include <map>
22 #include <memory>
23 #include <string>
24 #include <set>
25 #include <unordered_map>
26 #include <utility>
27 #include <vector>
28 
29 namespace mindspore {
StringToChar(const std::string & s)30 inline std::vector<char> StringToChar(const std::string &s) { return std::vector<char>(s.begin(), s.end()); }
31 
CharToString(const std::vector<char> & c)32 inline std::string CharToString(const std::vector<char> &c) { return std::string(c.begin(), c.end()); }
33 
PairStringToChar(const std::pair<std::string,int32_t> & s)34 inline std::pair<std::vector<char>, int32_t> PairStringToChar(const std::pair<std::string, int32_t> &s) {
35   return std::pair<std::vector<char>, int32_t>(std::vector<char>(s.first.begin(), s.first.end()), s.second);
36 }
37 
PairCharToString(const std::pair<std::vector<char>,int32_t> & c)38 inline std::pair<std::string, int32_t> PairCharToString(const std::pair<std::vector<char>, int32_t> &c) {
39   return std::pair<std::string, int32_t>(std::string(c.first.begin(), c.first.end()), c.second);
40 }
41 
VectorStringToChar(const std::vector<std::string> & s)42 inline std::vector<std::vector<char>> VectorStringToChar(const std::vector<std::string> &s) {
43   std::vector<std::vector<char>> ret;
44   std::transform(s.begin(), s.end(), std::back_inserter(ret),
45                  [](auto str) { return std::vector<char>(str.begin(), str.end()); });
46   return ret;
47 }
48 
VectorCharToString(const std::vector<std::vector<char>> & c)49 inline std::vector<std::string> VectorCharToString(const std::vector<std::vector<char>> &c) {
50   std::vector<std::string> ret;
51   std::transform(c.begin(), c.end(), std::back_inserter(ret),
52                  [](auto ch) { return std::string(ch.begin(), ch.end()); });
53   return ret;
54 }
55 
SetStringToChar(const std::set<std::string> & s)56 inline std::set<std::vector<char>> SetStringToChar(const std::set<std::string> &s) {
57   std::set<std::vector<char>> ret;
58   std::transform(s.begin(), s.end(), std::inserter(ret, ret.begin()),
59                  [](auto str) { return std::vector<char>(str.begin(), str.end()); });
60   return ret;
61 }
62 
SetCharToString(const std::set<std::vector<char>> & c)63 inline std::set<std::string> SetCharToString(const std::set<std::vector<char>> &c) {
64   std::set<std::string> ret;
65   std::transform(c.begin(), c.end(), std::inserter(ret, ret.begin()),
66                  [](auto ch) { return std::string(ch.begin(), ch.end()); });
67   return ret;
68 }
69 
MapStringToChar(const std::map<std::string,int32_t> & s)70 inline std::map<std::vector<char>, int32_t> MapStringToChar(const std::map<std::string, int32_t> &s) {
71   std::map<std::vector<char>, int32_t> ret;
72   std::transform(s.begin(), s.end(), std::inserter(ret, ret.begin()), [](auto str) {
73     return std::pair<std::vector<char>, int32_t>(std::vector<char>(str.first.begin(), str.first.end()), str.second);
74   });
75   return ret;
76 }
77 
MapCharToString(const std::map<std::vector<char>,int32_t> & c)78 inline std::map<std::string, int32_t> MapCharToString(const std::map<std::vector<char>, int32_t> &c) {
79   std::map<std::string, int32_t> ret;
80   std::transform(c.begin(), c.end(), std::inserter(ret, ret.begin()), [](auto ch) {
81     return std::pair<std::string, int32_t>(std::string(ch.first.begin(), ch.first.end()), ch.second);
82   });
83   return ret;
84 }
85 
UnorderedMapStringToChar(const std::unordered_map<std::string,std::string> & s)86 inline std::map<std::vector<char>, std::vector<char>> UnorderedMapStringToChar(
87   const std::unordered_map<std::string, std::string> &s) {
88   std::map<std::vector<char>, std::vector<char>> ret;
89   std::transform(s.begin(), s.end(), std::inserter(ret, ret.begin()), [](auto str) {
90     return std::pair<std::vector<char>, std::vector<char>>(std::vector<char>(str.first.begin(), str.first.end()),
91                                                            std::vector<char>(str.second.begin(), str.second.end()));
92   });
93   return ret;
94 }
95 
UnorderedMapCharToString(const std::map<std::vector<char>,std::vector<char>> & c)96 inline std::unordered_map<std::string, std::string> UnorderedMapCharToString(
97   const std::map<std::vector<char>, std::vector<char>> &c) {
98   std::unordered_map<std::string, std::string> ret;
99   std::transform(c.begin(), c.end(), std::inserter(ret, ret.begin()), [](auto ch) {
100     return std::pair<std::string, std::string>(std::string(ch.first.begin(), ch.first.end()),
101                                                std::string(ch.second.begin(), ch.second.end()));
102   });
103   return ret;
104 }
105 
ClassIndexStringToChar(const std::vector<std::pair<std::string,std::vector<int32_t>>> & s)106 inline std::vector<std::pair<std::vector<char>, std::vector<int32_t>>> ClassIndexStringToChar(
107   const std::vector<std::pair<std::string, std::vector<int32_t>>> &s) {
108   std::vector<std::pair<std::vector<char>, std::vector<int32_t>>> ret;
109   std::transform(s.begin(), s.end(), std::back_inserter(ret), [](auto str) {
110     return std::pair<std::vector<char>, std::vector<int32_t>>(std::vector<char>(str.first.begin(), str.first.end()),
111                                                               str.second);
112   });
113   return ret;
114 }
115 
ClassIndexCharToString(const std::vector<std::pair<std::vector<char>,std::vector<int32_t>>> & c)116 inline std::vector<std::pair<std::string, std::vector<int32_t>>> ClassIndexCharToString(
117   const std::vector<std::pair<std::vector<char>, std::vector<int32_t>>> &c) {
118   std::vector<std::pair<std::string, std::vector<int32_t>>> ret;
119   std::transform(c.begin(), c.end(), std::back_inserter(ret), [](auto ch) {
120     return std::pair<std::string, std::vector<int32_t>>(std::string(ch.first.begin(), ch.first.end()), ch.second);
121   });
122   return ret;
123 }
124 
PairStringInt64ToPairCharInt64(const std::vector<std::pair<std::string,int64_t>> & s)125 inline std::vector<std::pair<std::vector<char>, int64_t>> PairStringInt64ToPairCharInt64(
126   const std::vector<std::pair<std::string, int64_t>> &s) {
127   std::vector<std::pair<std::vector<char>, int64_t>> ret;
128   std::transform(s.begin(), s.end(), std::back_inserter(ret), [](auto str) {
129     return std::pair<std::vector<char>, int64_t>(std::vector<char>(str.first.begin(), str.first.end()), str.second);
130   });
131   return ret;
132 }
133 
134 template <class T>
PadInfoStringToChar(const std::map<std::string,T> & s_pad_info)135 inline std::map<std::vector<char>, T> PadInfoStringToChar(const std::map<std::string, T> &s_pad_info) {
136   std::map<std::vector<char>, T> ret;
137   std::transform(s_pad_info.begin(), s_pad_info.end(), std::inserter(ret, ret.begin()), [](auto str) {
138     return std::pair<std::vector<char>, T>(std::vector<char>(str.first.begin(), str.first.end()), str.second);
139   });
140   return ret;
141 }
142 
143 template <class T>
PadInfoCharToString(const std::map<std::vector<char>,T> & c_pad_info)144 inline std::map<std::string, T> PadInfoCharToString(const std::map<std::vector<char>, T> &c_pad_info) {
145   std::map<std::string, T> ret;
146   std::transform(c_pad_info.begin(), c_pad_info.end(), std::inserter(ret, ret.begin()), [](auto ch) {
147     return std::pair<std::string, T>(std::string(ch.first.begin(), ch.first.end()), ch.second);
148   });
149   return ret;
150 }
151 
152 template <class T>
TensorMapCharToString(const std::map<std::vector<char>,T> * c,std::unordered_map<std::string,T> * s)153 inline void TensorMapCharToString(const std::map<std::vector<char>, T> *c, std::unordered_map<std::string, T> *s) {
154   if (c == nullptr || s == nullptr) {
155     return;
156   }
157   for (auto ch : *c) {
158     auto key = std::string(ch.first.begin(), ch.first.end());
159     auto val = ch.second;
160     s->insert(std::pair<std::string, T>(key, val));
161   }
162 }
163 }  // namespace mindspore
164 #endif  // MINDSPORE_INCLUDE_API_DUAL_ABI_HELPER_H_
165