Utils: Add predicates for comparison to constant values

Many STL algorithms have value version. So you can only use a predicate:

std::stable_partition(std::begin(list), std::end(list), [](int entry) {
     return entry < 20;
});

With this predicates you can write it in a shorter form:

std::stable_partition(std::begin(list), std::end(list), lessThan(20));

The find helpers are moved to predicates too.

Change-Id: I791b3105ca84ef7c5a519d22589a91b5548cf3e4
Reviewed-by: Orgad Shaneh <orgads@gmail.com>
Reviewed-by: Eike Ziller <eike.ziller@qt.io>
This commit is contained in:
Marco Bubke
2017-10-25 14:32:45 +02:00
parent 2918cc59f8
commit 4911d5bea3
4 changed files with 136 additions and 18 deletions

View File

@@ -25,10 +25,11 @@
#pragma once
#include "predicates.h"
#include <qcompilerdetection.h> // for Q_REQUIRED_RESULT
#include <algorithm>
#include <functional>
#include <tuple>
#include <QStringList>
@@ -136,23 +137,6 @@ typename T::value_type findOrDefault(const T &container, R (S::*function)() cons
return findOr(container, typename T::value_type(), function);
}
//////////////////
// find helpers
//////////////////
template<typename R, typename S, typename T>
decltype(auto) equal(R (S::*function)() const, T value)
{
// This should use std::equal_to<> instead of std::eqaul_to<T>,
// but that's not supported everywhere yet, since it is C++14
return std::bind<bool>(std::equal_to<T>(), value, std::bind(function, std::placeholders::_1));
}
template<typename R, typename S, typename T>
decltype(auto) equal(R S::*member, T value)
{
return std::bind<bool>(std::equal_to<T>(), value, std::bind(member, std::placeholders::_1));
}
//////////////////
// max element
//////////////////