Typemap 资源并将列表转换为向量(和返回)
Typemap resources and converting a list to vector (and back)
我正在使用 SWIG 将 c++ 包装在 python 中,并且需要使用类型映射以使我的 python 脚本尽可能简单。作为第一次尝试,我只是发送 2 个列表,将它们转换为向量,添加两个向量,然后将结果返回到新列表中。
我的问题是,我发现 SWIG 手册没有太多指导意义,难以遵循,并且没有提供任何关于如何编写自己的类型映射的可靠、完整的示例。
我的问题是:
- 我将如何确保我的列表正确地转换为矢量的,然后再转换回来?
- 有没有更好的 tutorials/references 如何编写类型映射,以及所有 syntax/functions 的意思?
这是我的代码:
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)
这是一个元组(我希望它是一个列表)。
看起来我很幸运它可以将输入列表转换为向量(而在另一个方向上就没那么幸运了),但我真的很想知道这是怎么发生的,以及如果需要的话我如何为未来的代码改变它.
谢谢!
我不知道是否有特定原因,但包含的 std_vector.i
将输出向量转换为元组而不是列表。如果你想要一个列表,你需要编写一个自定义类型映射。
示例(无错误检查):
%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.
// 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(.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 < .size(); ++i)
PyList_SET_ITEM(tmp,i,PyLong_FromLong([i]));
// $result is where you assign the Python object that should be returned
// after converting the C++ 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 * 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 的任何信息。或者只是 google 其他示例。
我正在使用 SWIG 将 c++ 包装在 python 中,并且需要使用类型映射以使我的 python 脚本尽可能简单。作为第一次尝试,我只是发送 2 个列表,将它们转换为向量,添加两个向量,然后将结果返回到新列表中。
我的问题是,我发现 SWIG 手册没有太多指导意义,难以遵循,并且没有提供任何关于如何编写自己的类型映射的可靠、完整的示例。
我的问题是:
- 我将如何确保我的列表正确地转换为矢量的,然后再转换回来?
- 有没有更好的 tutorials/references 如何编写类型映射,以及所有 syntax/functions 的意思?
这是我的代码:
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)
这是一个元组(我希望它是一个列表)。 看起来我很幸运它可以将输入列表转换为向量(而在另一个方向上就没那么幸运了),但我真的很想知道这是怎么发生的,以及如果需要的话我如何为未来的代码改变它.
谢谢!
我不知道是否有特定原因,但包含的 std_vector.i
将输出向量转换为元组而不是列表。如果你想要一个列表,你需要编写一个自定义类型映射。
示例(无错误检查):
%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.
// 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(.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 < .size(); ++i)
PyList_SET_ITEM(tmp,i,PyLong_FromLong([i]));
// $result is where you assign the Python object that should be returned
// after converting the C++ 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 * 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 的任何信息。或者只是 google 其他示例。