cmAlgorithms.h 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file Copyright.txt or https://cmake.org/licensing for details. */
  3. #ifndef cmAlgorithms_h
  4. #define cmAlgorithms_h
  5. #include "cmConfigure.h" // IWYU pragma: keep
  6. #include "cmRange.h"
  7. #include "cm_kwiml.h"
  8. #include "cm_string_view.hxx"
  9. #include <algorithm>
  10. #include <functional>
  11. #include <iterator>
  12. #include <sstream>
  13. #include <string.h>
  14. #include <string>
  15. #include <unordered_set>
  16. #include <utility>
  17. #include <vector>
  18. inline bool cmHasLiteralPrefixImpl(const std::string& str1, const char* str2,
  19. size_t N)
  20. {
  21. return strncmp(str1.c_str(), str2, N) == 0;
  22. }
  23. inline bool cmHasLiteralPrefixImpl(const char* str1, const char* str2,
  24. size_t N)
  25. {
  26. return strncmp(str1, str2, N) == 0;
  27. }
  28. inline bool cmHasLiteralSuffixImpl(const std::string& str1, const char* str2,
  29. size_t N)
  30. {
  31. size_t len = str1.size();
  32. return len >= N && strcmp(str1.c_str() + len - N, str2) == 0;
  33. }
  34. inline bool cmHasLiteralSuffixImpl(const char* str1, const char* str2,
  35. size_t N)
  36. {
  37. size_t len = strlen(str1);
  38. return len >= N && strcmp(str1 + len - N, str2) == 0;
  39. }
  40. template <typename T, size_t N>
  41. bool cmHasLiteralPrefix(const T& str1, const char (&str2)[N])
  42. {
  43. return cmHasLiteralPrefixImpl(str1, str2, N - 1);
  44. }
  45. template <typename T, size_t N>
  46. bool cmHasLiteralSuffix(const T& str1, const char (&str2)[N])
  47. {
  48. return cmHasLiteralSuffixImpl(str1, str2, N - 1);
  49. }
  50. struct cmStrCmp
  51. {
  52. cmStrCmp(const char* test)
  53. : m_test(test)
  54. {
  55. }
  56. cmStrCmp(std::string test)
  57. : m_test(std::move(test))
  58. {
  59. }
  60. bool operator()(const std::string& input) const { return m_test == input; }
  61. bool operator()(const char* input) const
  62. {
  63. return strcmp(input, m_test.c_str()) == 0;
  64. }
  65. private:
  66. const std::string m_test;
  67. };
  68. template <typename FwdIt>
  69. FwdIt cmRotate(FwdIt first, FwdIt middle, FwdIt last)
  70. {
  71. const typename std::iterator_traits<FwdIt>::difference_type dist =
  72. std::distance(middle, last);
  73. std::rotate(first, middle, last);
  74. std::advance(first, dist);
  75. return first;
  76. }
  77. template <typename Container, typename Predicate>
  78. void cmEraseIf(Container& cont, Predicate pred)
  79. {
  80. cont.erase(std::remove_if(cont.begin(), cont.end(), pred), cont.end());
  81. }
  82. namespace ContainerAlgorithms {
  83. template <typename T>
  84. struct cmIsPair
  85. {
  86. enum
  87. {
  88. value = false
  89. };
  90. };
  91. template <typename K, typename V>
  92. struct cmIsPair<std::pair<K, V>>
  93. {
  94. enum
  95. {
  96. value = true
  97. };
  98. };
  99. template <typename Range,
  100. bool valueTypeIsPair = cmIsPair<typename Range::value_type>::value>
  101. struct DefaultDeleter
  102. {
  103. void operator()(typename Range::value_type value) const { delete value; }
  104. };
  105. template <typename Range>
  106. struct DefaultDeleter<Range, /* valueTypeIsPair = */ true>
  107. {
  108. void operator()(typename Range::value_type value) const
  109. {
  110. delete value.second;
  111. }
  112. };
  113. template <typename FwdIt>
  114. FwdIt RemoveN(FwdIt i1, FwdIt i2, size_t n)
  115. {
  116. FwdIt m = i1;
  117. std::advance(m, n);
  118. return cmRotate(i1, m, i2);
  119. }
  120. template <typename Range>
  121. struct BinarySearcher
  122. {
  123. typedef typename Range::value_type argument_type;
  124. BinarySearcher(Range const& r)
  125. : m_range(r)
  126. {
  127. }
  128. bool operator()(argument_type const& item) const
  129. {
  130. return std::binary_search(m_range.begin(), m_range.end(), item);
  131. }
  132. private:
  133. Range const& m_range;
  134. };
  135. }
  136. typedef cmRange<std::vector<std::string>::const_iterator> cmStringRange;
  137. class cmListFileBacktrace;
  138. typedef cmRange<std::vector<cmListFileBacktrace>::const_iterator>
  139. cmBacktraceRange;
  140. template <typename Range>
  141. void cmDeleteAll(Range const& r)
  142. {
  143. std::for_each(r.begin(), r.end(),
  144. ContainerAlgorithms::DefaultDeleter<Range>());
  145. }
  146. template <typename T, typename Range>
  147. void cmAppend(std::vector<T>& v, Range const& r)
  148. {
  149. v.insert(v.end(), r.begin(), r.end());
  150. }
  151. template <typename T, typename InputIt>
  152. void cmAppend(std::vector<T>& v, InputIt first, InputIt last)
  153. {
  154. v.insert(v.end(), first, last);
  155. }
  156. template <typename Range>
  157. std::string cmJoin(Range const& r, const char* delimiter)
  158. {
  159. if (r.empty()) {
  160. return std::string();
  161. }
  162. std::ostringstream os;
  163. typedef typename Range::value_type ValueType;
  164. typedef typename Range::const_iterator InputIt;
  165. const InputIt first = r.begin();
  166. InputIt last = r.end();
  167. --last;
  168. std::copy(first, last, std::ostream_iterator<ValueType>(os, delimiter));
  169. os << *last;
  170. return os.str();
  171. }
  172. template <typename Range>
  173. std::string cmJoin(Range const& r, std::string const& delimiter)
  174. {
  175. return cmJoin(r, delimiter.c_str());
  176. }
  177. template <typename Range>
  178. typename Range::const_iterator cmRemoveN(Range& r, size_t n)
  179. {
  180. return ContainerAlgorithms::RemoveN(r.begin(), r.end(), n);
  181. }
  182. template <typename Range, typename InputRange>
  183. typename Range::const_iterator cmRemoveIndices(Range& r, InputRange const& rem)
  184. {
  185. typename InputRange::const_iterator remIt = rem.begin();
  186. typename InputRange::const_iterator remEnd = rem.end();
  187. const typename Range::iterator rangeEnd = r.end();
  188. if (remIt == remEnd) {
  189. return rangeEnd;
  190. }
  191. typename Range::iterator writer = r.begin();
  192. std::advance(writer, *remIt);
  193. typename Range::iterator pivot = writer;
  194. typename InputRange::value_type prevRem = *remIt;
  195. ++remIt;
  196. size_t count = 1;
  197. for (; writer != rangeEnd && remIt != remEnd; ++count, ++remIt) {
  198. std::advance(pivot, *remIt - prevRem);
  199. prevRem = *remIt;
  200. writer = ContainerAlgorithms::RemoveN(writer, pivot, count);
  201. }
  202. return ContainerAlgorithms::RemoveN(writer, rangeEnd, count);
  203. }
  204. template <typename Range, typename MatchRange>
  205. typename Range::const_iterator cmRemoveMatching(Range& r, MatchRange const& m)
  206. {
  207. return std::remove_if(r.begin(), r.end(),
  208. ContainerAlgorithms::BinarySearcher<MatchRange>(m));
  209. }
  210. template <typename ForwardIterator>
  211. ForwardIterator cmRemoveDuplicates(ForwardIterator first, ForwardIterator last)
  212. {
  213. using Value = typename std::iterator_traits<ForwardIterator>::value_type;
  214. using Hash = struct
  215. {
  216. std::size_t operator()(ForwardIterator it) const
  217. {
  218. return std::hash<Value>{}(*it);
  219. }
  220. };
  221. using Equal = struct
  222. {
  223. bool operator()(ForwardIterator it1, ForwardIterator it2) const
  224. {
  225. return *it1 == *it2;
  226. }
  227. };
  228. std::unordered_set<ForwardIterator, Hash, Equal> uniq;
  229. ForwardIterator result = first;
  230. while (first != last) {
  231. if (uniq.find(first) == uniq.end()) {
  232. if (result != first) {
  233. *result = std::move(*first);
  234. }
  235. uniq.insert(result);
  236. ++result;
  237. }
  238. ++first;
  239. }
  240. return result;
  241. }
  242. template <typename Range>
  243. typename Range::const_iterator cmRemoveDuplicates(Range& r)
  244. {
  245. return cmRemoveDuplicates(r.begin(), r.end());
  246. }
  247. template <typename Range>
  248. std::string cmWrap(std::string const& prefix, Range const& r,
  249. std::string const& suffix, std::string const& sep)
  250. {
  251. if (r.empty()) {
  252. return std::string();
  253. }
  254. return prefix + cmJoin(r, suffix + sep + prefix) + suffix;
  255. }
  256. template <typename Range>
  257. std::string cmWrap(char prefix, Range const& r, char suffix,
  258. std::string const& sep)
  259. {
  260. return cmWrap(std::string(1, prefix), r, std::string(1, suffix), sep);
  261. }
  262. template <typename Range, typename T>
  263. typename Range::const_iterator cmFindNot(Range const& r, T const& t)
  264. {
  265. return std::find_if(r.begin(), r.end(), [&t](T const& i) { return i != t; });
  266. }
  267. template <class Iter>
  268. std::reverse_iterator<Iter> cmMakeReverseIterator(Iter it)
  269. {
  270. return std::reverse_iterator<Iter>(it);
  271. }
  272. /** Returns true if string @a str starts with the character @a prefix. **/
  273. inline bool cmHasPrefix(cm::string_view str, char prefix)
  274. {
  275. return !str.empty() && (str.front() == prefix);
  276. }
  277. /** Returns true if string @a str starts with string @a prefix. **/
  278. inline bool cmHasPrefix(cm::string_view str, cm::string_view prefix)
  279. {
  280. return str.compare(0, prefix.size(), prefix) == 0;
  281. }
  282. /** Returns true if string @a str ends with the character @a suffix. **/
  283. inline bool cmHasSuffix(cm::string_view str, char suffix)
  284. {
  285. return !str.empty() && (str.back() == suffix);
  286. }
  287. /** Returns true if string @a str ends with string @a suffix. **/
  288. inline bool cmHasSuffix(cm::string_view str, cm::string_view suffix)
  289. {
  290. return str.size() >= suffix.size() &&
  291. str.compare(str.size() - suffix.size(), suffix.size(), suffix) == 0;
  292. }
  293. /** Removes an existing suffix character of from the string @a str. **/
  294. inline void cmStripSuffixIfExists(std::string& str, char suffix)
  295. {
  296. if (cmHasSuffix(str, suffix)) {
  297. str.pop_back();
  298. }
  299. }
  300. /** Removes an existing suffix string of from the string @a str. **/
  301. inline void cmStripSuffixIfExists(std::string& str, cm::string_view suffix)
  302. {
  303. if (cmHasSuffix(str, suffix)) {
  304. str.resize(str.size() - suffix.size());
  305. }
  306. }
  307. namespace cm {
  308. #if __cplusplus >= 201703L || defined(_MSVC_LANG) && _MSVC_LANG >= 201703L
  309. using std::size;
  310. #else
  311. // std::size backport from C++17.
  312. template <class C>
  313. # if !defined(_MSC_VER) || _MSC_VER >= 1900
  314. constexpr
  315. # endif
  316. auto
  317. size(C const& c) -> decltype(c.size())
  318. {
  319. return c.size();
  320. }
  321. template <typename T, size_t N>
  322. # if !defined(_MSC_VER) || _MSC_VER >= 1900
  323. constexpr
  324. # endif
  325. std::size_t
  326. size(const T (&)[N]) throw()
  327. {
  328. return N;
  329. }
  330. #endif
  331. template <typename T>
  332. int isize(const T& t)
  333. {
  334. return static_cast<int>(cm::size(t));
  335. }
  336. #if __cplusplus >= 201402L || defined(_MSVC_LANG) && _MSVC_LANG >= 201402L
  337. using std::cbegin;
  338. using std::cend;
  339. #else
  340. // std::c{begin,end} backport from C++14
  341. template <class C>
  342. # if defined(_MSC_VER) && _MSC_VER < 1900
  343. auto cbegin(C const& c)
  344. # else
  345. constexpr auto cbegin(C const& c) noexcept(noexcept(std::begin(c)))
  346. # endif
  347. -> decltype(std::begin(c))
  348. {
  349. return std::begin(c);
  350. }
  351. template <class C>
  352. # if defined(_MSC_VER) && _MSC_VER < 1900
  353. auto cend(C const& c)
  354. # else
  355. constexpr auto cend(C const& c) noexcept(noexcept(std::end(c)))
  356. # endif
  357. -> decltype(std::end(c))
  358. {
  359. return std::end(c);
  360. }
  361. #endif
  362. } // namespace cm
  363. #endif