Opened 12 years ago

Last modified 8 years ago

#5027 new Feature Requests

Extension of Boost.Range algorithms

Reported by: gast128 <gast128@…> Owned by: Marshall Clow
Milestone: To Be Determined Component: algorithm
Version: Boost 1.45.0 Severity: Not Applicable
Keywords: Cc:

Description

Maybe an idea to extend Boost.Range algorithms:

  • copy_if
  • for_each_if
  • transform_if
  • contains (/contains_if, shortcut for std::find(...) != end)
  • all (/all_if)
  • none(/none_if, shortcut for std::find(...) == end))
  • bsearch (Austern 13.2.1.2)

Some of them are listed in STLAlgorithmExtensions.

Change History (7)

comment:1 by Steven Watanabe, 12 years ago

I think Marshall was handling many of these. http://svn.boost.org/svn/boost/sandbox/boost/algorithm/

comment:2 by Neil Groves, 12 years ago

The xxx_if versions of algorithms are all redundant due to the filtered Range Adaptor. Please see the Range Adaptor documentation. The best part of the Range Adaptor design, which I believe was first brought up by Eric Niebler is that we can easily compose the filter onto any algorithm. This is a far better separation of concerns than we see in the standard C++ library where we have only the most common use-cases covered by _if suffixed functions. I believe that Thortsen Ottosens' comment that the copy_if algorithm is '... thoroughly mis-designed' is very true. The Range Adaptors are a superior alternative requiring no duplication when writing algorithms, provide superior consistency and readability.

The functions contains, any, all, and none I have frequently wanted and it does make sense for this to be available in Boost.

comment:3 by Marshall Clow, 12 years ago

Owner: changed from Neil Groves to Marshall Clow

I am preparing a submission for Boost.Algorithm, which will contain many of these (and an extension mechanism to make adding the rest fairly simple)

comment:4 by gast128 <gast128@…>, 12 years ago

Ah yes I read about it but didn't think of it. It could be then:

std::vector<Base> vec; std::vector<Base> vec2;

boost::copy(vec | boost::adaptors::filtered(boost::bind(&Base::Get, _1) == 2), std::back_inserter(vec2));

Traditionally it would be then: std::copy_if(vec, std::back_inserter(vec2), boost::bind(&Base::Get, _1) == 2));

comment:5 by gast128, 11 years ago

void Test() {

std::vector<Person> vecPersons; std::vector<Person> vecPersons2;

std::copy_if(vecPersons.cbegin(), vecPersons.cend(), std::back_inserter(vecPersons2), [](const Person& crPerson) -> bool {

return crPerson.GetAge() == 27;

});

auto fc = [](const Person& crPerson) -> bool {

return crPerson.GetAge() == 27;

};

boost::copy(vecPersons| boost::adaptors::filtered(fc), std::back_inserter(vecPersons2));

}

note: somehow my formatting gets lost, forgot how to fix that.

comment:6 by Marshall Clow, 10 years ago

copy_if, contains (but named any_of), all (but named all_of), none (named none_of) are part of Boost.150

for_each_if, transform_if, and bsearch are not.

comment:7 by Neil Groves, 8 years ago

Component: rangealgorithm

I've just moved this to the Algorithm component, since the comments above indicate that Marshal shall be implementing these in Boost.Algorithm.

I'm happy to assist if desired.

Note: See TracTickets for help on using tickets.