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.

utils.h 4.0 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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 PREDICT_COMMON_UTILS_H_
  17. #define PREDICT_COMMON_UTILS_H_
  18. #include <stdint.h>
  19. #include <ctime>
  20. #include <cstdint>
  21. #include <vector>
  22. #include <string>
  23. #include "common/mslog.h"
  24. #include "common/option.h"
  25. #include "include/errorcode.h"
  26. namespace mindspore {
  27. namespace predict {
  28. const int USEC = 1000000;
  29. const int MSEC = 1000;
  30. uint64_t GetTimeUs();
  31. int16_t Float32ToShort(float srcValue);
  32. float ShortToFloat32(int16_t srcValue);
  33. void ShortToFloat32(const int16_t *srcData, float *dstData, size_t elementSize);
  34. void Float32ToShort(const float *srcData, int16_t *dstData, size_t elementSize);
  35. template <typename T>
  36. bool IsContain(const std::vector<T> &vec, T element) {
  37. for (auto iter = vec.begin(); iter != vec.end(); iter++) {
  38. if (*iter == element) {
  39. return true;
  40. }
  41. }
  42. return false;
  43. }
  44. const char WHITESPACE[] = "\t\n\v\f\r ";
  45. const char STR_TRUE[] = "true";
  46. const char STR_FALSE[] = "false";
  47. template <typename T>
  48. Option<std::string> ToString(T t) {
  49. std::ostringstream out;
  50. out << t;
  51. if (!out.good()) {
  52. return Option<std::string>(None());
  53. }
  54. return Option<std::string>(out.str());
  55. }
  56. template <>
  57. inline Option<std::string> ToString(bool value) {
  58. return value ? Option<std::string>(STR_TRUE) : Option<std::string>(STR_FALSE);
  59. }
  60. // get the file name from a given path
  61. // for example: "/usr/bin", we will get "bin"
  62. inline std::string GetFileName(const std::string &path) {
  63. char delim = '/';
  64. size_t i = path.rfind(delim, path.length());
  65. if (i != std::string::npos) {
  66. return (path.substr(i + 1, path.length() - i));
  67. }
  68. return "";
  69. }
  70. // trim the white space character in a string
  71. // see also: macro WHITESPACE defined above
  72. inline void Trim(std::string *input) {
  73. if (input == nullptr) {
  74. return;
  75. }
  76. if (input->empty()) {
  77. return;
  78. }
  79. input->erase(0, input->find_first_not_of(WHITESPACE));
  80. input->erase(input->find_last_not_of(WHITESPACE) + 1);
  81. }
  82. // to judge whether a string is starting with prefix
  83. // for example: "hello world" is starting with "hello"
  84. inline bool StartsWithPrefix(const std::string &source, const std::string &prefix) {
  85. if (source.length() < prefix.length()) {
  86. return false;
  87. }
  88. return (source.compare(0, prefix.length(), prefix) == 0);
  89. }
  90. // split string
  91. std::vector<std::string> StrSplit(const std::string &str, const std::string &pattern);
  92. // tokenize string
  93. std::vector<std::string> Tokenize(const std::string &src, const std::string &delimiters,
  94. const Option<size_t> &maxTokenNum = Option<size_t>(None()));
  95. enum Mode { PREFIX, SUFFIX, ANY };
  96. // remove redundant character
  97. std::string Remove(const std::string &from, const std::string &subStr, Mode mode = ANY);
  98. template <typename T>
  99. inline Option<T> GenericParseValue(const std::string &value) {
  100. T ret;
  101. std::istringstream input(value);
  102. input >> ret;
  103. if (input && input.eof()) {
  104. return Option<T>(ret);
  105. }
  106. return Option<T>(None());
  107. }
  108. template <>
  109. inline Option<std::string> GenericParseValue(const std::string &value) {
  110. return Option<std::string>(value);
  111. }
  112. template <>
  113. inline Option<bool> GenericParseValue(const std::string &value) {
  114. if (value == "true") {
  115. return Option<bool>(true);
  116. } else if (value == "false") {
  117. return Option<bool>(false);
  118. }
  119. return Option<bool>(None());
  120. }
  121. } // namespace predict
  122. } // namespace mindspore
  123. #endif // PREDICT_COMMON_UTILS_H_