• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright 2020 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_LITE_SRC_COMMON_UTILS_H_
18 #define MINDSPORE_LITE_SRC_COMMON_UTILS_H_
19 
20 #include <ctime>
21 #include <cstdint>
22 #include <vector>
23 #include <set>
24 #include <string>
25 #include <utility>
26 #include "src/common/log_adapter.h"
27 #include "tools/common/option.h"
28 #include "include/errorcode.h"
29 
30 namespace mindspore {
31 namespace lite {
32 enum class MSNodeType {
33   NodeType_ValueNode,  // const
34   NodeType_Parameter,  // var
35   NodeType_CNode       // op
36 };
37 
38 size_t GetMaxMallocSize();
39 
40 const int USEC = 1000000;
41 const int MSEC = 1000;
42 uint64_t GetTimeUs();
43 
44 bool IsSupportSDot();
45 
46 #ifdef __ANDROID__
47 uint32_t getHwCap(int hwcap_type);
48 #endif
49 
50 template <typename T>
IsContain(const std::vector<T> & vec,T element)51 bool IsContain(const std::vector<T> &vec, T element) {
52   for (auto iter = vec.begin(); iter != vec.end(); iter++) {
53     if (*iter == element) {
54       return true;
55     }
56   }
57   return false;
58 }
59 
60 template <typename T>
VectorErase(std::vector<T> * vec,T element)61 bool VectorErase(std::vector<T> *vec, T element) {
62   bool ret = false;
63   for (auto iter = vec->begin(); iter != vec->end();) {
64     if (*iter == element) {
65       iter = vec->erase(iter);
66       ret = true;
67     } else {
68       iter++;
69     }
70   }
71   return ret;
72 }
73 
74 template <typename T>
VectorSetNull(std::vector<T> * vec,T element)75 bool VectorSetNull(std::vector<T> *vec, T element) {
76   bool ret = false;
77   for (size_t i = 0; i < vec->size(); i++) {
78     if (vec->at(i) == element) {
79       vec->at(i) = nullptr;
80     }
81   }
82   return ret;
83 }
84 
85 template <typename T>
VectorReplace(std::vector<T> * vec,T srcElement,T dstElement)86 bool VectorReplace(std::vector<T> *vec, T srcElement, T dstElement) {
87   bool ret = false;
88   for (auto iter = vec->begin(); iter != vec->end(); iter++) {
89     if (*iter == srcElement) {
90       if (!IsContain(*vec, dstElement)) {
91         *iter = std::move(dstElement);
92       } else {
93         vec->erase(iter);
94       }
95       ret = true;
96       break;
97     }
98   }
99   return ret;
100 }
101 
102 const char WHITESPACE[] = "\t\n\v\f\r ";
103 const char STR_TRUE[] = "true";
104 const char STR_FALSE[] = "false";
105 
106 template <typename T>
ToString(T t)107 Option<std::string> ToString(T t) {
108   std::ostringstream out;
109   out << t;
110   if (!out.good()) {
111     return Option<std::string>(None());
112   }
113 
114   return Option<std::string>(out.str());
115 }
116 
117 template <>
ToString(bool value)118 inline Option<std::string> ToString(bool value) {
119   return value ? Option<std::string>(STR_TRUE) : Option<std::string>(STR_FALSE);
120 }
121 
122 // get the file name from a given path
123 // for example: "/usr/bin", we will get "bin"
GetFileName(const std::string & path)124 inline std::string GetFileName(const std::string &path) {
125   if (path.empty()) {
126     MS_LOG(ERROR) << "string is empty";
127     return "";
128   }
129 
130   char delim = '/';
131 
132   size_t i = path.rfind(delim, path.length());
133   if (i != std::string::npos && i + 1 < path.length()) {
134     return (path.substr(i + 1, path.length() - i));
135   }
136 
137   return "";
138 }
139 
140 // trim the white space character in a string
141 // see also: macro WHITESPACE defined above
Trim(std::string * input)142 inline void Trim(std::string *input) {
143   if (input == nullptr) {
144     return;
145   }
146   if (input->empty()) {
147     return;
148   }
149 
150   input->erase(0, input->find_first_not_of(WHITESPACE));
151   input->erase(input->find_last_not_of(WHITESPACE) + 1);
152 }
153 
154 // to judge whether a string is starting with  prefix
155 // for example: "hello world" is starting with "hello"
StartsWithPrefix(const std::string & source,const std::string & prefix)156 inline bool StartsWithPrefix(const std::string &source, const std::string &prefix) {
157   if (source.length() < prefix.length()) {
158     return false;
159   }
160 
161   return (source.compare(0, prefix.length(), prefix) == 0);
162 }
163 
164 // split string
165 std::vector<std::string> StrSplit(const std::string &str, const std::string &pattern);
166 
167 // tokenize string
168 std::vector<std::string> Tokenize(const std::string &src, const std::string &delimiters,
169                                   const Option<size_t> &maxTokenNum = Option<size_t>(None()));
170 
171 enum RemoveSubStrMode { PREFIX, SUFFIX, ANY };
172 
173 // remove redundant character
174 std::string RemoveSubStr(const std::string &from, const std::string &sub_str, RemoveSubStrMode mode = ANY);
175 
176 template <typename T>
GenericParseValue(const std::string & value)177 inline Option<T> GenericParseValue(const std::string &value) {
178   T ret;
179   std::istringstream input(value);
180   input >> ret;
181 
182   if (input && input.eof()) {
183     return Option<T>(ret);
184   }
185 
186   return Option<T>(None());
187 }
188 
189 template <>
GenericParseValue(const std::string & value)190 inline Option<std::string> GenericParseValue(const std::string &value) {
191   return Option<std::string>(value);
192 }
193 
194 template <>
GenericParseValue(const std::string & value)195 inline Option<bool> GenericParseValue(const std::string &value) {
196   if (value == "true") {
197     return Option<bool>(true);
198   } else if (value == "false") {
199     return Option<bool>(false);
200   }
201 
202   return Option<bool>(None());
203 }
204 
205 }  // namespace lite
206 }  // namespace mindspore
207 
208 #endif  // MINDSPORE_LITE_SRC_COMMON_UTILS_H_
209