Unify std::binders and lambdas with custom binders

This commit is contained in:
Ion Gaztañaga
2017-05-26 22:31:26 +02:00
parent a451aeda93
commit d5aede745e
5 changed files with 151 additions and 47 deletions

View File

@@ -1218,9 +1218,11 @@ use [*Boost.Container]? There are several reasons for that:
* Implemented `extract_sequence`, `adopt_sequence` functions for flat_[multi]map/set associative containers.
* Fixed bugs:
* [@https://github.com/boostorg/container/pull/48 GitHub #48: ['"Replace deprecated/removed C++98 binders"]].
* [@https://github.com/boostorg/container/pull/49 GitHub #49: ['"Remove useless allocator copy in map"]].
* [@https://github.com/boostorg/container/pull/50 GitHub #50: ['"Fixed bug Trac #13038"]].
[endsect]
[section:release_notes_boost_1_64_00 Boost 1.64 Release]

View File

@@ -29,6 +29,128 @@ namespace container {
using boost::intrusive::algo_equal;
using boost::intrusive::algo_lexicographical_compare;
template<class Func>
class binder1st
{
public:
typedef typename Func::second_argument_type argument_type;
typedef typename Func::result_type result_type;
binder1st(const Func& func, const typename Func::first_argument_type& arg)
: op(func), value(arg)
{}
result_type operator()(const argument_type& arg) const
{ return op(value, arg); }
result_type operator()(argument_type& arg) const
{ return op(value, arg); }
private:
Func op;
typename Func::first_argument_type value;
};
template<class Func, class T>
inline binder1st<Func> bind1st(const Func& func, const T& arg)
{ return boost::container::binder1st<Func>(func, arg); }
template<class Func>
class binder2nd
{
public:
typedef typename Func::first_argument_type argument_type;
typedef typename Func::result_type result_type;
binder2nd(const Func& func, const typename Func::second_argument_type& arg)
: op(func), value(arg)
{}
result_type operator()(const argument_type& arg) const
{ return op(arg, value); }
result_type operator()(argument_type& arg) const
{ return op(arg, value); }
private:
Func op;
typename Func::second_argument_type value;
};
template<class Func, class T>
inline binder2nd<Func> bind2nd(const Func& func, const T& arg)
{
return (boost::container::binder2nd<Func>(func, arg));
}
template<class Func>
class unary_negate
{
public:
typedef typename Func::argument_type argument_type;
typedef typename Func::result_type result_type;
explicit unary_negate(const Func& func)
: m_func(func)
{}
bool operator()(const typename Func::argument_type& arg) const
{ return !m_func(arg); }
private:
Func m_func;
};
template<class Func> inline
unary_negate<Func> not1(const Func& func)
{
return boost::container::unary_negate<Func>(func);
}
template<class InputIt, class UnaryPredicate>
InputIt find_if(InputIt first, InputIt last, UnaryPredicate p)
{
for (; first != last; ++first) {
if (p(*first)) {
return first;
}
}
return last;
}
template<class InputIt, class ForwardIt, class BinaryPredicate>
InputIt find_first_of(InputIt first1, InputIt last1, ForwardIt first2, ForwardIt last2, BinaryPredicate p)
{
for (; first1 != last1; ++first1) {
for (ForwardIt it = first2; it != last2; ++it) {
if (p(*first1, *it)) {
return first1;
}
}
}
return last1;
}
template<class ForwardIt1, class ForwardIt2, class BinaryPredicate>
ForwardIt1 search(ForwardIt1 first1, ForwardIt1 last1,
ForwardIt2 first2, ForwardIt2 last2, BinaryPredicate p)
{
for (; ; ++first1) {
ForwardIt1 it = first1;
for (ForwardIt2 it2 = first2; ; ++it, ++it2) {
if (it2 == last2) {
return first1;
}
if (it == last1) {
return last1;
}
if (!p(*it, *it2)) {
break;
}
}
}
}
} //namespace container {
} //namespace boost {

View File

@@ -39,6 +39,7 @@
#include <boost/container/detail/version_type.hpp>
#include <boost/container/detail/type_traits.hpp>
#include <boost/container/detail/minimal_char_traits_header.hpp>
#include <boost/container/detail/algorithm.hpp>
#include <boost/intrusive/pointer_traits.hpp>
@@ -51,7 +52,6 @@
#include <boost/functional/hash.hpp>
#include <algorithm>
#include <functional> //bind2nd, etc.
#include <iosfwd>
#include <istream>
#include <ostream>
@@ -541,12 +541,8 @@ class basic_string
bool operator()(const typename Tr::char_type& x) const
{
return std::find_if(m_first, m_last,
#ifdef BOOST_NO_CXX98_BINDERS
[&](argument_type ch) { return Eq_traits<Tr>()(x, ch); }) == m_last;
#else
std::bind1st(Eq_traits<Tr>(), x)) == m_last;
#endif
return boost::container::find_if(m_first, m_last,
boost::container::bind1st(Eq_traits<Tr>(), x)) == m_last;
}
};
#endif //#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
@@ -2276,7 +2272,7 @@ class basic_string
const pointer addr = this->priv_addr();
pointer finish = addr + this->priv_size();
const const_iterator result =
std::search(boost::movelib::to_raw_pointer(addr + pos),
boost::container::search(boost::movelib::to_raw_pointer(addr + pos),
boost::movelib::to_raw_pointer(finish),
s, s + n, Eq_traits<Traits>());
return result != finish ? result - begin() : npos;
@@ -2303,12 +2299,8 @@ class basic_string
const pointer addr = this->priv_addr();
pointer finish = addr + sz;
const const_iterator result =
std::find_if(addr + pos, finish,
#ifdef BOOST_NO_CXX98_BINDERS
[&](value_type ch) { return Eq_traits<Traits>()(ch, c); });
#else
std::bind2nd(Eq_traits<Traits>(), c));
#endif
boost::container::find_if(addr + pos, finish,
boost::container::bind2nd(Eq_traits<Traits>(), c));
return result != finish ? result - begin() : npos;
}
}
@@ -2379,12 +2371,8 @@ class basic_string
else {
const const_iterator last = begin() + container_detail::min_value(len - 1, pos) + 1;
const_reverse_iterator rresult =
std::find_if(const_reverse_iterator(last), rend(),
#ifdef BOOST_NO_CXX98_BINDERS
[&](value_type ch) { return Eq_traits<Traits>()(ch, c); });
#else
std::bind2nd(Eq_traits<Traits>(), c));
#endif
boost::container::find_if(const_reverse_iterator(last), rend(),
boost::container::bind2nd(Eq_traits<Traits>(), c));
return rresult != rend() ? (rresult.base() - 1) - begin() : npos;
}
}
@@ -2397,7 +2385,7 @@ class basic_string
//!
//! <b>Returns</b>: xpos if the function can determine such a value for xpos. Otherwise, returns npos.
size_type find_first_of(const basic_string& str, size_type pos = 0) const
{ return find_first_of(str.c_str(), pos, str.size()); }
{ return this->find_first_of(str.c_str(), pos, str.size()); }
//! <b>Effects</b>: Determines the lowest position xpos, if possible, such that both of the
//! following conditions obtain: a) pos <= xpos and xpos < size();
@@ -2408,7 +2396,7 @@ class basic_string
//! <b>Returns</b>: xpos if the function can determine such a value for xpos. Otherwise, returns npos.
template<template <class, class> class BasicStringView>
size_type find_first_of(BasicStringView<CharT, Traits> sv, size_type pos = 0) const
{ return find_first_of(sv.data(), pos, sv.size()); }
{ return this->find_first_of(sv.data(), pos, sv.size()); }
//! <b>Requires</b>: s points to an array of at least n elements of CharT.
//!
@@ -2423,7 +2411,7 @@ class basic_string
else {
const pointer addr = this->priv_addr();
pointer finish = addr + sz;
const_iterator result = std::find_first_of
const_iterator result = boost::container::find_first_of
(addr + pos, finish, s, s + n, Eq_traits<Traits>());
return result != finish ? result - this->begin() : npos;
}
@@ -2435,7 +2423,7 @@ class basic_string
//!
//! <b>Returns</b>: find_first_of(basic_string(s), pos).
size_type find_first_of(const CharT* s, size_type pos = 0) const
{ return find_first_of(s, pos, Traits::length(s)); }
{ return this->find_first_of(s, pos, Traits::length(s)); }
//! <b>Requires</b>: s points to an array of at least traits::length(s) + 1 elements of CharT.
//!
@@ -2443,7 +2431,7 @@ class basic_string
//!
//! <b>Returns</b>: find_first_of(basic_string<CharT,traits,Allocator>(1,c), pos).
size_type find_first_of(CharT c, size_type pos = 0) const
{ return find(c, pos); }
{ return this->find(c, pos); }
//! <b>Effects</b>: Determines the highest position xpos, if possible, such that both of
//! the following conditions obtain: a) xpos <= pos and xpos < size(); b)
@@ -2453,7 +2441,7 @@ class basic_string
//!
//! <b>Returns</b>: xpos if the function can determine such a value for xpos. Otherwise, returns npos.
size_type find_last_of(const basic_string& str, size_type pos = npos) const
{ return find_last_of(str.c_str(), pos, str.size()); }
{ return this->find_last_of(str.c_str(), pos, str.size()); }
//! <b>Effects</b>: Determines the highest position xpos, if possible, such that both of
//! the following conditions obtain: a) xpos <= pos and xpos < size(); b)
@@ -2464,7 +2452,7 @@ class basic_string
//! <b>Returns</b>: xpos if the function can determine such a value for xpos. Otherwise, returns npos.
template<template <class, class> class BasicStringView>
size_type find_last_of(BasicStringView<CharT, Traits> sv, size_type pos = npos) const
{ return find_last_of(sv.data(), pos, sv.size()); }
{ return this->find_last_of(sv.data(), pos, sv.size()); }
//! <b>Requires</b>: s points to an array of at least n elements of CharT.
//!
@@ -2481,7 +2469,7 @@ class basic_string
const pointer addr = this->priv_addr();
const const_iterator last = addr + container_detail::min_value(len - 1, pos) + 1;
const const_reverse_iterator rresult =
std::find_first_of(const_reverse_iterator(last), rend(),
boost::container::find_first_of(const_reverse_iterator(last), rend(),
s, s + n, Eq_traits<Traits>());
return rresult != rend() ? (rresult.base() - 1) - addr : npos;
}
@@ -2536,7 +2524,7 @@ class basic_string
else {
const pointer addr = this->priv_addr();
const pointer finish = addr + this->priv_size();
const const_iterator result = std::find_if
const const_iterator result = boost::container::find_if
(addr + pos, finish, Not_within_traits<Traits>(s, s + n));
return result != finish ? result - addr : npos;
}
@@ -2561,12 +2549,8 @@ class basic_string
const pointer addr = this->priv_addr();
const pointer finish = addr + this->priv_size();
const const_iterator result
= std::find_if(addr + pos, finish,
#ifdef BOOST_NO_CXX98_BINDERS
[&](value_type ch) { return !Eq_traits<Traits>()(ch, c); });
#else
std::not1(std::bind2nd(Eq_traits<Traits>(), c)));
#endif
= boost::container::find_if(addr + pos, finish,
boost::container::not1(boost::container::bind2nd(Eq_traits<Traits>(), c)));
return result != finish ? result - begin() : npos;
}
}
@@ -2606,7 +2590,7 @@ class basic_string
else {
const const_iterator last = begin() + container_detail::min_value(len - 1, pos) + 1;
const const_reverse_iterator rresult =
std::find_if(const_reverse_iterator(last), rend(),
boost::container::find_if(const_reverse_iterator(last), rend(),
Not_within_traits<Traits>(s, s + n));
return rresult != rend() ? (rresult.base() - 1) - begin() : npos;
}
@@ -2632,12 +2616,8 @@ class basic_string
else {
const const_iterator last = begin() + container_detail::min_value(len - 1, pos) + 1;
const const_reverse_iterator rresult =
std::find_if(const_reverse_iterator(last), rend(),
#ifdef BOOST_NO_CXX98_BINDERS
[&](value_type ch) { return !Eq_traits<Traits>()(ch, c); });
#else
std::not1(std::bind2nd(Eq_traits<Traits>(), c)));
#endif
boost::container::find_if(const_reverse_iterator(last), rend(),
boost::container::not1(boost::container::bind2nd(Eq_traits<Traits>(), c)));
return rresult != rend() ? (rresult.base() - 1) - begin() : npos;
}
}

