Use C++-Iterators on Python-List with the Python/C API? -


is possible use tools needing iterators , function-pointer module <algorithm> on pyobjects ?

the concrete problem want solve (it constructed learn it):

  • i have huge list of ids stored in python-list
  • now want perform std::binary_search on list, using module written in c++

one way access python-list c-array, constructing vector (which uses pointers/ not copy), doing binary_search , export array pyobject.

would possible?

well, binary search isn't complicated, why don't code 1 based on range of indices instead of iterators? believe list conforms sequence protocol of python, should pretty easy.

if want use binary_search() algorithm learning experience, there possibility create stl-style iterators on top of python sequence. need pointer sequence , index create random-access iterator. if want to, can transparently convert python objects in list according id type (some integer type, guess).

struct iterator {     // typedefs required compliant stl-style iterators     typedef pyobject* value_type;      iterator(pyobject* seqeunce, py_ssize_t position):         m_sequence(sequence), m_position(position)     {         assert(pysequence_check(m_sequence));         assert(m_position >= 0);         assert(m_position <= pysequence_getsize(m_sequence));     }     value_type operator*() const     {         assert(m_position < pysequence_getsize(m_sequence));         return pysequence_getitem(m_sequence, m_position);     }     iterator& operator++()     {         assert(m_position <= pysequence_getsize(m_sequence));         ++m_position;         return *this;     }     iterator& operator+=(size_t l)     {         m_position += l;         return *this;     } }; 

i haven't compiled , forgot few parts, guess idea. init 2 iterators, 1 offset of 0 , 1 offset of size of container , give binary_search().


Comments

Popular posts from this blog

Perl - how to grep a block of text from a file -

delphi - How to remove all the grips on a coolbar if I have several coolbands? -

javascript - Animating array of divs; only the final element is modified -