cmAlgorithms.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  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 "cm_kwiml.h"
  7. #include <algorithm>
  8. #include <functional>
  9. #include <iterator>
  10. #include <memory>
  11. #include <sstream>
  12. #include <string.h>
  13. #include <string>
  14. #include <utility>
  15. #include <vector>
  16. inline bool cmHasLiteralPrefixImpl(const std::string& str1, const char* str2,
  17. size_t N)
  18. {
  19. return strncmp(str1.c_str(), str2, N) == 0;
  20. }
  21. inline bool cmHasLiteralPrefixImpl(const char* str1, const char* str2,
  22. size_t N)
  23. {
  24. return strncmp(str1, str2, N) == 0;
  25. }
  26. inline bool cmHasLiteralSuffixImpl(const std::string& str1, const char* str2,
  27. size_t N)
  28. {
  29. size_t len = str1.size();
  30. return len >= N && strcmp(str1.c_str() + len - N, str2) == 0;
  31. }
  32. inline bool cmHasLiteralSuffixImpl(const char* str1, const char* str2,
  33. size_t N)
  34. {
  35. size_t len = strlen(str1);
  36. return len >= N && strcmp(str1 + len - N, str2) == 0;
  37. }
  38. template <typename T, size_t N>
  39. bool cmHasLiteralPrefix(const T& str1, const char (&str2)[N])
  40. {
  41. return cmHasLiteralPrefixImpl(str1, str2, N - 1);
  42. }
  43. template <typename T, size_t N>
  44. bool cmHasLiteralSuffix(const T& str1, const char (&str2)[N])
  45. {
  46. return cmHasLiteralSuffixImpl(str1, str2, N - 1);
  47. }
  48. struct cmStrCmp
  49. {
  50. cmStrCmp(const char* test)
  51. : m_test(test)
  52. {
  53. }
  54. cmStrCmp(const std::string& test)
  55. : m_test(test)
  56. {
  57. }
  58. bool operator()(const std::string& input) const { return m_test == input; }
  59. bool operator()(const char* input) const
  60. {
  61. return strcmp(input, m_test.c_str()) == 0;
  62. }
  63. private:
  64. const std::string m_test;
  65. };
  66. template <typename FwdIt>
  67. FwdIt cmRotate(FwdIt first, FwdIt middle, FwdIt last)
  68. {
  69. const typename std::iterator_traits<FwdIt>::difference_type dist =
  70. std::distance(middle, last);
  71. std::rotate(first, middle, last);
  72. std::advance(first, dist);
  73. return first;
  74. }
  75. template <typename Container, typename Predicate>
  76. void cmEraseIf(Container& cont, Predicate pred)
  77. {
  78. cont.erase(std::remove_if(cont.begin(), cont.end(), pred), cont.end());
  79. }
  80. namespace ContainerAlgorithms {
  81. template <typename T>
  82. struct cmIsPair
  83. {
  84. enum
  85. {
  86. value = false
  87. };
  88. };
  89. template <typename K, typename V>
  90. struct cmIsPair<std::pair<K, V>>
  91. {
  92. enum
  93. {
  94. value = true
  95. };
  96. };
  97. template <typename Range,
  98. bool valueTypeIsPair = cmIsPair<typename Range::value_type>::value>
  99. struct DefaultDeleter
  100. {
  101. void operator()(typename Range::value_type value) const { delete value; }
  102. };
  103. template <typename Range>
  104. struct DefaultDeleter<Range, /* valueTypeIsPair = */ true>
  105. {
  106. void operator()(typename Range::value_type value) const
  107. {
  108. delete value.second;
  109. }
  110. };
  111. template <typename FwdIt>
  112. FwdIt RemoveN(FwdIt i1, FwdIt i2, size_t n)
  113. {
  114. FwdIt m = i1;
  115. std::advance(m, n);
  116. return cmRotate(i1, m, i2);
  117. }
  118. template <typename Range>
  119. struct BinarySearcher
  120. {
  121. typedef typename Range::value_type argument_type;
  122. BinarySearcher(Range const& r)
  123. : m_range(r)
  124. {
  125. }
  126. bool operator()(argument_type const& item) const
  127. {
  128. return std::binary_search(m_range.begin(), m_range.end(), item);
  129. }
  130. private:
  131. Range const& m_range;
  132. };
  133. }
  134. template <typename const_iterator_>
  135. struct cmRange
  136. {
  137. typedef const_iterator_ const_iterator;
  138. typedef typename std::iterator_traits<const_iterator>::value_type value_type;
  139. typedef typename std::iterator_traits<const_iterator>::difference_type
  140. difference_type;
  141. cmRange(const_iterator begin_, const_iterator end_)
  142. : Begin(begin_)
  143. , End(end_)
  144. {
  145. }
  146. const_iterator begin() const { return Begin; }
  147. const_iterator end() const { return End; }
  148. bool empty() const { return std::distance(Begin, End) == 0; }
  149. difference_type size() const { return std::distance(Begin, End); }
  150. cmRange& advance(KWIML_INT_intptr_t amount)
  151. {
  152. std::advance(Begin, amount);
  153. return *this;
  154. }
  155. cmRange& retreat(KWIML_INT_intptr_t amount)
  156. {
  157. std::advance(End, -amount);
  158. return *this;
  159. }
  160. private:
  161. const_iterator Begin;
  162. const_iterator End;
  163. };
  164. typedef cmRange<std::vector<std::string>::const_iterator> cmStringRange;
  165. class cmListFileBacktrace;
  166. typedef cmRange<std::vector<cmListFileBacktrace>::const_iterator>
  167. cmBacktraceRange;
  168. template <typename Iter1, typename Iter2>
  169. cmRange<Iter1> cmMakeRange(Iter1 begin, Iter2 end)
  170. {
  171. return cmRange<Iter1>(begin, end);
  172. }
  173. template <typename Range>
  174. cmRange<typename Range::const_iterator> cmMakeRange(Range const& range)
  175. {
  176. return cmRange<typename Range::const_iterator>(range.begin(), range.end());
  177. }
  178. template <typename Range>
  179. void cmDeleteAll(Range const& r)
  180. {
  181. std::for_each(r.begin(), r.end(),
  182. ContainerAlgorithms::DefaultDeleter<Range>());
  183. }
  184. template <typename Range>
  185. std::string cmJoin(Range const& r, const char* delimiter)
  186. {
  187. if (r.empty()) {
  188. return std::string();
  189. }
  190. std::ostringstream os;
  191. typedef typename Range::value_type ValueType;
  192. typedef typename Range::const_iterator InputIt;
  193. const InputIt first = r.begin();
  194. InputIt last = r.end();
  195. --last;
  196. std::copy(first, last, std::ostream_iterator<ValueType>(os, delimiter));
  197. os << *last;
  198. return os.str();
  199. }
  200. template <typename Range>
  201. std::string cmJoin(Range const& r, std::string const& delimiter)
  202. {
  203. return cmJoin(r, delimiter.c_str());
  204. }
  205. template <typename Range>
  206. typename Range::const_iterator cmRemoveN(Range& r, size_t n)
  207. {
  208. return ContainerAlgorithms::RemoveN(r.begin(), r.end(), n);
  209. }
  210. template <typename Range, typename InputRange>
  211. typename Range::const_iterator cmRemoveIndices(Range& r, InputRange const& rem)
  212. {
  213. typename InputRange::const_iterator remIt = rem.begin();
  214. typename InputRange::const_iterator remEnd = rem.end();
  215. const typename Range::iterator rangeEnd = r.end();
  216. if (remIt == remEnd) {
  217. return rangeEnd;
  218. }
  219. typename Range::iterator writer = r.begin();
  220. std::advance(writer, *remIt);
  221. typename Range::iterator pivot = writer;
  222. typename InputRange::value_type prevRem = *remIt;
  223. ++remIt;
  224. size_t count = 1;
  225. for (; writer != rangeEnd && remIt != remEnd; ++count, ++remIt) {
  226. std::advance(pivot, *remIt - prevRem);
  227. prevRem = *remIt;
  228. writer = ContainerAlgorithms::RemoveN(writer, pivot, count);
  229. }
  230. return ContainerAlgorithms::RemoveN(writer, rangeEnd, count);
  231. }
  232. template <typename Range, typename MatchRange>
  233. typename Range::const_iterator cmRemoveMatching(Range& r, MatchRange const& m)
  234. {
  235. return std::remove_if(r.begin(), r.end(),
  236. ContainerAlgorithms::BinarySearcher<MatchRange>(m));
  237. }
  238. namespace ContainerAlgorithms {
  239. template <typename Range, typename T = typename Range::value_type>
  240. struct RemoveDuplicatesAPI
  241. {
  242. typedef typename Range::const_iterator const_iterator;
  243. typedef typename Range::const_iterator value_type;
  244. static bool lessThan(value_type a, value_type b) { return *a < *b; }
  245. static value_type uniqueValue(const_iterator a) { return a; }
  246. template <typename It>
  247. static bool valueCompare(It it, const_iterator it2)
  248. {
  249. return **it != *it2;
  250. }
  251. };
  252. template <typename Range, typename T>
  253. struct RemoveDuplicatesAPI<Range, T*>
  254. {
  255. typedef typename Range::const_iterator const_iterator;
  256. typedef T* value_type;
  257. static bool lessThan(value_type a, value_type b) { return a < b; }
  258. static value_type uniqueValue(const_iterator a) { return *a; }
  259. template <typename It>
  260. static bool valueCompare(It it, const_iterator it2)
  261. {
  262. return *it != *it2;
  263. }
  264. };
  265. }
  266. template <typename Range>
  267. typename Range::const_iterator cmRemoveDuplicates(Range& r)
  268. {
  269. typedef typename ContainerAlgorithms::RemoveDuplicatesAPI<Range> API;
  270. typedef typename API::value_type T;
  271. std::vector<T> unique;
  272. unique.reserve(r.size());
  273. std::vector<size_t> indices;
  274. size_t count = 0;
  275. const typename Range::const_iterator end = r.end();
  276. for (typename Range::const_iterator it = r.begin(); it != end;
  277. ++it, ++count) {
  278. const typename std::vector<T>::iterator low = std::lower_bound(
  279. unique.begin(), unique.end(), API::uniqueValue(it), API::lessThan);
  280. if (low == unique.end() || API::valueCompare(low, it)) {
  281. unique.insert(low, API::uniqueValue(it));
  282. } else {
  283. indices.push_back(count);
  284. }
  285. }
  286. if (indices.empty()) {
  287. return end;
  288. }
  289. return cmRemoveIndices(r, indices);
  290. }
  291. template <typename Range>
  292. std::string cmWrap(std::string const& prefix, Range const& r,
  293. std::string const& suffix, std::string const& sep)
  294. {
  295. if (r.empty()) {
  296. return std::string();
  297. }
  298. return prefix + cmJoin(r, suffix + sep + prefix) + suffix;
  299. }
  300. template <typename Range>
  301. std::string cmWrap(char prefix, Range const& r, char suffix,
  302. std::string const& sep)
  303. {
  304. return cmWrap(std::string(1, prefix), r, std::string(1, suffix), sep);
  305. }
  306. template <typename Range, typename T>
  307. typename Range::const_iterator cmFindNot(Range const& r, T const& t)
  308. {
  309. return std::find_if(r.begin(), r.end(), [&t](T const& i) { return i != t; });
  310. }
  311. template <typename Range>
  312. cmRange<typename Range::const_reverse_iterator> cmReverseRange(
  313. Range const& range)
  314. {
  315. return cmRange<typename Range::const_reverse_iterator>(range.rbegin(),
  316. range.rend());
  317. }
  318. template <class Iter>
  319. std::reverse_iterator<Iter> cmMakeReverseIterator(Iter it)
  320. {
  321. return std::reverse_iterator<Iter>(it);
  322. }
  323. inline bool cmHasSuffix(const std::string& str, const std::string& suffix)
  324. {
  325. if (str.size() < suffix.size()) {
  326. return false;
  327. }
  328. return str.compare(str.size() - suffix.size(), suffix.size(), suffix) == 0;
  329. }
  330. inline void cmStripSuffixIfExists(std::string& str, const std::string& suffix)
  331. {
  332. if (cmHasSuffix(str, suffix)) {
  333. str.resize(str.size() - suffix.size());
  334. }
  335. }
  336. namespace cm {
  337. #if defined(CMake_HAVE_CXX_MAKE_UNIQUE)
  338. using std::make_unique;
  339. #else
  340. template <typename T, typename... Args>
  341. std::unique_ptr<T> make_unique(Args&&... args)
  342. {
  343. return std::unique_ptr<T>(new T(std::forward<Args>(args)...));
  344. }
  345. #endif
  346. #if __cplusplus >= 201703L || defined(_MSVC_LANG) && _MSVC_LANG >= 201703L
  347. using std::size;
  348. #else
  349. // std::size backport from C++17.
  350. template <class C>
  351. #if !defined(_MSC_VER) || _MSC_VER >= 1900
  352. constexpr
  353. #endif
  354. auto
  355. size(C const& c) -> decltype(c.size())
  356. {
  357. return c.size();
  358. }
  359. template <typename T, size_t N>
  360. #if !defined(_MSC_VER) || _MSC_VER >= 1900
  361. constexpr
  362. #endif
  363. std::size_t
  364. size(const T (&)[N]) throw()
  365. {
  366. return N;
  367. }
  368. #endif
  369. #if __cplusplus >= 201402L || defined(_MSVC_LANG) && _MSVC_LANG >= 201402L
  370. using std::cbegin;
  371. using std::cend;
  372. #else
  373. // std::c{begin,end} backport from C++14
  374. template <class C>
  375. #if defined(_MSC_VER) && _MSC_VER < 1900
  376. auto cbegin(C const& c)
  377. #else
  378. constexpr auto cbegin(C const& c) noexcept(noexcept(std::begin(c)))
  379. #endif
  380. -> decltype(std::begin(c))
  381. {
  382. return std::begin(c);
  383. }
  384. template <class C>
  385. #if defined(_MSC_VER) && _MSC_VER < 1900
  386. auto cend(C const& c)
  387. #else
  388. constexpr auto cend(C const& c) noexcept(noexcept(std::end(c)))
  389. #endif
  390. -> decltype(std::end(c))
  391. {
  392. return std::end(c);
  393. }
  394. #endif
  395. } // namespace cm
  396. #endif