gpt4 book ai didi

python - Typemap 资源并将列表转换为 vector (和返回)

转载 作者:行者123 更新时间:2023-11-30 03:13:49 26 4
gpt4 key购买 nike

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

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

我的问题是:

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

这是我的代码:

添加数组.h

#include <vector>
#include <functional>

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

添加数组.i

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

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

%include "add_array.h"

添加数组.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 - Typemap 资源并将列表转换为 vector (和返回),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58405205/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com