cmAlgorithms.h 9.7 KB

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