You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

overload.h 3.2 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. #ifndef MINDSPORE_CCSRC_UTILS_OVERLOAD_H_
  17. #define MINDSPORE_CCSRC_UTILS_OVERLOAD_H_
  18. #include <list>
  19. #include <utility>
  20. #include <vector>
  21. #include <iostream>
  22. #include <initializer_list>
  23. #include <unordered_map>
  24. #include <map>
  25. #include <memory>
  26. #include <string>
  27. namespace mindspore {
  28. template <typename T>
  29. std::ostream &operator<<(std::ostream &out, const std::vector<T> &v) {
  30. out << "[const vector][";
  31. size_t last = v.size() - 1;
  32. for (size_t i = 0; i < v.size(); ++i) {
  33. out << v[i];
  34. if (i != last) out << ", ";
  35. }
  36. out << "]";
  37. return out;
  38. }
  39. template <typename T>
  40. std::ostream &operator<<(std::ostream &os, const std::list<T> &vec) {
  41. bool begin = true;
  42. os << "[const list][";
  43. for (auto &item : vec) {
  44. if (!begin) {
  45. os << ", ";
  46. } else {
  47. begin = false;
  48. }
  49. os << item;
  50. }
  51. os << "]";
  52. return os;
  53. }
  54. template <typename T>
  55. std::ostream &operator<<(std::ostream &os, const std::initializer_list<T> &vec) {
  56. bool begin = true;
  57. os << "[";
  58. for (auto &item : vec) {
  59. if (!begin) {
  60. os << ", ";
  61. } else {
  62. begin = false;
  63. }
  64. os << item;
  65. }
  66. os << "]";
  67. return os;
  68. }
  69. template <typename T>
  70. bool operator==(const std::initializer_list<T> &lhs, const std::initializer_list<T> &rhs) {
  71. if (lhs.size() != rhs.size()) {
  72. return false;
  73. }
  74. auto lit = lhs.begin();
  75. auto rit = rhs.begin();
  76. while (lit != lhs.end()) {
  77. if (!(*lit == *rit)) {
  78. return false;
  79. }
  80. lit++;
  81. rit++;
  82. }
  83. return true;
  84. }
  85. template <typename T1, typename T2>
  86. std::ostream &operator<<(std::ostream &os, const std::pair<T1, T2> &pair) {
  87. os << "[const pair]";
  88. return os;
  89. }
  90. template <typename T1, typename T2, typename T3>
  91. std::ostream &operator<<(std::ostream &os, const std::unordered_map<T1, T2, T3> &map) {
  92. os << "[const unordered_map]";
  93. return os;
  94. }
  95. template <typename T1, typename T2, typename T3>
  96. std::ostream &operator<<(std::ostream &os, const std::map<T1, T2, T3> &map) {
  97. os << "[const map]";
  98. return os;
  99. }
  100. template <typename T>
  101. std::string ToString(const std::vector<T> &vec) {
  102. std::ostringstream buffer;
  103. buffer << vec;
  104. return buffer.str();
  105. }
  106. template <typename T1, typename T2>
  107. std::string ToString(const std::unordered_map<T1, T2> &map) {
  108. std::ostringstream buffer;
  109. buffer << map;
  110. return buffer.str();
  111. }
  112. template <typename T1, typename T2>
  113. std::string ToString(const std::map<T1, T2> &map) {
  114. std::ostringstream buffer;
  115. buffer << map;
  116. return buffer.str();
  117. }
  118. } // namespace mindspore
  119. #endif // MINDSPORE_CCSRC_UTILS_OVERLOAD_H_