bindings/python/filtration.cpp
author Dmitriy Morozov <dmitriy@mrzv.org>
Tue, 14 Apr 2009 14:38:08 -0700
branchdev
changeset 129 95454ea3f9c0
parent 128 a5fd0c2a1c88
child 130 580eaa850c4f
permissions -rw-r--r--
Cleaned up Python bindings to restore functionality: * StaticPersistence is iterable, its nodes are usable (sign, pair, cycle) * StaticPersistence knows how to map its nodes into Filtration indices * moved PythonCmp into utils.h * minor cosmetic changes

#include <topology/filtration.h>
#include <boost/iterator.hpp>
#include "simplex.h"
#include <iostream>

#include <boost/python.hpp>
using namespace boost::python;


#include "filtration.h"      // defines ListFiltration, ListTraits, ListRandomAccessIterator
#include "utils.h"           // defines PythonCmp

boost::shared_ptr<ListFiltration>  init_from_list(list lst, object cmp)
{
    boost::shared_ptr<ListFiltration>  p(new ListFiltration(ListTraits::begin(lst),
                                                            ListTraits::end(lst),
                                                            PythonCmp(cmp)));
    return p;
}

FiltrationPythonIterator
lf_begin(const ListFiltration& lf)
{ return lf.begin(); }

FiltrationPythonIterator
lf_end(const ListFiltration& lf)
{ return lf.end(); }

unsigned
lf_getitem(const ListFiltration& lf, unsigned i)       
{ return *(lf_begin(lf) + i); }


void export_filtration()
{
    class_<ListFiltration>("Filtration", no_init)
        .def("__init__",        make_constructor(&init_from_list))

        .def("__getitem__",     &lf_getitem)
        .def("__iter__",        range(&lf_begin, &lf_end))
        .def("__len__",         &ListFiltration::size)
    ;
}