2008-05-16 18:14:08 +00:00
|
|
|
/*
|
|
|
|
Copyright (c) Marshall Clow 2008.
|
|
|
|
|
|
|
|
Distributed under the Boost Software License, Version 1.0. (See accompanying
|
|
|
|
file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
|
|
|
|
|
|
|
Revision history:
|
|
|
|
05 May 2008 mtc First version - as part of BoostCon 2008
|
2009-05-10 23:16:59 +00:00
|
|
|
07 May 2009 mtc Changed names to match n2666
|
|
|
|
|
2008-05-16 18:14:08 +00:00
|
|
|
*/
|
|
|
|
|
2009-05-10 23:16:59 +00:00
|
|
|
/// \file all.hpp
|
|
|
|
/// \brief Test ranges against predicates.
|
|
|
|
/// \author Marshall Clow
|
2008-05-16 18:14:08 +00:00
|
|
|
|
|
|
|
#ifndef BOOST_ALGORITHM_ALL_HPP
|
|
|
|
#define BOOST_ALGORITHM_ALL_HPP
|
|
|
|
|
2009-05-10 23:16:59 +00:00
|
|
|
#include <boost/range.hpp> // For boost::begin and boost::end
|
2008-05-16 18:14:08 +00:00
|
|
|
|
2009-05-10 23:16:59 +00:00
|
|
|
// I would love to use the all_of, any_of and none_of that are in the C++0x
|
|
|
|
// standard library if they are available, but I don't know how to do that.
|
|
|
|
// -- mtc 11-May-2009
|
|
|
|
// Something like:
|
|
|
|
// #ifdef <something>
|
|
|
|
// use std::all_of;
|
|
|
|
// #else
|
|
|
|
// template<typename I, typename V>
|
|
|
|
// bool all_of ( I first, I last, const V &val )
|
|
|
|
// ... and so on.
|
|
|
|
// #endif
|
2008-05-16 18:14:08 +00:00
|
|
|
|
|
|
|
|
|
|
|
namespace boost { namespace algorithm {
|
|
|
|
|
2009-05-10 23:16:59 +00:00
|
|
|
/// \fn all_of ( I first, I last, const V &val )
|
2008-05-16 18:14:08 +00:00
|
|
|
/// \brief Returns true if all elements in [first, last) are equal to 'val'
|
|
|
|
///
|
|
|
|
/// \param first The start of the input sequence
|
|
|
|
/// \param last One past the end of the input sequence
|
|
|
|
/// \param val A value to compare against
|
|
|
|
///
|
|
|
|
template<typename I, typename V>
|
2009-05-10 23:16:59 +00:00
|
|
|
bool all_of ( I first, I last, const V &val )
|
2008-05-16 18:14:08 +00:00
|
|
|
{
|
|
|
|
while (first != last) {
|
|
|
|
if ( *first++ != val )
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2009-05-10 23:16:59 +00:00
|
|
|
|
|
|
|
/// \fn all_of ( Range range, const V &val )
|
2008-05-16 18:14:08 +00:00
|
|
|
/// \brief Returns true if all elements in the range are equal to 'val'
|
|
|
|
///
|
|
|
|
/// \param range The input range
|
|
|
|
/// \param val A value to compare against
|
|
|
|
///
|
|
|
|
template<typename Range, typename V>
|
2009-05-10 23:16:59 +00:00
|
|
|
bool all_of ( Range range, const V &val )
|
2008-05-16 18:14:08 +00:00
|
|
|
{
|
2009-05-10 23:16:59 +00:00
|
|
|
return all_of ( boost::begin ( range ), boost::end ( range ), val );
|
2008-05-16 18:14:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-05-10 23:16:59 +00:00
|
|
|
/// \fn all_of_if ( I first, I last, Pred p )
|
2008-05-16 18:14:08 +00:00
|
|
|
/// \brief Returns true if all elements in [first, last) satisfy the predicate
|
|
|
|
///
|
|
|
|
/// \param first The start of the input sequence
|
|
|
|
/// \param last One past the end of the input sequence
|
|
|
|
/// \param p A predicate
|
|
|
|
///
|
|
|
|
template<typename I, typename Pred>
|
2009-05-10 23:16:59 +00:00
|
|
|
bool all_of_if ( I first, I last, Pred p )
|
2008-05-16 18:14:08 +00:00
|
|
|
{
|
|
|
|
while (first != last) {
|
|
|
|
if ( !p(*first++))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2009-05-10 23:16:59 +00:00
|
|
|
/// \fn all_of_if ( Range range, Pred p )
|
2008-05-16 18:14:08 +00:00
|
|
|
/// \brief Returns true if all elements in the range satisfy the predicate
|
|
|
|
///
|
|
|
|
/// \param range The input range
|
|
|
|
/// \param p A predicate to test the elements
|
|
|
|
///
|
|
|
|
template<typename Range, typename Pred>
|
2009-05-10 23:16:59 +00:00
|
|
|
bool all_of_if ( Range range, Pred p )
|
2008-05-16 18:14:08 +00:00
|
|
|
{
|
2009-05-10 23:16:59 +00:00
|
|
|
return all_of_if ( boost::begin ( range ), boost::end ( range ), p );
|
2008-05-16 18:14:08 +00:00
|
|
|
}
|
|
|
|
|
2009-05-10 23:16:59 +00:00
|
|
|
/// \fn none_of ( I first, I last, const V &val )
|
2008-05-16 18:14:08 +00:00
|
|
|
/// \brief Returns true if none of the elements in [first, last) are equal to 'val'
|
|
|
|
///
|
|
|
|
/// \param first The start of the input sequence
|
|
|
|
/// \param last One past the end of the input sequence
|
|
|
|
/// \param val A value to compare against
|
|
|
|
///
|
|
|
|
template<typename I, typename V>
|
2009-05-10 23:16:59 +00:00
|
|
|
bool none_of ( I first, I last, const V &val )
|
2008-05-16 18:14:08 +00:00
|
|
|
{
|
|
|
|
while (first != last) {
|
|
|
|
if ( *first++ == val )
|
2009-04-28 19:57:54 +00:00
|
|
|
return false;
|
2008-05-16 18:14:08 +00:00
|
|
|
}
|
2009-04-28 19:57:54 +00:00
|
|
|
return true;
|
2008-05-16 18:14:08 +00:00
|
|
|
}
|
|
|
|
|
2009-05-10 23:16:59 +00:00
|
|
|
/// \fn none_of ( Range range, const V &val )
|
2008-05-16 18:14:08 +00:00
|
|
|
/// \brief Returns true if none of the elements in the range are equal to 'val'
|
|
|
|
///
|
|
|
|
/// \param range The input range
|
|
|
|
/// \param val A value to compare against
|
|
|
|
///
|
|
|
|
template<typename Range, typename V>
|
2009-05-10 23:16:59 +00:00
|
|
|
bool none_of ( Range range, const V & val )
|
2008-05-16 18:14:08 +00:00
|
|
|
{
|
2009-05-10 23:16:59 +00:00
|
|
|
return none_of ( boost::begin ( range ), boost::end ( range ), val );
|
2008-05-16 18:14:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-05-10 23:16:59 +00:00
|
|
|
/// \fn none_of_if ( I first, I last, Pred p )
|
2008-05-16 18:14:08 +00:00
|
|
|
/// \brief Returns true if none of the elements in [first, last) satisfy the predicate
|
|
|
|
///
|
|
|
|
/// \param first The start of the input sequence
|
|
|
|
/// \param last One past the end of the input sequence
|
|
|
|
/// \param p A predicate
|
|
|
|
///
|
|
|
|
template<typename I, typename Pred>
|
2009-05-10 23:16:59 +00:00
|
|
|
bool none_of_if ( I first, I last, Pred p )
|
2008-05-16 18:14:08 +00:00
|
|
|
{
|
|
|
|
while (first != last) {
|
|
|
|
if ( p(*first++))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2009-05-10 23:16:59 +00:00
|
|
|
/// \fn none_of_if ( Range range, Pred p )
|
2008-05-16 18:14:08 +00:00
|
|
|
/// \brief Returns true if none of the elements in the range satisfy the predicate
|
|
|
|
///
|
|
|
|
/// \param range The input range
|
|
|
|
/// \param p A predicate to test the elements
|
|
|
|
///
|
|
|
|
template<typename Range, typename Pred>
|
|
|
|
bool none_if ( Range range, Pred p )
|
|
|
|
{
|
2009-05-10 23:16:59 +00:00
|
|
|
return none_of_if ( boost::begin ( range ), boost::end ( range ), p );
|
2008-05-16 18:14:08 +00:00
|
|
|
}
|
|
|
|
|
2009-05-10 23:16:59 +00:00
|
|
|
/// \fn any_of ( I first, I last, const V &val )
|
2008-05-16 18:14:08 +00:00
|
|
|
/// \brief Returns true if any of the elements in [first, last) are equal to 'val'
|
|
|
|
///
|
|
|
|
/// \param first The start of the input sequence
|
|
|
|
/// \param last One past the end of the input sequence
|
|
|
|
/// \param val A value to compare against
|
|
|
|
///
|
|
|
|
template<typename I, typename V>
|
2009-05-10 23:16:59 +00:00
|
|
|
bool any_of ( I first, I last, const V &val )
|
2008-05-16 18:14:08 +00:00
|
|
|
{
|
|
|
|
while (first != last) {
|
|
|
|
if ( *first++ == val )
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2009-05-10 23:16:59 +00:00
|
|
|
/// \fn any_of ( Range range, const V &val )
|
2008-05-16 18:14:08 +00:00
|
|
|
/// \brief Returns true if any of the elements in the range are equal to 'val'
|
|
|
|
///
|
|
|
|
/// \param range The input range
|
|
|
|
/// \param val A value to compare against
|
|
|
|
///
|
|
|
|
template<typename Range, typename V>
|
2009-05-10 23:16:59 +00:00
|
|
|
bool any_of ( Range range, const V &val )
|
2008-05-16 18:14:08 +00:00
|
|
|
{
|
2009-05-10 23:16:59 +00:00
|
|
|
return any_of ( boost::begin ( range ), boost::end ( range ), val );
|
2008-05-16 18:14:08 +00:00
|
|
|
}
|
|
|
|
|
2009-05-10 23:16:59 +00:00
|
|
|
/// \fn any_of_if ( I first, I last, Pred p )
|
2008-05-16 18:14:08 +00:00
|
|
|
/// \brief Returns true if any of the elements in [first, last) satisfy the predicate
|
|
|
|
///
|
|
|
|
/// \param first The start of the input sequence
|
|
|
|
/// \param last One past the end of the input sequence
|
|
|
|
/// \param p A predicate
|
|
|
|
///
|
|
|
|
template<typename I, typename Pred>
|
2009-05-10 23:16:59 +00:00
|
|
|
bool any_of_if ( I first, I last, Pred p)
|
2008-05-16 18:14:08 +00:00
|
|
|
{
|
|
|
|
while (first != last) {
|
|
|
|
if ( p(*first++))
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2009-05-10 23:16:59 +00:00
|
|
|
/// \fn any_of_if ( Range range, Pred p )
|
2008-05-16 18:14:08 +00:00
|
|
|
/// \brief Returns true if any elements in the range satisfy the predicate
|
|
|
|
///
|
|
|
|
/// \param range The input range
|
|
|
|
/// \param p A predicate to test the elements
|
|
|
|
///
|
|
|
|
template<typename Range, typename Pred>
|
2009-05-10 23:16:59 +00:00
|
|
|
bool any_of_if ( Range range, Pred p )
|
2008-05-16 18:14:08 +00:00
|
|
|
{
|
2009-05-10 23:16:59 +00:00
|
|
|
return any_of_if ( boost::begin ( range ), boost::end ( range ), p );
|
2008-05-16 18:14:08 +00:00
|
|
|
}
|
|
|
|
|
2009-04-28 19:57:54 +00:00
|
|
|
/// \fn exists_and_only ( I first, I last, const V &val )
|
2008-05-16 18:14:08 +00:00
|
|
|
/// \brief Returns true if the value 'val' exists only once in [first, last).
|
|
|
|
///
|
|
|
|
/// \param first The start of the input sequence
|
|
|
|
/// \param last One past the end of the input sequence
|
|
|
|
/// \param val A value to compare against
|
|
|
|
///
|
|
|
|
template<typename I, typename V>
|
|
|
|
bool exists_and_only ( I first, I last, const V &val )
|
|
|
|
{
|
|
|
|
I i = std::find (first, last, val);
|
|
|
|
if (i == last) return false;
|
|
|
|
if ( std::find (++i, last, val) != last) return false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2009-04-28 19:57:54 +00:00
|
|
|
/// \fn exists_and_only ( Range range, const V &val )
|
2008-05-16 18:14:08 +00:00
|
|
|
/// \brief Returns true if the value 'val' exists only once in the range.
|
|
|
|
///
|
|
|
|
/// \param range The input range
|
|
|
|
/// \param val A value to compare against
|
|
|
|
///
|
|
|
|
template<typename Range, typename V>
|
|
|
|
bool exists_and_only ( Range range, const V &val )
|
|
|
|
{
|
|
|
|
return exists_and_only ( boost::begin ( range ), boost::end ( range ), val );
|
|
|
|
}
|
|
|
|
|
|
|
|
/// \fn exists_and_only_if ( I first, I last, Pred p )
|
|
|
|
/// \brief Returns true if the predicate 'p' is true for exactly one item in [first, last).
|
|
|
|
///
|
|
|
|
/// \param first The start of the input sequence
|
|
|
|
/// \param last One past the end of the input sequence
|
|
|
|
/// \param p A predicate to test the elements
|
|
|
|
///
|
|
|
|
template<typename I, typename Pred>
|
|
|
|
bool exists_and_only_if ( I first, I last, Pred p )
|
|
|
|
{
|
|
|
|
I i = find_if (first, last, p);
|
|
|
|
if (i == last) return false;
|
|
|
|
if (find_if(++i, last, p) != last) return false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// \fn exists_and_only_if ( Range range, Pred p )
|
|
|
|
/// \brief Returns true if the predicate 'p' is true for exactly one item in the range.
|
|
|
|
///
|
|
|
|
/// \param range The input range
|
|
|
|
/// \param p A predicate to test the elements
|
|
|
|
///
|
|
|
|
template<typename Range, typename Pred>
|
|
|
|
bool exists_and_only_if ( Range range, Pred p )
|
|
|
|
{
|
|
|
|
return exists_and_only_if ( boost::begin ( range ), boost::end ( range ), p );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}} // namespace boost and algorithm
|
|
|
|
|
2009-05-10 23:16:59 +00:00
|
|
|
#undef ANY_DOING_0X
|
2008-05-16 18:14:08 +00:00
|
|
|
#endif // BOOST_ALGORITHM_ALL_HPP
|