python - 类型映射资源以及将列表转换为 vector (以及反之)

标签 python c++ swig typemaps

我正在使用 SWIG 将 c++ 包装在 python 中,并且需要使用类型映射以使我的 python 脚本尽可能简单。作为第一次尝试,我只是发送 2 个列表,将它们转换为 vector ,将两个 vector 相加,然后将结果返回到新列表中。

我的问题是,我发现 SWIG 手册没有太多指导意义,很难理解,并且没有给出任何可靠、完整的示例来说明如何编写自己的类型映射。

我的问题是:

  1. 如何确保我的列表正确转换为 vector 列表,然后再转换回来?
  2. 对于如何编写类型映射以及所有语法/函数的含义,是否有更好的教程/引用?

这是我的代码:

add_array.h

#include <vector>
#include <functional>

std::vector<int> add_array(std::vector<int> src1, std::vector<int> src2);

add_array.i

%module add_array
%{
#include "add_array.h"
%}

%include std_vector.i 
%template(vectorInt) std::vector<int>;

%include "add_array.h"

add_array.cpp

#include "add_array.h"
#include <cassert>
#include <cstring>

std::vector<int> add_array(std::vector<int> src1, std::vector<int> src2) {  
  assert(src1.size() == src2.size());
  std::vector<int> dst;
  dst.resize(src1.size());

  for (size_t i = 0; i < src1.size(); i++) {
    dst[i] = src1[i] + src2[i];
  }
  return dst;
}

生成文件

all:
rm -f *.so *.o *_wrap.* *.pyc *.gch add_array.py
swig -c++ -python add_array.i
g++ -fpic -c add_array_wrap.cxx add_array.h add_array.cpp -I/home/tools/anaconda3/pkgs/python-3.7.3-h0371630_0/include/python3.7m/
g++ -shared add_array_wrap.o add_array.o -o _add_array.so

array.py(这是我正在运行的文件)

import add_array

a = [1, 2, 3, 4, 5, 6]
b = [5, 6, 7, 8, 9, 10]
c = add_array.add_array(a, b)
print(c)

输出:(6、8、10、12、14、16)

这是一个元组(我希望它是一个列表)。 看起来我很幸运它可以将输入列表转换为 vector (而在另一个方向则不太幸运),但我真的很想知道这是如何发生的以及如果需要的话如何为 future 的代码更改它.

谢谢!

最佳答案

我不知道是否有具体原因,但包含的 std_vector.i 将输出 vector 转换为元组而不是列表。如果您想要一个列表,则需要编写自定义类型映射。

示例(无错误检查):

%module add_array
%{
#include "add_array.h"
%}

%include <std_vector.i>
%template(vectorInt) std::vector<int>;

// Override the template output typemap with one that returns a list.
// An "out" typemap controls how a value is returned.
// When a function returns std::vector<int> this template will convert it to
// a Python object.  In this case, a PyList.
// 
// Note: PyObject* tmp declares a local variable that will be used by this code snippet.
// Make sure to look at the generated wrapper code and find the add_array_wrap function
// and how this code is integrated into it.
// 
%typemap(out) std::vector<int> (PyObject* tmp) %{

    // Allocate a PyList object of the requested size.
    // $1 references the first type in the type list (in this case, std::vector<int>)
    // and represents the c++ return value of a function that returns
    // this type; therefore, we can call methods on that value to get the size.
    //
    // Note: The elements of the new PyList are null pointers and MUST be
    //       populated before returning it to Python.
    //
    tmp = PyList_New($1.size());

    // Populate the PyList.  PyLong_FromLong converts a C++ "long" to a
    // Python PyLong object.  PyList_SET_ITEM takes a PyList object (tmp),
    // an index (i), and a Python object to put in the list.  This particular
    // function *steals* the reference to the Python object, so you don't have to
    // Py_DECREF the object to free it later.
    //
    for(int i = 0; i < $1.size(); ++i)
        PyList_SET_ITEM(tmp,i,PyLong_FromLong($1[i]));

    // $result is where you assign the Python object that should be returned
    // after converting the C++ $1 object.  SWIG_Python_AppendOutput is not
    // well-documented, but it appends the return object to an existing
    // returned object.  It's most useful for "argout" typemaps where multiple
    // output or in/out arguments in C++ can be returned as a tuple of
    // return values in Python.  For example, a function like:
    //
    //     void func(int* pValue1, int* pValue2);
    //
    // would normally return None ($result = Py_None), but an "argout" typemap
    // could *$1 to a PyLong and use SWIG_Python_AppendOutput to add it to
    // the result.  The template would be applied twice and you'd get a tuple.
    //
    $result = SWIG_Python_AppendOutput($result,tmp);
%}

%include "add_array.h"

输出:

>>> import add_array
>>> add_array.add_array([1,2,3],[4,5,6])
[5, 7, 9]

就教程而言,我只阅读过 SWIG 文档和 C 语言扩展的特定语言文档。就文档而言,它实际上相当不错,但您不能只是挑选要阅读的内容。研究前十几节的基础知识,然后跳到特定于语言的部分(例如 Python)。 SWIG 安装下也有一个 Examples 目录。

引用文献:

您必须查看 SWIG 源才能获取有关 SWIG_Python_AppendOutput 的任何信息。或者只是通过谷歌搜索其他示例。

关于python - 类型映射资源以及将列表转换为 vector (以及反之),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58405205/

相关文章:

c++ - 用于 C++(或 C)的 mmap 可加载数据结构库

python - SWIG、Python 和指针

python - swig:如何在 Python 中访问 C 数组

python - 仅将唯一对象插入列表

python - pySerial 程序停止正常工作

python - VPython 在 Jupyter 中明显变慢

c++ - 使用 SFINAE 检查类型是否可以绑定(bind)到模板模板参数

c++ - 在2维数组上使用delete []时发生访问冲突

python - 未找到 SWIG_AsVal_wchar_t 标识符

python - 使用条件更改列值或附加新行的数据框更新 SQLite DB