standard case (using STL container) solved, incl. binding arguments and member functions

This commit is contained in:
Fischlurch 2009-12-31 03:25:25 +01:00
parent e7fcfaca8d
commit e94927d5a3
3 changed files with 120 additions and 56 deletions

View file

@ -52,6 +52,21 @@ namespace util {
template <typename SEQ, typename Oper> template <typename SEQ, typename Oper>
inline bool inline bool
and_all (SEQ& coll, Oper predicate) and_all (SEQ& coll, Oper predicate)
{
typename SEQ::iterator e = coll.end();
typename SEQ::iterator i = coll.begin();
for ( ; i!=e; ++i )
if (!predicate(*i))
return false;
return true;
}
template <typename SEQ, typename Oper>
inline bool
and_all (SEQ const& coll, Oper predicate)
{ {
typename SEQ::const_iterator e = coll.end(); typename SEQ::const_iterator e = coll.end();
typename SEQ::const_iterator i = coll.begin(); typename SEQ::const_iterator i = coll.begin();
@ -71,6 +86,21 @@ namespace util {
template <typename SEQ, typename Oper> template <typename SEQ, typename Oper>
inline bool inline bool
has_any (SEQ& coll, Oper predicate) has_any (SEQ& coll, Oper predicate)
{
typename SEQ::iterator e = coll.end();
typename SEQ::iterator i = coll.begin();
for ( ; i!=e; ++i )
if (predicate(*i))
return true;
return false;
}
template <typename SEQ, typename Oper>
inline bool
has_any (SEQ const& coll, Oper predicate)
{ {
typename SEQ::const_iterator e = coll.end(); typename SEQ::const_iterator e = coll.end();
typename SEQ::const_iterator i = coll.begin(); typename SEQ::const_iterator i = coll.begin();

View file

@ -16,9 +16,9 @@
// 12/9 - tracking down a strange "warning: type qualifiers ignored on function return type" // 12/9 - tracking down a strange "warning: type qualifiers ignored on function return type"
#include <nobug.h> //#include <nobug.h>
#define LUMIERA_LOGGING_CXX //#define LUMIERA_LOGGING_CXX
#include "include/logging.h" //#include "include/logging.h"
//#include "include/nobugcfg.h" //#include "include/nobugcfg.h"
#include <tr1/functional> #include <tr1/functional>
@ -33,7 +33,7 @@
//using std::tr1::bind; //using std::tr1::bind;
using std::tr1::placeholders::_1; using std::tr1::placeholders::_1;
using std::rand; //using std::rand;
using std::string; using std::string;
using std::cout; using std::cout;
@ -46,35 +46,30 @@ using std::cout;
typename SEQ::const_iterator e = coll.end(); typename SEQ::const_iterator e = coll.end();
typename SEQ::const_iterator i = coll.begin(); typename SEQ::const_iterator i = coll.begin();
predicate(*i); for ( ; i!=e; ++i )
if (!predicate(*i))
// for ( ; i!=e; ++i ) return false;
// if (!predicate(*i))
// return false;
return true; return true;
} }
// template < typename CON, typename FUN template < typename CON, typename FUN
// , typename P1 , typename P1
// , typename P2 , typename P2
// > >
// inline bool inline bool
// eat_all (CON& elements, FUN function, P1 bind1, P2 bind2) eat_all (CON& elements, FUN function, P1 bind1, P2 bind2)
// { {
// return eat_all (elements, std::tr1::bind (function, bind1, bind2)); return eat_all (elements, std::tr1::bind<bool> (function, bind1, bind2));
// } }
namespace {
// std::tr1::_Placeholder<1> _1;
bool bool
plainFunc (int i, int j) plainFunc (int i, int j)
{ {
cout <<':'<< i+j; cout <<':'<< i+j;
return i+j; return i+j;
}
} }
@ -82,7 +77,7 @@ int
main (int, char**) main (int, char**)
{ {
NOBUG_INIT; // NOBUG_INIT;
typedef std::vector<int> VecI; typedef std::vector<int> VecI;
@ -91,8 +86,7 @@ main (int, char**)
while (count) while (count)
numberz.push_back(count--); numberz.push_back(count--);
// eat_all (numberz, plainFunc, 10, _1 ); eat_all (numberz, plainFunc, 10, _1 );
eat_all (numberz, std::tr1::bind<bool> (plainFunc, 10, _1));
cout << "\n.gulp.\n"; cout << "\n.gulp.\n";

View file

@ -95,7 +95,7 @@ namespace util {
inline bool //________________________________ inline bool //________________________________
and_all (CON& elements, FUN function, P1 bind1) ///< Accept binding for 1 Argument and_all (CON& elements, FUN function, P1 bind1) ///< Accept binding for 1 Argument
{ {
return and_all (elements, std::tr1::bind (function, bind1)); return and_all (elements, std::tr1::bind<bool> (function, bind1));
} }
@ -106,7 +106,7 @@ namespace util {
inline bool //________________________________ inline bool //________________________________
and_all (CON& elements, FUN function, P1 bind1, P2 bind2) ///< Accept binding for 2 Arguments and_all (CON& elements, FUN function, P1 bind1, P2 bind2) ///< Accept binding for 2 Arguments
{ {
return and_all (elements, std::tr1::bind (function, bind1, bind2)); return and_all (elements, std::tr1::bind<bool> (function, bind1, bind2));
} }
@ -118,7 +118,7 @@ namespace util {
inline bool //________________________________ inline bool //________________________________
and_all (CON& elements, FUN function, P1 bind1, P2 bind2, P3 bind3) ///< Accept binding for 3 Arguments and_all (CON& elements, FUN function, P1 bind1, P2 bind2, P3 bind3) ///< Accept binding for 3 Arguments
{ {
return and_all (elements, std::tr1::bind (function, bind1, bind2, bind3)); return and_all (elements, std::tr1::bind<bool> (function, bind1, bind2, bind3));
} }
@ -131,7 +131,7 @@ namespace util {
inline bool //________________________________ inline bool //________________________________
has_any (CON& elements, FUN function, P1 bind1) ///< Accept binding for 1 Argument has_any (CON& elements, FUN function, P1 bind1) ///< Accept binding for 1 Argument
{ {
return has_any (elements, std::tr1::bind (function, bind1)); return has_any (elements, std::tr1::bind<bool> (function, bind1));
} }
@ -142,7 +142,7 @@ namespace util {
inline bool //________________________________ inline bool //________________________________
has_any (CON& elements, FUN function, P1 bind1, P2 bind2) ///< Accept binding for 2 Arguments has_any (CON& elements, FUN function, P1 bind1, P2 bind2) ///< Accept binding for 2 Arguments
{ {
return has_any (elements, std::tr1::bind (function, bind1, bind2)); return has_any (elements, std::tr1::bind<bool> (function, bind1, bind2));
} }
@ -154,7 +154,7 @@ namespace util {
inline bool //________________________________ inline bool //________________________________
has_any (CON& elements, FUN function, P1 bind1, P2 bind2, P3 bind3) ///< Accept binding for 3 Arguments has_any (CON& elements, FUN function, P1 bind1, P2 bind2, P3 bind3) ///< Accept binding for 3 Arguments
{ {
return has_any (elements, std::tr1::bind (function, bind1, bind2, bind3)); return has_any (elements, std::tr1::bind<bool> (function, bind1, bind2, bind3));
} }
@ -213,6 +213,7 @@ namespace test {
#define _NL_ cout << endl; #define _NL_ cout << endl;
#define ANNOUNCE(_LABEL_) cout << "---:" << STRINGIFY(_LABEL_) << endl;
} // (End) test data and operations } // (End) test data and operations
@ -249,6 +250,8 @@ namespace test {
check_foreach_bind (container); check_foreach_bind (container);
// check_foreach_bind (iterator); // check_foreach_bind (iterator);
check_foreach_bind_const (container);
check_foreach_memFun (container); check_foreach_memFun (container);
// check_foreach_memFun (iterator); // check_foreach_memFun (iterator);
@ -270,6 +273,7 @@ namespace test {
void void
check_foreach_plain (CO coll) check_foreach_plain (CO coll)
{ {
ANNOUNCE (check_foreach_plain);
function<bool(int)> func(plainFunc); function<bool(int)> func(plainFunc);
for_each (coll, plainFunc); _NL_ for_each (coll, plainFunc); _NL_
@ -293,10 +297,10 @@ namespace test {
void void
check_foreach_bind (CO coll) check_foreach_bind (CO coll)
{ {
ANNOUNCE (check_foreach_bind);
function<bool(int,int)> fun1(function1); function<bool(int,int)> fun1(function1);
function<bool(int,int,int)> fun2(function2); function<bool(int,int,int)> fun2(function2);
#if false //////////////////////////////////////////////////////////////////////////////////////////////////////////TICKET 479 !!!!!!!!!
for_each (coll, function1, 10, _1 ); _NL_ for_each (coll, function1, 10, _1 ); _NL_
for_each (coll, &function1,10, _1 ); _NL_ for_each (coll, &function1,10, _1 ); _NL_
for_each (coll, fun1, 10, _1 ); _NL_ for_each (coll, fun1, 10, _1 ); _NL_
@ -321,12 +325,13 @@ namespace test {
has_any (coll, &function1, _1, _1 ); _NL_ has_any (coll, &function1, _1, _1 ); _NL_
has_any (coll, fun1, _1, _1 ); _NL_ has_any (coll, fun1, _1, _1 ); _NL_
// does not compile: //does not compile.....
for_each (coll, function1, 10, 20, _1 ); // for_each (coll, function1, 10, 20, _1 );
for_each (coll, function1, 10, 20 ); // for_each (coll, function1, _1, _2 );
for_each (coll, function1, 10 ); // for_each (coll, function1, 10 );
ANNOUNCE (assign_to_input);
for_each (coll, function2, 10, 20, _1 ); _NL_ for_each (coll, function2, 10, 20, _1 ); _NL_
for_each (coll, &function2,10, 20, _1 ); _NL_ for_each (coll, &function2,10, 20, _1 ); _NL_
for_each (coll, fun2, 10, 20, _1 ); _NL_ for_each (coll, fun2, 10, 20, _1 ); _NL_
@ -340,20 +345,49 @@ namespace test {
has_any (coll, fun2, 10, 20, _1 ); _NL_ has_any (coll, fun2, 10, 20, _1 ); _NL_
int sum=0; int sum=0;
ANNOUNCE (assign_to_var);
for_each (coll, function2, _1, _1, ref(sum) ); _NL_ for_each (coll, function2, _1, _1, ref(sum) ); _NL_
for_each (coll, &function2,_1, _1, ref(sum) ); _NL_ for_each (coll, &function2,_1, _1, ref(sum) ); _NL_
for_each (coll, fun2, _1, _1, ref(sum) ); _NL_ for_each (coll, fun2, _1, _1, ref(sum) ); _NL_
cout << "sum=" << sum << endl;
and_all (coll, function2, _1, _1, ref(sum) ); _NL_ and_all (coll, function2, _1, _1, ref(sum) ); _NL_
and_all (coll, &function2, _1, _1, ref(sum) ); _NL_ and_all (coll, &function2, _1, _1, ref(sum) ); _NL_
and_all (coll, fun2, _1, _1, ref(sum) ); _NL_ and_all (coll, fun2, _1, _1, ref(sum) ); _NL_
cout << "sum=" << sum << endl;
has_any (coll, function2, _1, _1, ref(sum) ); _NL_ has_any (coll, function2, _1, _1, ref(sum) ); _NL_
has_any (coll, &function2, _1, _1, ref(sum) ); _NL_ has_any (coll, &function2, _1, _1, ref(sum) ); _NL_
has_any (coll, fun2, _1, _1, ref(sum) ); _NL_ has_any (coll, fun2, _1, _1, ref(sum) ); _NL_
#endif ////////////////////////////////////////////////////////////////////////////////////////TODO lots of things unimplemented.....!!!!! cout << "sum=" << sum << endl;
}
/** @test the input sequence can be also taken
* from a const container (for iterators this
* obviously doesn't make sense */
template<typename CO>
void
check_foreach_bind_const (CO const& coll)
{
ANNOUNCE (check_foreach_bind_const);
for_each (coll,function1, 10, _1 ); _NL_
and_all (coll, function1, 10, _1 ); _NL_
has_any (coll, function1, 10, _1 ); _NL_
for_each (coll,function1, _1, _1 ); _NL_
and_all (coll, function1, _1, _1 ); _NL_
has_any (coll, function1, _1, _1 ); _NL_
int sum=0;
for_each (coll,function2, _1, _1, ref(sum) ); _NL_
and_all (coll, function2, _1, _1, ref(sum) ); _NL_
has_any (coll, function2, _1, _1, ref(sum) ); _NL_
} }
@ -375,18 +409,20 @@ namespace test {
void void
check_foreach_memFun (CO coll) check_foreach_memFun (CO coll)
{ {
ANNOUNCE (check_foreach_memFun);
Dummy dummy; Dummy dummy;
dummy.sum_ = 0;
#if false //////////////////////////////////////////////////////////////////////////////////////////////////////////TICKET 479 !!!!!!!!! for_each (coll, &Dummy::fun, dummy, _1 ); _NL_
for_each (coll, &Dummy::fun, dummy, _1 ); _NL_ and_all (coll, &Dummy::fun, dummy, _1 ); _NL_
and_all (coll, &Dummy::fun, dummy, _1 ); _NL_ has_any (coll, &Dummy::fun, dummy, _1 ); _NL_
has_any (coll, &Dummy::fun, dummy, _1 ); _NL_
for_each (coll, &Dummy::fun, &dummy, _1 ); _NL_ for_each (coll, &Dummy::fun, &dummy, _1 ); _NL_
and_all (coll, &Dummy::fun, &dummy, _1 ); _NL_ and_all (coll, &Dummy::fun, &dummy, _1 ); _NL_
has_any (coll, &Dummy::fun, &dummy, _1 ); _NL_ has_any (coll, &Dummy::fun, &dummy, _1 ); _NL_
#endif ////////////////////////////////////////////////////////////////////////////////////////TODO lots of things unimplemented.....!!!!!
cout << "sum=" << dummy.sum_ << endl;
} }
@ -395,8 +431,10 @@ namespace test {
void void
check_foreach_lambda (CO coll) check_foreach_lambda (CO coll)
{ {
uint sum; ANNOUNCE (check_foreach_lambda);
for_each (coll, _1_ + var(sum)); uint sum(0);
for_each (coll, var(sum) += _1_ );
ASSERT (sum == NUM_ELMS/2 * (NUM_ELMS+1)); ASSERT (sum == NUM_ELMS/2 * (NUM_ELMS+1));
@ -411,6 +449,8 @@ namespace test {
void void
check_existence_quant (CO coll) check_existence_quant (CO coll)
{ {
ANNOUNCE (check_existence_quant);
ASSERT ( and_all (coll, 0 < _1_ )); ASSERT ( and_all (coll, 0 < _1_ ));
ASSERT (!and_all (coll, 1 < _1_ )); ASSERT (!and_all (coll, 1 < _1_ ));
@ -425,7 +465,7 @@ namespace test {
uint n_; uint n_;
TestElm(uint i) : n_(i) {} TestElm(uint i) : n_(i) {}
void operation() { plainFunc (n_); } bool operation() { return plainFunc (n_); }
}; };
@ -435,6 +475,8 @@ namespace test {
void void
check_invoke_on_each () check_invoke_on_each ()
{ {
ANNOUNCE (check_invoke_on_each);
std::vector<TestElm> elms; std::vector<TestElm> elms;
for (uint i=0; i<6; ++i) for (uint i=0; i<6; ++i)
elms.push_back (TestElm(i)); elms.push_back (TestElm(i));
@ -444,16 +486,14 @@ namespace test {
elmPtrs.push_back (& elms[i]); elmPtrs.push_back (& elms[i]);
// fed the element pointer as "this" pointer of the member function // fed the element pointer as "this" pointer of the member function
#if false //////////////////////////////////////////////////////////////////////////////////////////////////////////TICKET 479 !!!!!!!!!
for_each (elmPtrs, &TestElm::operation, _1 ); _NL_ for_each (elmPtrs, &TestElm::operation, _1 ); _NL_
and_all (elmPtrs, &TestElm::operation, _1 ); _NL_ and_all (elmPtrs, &TestElm::operation, _1 ); _NL_
has_any (elmPtrs, &TestElm::operation, _1 ); _NL_ has_any (elmPtrs, &TestElm::operation, _1 ); _NL_
// but works with references as well // the same works with references as well...
for_each (elms, &TestElm::operation, _1 ); _NL_ for_each (elms, &TestElm::operation, _1 ); _NL_
and_all (elms, &TestElm::operation, _1 ); _NL_ and_all (elms, &TestElm::operation, _1 ); _NL_
has_any (elms, &TestElm::operation, _1 ); _NL_ has_any (elms, &TestElm::operation, _1 ); _NL_
#endif ////////////////////////////////////////////////////////////////////////////////////////TODO lots of things unimplemented.....!!!!!
} }
}; };