bindings/python/python-filtration.h
author Dmitriy Morozov <dmitriy@mrzv.org>
Mon, 13 Apr 2009 20:38:46 -0700
branchdev
changeset 127 406c6cc00b9c
parent 104 2cc1db3b98c6
permissions -rw-r--r--
Changes in Python Bindings: * the exposed C++ simplex has Empty data * Python Simplex constructor with data as well as data-based comparison functions are implemented in pure Python

#ifndef __PYTHON_FILTRATION_H__
#define __PYTHON_FILTRATION_H__

#include <topology/filtration.h>
#include <boost/python.hpp>
#include "python-simplex.h"

namespace bp = boost::python;

// Random access iterator into python's list (using integer indices)
class ListRandomAccessIterator:
    public boost::iterator_adaptor<ListRandomAccessIterator,                // Derived
                                   boost::counting_iterator<unsigned>,      // Base
                                   SimplexObject,                           // Value
                                   boost::use_default,
                                   SimplexObject>
{
    public:
        typedef                 ListRandomAccessIterator                                        Self;
        typedef                 boost::iterator_adaptor<ListRandomAccessIterator,           
                                                        boost::counting_iterator<unsigned>,     
                                                        SimplexObject,
                                                        boost::use_default,
                                                        SimplexObject>                          Parent;
                    
                                ListRandomAccessIterator()                                      {}

                                ListRandomAccessIterator(bp::list l, unsigned i):
                                    Parent(i), l_(l)                                            {}

    private:
        friend class boost::iterator_core_access;
        friend class FiltrationPythonIterator;

        Parent::reference       dereference() const                                             { return bp::object(l_[*(this->base())]); }

        bp::list                l_;
};

// ComplexTraits describing complexes of type list
struct ListTraits
{
    typedef     bp::list                                        Complex;
    typedef     SimplexObject                                   Simplex;
    typedef     ListRandomAccessIterator                        Index;
    typedef     std::less<Index>                                IndexComparison;

    typedef     BinarySearchMap<SimplexVD, Index,
                                SimplexVD::VertexComparison>    SimplexIndexMap;

    static SimplexIndexMap      simplex_index_map(const Complex& l)             { return SimplexIndexMap(begin(l), end(l)); }
    static SimplexIndexMap      simplex_index_map(Index bg, Index end)          { return SimplexIndexMap(bg, end); }

    static unsigned             size(const Complex& l)                          { return bp::len(l); }
    static Index                begin(const Complex& l)                         { return Index(l, 0); }
    static Index                end(const Complex& l)                           { return Index(l, size(l)); }
};

typedef         Filtration<bp::list, unsigned, ListTraits>          ListFiltration;

#endif