00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016 #ifndef EVT_PRED_GEN_HH
00017 #define EVT_PRED_GEN_HH
00018
00019 #include <stdio.h>
00020
00021 template <class Generator, class Predicate>
00022 class EvtPredGen {
00023
00024 public:
00025
00026 typedef typename Generator::result_type result_type;
00027
00028 EvtPredGen()
00029 : itsTried(0), itsPassed(0)
00030 {}
00031
00032 EvtPredGen(Generator gen, Predicate pred)
00033 : itsGen(gen), itsPred(pred), itsTried(0), itsPassed(0)
00034 {}
00035
00036 EvtPredGen(const EvtPredGen& other)
00037 : itsGen(other.itsGen), itsPred(other.itsPred),
00038 itsTried(other.itsTried), itsPassed(other.itsPassed)
00039 {}
00040
00041 ~EvtPredGen()
00042 {}
00043
00044 result_type operator()() {
00045
00046 int i = 0;
00047 int MAX = 10000;
00048 while(i++ < MAX) {
00049
00050 itsTried++;
00051 result_type point = itsGen();
00052 if(itsPred(point)) {
00053 itsPassed++;
00054 return point;
00055 }
00056 }
00057
00058 printf("No random point generated after %d attempts\n",MAX);
00059 printf("Sharp peak? Consider using pole compensation.\n");
00060 printf("I will now pick a point at random to return.\n");
00061 return itsGen();
00062 }
00063
00064 inline int getTried() const { return itsTried; }
00065 inline int getPassed() const { return itsPassed; }
00066
00067 protected:
00068
00069 Generator itsGen;
00070 Predicate itsPred;
00071 int itsTried;
00072 int itsPassed;
00073
00074 };
00075
00076 #endif
00077