2016-09-27 19:01:08 +00: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 20:50:16 +00:00
|
|
|
#ifndef cmAlgorithms_h
|
|
|
|
#define cmAlgorithms_h
|
|
|
|
|
2017-04-11 20:00:21 +00:00
|
|
|
#include "cmConfigure.h" // IWYU pragma: keep
|
2016-09-01 18:05:48 +00:00
|
|
|
|
2019-02-15 20:34:44 +00:00
|
|
|
#include "cmRange.h"
|
2017-04-11 20:00:21 +00:00
|
|
|
#include "cm_kwiml.h"
|
2016-11-05 20:40:14 +00:00
|
|
|
#include <algorithm>
|
|
|
|
#include <functional>
|
|
|
|
#include <iterator>
|
2018-05-23 08:43:04 +00:00
|
|
|
#include <unordered_set>
|
2016-11-05 20:40:14 +00:00
|
|
|
#include <utility>
|
|
|
|
#include <vector>
|
2015-02-10 20:50:16 +00:00
|
|
|
|
2019-08-19 12:31:52 +00:00
|
|
|
template <std::size_t N>
|
|
|
|
struct cmOverloadPriority : cmOverloadPriority<N - 1>
|
|
|
|
{
|
|
|
|
};
|
|
|
|
|
|
|
|
template <>
|
|
|
|
struct cmOverloadPriority<0>
|
|
|
|
{
|
|
|
|
};
|
|
|
|
|
2016-05-16 14:34:04 +00:00
|
|
|
template <typename FwdIt>
|
2015-02-17 20:59:26 +00:00
|
|
|
FwdIt cmRotate(FwdIt first, FwdIt middle, FwdIt last)
|
2015-02-15 22:39:38 +00:00
|
|
|
{
|
2015-02-20 20:52:09 +00:00
|
|
|
const typename std::iterator_traits<FwdIt>::difference_type dist =
|
2016-05-16 14:34:04 +00:00
|
|
|
std::distance(middle, last);
|
2015-02-15 22:39:38 +00:00
|
|
|
std::rotate(first, middle, last);
|
2015-02-17 20:59:26 +00:00
|
|
|
std::advance(first, dist);
|
|
|
|
return first;
|
2015-02-15 22:39:38 +00:00
|
|
|
}
|
|
|
|
|
2017-02-10 22:04:32 +00:00
|
|
|
template <typename Container, typename Predicate>
|
|
|
|
void cmEraseIf(Container& cont, Predicate pred)
|
|
|
|
{
|
|
|
|
cont.erase(std::remove_if(cont.begin(), cont.end(), pred), cont.end());
|
|
|
|
}
|
|
|
|
|
2019-08-19 12:31:52 +00:00
|
|
|
template <typename Range, typename Key>
|
|
|
|
auto cmContainsImpl(Range const& range, Key const& key, cmOverloadPriority<2>)
|
|
|
|
-> decltype(range.exists(key))
|
|
|
|
{
|
|
|
|
return range.exists(key);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename Range, typename Key>
|
|
|
|
auto cmContainsImpl(Range const& range, Key const& key, cmOverloadPriority<1>)
|
|
|
|
-> decltype(range.find(key) != range.end())
|
|
|
|
{
|
|
|
|
return range.find(key) != range.end();
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename Range, typename Key>
|
|
|
|
bool cmContainsImpl(Range const& range, Key const& key, cmOverloadPriority<0>)
|
|
|
|
{
|
|
|
|
using std::begin;
|
|
|
|
using std::end;
|
|
|
|
return std::find(begin(range), end(range), key) != end(range);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename Range, typename Key>
|
|
|
|
bool cmContains(Range const& range, Key const& key)
|
|
|
|
{
|
|
|
|
return cmContainsImpl(range, key, cmOverloadPriority<2>{});
|
|
|
|
}
|
|
|
|
|
2015-02-10 20:50:16 +00:00
|
|
|
namespace ContainerAlgorithms {
|
|
|
|
|
2016-05-16 14:34:04 +00:00
|
|
|
template <typename T>
|
2015-02-10 20:50:16 +00:00
|
|
|
struct cmIsPair
|
|
|
|
{
|
2016-05-16 14:34:04 +00:00
|
|
|
enum
|
|
|
|
{
|
|
|
|
value = false
|
|
|
|
};
|
2015-02-10 20:50:16 +00:00
|
|
|
};
|
|
|
|
|
2016-05-16 14:34:04 +00:00
|
|
|
template <typename K, typename V>
|
2017-08-25 21:25:09 +00:00
|
|
|
struct cmIsPair<std::pair<K, V>>
|
2015-02-10 20:50:16 +00:00
|
|
|
{
|
2016-05-16 14:34:04 +00:00
|
|
|
enum
|
|
|
|
{
|
|
|
|
value = true
|
|
|
|
};
|
2015-02-10 20:50:16 +00:00
|
|
|
};
|
|
|
|
|
2016-05-16 14:34:04 +00:00
|
|
|
template <typename Range,
|
|
|
|
bool valueTypeIsPair = cmIsPair<typename Range::value_type>::value>
|
2015-02-10 20:50:16 +00:00
|
|
|
struct DefaultDeleter
|
|
|
|
{
|
2016-05-16 14:34:04 +00:00
|
|
|
void operator()(typename Range::value_type value) const { delete value; }
|
2015-02-10 20:50:16 +00:00
|
|
|
};
|
|
|
|
|
2016-05-16 14:34:04 +00:00
|
|
|
template <typename Range>
|
2015-02-20 20:52:36 +00:00
|
|
|
struct DefaultDeleter<Range, /* valueTypeIsPair = */ true>
|
2015-02-10 20:50:16 +00:00
|
|
|
{
|
2016-05-16 14:34:04 +00:00
|
|
|
void operator()(typename Range::value_type value) const
|
|
|
|
{
|
2015-02-10 20:50:16 +00:00
|
|
|
delete value.second;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-05-16 14:34:04 +00:00
|
|
|
template <typename FwdIt>
|
2015-02-20 20:56:45 +00:00
|
|
|
FwdIt RemoveN(FwdIt i1, FwdIt i2, size_t n)
|
2015-02-12 17:47:10 +00:00
|
|
|
{
|
2015-02-20 20:56:45 +00:00
|
|
|
FwdIt m = i1;
|
2015-02-20 20:55:19 +00:00
|
|
|
std::advance(m, n);
|
|
|
|
return cmRotate(i1, m, i2);
|
2015-02-12 17:47:10 +00:00
|
|
|
}
|
|
|
|
|
2016-05-16 14:34:04 +00:00
|
|
|
template <typename Range>
|
2015-02-15 17:58:36 +00:00
|
|
|
struct BinarySearcher
|
|
|
|
{
|
|
|
|
typedef typename Range::value_type argument_type;
|
|
|
|
BinarySearcher(Range const& r)
|
|
|
|
: m_range(r)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2015-02-17 18:32:52 +00:00
|
|
|
bool operator()(argument_type const& item) const
|
2015-02-15 17:58:36 +00:00
|
|
|
{
|
|
|
|
return std::binary_search(m_range.begin(), m_range.end(), item);
|
|
|
|
}
|
2016-05-16 14:34:04 +00:00
|
|
|
|
2015-02-15 17:58:36 +00:00
|
|
|
private:
|
|
|
|
Range const& m_range;
|
|
|
|
};
|
2015-02-10 21:19:21 +00:00
|
|
|
}
|
|
|
|
|
2015-07-18 09:09:20 +00:00
|
|
|
class cmListFileBacktrace;
|
2016-05-16 14:34:04 +00:00
|
|
|
typedef cmRange<std::vector<cmListFileBacktrace>::const_iterator>
|
|
|
|
cmBacktraceRange;
|
2015-07-18 09:09:20 +00:00
|
|
|
|
2016-05-16 14:34:04 +00:00
|
|
|
template <typename Range>
|
2015-02-20 20:52:36 +00:00
|
|
|
void cmDeleteAll(Range const& r)
|
2015-02-10 20:50:16 +00:00
|
|
|
{
|
2015-02-20 20:52:36 +00:00
|
|
|
std::for_each(r.begin(), r.end(),
|
|
|
|
ContainerAlgorithms::DefaultDeleter<Range>());
|
2015-02-10 20:50:16 +00:00
|
|
|
}
|
|
|
|
|
2019-05-23 12:48:27 +00:00
|
|
|
template <typename T, typename Range>
|
|
|
|
void cmAppend(std::vector<T>& v, Range const& r)
|
|
|
|
{
|
|
|
|
v.insert(v.end(), r.begin(), r.end());
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T, typename InputIt>
|
|
|
|
void cmAppend(std::vector<T>& v, InputIt first, InputIt last)
|
|
|
|
{
|
|
|
|
v.insert(v.end(), first, last);
|
|
|
|
}
|
|
|
|
|
2016-05-16 14:34:04 +00:00
|
|
|
template <typename Range>
|
2015-02-12 17:47:10 +00:00
|
|
|
typename Range::const_iterator cmRemoveN(Range& r, size_t n)
|
|
|
|
{
|
|
|
|
return ContainerAlgorithms::RemoveN(r.begin(), r.end(), n);
|
|
|
|
}
|
|
|
|
|
2016-05-16 14:34:04 +00:00
|
|
|
template <typename Range, typename InputRange>
|
2015-02-12 17:47:10 +00:00
|
|
|
typename Range::const_iterator cmRemoveIndices(Range& r, InputRange const& rem)
|
|
|
|
{
|
|
|
|
typename InputRange::const_iterator remIt = rem.begin();
|
2015-02-20 21:19:14 +00:00
|
|
|
typename InputRange::const_iterator remEnd = rem.end();
|
2015-03-05 22:14:27 +00:00
|
|
|
const typename Range::iterator rangeEnd = r.end();
|
2016-05-16 14:34:04 +00:00
|
|
|
if (remIt == remEnd) {
|
2015-03-05 22:14:27 +00:00
|
|
|
return rangeEnd;
|
2016-05-16 14:34:04 +00:00
|
|
|
}
|
2015-02-12 17:47:10 +00:00
|
|
|
|
2015-02-20 21:07:28 +00:00
|
|
|
typename Range::iterator writer = r.begin();
|
|
|
|
std::advance(writer, *remIt);
|
2015-02-20 21:10:41 +00:00
|
|
|
typename Range::iterator pivot = writer;
|
|
|
|
typename InputRange::value_type prevRem = *remIt;
|
2015-02-12 17:47:10 +00:00
|
|
|
++remIt;
|
|
|
|
size_t count = 1;
|
2016-05-16 14:34:04 +00:00
|
|
|
for (; writer != rangeEnd && remIt != remEnd; ++count, ++remIt) {
|
2015-02-20 21:10:41 +00:00
|
|
|
std::advance(pivot, *remIt - prevRem);
|
|
|
|
prevRem = *remIt;
|
2015-02-20 21:07:28 +00:00
|
|
|
writer = ContainerAlgorithms::RemoveN(writer, pivot, count);
|
2016-05-16 14:34:04 +00:00
|
|
|
}
|
2015-02-20 21:19:14 +00:00
|
|
|
return ContainerAlgorithms::RemoveN(writer, rangeEnd, count);
|
2015-02-12 17:47:10 +00:00
|
|
|
}
|
|
|
|
|
2016-05-16 14:34:04 +00:00
|
|
|
template <typename Range, typename MatchRange>
|
|
|
|
typename Range::const_iterator cmRemoveMatching(Range& r, MatchRange const& m)
|
2015-02-15 17:58:36 +00:00
|
|
|
{
|
|
|
|
return std::remove_if(r.begin(), r.end(),
|
|
|
|
ContainerAlgorithms::BinarySearcher<MatchRange>(m));
|
|
|
|
}
|
|
|
|
|
2019-03-03 13:19:18 +00:00
|
|
|
template <typename ForwardIterator>
|
|
|
|
ForwardIterator cmRemoveDuplicates(ForwardIterator first, ForwardIterator last)
|
2015-02-15 18:08:12 +00:00
|
|
|
{
|
2019-03-03 13:19:18 +00:00
|
|
|
using Value = typename std::iterator_traits<ForwardIterator>::value_type;
|
|
|
|
using Hash = struct
|
|
|
|
{
|
|
|
|
std::size_t operator()(ForwardIterator it) const
|
|
|
|
{
|
|
|
|
return std::hash<Value>{}(*it);
|
2015-02-15 18:08:12 +00:00
|
|
|
}
|
2019-03-03 13:19:18 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
using Equal = struct
|
|
|
|
{
|
|
|
|
bool operator()(ForwardIterator it1, ForwardIterator it2) const
|
|
|
|
{
|
|
|
|
return *it1 == *it2;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
std::unordered_set<ForwardIterator, Hash, Equal> uniq;
|
|
|
|
|
|
|
|
ForwardIterator result = first;
|
|
|
|
while (first != last) {
|
2019-08-19 12:31:52 +00:00
|
|
|
if (!cmContains(uniq, first)) {
|
2019-03-03 13:19:18 +00:00
|
|
|
if (result != first) {
|
|
|
|
*result = std::move(*first);
|
|
|
|
}
|
|
|
|
uniq.insert(result);
|
|
|
|
++result;
|
|
|
|
}
|
|
|
|
++first;
|
2016-05-16 14:34:04 +00:00
|
|
|
}
|
2019-03-03 13:19:18 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename Range>
|
|
|
|
typename Range::const_iterator cmRemoveDuplicates(Range& r)
|
|
|
|
{
|
|
|
|
return cmRemoveDuplicates(r.begin(), r.end());
|
2015-02-15 18:08:12 +00:00
|
|
|
}
|
|
|
|
|
2016-05-16 14:34:04 +00:00
|
|
|
template <typename Range, typename T>
|
2015-02-11 22:53:41 +00:00
|
|
|
typename Range::const_iterator cmFindNot(Range const& r, T const& t)
|
|
|
|
{
|
2017-09-22 18:06:33 +00:00
|
|
|
return std::find_if(r.begin(), r.end(), [&t](T const& i) { return i != t; });
|
2015-02-11 22:53:41 +00:00
|
|
|
}
|
|
|
|
|
2015-07-21 20:42:03 +00:00
|
|
|
template <class Iter>
|
|
|
|
std::reverse_iterator<Iter> cmMakeReverseIterator(Iter it)
|
|
|
|
{
|
|
|
|
return std::reverse_iterator<Iter>(it);
|
|
|
|
}
|
|
|
|
|
2017-08-16 22:41:18 +00:00
|
|
|
namespace cm {
|
|
|
|
|
2017-10-05 10:43:06 +00:00
|
|
|
#if __cplusplus >= 201703L || defined(_MSVC_LANG) && _MSVC_LANG >= 201703L
|
|
|
|
|
|
|
|
using std::size;
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
// std::size backport from C++17.
|
|
|
|
template <class C>
|
2018-06-01 13:53:41 +00:00
|
|
|
# if !defined(_MSC_VER) || _MSC_VER >= 1900
|
2017-10-05 10:43:06 +00:00
|
|
|
constexpr
|
2018-06-01 13:53:41 +00:00
|
|
|
# endif
|
2017-10-05 10:43:06 +00:00
|
|
|
auto
|
|
|
|
size(C const& c) -> decltype(c.size())
|
|
|
|
{
|
|
|
|
return c.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T, size_t N>
|
2018-06-01 13:53:41 +00:00
|
|
|
# if !defined(_MSC_VER) || _MSC_VER >= 1900
|
2017-10-05 10:43:06 +00:00
|
|
|
constexpr
|
2018-06-01 13:53:41 +00:00
|
|
|
# endif
|
2017-10-05 10:43:06 +00:00
|
|
|
std::size_t
|
|
|
|
size(const T (&)[N]) throw()
|
|
|
|
{
|
|
|
|
return N;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2018-12-24 20:52:58 +00:00
|
|
|
template <typename T>
|
|
|
|
int isize(const T& t)
|
|
|
|
{
|
|
|
|
return static_cast<int>(cm::size(t));
|
|
|
|
}
|
|
|
|
|
2017-10-05 10:43:06 +00:00
|
|
|
#if __cplusplus >= 201402L || defined(_MSVC_LANG) && _MSVC_LANG >= 201402L
|
|
|
|
|
|
|
|
using std::cbegin;
|
|
|
|
using std::cend;
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
// std::c{begin,end} backport from C++14
|
|
|
|
template <class C>
|
2018-06-01 13:53:41 +00:00
|
|
|
# if defined(_MSC_VER) && _MSC_VER < 1900
|
2017-10-05 10:43:06 +00:00
|
|
|
auto cbegin(C const& c)
|
2018-06-01 13:53:41 +00:00
|
|
|
# else
|
2017-10-05 10:43:06 +00:00
|
|
|
constexpr auto cbegin(C const& c) noexcept(noexcept(std::begin(c)))
|
2018-06-01 13:53:41 +00:00
|
|
|
# endif
|
2017-10-05 10:43:06 +00:00
|
|
|
-> decltype(std::begin(c))
|
|
|
|
{
|
|
|
|
return std::begin(c);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class C>
|
2018-06-01 13:53:41 +00:00
|
|
|
# if defined(_MSC_VER) && _MSC_VER < 1900
|
2017-10-05 10:43:06 +00:00
|
|
|
auto cend(C const& c)
|
2018-06-01 13:53:41 +00:00
|
|
|
# else
|
2017-10-05 10:43:06 +00:00
|
|
|
constexpr auto cend(C const& c) noexcept(noexcept(std::end(c)))
|
2018-06-01 13:53:41 +00:00
|
|
|
# endif
|
2017-10-05 10:43:06 +00:00
|
|
|
-> decltype(std::end(c))
|
|
|
|
{
|
|
|
|
return std::end(c);
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2017-08-16 22:41:18 +00:00
|
|
|
} // namespace cm
|
|
|
|
|
2015-02-10 20:50:16 +00:00
|
|
|
#endif
|