cmAlgorithms.h 9.4 KB

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