[C++-sig] std::vector

Ralf W. Grosse-Kunstleve rwgk at yahoo.com
Thu Oct 3 20:35:40 CEST 2002


--- "Ralf W. Grosse-Kunstleve" <rwgk at yahoo.com> wrote:
> --- "Scott A. Smith" <ssmith at magnet.fsu.edu> wrote:
> > It would be great if I can get vectors, lists, etc. working smoothly.
> > Your (& Dave's) help is and has been much appreciated.
> 
> I am sure the Boost.Python V2 facilities will help you achieving this goal. I
> am planning to write a little summary of the choices that you have. But first
> we have to get the CVS going again.

I have verified that the RC_1_29_0 branch of boost is working properly. 

Attached is the little summary that I promised earlier. Feedback is most
welcome.

Ralf

Notes on container conversions in Boost.Python V2:

1. Using the regular class_<> wrapper:

  class_<std::vector<double> >("std_vector_double")
    .def(...)
    ...
  ;

  This can be moved to a template so that several types (double, int,
  long, etc.) can be wrapped with the same code. This technique
  is used in the file

    scitbx/include/scitbx/array_family/boost_python/flex_wrapper.h

  in the "scitbx" package. The file could easily be modified for
  wrapping std::vector<> instantiations.

  This type of C++/Python binding is most suitable for containers
  that may contain a large number of elements (>10000).

2. Using custom rvalue converters.

  Boost.Python "rvalue converters" match function signatures
  such as:

    void foo(std::vector<double> const& array); // pass by const-reference
    void foo(std::vector<double> array); // pass by value

  Some custom rvalue converters are implemented in the file

    scitbx/include/scitbx/boost_python/container_conversions.h

  This code can be used to convert from C++ container types
  such as std::vector<> or std::list<> to Python tuples and
  vice versa. A few simple examples can be found in the file

    scitbx/array_family/boost_python/regression_test_module.cpp

  Automatic C++ container <-> Python tuple conversions are most
  suitable for containers of moderate size.
  These converters generate significantly less object code
  compared to alternative 1 above.

A disadvantage of using alternative 2 is that operators such as
arithmetic +,-,*,/,% are not available. It would be useful to have
custom rvalue converters that convert to a "math_array" type instead of
tuples. This is currently not implemented but is possible within
the framework of Boost.Python V2 as it will be released in the next
couple of weeks.

It would also be useful to also have "custom lvalue converters"
such as std::vector<> <-> Python list. These converters would
support the modification of the Python list from C++. For example:

  C++:

  void foo(std::vector<double>& array)
  {
    for(std::size_t i=0;i<array.size();i++) {
      array[i] *= 2;
    }
  }

  Python:

  >>> l = [1, 2, 3]
  >>> foo(l)
  >>> print l
  [2, 4, 6]

Custom lvalue converters require changes to the Boost.Python core
library and are currently not available.

P.S.:

The "scitbx" files referenced above are available via anonymous CVS:

cvs -d:pserver:anonymous at cvs.cctbx.sourceforge.net:/cvsroot/cctbx login
cvs -d:pserver:anonymous at cvs.cctbx.sourceforge.net:/cvsroot/cctbx co scitbx


__________________________________________________
Do you Yahoo!?
New DSL Internet Access from SBC & Yahoo!
http://sbc.yahoo.com




More information about the Cplusplus-sig mailing list