2009-12-29 04:39:27 +01:00
|
|
|
/*
|
|
|
|
|
UTIL-FOREACH.hpp - helpers for doing something for each element
|
|
|
|
|
|
|
|
|
|
Copyright (C) Lumiera.org
|
|
|
|
|
2009, Hermann Vosseler <Ichthyostega@web.de>
|
|
|
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or
|
|
|
|
|
modify it under the terms of the GNU General Public License as
|
|
|
|
|
published by the Free Software Foundation; either version 2 of the
|
|
|
|
|
License, or (at your option) any later version.
|
|
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
|
along with this program; if not, write to the Free Software
|
|
|
|
|
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
|
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#ifndef UTIL_FOREACH_H
|
|
|
|
|
#define UTIL_FOREACH_H
|
|
|
|
|
|
|
|
|
|
#include "lib/util.hpp"
|
2010-01-04 11:19:01 +01:00
|
|
|
#include "lib/meta/trait.hpp"
|
2009-12-29 04:39:27 +01:00
|
|
|
|
2010-01-04 11:19:01 +01:00
|
|
|
#include <boost/utility/enable_if.hpp>
|
2009-12-29 04:39:27 +01:00
|
|
|
#include <algorithm>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
namespace util {
|
|
|
|
|
|
2010-01-04 11:19:01 +01:00
|
|
|
using boost::enable_if;
|
|
|
|
|
using boost::disable_if;
|
2009-12-29 04:39:27 +01:00
|
|
|
|
2010-01-04 11:19:01 +01:00
|
|
|
using lib::meta::can_STL_ForEach;
|
|
|
|
|
using lib::meta::can_IterForEach;
|
2009-12-29 04:39:27 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** All quantification: check if all elements of a collection
|
|
|
|
|
* satisfy the given predicate. Actually a short-circuit
|
|
|
|
|
* evaluation is performed.
|
|
|
|
|
*/
|
2010-01-04 11:19:01 +01:00
|
|
|
template <typename IT, typename FUN>
|
2009-12-29 04:39:27 +01:00
|
|
|
inline bool
|
2010-01-04 11:19:01 +01:00
|
|
|
and_all (IT i, IT end, FUN predicate)
|
2009-12-31 03:25:25 +01:00
|
|
|
{
|
2010-01-04 11:19:01 +01:00
|
|
|
for ( ; i!=end; ++i )
|
2009-12-29 04:39:27 +01:00
|
|
|
if (!predicate(*i))
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** Existential quantification: check if any element
|
|
|
|
|
* of a collection satisfies the given predicate.
|
|
|
|
|
* Actually, a short-circuit evaluation is performed.
|
|
|
|
|
*/
|
2010-01-04 11:19:01 +01:00
|
|
|
template <typename IT, typename FUN>
|
2009-12-29 04:39:27 +01:00
|
|
|
inline bool
|
2010-01-04 11:19:01 +01:00
|
|
|
has_any (IT i, IT end, FUN predicate)
|
2009-12-31 03:25:25 +01:00
|
|
|
{
|
2010-01-04 11:19:01 +01:00
|
|
|
for ( ; i!=end; ++i )
|
2009-12-31 03:25:25 +01:00
|
|
|
if (predicate(*i))
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2010-01-04 11:19:01 +01:00
|
|
|
/* === specialisations for STL containers and Lumiera Forward Iterators === */
|
|
|
|
|
|
|
|
|
|
/** shortcut for operating on all elements of a STL container. */
|
|
|
|
|
template <typename Container
|
|
|
|
|
,typename FUN
|
|
|
|
|
>
|
|
|
|
|
inline typename disable_if< can_IterForEach<Container>,
|
|
|
|
|
FUN >::type
|
|
|
|
|
for_each (Container& c, FUN doIt)
|
2009-12-29 04:39:27 +01:00
|
|
|
{
|
2010-01-04 11:19:01 +01:00
|
|
|
return std::for_each (c.begin(),c.end(), doIt);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
template <typename Container
|
|
|
|
|
,typename FUN
|
|
|
|
|
>
|
|
|
|
|
inline typename enable_if< can_STL_ForEach<Container>,
|
|
|
|
|
bool >::type
|
|
|
|
|
and_all (Container& coll, FUN predicate)
|
|
|
|
|
{
|
|
|
|
|
return and_all (coll.begin(),coll.end(), predicate);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
template <typename Container
|
|
|
|
|
,typename FUN
|
|
|
|
|
>
|
|
|
|
|
inline typename enable_if< can_STL_ForEach<Container>,
|
|
|
|
|
bool >::type
|
|
|
|
|
has_any (Container& coll, FUN predicate)
|
|
|
|
|
{
|
|
|
|
|
return has_any (coll.begin(),coll.end(), predicate);
|
2009-12-29 04:39:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} // namespace util
|
|
|
|
|
#endif /*UTIL_FOREACH_H*/
|