2016-09-27 22:01:08 +03:00
|
|
|
/* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
|
|
|
|
file Copyright.txt or https://cmake.org/licensing for details. */
|
2015-02-10 23:50:16 +03:00
|
|
|
#ifndef cmAlgorithms_h
|
|
|
|
#define cmAlgorithms_h
|
|
|
|
|
2016-09-01 21:05:48 +03:00
|
|
|
#include <cmConfigure.h>
|
|
|
|
|
2015-02-10 23:50:16 +03:00
|
|
|
#include "cmStandardIncludes.h"
|
|
|
|
|
2016-05-16 17:34:04 +03:00
|
|
|
inline bool cmHasLiteralPrefixImpl(const std::string& str1, const char* str2,
|
|
|
|
size_t N)
|
2015-02-10 23:50:16 +03:00
|
|
|
{
|
|
|
|
return strncmp(str1.c_str(), str2, N) == 0;
|
|
|
|
}
|
|
|
|
|
2016-05-16 17:34:04 +03:00
|
|
|
inline bool cmHasLiteralPrefixImpl(const char* str1, const char* str2,
|
|
|
|
size_t N)
|
2015-02-10 23:50:16 +03:00
|
|
|
{
|
|
|
|
return strncmp(str1, str2, N) == 0;
|
|
|
|
}
|
|
|
|
|
2016-05-16 17:34:04 +03:00
|
|
|
inline bool cmHasLiteralSuffixImpl(const std::string& str1, const char* str2,
|
2015-02-10 23:50:16 +03:00
|
|
|
size_t N)
|
|
|
|
{
|
|
|
|
size_t len = str1.size();
|
|
|
|
return len >= N && strcmp(str1.c_str() + len - N, str2) == 0;
|
|
|
|
}
|
|
|
|
|
2016-05-16 17:34:04 +03:00
|
|
|
inline bool cmHasLiteralSuffixImpl(const char* str1, const char* str2,
|
2015-02-10 23:50:16 +03:00
|
|
|
size_t N)
|
|
|
|
{
|
|
|
|
size_t len = strlen(str1);
|
|
|
|
return len >= N && strcmp(str1 + len - N, str2) == 0;
|
|
|
|
}
|
|
|
|
|
2016-05-16 17:34:04 +03:00
|
|
|
template <typename T, size_t N>
|
|
|
|
const T* cmArrayBegin(const T (&a)[N])
|
|
|
|
{
|
|
|
|
return a;
|
|
|
|
}
|
|
|
|
template <typename T, size_t N>
|
|
|
|
const T* cmArrayEnd(const T (&a)[N])
|
|
|
|
{
|
|
|
|
return a + N;
|
|
|
|
}
|
|
|
|
template <typename T, size_t N>
|
|
|
|
size_t cmArraySize(const T (&)[N])
|
|
|
|
{
|
|
|
|
return N;
|
|
|
|
}
|
2015-02-10 23:50:16 +03:00
|
|
|
|
2016-05-16 17:34:04 +03:00
|
|
|
template <typename T, size_t N>
|
2016-01-15 16:55:22 +03:00
|
|
|
bool cmHasLiteralPrefix(const T& str1, const char (&str2)[N])
|
2015-02-10 23:50:16 +03:00
|
|
|
{
|
|
|
|
return cmHasLiteralPrefixImpl(str1, str2, N - 1);
|
|
|
|
}
|
|
|
|
|
2016-05-16 17:34:04 +03:00
|
|
|
template <typename T, size_t N>
|
2016-01-15 16:55:22 +03:00
|
|
|
bool cmHasLiteralSuffix(const T& str1, const char (&str2)[N])
|
2015-02-10 23:50:16 +03:00
|
|
|
{
|
|
|
|
return cmHasLiteralSuffixImpl(str1, str2, N - 1);
|
|
|
|
}
|
|
|
|
|
2016-05-16 17:34:04 +03:00
|
|
|
struct cmStrCmp
|
|
|
|
{
|
|
|
|
cmStrCmp(const char* test)
|
|
|
|
: m_test(test)
|
2015-02-10 23:50:16 +03:00
|
|
|
{
|
|
|
|
}
|
2016-05-16 17:34:04 +03:00
|
|
|
cmStrCmp(const std::string& test)
|
|
|
|
: m_test(test)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
bool operator()(const std::string& input) const { return m_test == input; }
|
2015-02-10 23:50:16 +03:00
|
|
|
|
2016-05-16 17:34:04 +03:00
|
|
|
bool operator()(const char* input) const
|
2015-02-10 23:50:16 +03:00
|
|
|
{
|
|
|
|
return strcmp(input, m_test.c_str()) == 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
const std::string m_test;
|
|
|
|
};
|
|
|
|
|
2016-05-16 17:34:04 +03:00
|
|
|
template <typename FwdIt>
|
2015-02-17 23:59:26 +03:00
|
|
|
FwdIt cmRotate(FwdIt first, FwdIt middle, FwdIt last)
|
2015-02-16 01:39:38 +03:00
|
|
|
{
|
2015-02-20 23:52:09 +03:00
|
|
|
const typename std::iterator_traits<FwdIt>::difference_type dist =
|
2016-05-16 17:34:04 +03:00
|
|
|
std::distance(middle, last);
|
2015-02-16 01:39:38 +03:00
|
|
|
std::rotate(first, middle, last);
|
2015-02-17 23:59:26 +03:00
|
|
|
std::advance(first, dist);
|
|
|
|
return first;
|
2015-02-16 01:39:38 +03:00
|
|
|
}
|
|
|
|
|
2015-02-10 23:50:16 +03:00
|
|
|
namespace ContainerAlgorithms {
|
|
|
|
|
2016-05-16 17:34:04 +03:00
|
|
|
template <typename T>
|
2015-02-10 23:50:16 +03:00
|
|
|
struct cmIsPair
|
|
|
|
{
|
2016-05-16 17:34:04 +03:00
|
|
|
enum
|
|
|
|
{
|
|
|
|
value = false
|
|
|
|
};
|
2015-02-10 23:50:16 +03:00
|
|
|
};
|
|
|
|
|
2016-05-16 17:34:04 +03:00
|
|
|
template <typename K, typename V>
|
2015-02-10 23:50:16 +03:00
|
|
|
struct cmIsPair<std::pair<K, V> >
|
|
|
|
{
|
2016-05-16 17:34:04 +03:00
|
|
|
enum
|
|
|
|
{
|
|
|
|
value = true
|
|
|
|
};
|
2015-02-10 23:50:16 +03:00
|
|
|
};
|
|
|
|
|
2016-05-16 17:34:04 +03:00
|
|
|
template <typename Range,
|
|
|
|
bool valueTypeIsPair = cmIsPair<typename Range::value_type>::value>
|
2015-02-10 23:50:16 +03:00
|
|
|
struct DefaultDeleter
|
|
|
|
{
|
2016-05-16 17:34:04 +03:00
|
|
|
void operator()(typename Range::value_type value) const { delete value; }
|
2015-02-10 23:50:16 +03:00
|
|
|
};
|
|
|
|
|
2016-05-16 17:34:04 +03:00
|
|
|
template <typename Range>
|
2015-02-20 23:52:36 +03:00
|
|
|
struct DefaultDeleter<Range, /* valueTypeIsPair = */ true>
|
2015-02-10 23:50:16 +03:00
|
|
|
{
|
2016-05-16 17:34:04 +03:00
|
|
|
void operator()(typename Range::value_type value) const
|
|
|
|
{
|
2015-02-10 23:50:16 +03:00
|
|
|
delete value.second;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-05-16 17:34:04 +03:00
|
|
|
template <typename FwdIt>
|
2015-02-20 23:56:45 +03:00
|
|
|
FwdIt RemoveN(FwdIt i1, FwdIt i2, size_t n)
|
2015-02-12 20:47:10 +03:00
|
|
|
{
|
2015-02-20 23:56:45 +03:00
|
|
|
FwdIt m = i1;
|
2015-02-20 23:55:19 +03:00
|
|
|
std::advance(m, n);
|
|
|
|
return cmRotate(i1, m, i2);
|
2015-02-12 20:47:10 +03:00
|
|
|
}
|
|
|
|
|
2016-05-16 17:34:04 +03:00
|
|
|
template <typename Range>
|
2015-02-15 20:58:36 +03:00
|
|
|
struct BinarySearcher
|
|
|
|
{
|
|
|
|
typedef typename Range::value_type argument_type;
|
|
|
|
BinarySearcher(Range const& r)
|
|
|
|
: m_range(r)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2015-02-17 21:32:52 +03:00
|
|
|
bool operator()(argument_type const& item) const
|
2015-02-15 20:58:36 +03:00
|
|
|
{
|
|
|
|
return std::binary_search(m_range.begin(), m_range.end(), item);
|
|
|
|
}
|
2016-05-16 17:34:04 +03:00
|
|
|
|
2015-02-15 20:58:36 +03:00
|
|
|
private:
|
|
|
|
Range const& m_range;
|
|
|
|
};
|
2015-02-11 00:19:21 +03:00
|
|
|
}
|
|
|
|
|
2016-05-16 17:34:04 +03:00
|
|
|
template <typename const_iterator_>
|
2015-07-18 11:47:04 +03:00
|
|
|
struct cmRange
|
|
|
|
{
|
|
|
|
typedef const_iterator_ const_iterator;
|
|
|
|
typedef typename std::iterator_traits<const_iterator>::value_type value_type;
|
|
|
|
typedef typename std::iterator_traits<const_iterator>::difference_type
|
|
|
|
difference_type;
|
|
|
|
cmRange(const_iterator begin_, const_iterator end_)
|
2016-05-16 17:34:04 +03:00
|
|
|
: Begin(begin_)
|
|
|
|
, End(end_)
|
|
|
|
{
|
|
|
|
}
|
2015-07-18 11:47:04 +03:00
|
|
|
const_iterator begin() const { return Begin; }
|
|
|
|
const_iterator end() const { return End; }
|
|
|
|
bool empty() const { return std::distance(Begin, End) == 0; }
|
|
|
|
difference_type size() const { return std::distance(Begin, End); }
|
2015-12-17 23:03:42 +03:00
|
|
|
cmRange& advance(KWIML_INT_intptr_t amount)
|
2015-07-18 11:47:04 +03:00
|
|
|
{
|
|
|
|
std::advance(Begin, amount);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2015-12-17 23:03:42 +03:00
|
|
|
cmRange& retreat(KWIML_INT_intptr_t amount)
|
2015-07-18 11:47:04 +03:00
|
|
|
{
|
|
|
|
std::advance(End, -amount);
|
|
|
|
return *this;
|
|
|
|
}
|
2016-05-16 17:34:04 +03:00
|
|
|
|
2015-07-18 11:47:04 +03:00
|
|
|
private:
|
|
|
|
const_iterator Begin;
|
|
|
|
const_iterator End;
|
|
|
|
};
|
|
|
|
|
2015-07-18 12:09:20 +03:00
|
|
|
typedef cmRange<std::vector<std::string>::const_iterator> cmStringRange;
|
|
|
|
|
|
|
|
class cmListFileBacktrace;
|
2016-05-16 17:34:04 +03:00
|
|
|
typedef cmRange<std::vector<cmListFileBacktrace>::const_iterator>
|
|
|
|
cmBacktraceRange;
|
2015-07-18 12:09:20 +03:00
|
|
|
|
2016-05-16 17:34:04 +03:00
|
|
|
template <typename Iter1, typename Iter2>
|
2015-07-18 11:47:04 +03:00
|
|
|
cmRange<Iter1> cmMakeRange(Iter1 begin, Iter2 end)
|
2015-02-11 00:19:21 +03:00
|
|
|
{
|
2015-07-18 11:47:04 +03:00
|
|
|
return cmRange<Iter1>(begin, end);
|
2015-02-10 23:50:16 +03:00
|
|
|
}
|
|
|
|
|
2016-05-16 17:34:04 +03:00
|
|
|
template <typename Range>
|
|
|
|
cmRange<typename Range::const_iterator> cmMakeRange(Range const& range)
|
2015-02-11 00:14:54 +03:00
|
|
|
{
|
2016-05-16 17:34:04 +03:00
|
|
|
return cmRange<typename Range::const_iterator>(range.begin(), range.end());
|
2015-02-11 00:14:54 +03:00
|
|
|
}
|
|
|
|
|
2016-05-16 17:34:04 +03:00
|
|
|
template <typename Range>
|
2015-02-20 23:52:36 +03:00
|
|
|
void cmDeleteAll(Range const& r)
|
2015-02-10 23:50:16 +03:00
|
|
|
{
|
2015-02-20 23:52:36 +03:00
|
|
|
std::for_each(r.begin(), r.end(),
|
|
|
|
ContainerAlgorithms::DefaultDeleter<Range>());
|
2015-02-10 23:50:16 +03:00
|
|
|
}
|
|
|
|
|
2016-05-16 17:34:04 +03:00
|
|
|
template <typename Range>
|
2015-02-10 23:50:16 +03:00
|
|
|
std::string cmJoin(Range const& r, const char* delimiter)
|
|
|
|
{
|
2016-05-16 17:34:04 +03:00
|
|
|
if (r.empty()) {
|
2015-02-10 23:50:16 +03:00
|
|
|
return std::string();
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2015-02-10 23:50:16 +03:00
|
|
|
std::ostringstream os;
|
|
|
|
typedef typename Range::value_type ValueType;
|
|
|
|
typedef typename Range::const_iterator InputIt;
|
2015-02-20 23:52:09 +03:00
|
|
|
const InputIt first = r.begin();
|
2015-02-10 23:50:16 +03:00
|
|
|
InputIt last = r.end();
|
|
|
|
--last;
|
2016-05-16 17:34:04 +03:00
|
|
|
std::copy(first, last, std::ostream_iterator<ValueType>(os, delimiter));
|
2015-02-10 23:50:16 +03:00
|
|
|
|
|
|
|
os << *last;
|
|
|
|
|
|
|
|
return os.str();
|
|
|
|
}
|
|
|
|
|
2016-05-16 17:34:04 +03:00
|
|
|
template <typename Range>
|
2015-02-10 23:50:16 +03:00
|
|
|
std::string cmJoin(Range const& r, std::string delimiter)
|
|
|
|
{
|
|
|
|
return cmJoin(r, delimiter.c_str());
|
2016-01-30 00:16:03 +03:00
|
|
|
}
|
2015-02-10 23:50:16 +03:00
|
|
|
|
2016-05-16 17:34:04 +03:00
|
|
|
template <typename Range>
|
2015-02-12 20:47:10 +03:00
|
|
|
typename Range::const_iterator cmRemoveN(Range& r, size_t n)
|
|
|
|
{
|
|
|
|
return ContainerAlgorithms::RemoveN(r.begin(), r.end(), n);
|
|
|
|
}
|
|
|
|
|
2016-05-16 17:34:04 +03:00
|
|
|
template <typename Range, typename InputRange>
|
2015-02-12 20:47:10 +03:00
|
|
|
typename Range::const_iterator cmRemoveIndices(Range& r, InputRange const& rem)
|
|
|
|
{
|
|
|
|
typename InputRange::const_iterator remIt = rem.begin();
|
2015-02-21 00:19:14 +03:00
|
|
|
typename InputRange::const_iterator remEnd = rem.end();
|
2015-03-06 01:14:27 +03:00
|
|
|
const typename Range::iterator rangeEnd = r.end();
|
2016-05-16 17:34:04 +03:00
|
|
|
if (remIt == remEnd) {
|
2015-03-06 01:14:27 +03:00
|
|
|
return rangeEnd;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2015-02-12 20:47:10 +03:00
|
|
|
|
2015-02-21 00:07:28 +03:00
|
|
|
typename Range::iterator writer = r.begin();
|
|
|
|
std::advance(writer, *remIt);
|
2015-02-21 00:10:41 +03:00
|
|
|
typename Range::iterator pivot = writer;
|
|
|
|
typename InputRange::value_type prevRem = *remIt;
|
2015-02-12 20:47:10 +03:00
|
|
|
++remIt;
|
|
|
|
size_t count = 1;
|
2016-05-16 17:34:04 +03:00
|
|
|
for (; writer != rangeEnd && remIt != remEnd; ++count, ++remIt) {
|
2015-02-21 00:10:41 +03:00
|
|
|
std::advance(pivot, *remIt - prevRem);
|
|
|
|
prevRem = *remIt;
|
2015-02-21 00:07:28 +03:00
|
|
|
writer = ContainerAlgorithms::RemoveN(writer, pivot, count);
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2015-02-21 00:19:14 +03:00
|
|
|
return ContainerAlgorithms::RemoveN(writer, rangeEnd, count);
|
2015-02-12 20:47:10 +03:00
|
|
|
}
|
|
|
|
|
2016-05-16 17:34:04 +03:00
|
|
|
template <typename Range, typename MatchRange>
|
|
|
|
typename Range::const_iterator cmRemoveMatching(Range& r, MatchRange const& m)
|
2015-02-15 20:58:36 +03:00
|
|
|
{
|
|
|
|
return std::remove_if(r.begin(), r.end(),
|
|
|
|
ContainerAlgorithms::BinarySearcher<MatchRange>(m));
|
|
|
|
}
|
|
|
|
|
2015-03-01 23:57:16 +03:00
|
|
|
namespace ContainerAlgorithms {
|
|
|
|
|
2016-05-16 17:34:04 +03:00
|
|
|
template <typename Range, typename T = typename Range::value_type>
|
2015-03-01 23:57:16 +03:00
|
|
|
struct RemoveDuplicatesAPI
|
|
|
|
{
|
|
|
|
typedef typename Range::const_iterator const_iterator;
|
|
|
|
typedef typename Range::const_iterator value_type;
|
|
|
|
|
|
|
|
static bool lessThan(value_type a, value_type b) { return *a < *b; }
|
|
|
|
static value_type uniqueValue(const_iterator a) { return a; }
|
2016-05-16 17:34:04 +03:00
|
|
|
template <typename It>
|
|
|
|
static bool valueCompare(It it, const_iterator it2)
|
|
|
|
{
|
|
|
|
return **it != *it2;
|
|
|
|
}
|
2015-03-01 23:57:16 +03:00
|
|
|
};
|
|
|
|
|
2016-05-16 17:34:04 +03:00
|
|
|
template <typename Range, typename T>
|
2015-03-08 11:43:11 +03:00
|
|
|
struct RemoveDuplicatesAPI<Range, T*>
|
|
|
|
{
|
|
|
|
typedef typename Range::const_iterator const_iterator;
|
|
|
|
typedef T* value_type;
|
|
|
|
|
|
|
|
static bool lessThan(value_type a, value_type b) { return a < b; }
|
|
|
|
static value_type uniqueValue(const_iterator a) { return *a; }
|
2016-05-16 17:34:04 +03:00
|
|
|
template <typename It>
|
|
|
|
static bool valueCompare(It it, const_iterator it2)
|
|
|
|
{
|
|
|
|
return *it != *it2;
|
|
|
|
}
|
2015-03-08 11:43:11 +03:00
|
|
|
};
|
2015-03-01 23:57:16 +03:00
|
|
|
}
|
|
|
|
|
2016-05-16 17:34:04 +03:00
|
|
|
template <typename Range>
|
2015-02-15 21:08:12 +03:00
|
|
|
typename Range::const_iterator cmRemoveDuplicates(Range& r)
|
|
|
|
{
|
2015-03-01 23:57:16 +03:00
|
|
|
typedef typename ContainerAlgorithms::RemoveDuplicatesAPI<Range> API;
|
|
|
|
typedef typename API::value_type T;
|
2015-03-01 23:53:04 +03:00
|
|
|
std::vector<T> unique;
|
2015-02-15 21:08:12 +03:00
|
|
|
unique.reserve(r.size());
|
|
|
|
std::vector<size_t> indices;
|
|
|
|
size_t count = 0;
|
2015-03-08 16:04:54 +03:00
|
|
|
const typename Range::const_iterator end = r.end();
|
2016-05-16 17:34:04 +03:00
|
|
|
for (typename Range::const_iterator it = r.begin(); it != end;
|
|
|
|
++it, ++count) {
|
|
|
|
const typename std::vector<T>::iterator low = std::lower_bound(
|
|
|
|
unique.begin(), unique.end(), API::uniqueValue(it), API::lessThan);
|
|
|
|
if (low == unique.end() || API::valueCompare(low, it)) {
|
2015-03-01 23:57:16 +03:00
|
|
|
unique.insert(low, API::uniqueValue(it));
|
2016-05-16 17:34:04 +03:00
|
|
|
} else {
|
2015-02-15 21:08:12 +03:00
|
|
|
indices.push_back(count);
|
|
|
|
}
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
|
|
|
if (indices.empty()) {
|
2015-02-21 00:19:14 +03:00
|
|
|
return end;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2015-02-15 21:08:12 +03:00
|
|
|
return cmRemoveIndices(r, indices);
|
|
|
|
}
|
|
|
|
|
2016-05-16 17:34:04 +03:00
|
|
|
template <typename Range>
|
2015-02-19 01:50:36 +03:00
|
|
|
std::string cmWrap(std::string prefix, Range const& r, std::string suffix,
|
|
|
|
std::string sep)
|
|
|
|
{
|
2016-05-16 17:34:04 +03:00
|
|
|
if (r.empty()) {
|
2015-02-19 01:50:36 +03:00
|
|
|
return std::string();
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2015-02-19 01:50:36 +03:00
|
|
|
return prefix + cmJoin(r, (suffix + sep + prefix).c_str()) + suffix;
|
|
|
|
}
|
|
|
|
|
2016-05-16 17:34:04 +03:00
|
|
|
template <typename Range>
|
2015-02-19 01:50:36 +03:00
|
|
|
std::string cmWrap(char prefix, Range const& r, char suffix, std::string sep)
|
|
|
|
{
|
|
|
|
return cmWrap(std::string(1, prefix), r, std::string(1, suffix), sep);
|
|
|
|
}
|
|
|
|
|
2016-05-16 17:34:04 +03:00
|
|
|
template <typename Range, typename T>
|
2015-02-12 01:53:41 +03:00
|
|
|
typename Range::const_iterator cmFindNot(Range const& r, T const& t)
|
|
|
|
{
|
|
|
|
return std::find_if(r.begin(), r.end(),
|
|
|
|
std::bind1st(std::not_equal_to<T>(), t));
|
|
|
|
}
|
|
|
|
|
2016-05-16 17:34:04 +03:00
|
|
|
template <typename Range>
|
|
|
|
cmRange<typename Range::const_reverse_iterator> cmReverseRange(
|
|
|
|
Range const& range)
|
2015-02-18 00:04:25 +03:00
|
|
|
{
|
2016-05-16 17:34:04 +03:00
|
|
|
return cmRange<typename Range::const_reverse_iterator>(range.rbegin(),
|
|
|
|
range.rend());
|
2015-02-18 00:04:25 +03:00
|
|
|
}
|
|
|
|
|
2015-07-21 23:42:03 +03:00
|
|
|
template <class Iter>
|
|
|
|
std::reverse_iterator<Iter> cmMakeReverseIterator(Iter it)
|
|
|
|
{
|
|
|
|
return std::reverse_iterator<Iter>(it);
|
|
|
|
}
|
|
|
|
|
2016-05-14 02:18:20 +03:00
|
|
|
inline bool cmHasSuffix(const std::string& str, const std::string& suffix)
|
|
|
|
{
|
|
|
|
if (str.size() < suffix.size()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return str.compare(str.size() - suffix.size(), suffix.size(), suffix) == 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void cmStripSuffixIfExists(std::string& str, const std::string& suffix)
|
|
|
|
{
|
|
|
|
if (cmHasSuffix(str, suffix)) {
|
|
|
|
str.resize(str.size() - suffix.size());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-10 23:50:16 +03:00
|
|
|
#endif
|