/** * Copyright 2021 Huawei Technologies Co., Ltd * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #ifndef MINDSPORE_INCLUDE_API_DUAL_ABI_HELPER_H_ #define MINDSPORE_INCLUDE_API_DUAL_ABI_HELPER_H_ #include #include #include #include #include #include #include #include #include namespace mindspore { inline std::vector StringToChar(const std::string &s) { return std::vector(s.begin(), s.end()); } inline std::string CharToString(const std::vector &c) { return std::string(c.begin(), c.end()); } inline std::optional> OptionalStringToChar(const std::optional &s) { if (s == std::nullopt) return std::nullopt; std::optional> ret = std::vector(s->begin(), s->end()); return ret; } inline std::optional OptionalCharToString(const std::optional> &c) { if (c == std::nullopt) return std::nullopt; std::optional ret = std::string(c->begin(), c->end()); return ret; } inline std::pair, int32_t> PairStringToChar(const std::pair &s) { return std::pair, int32_t>(std::vector(s.first.begin(), s.first.end()), s.second); } inline std::pair PairCharToString(const std::pair, int32_t> &c) { return std::pair(std::string(c.first.begin(), c.first.end()), c.second); } inline std::vector> VectorStringToChar(const std::vector &s) { std::vector> ret; std::transform(s.begin(), s.end(), std::back_inserter(ret), [](auto str) { return std::vector(str.begin(), str.end()); }); return ret; } inline std::vector VectorCharToString(const std::vector> &c) { std::vector ret; std::transform(c.begin(), c.end(), std::back_inserter(ret), [](auto ch) { return std::string(ch.begin(), ch.end()); }); return ret; } inline std::set> SetStringToChar(const std::set &s) { std::set> ret; std::transform(s.begin(), s.end(), std::inserter(ret, ret.begin()), [](auto str) { return std::vector(str.begin(), str.end()); }); return ret; } inline std::set SetCharToString(const std::set> &c) { std::set ret; std::transform(c.begin(), c.end(), std::inserter(ret, ret.begin()), [](auto ch) { return std::string(ch.begin(), ch.end()); }); return ret; } inline std::map, int32_t> MapStringToChar(const std::map &s) { std::map, int32_t> ret; std::transform(s.begin(), s.end(), std::inserter(ret, ret.begin()), [](auto str) { return std::pair, int32_t>(std::vector(str.first.begin(), str.first.end()), str.second); }); return ret; } inline std::map MapCharToString(const std::map, int32_t> &c) { std::map ret; std::transform(c.begin(), c.end(), std::inserter(ret, ret.begin()), [](auto ch) { return std::pair(std::string(ch.first.begin(), ch.first.end()), ch.second); }); return ret; } inline std::map, std::vector> UnorderedMapStringToChar( const std::unordered_map &s) { std::map, std::vector> ret; std::transform(s.begin(), s.end(), std::inserter(ret, ret.begin()), [](auto str) { return std::pair, std::vector>(std::vector(str.first.begin(), str.first.end()), std::vector(str.second.begin(), str.second.end())); }); return ret; } inline std::unordered_map UnorderedMapCharToString( const std::map, std::vector> &c) { std::unordered_map ret; std::transform(c.begin(), c.end(), std::inserter(ret, ret.begin()), [](auto ch) { return std::pair(std::string(ch.first.begin(), ch.first.end()), std::string(ch.second.begin(), ch.second.end())); }); return ret; } inline std::vector, std::vector>> ClassIndexStringToChar( const std::vector>> &s) { std::vector, std::vector>> ret; std::transform(s.begin(), s.end(), std::back_inserter(ret), [](auto str) { return std::pair, std::vector>(std::vector(str.first.begin(), str.first.end()), str.second); }); return ret; } inline std::vector>> ClassIndexCharToString( const std::vector, std::vector>> &c) { std::vector>> ret; std::transform(c.begin(), c.end(), std::back_inserter(ret), [](auto ch) { return std::pair>(std::string(ch.first.begin(), ch.first.end()), ch.second); }); return ret; } template inline std::map, T> PadInfoStringToChar(const std::map &s_pad_info) { std::map, T> ret; std::transform(s_pad_info.begin(), s_pad_info.end(), std::inserter(ret, ret.begin()), [](auto str) { return std::pair, T>(std::vector(str.first.begin(), str.first.end()), str.second); }); return ret; } template inline std::map PadInfoCharToString(const std::map, T> &c_pad_info) { std::map ret; std::transform(c_pad_info.begin(), c_pad_info.end(), std::inserter(ret, ret.begin()), [](auto ch) { return std::pair(std::string(ch.first.begin(), ch.first.end()), ch.second); }); return ret; } template inline void TensorMapCharToString(const std::map, T> *c, std::unordered_map *s) { for (auto ch : *c) { auto key = std::string(ch.first.begin(), ch.first.end()); auto val = ch.second; s->insert(std::pair(key, val)); } } } // namespace mindspore #endif // MINDSPORE_INCLUDE_API_DUAL_ABI_HELPER_H_