cmAlgorithms.h 9.2 KB

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