# statistics.cpp

    00001 #include <utils/eoStat.h>
    00002
    00003 #include "PyEO.h"
    00004 #include "valueParam.h"
    00005
    00006 using namespace boost::python;
    00007
    00008 class StatBaseWrapper : public eoStatBase<PyEO>
    00009 {
    00010     public:
    00011     PyObject* self;
    00012     StatBaseWrapper(PyObject* p) : self(p) {}
    00013
    00014     void operator()(const eoPop<PyEO>& pop)
    00015     {
    00016         call_method<void>(self, "__call__", boost::ref(pop));
    00017     }
    00018 };
    00019
    00020 class SortedStatBaseWrapper : public eoSortedStatBase<PyEO>
    00021 {
    00022     public:
    00023     PyObject* self;
    00024     SortedStatBaseWrapper(PyObject* p) : self(p) {}
    00025
    00026     void operator()(const std::vector<const PyEO*>& pop)
    00027     {
    00028         call_method<void>(self, "__call__", boost::ref(pop));
    00029     }
    00030 };
    00031
    00032 typedef std::vector<const PyEO*> eoPopView;
    00033
    00034 const PyEO& popview_getitem(const std::vector<const PyEO*>& pop, int it)
    00035 {
    00036     unsigned item = unsigned(it);
    00037     if (item > pop.size())
    00038         throw index_error("too much");
    00039
    00040     return *pop[item];
    00041 }
    00042
    00043 void statistics()
    00044 {
    00045     class_<eoStatBase<PyEO>, StatBaseWrapper, boost::noncopyable>
    00046         ("eoStatBase", init<>())
    00047         .def("lastCall", &eoStatBase<PyEO>::lastCall)
    00048         .def("__call__", &StatBaseWrapper::operator())
    00049         ;
    00050
    00051     class_< eoPopView >("eoPopView")
    00052         .def("__getitem__", popview_getitem, return_internal_reference<>() )
    00053         .def("__len__", &eoPopView::size)
    00054         ;
    00055
    00056     class_<eoSortedStatBase<PyEO>, SortedStatBaseWrapper, boost::noncopyable>
    00057         ("eoSortedStatBase", init<>())
    00058         .def("lastCall", &eoSortedStatBase<PyEO>::lastCall)
    00059         .def("__call__", &SortedStatBaseWrapper::operator())
    00060         ;
    00061 }