【发布时间】:2013-11-16 22:18:33
【问题描述】:
我能够在 C/C++ 中编写一个 void 函数,并使用 SWIG (int* INPLACE_ARRAY1, int DIM1) 包装到 Python/Numpy,它接收 int* vector 作为参数,对此向量进行一些数学运算,并覆盖结果相同的向量,并且此结果在 Python 的对象中可用。如下:
extern "C" void soma_aloc(int* vetor, int tamanho)
{
int m = 0;
int* ponteiro = new int[tamanho];
for(m = 0; m < tamanho; m++)
{
ponteiro[m] = vetor[m];
};
for(m = 0; m < tamanho; m++)
{
ponteiro[m] = ponteiro[m] * 10;
};
for(m = 0; m < tamanho; m++)
{
vetor[m] = ponteiro[m];
};
delete [] ponteiro;
};
这是一个学习如何使用 typemaps (DATA_TYPE* INPLACE_ARRAY1, int DIM1) 和 (DATA_TYPE* INPLACE_ARRAY2, int DIM1, int DIM2) 使用 SWIG 包装指向 int 和 double 数组的指针的测试,并且运行良好。
但问题是,我已经尝试使用 char/string Numpy 向量(如向量 vec1 = numpy.array(['a','a','a']) 或 numpy.array(['a','a','a'],dtype=str),并将每个位置更改为 (['b','b','b']),但 Python 显示 in method 'vector_char2D', argument 1 of type 'char *' . char/string 可以做同样的事情吗?
.cpp:
extern "C" void vetor_char2D(char* vetorchar, int tamanho_vetor)
{
for(int i = 0; i < tamanho_vetor; i++)
{
vetorchar[i] = 'b';
};
};
.i:
%module testestring
%include stl.i
%include std_string.i
%{
#include <stdio.h>
#include <stdlib.h>
//#include <string.h>
#include <string>
#include <iostream>
#define SWIG_FILE_WITH_INIT
#include "testestring.hpp"
%}
%include "numpy.i"
%init %{
import_array();
%}
%apply (char* INPLACE_ARRAY1, int DIM1) {(char* vetorchar, int tamanho_vetor)}
%include "testestring.hpp" (just the header of the above function vetor_char2D)
%clear (char* vetorchar, int tamanho_vetor);
我在 SWIG 方面的经验非常丰富。可以用char*、char** 和/或std::string*/std::string** 做到这一点吗?提前致谢!
【问题讨论】:
标签: python arrays numpy char swig