View File

@@ -952,7 +952,7 @@ bool instantiate_constructors()
typedef typename MapType::value_type value_type;
typename MapType::key_compare comp;
typename MapType::allocator_type a;
typename MapType::value_type value;
value_type value;
{
MapType s0;
MapType s1(comp);
@@ -991,7 +991,7 @@ bool instantiate_constructors()
typedef typename MultimapType::value_type value_type;
typename MultimapType::key_compare comp;
typename MultimapType::allocator_type a;
typename MultimapType::value_type value;
value_type value;
{
MultimapType s0;
MultimapType s1(comp);

View File

@@ -849,7 +849,7 @@ bool instantiate_constructors()
typedef typename SetType::value_type value_type;
typename SetType::key_compare comp;
typename SetType::allocator_type a;
typename SetType::value_type value;
value_type value;
{
SetType s0;
SetType s1(comp);
@@ -884,9 +884,10 @@ bool instantiate_constructors()
}
{
typedef typename MultisetType::value_type value_type;
typename MultisetType::key_compare comp;
typename MultisetType::allocator_type a;
typename MultisetType::value_type value;
value_type value;
{
MultisetType s0;
MultisetType s1(comp);
@@ -907,7 +908,6 @@ bool instantiate_constructors()
MultisetType s3({ 0 },comp, a);
}
{
typedef typename MultisetType::value_type value_type;
std::initializer_list<value_type>il{0};
MultisetType s0(ordered_range, il);
MultisetType s1(ordered_range, il,comp);