-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample.cpp
77 lines (66 loc) · 2.13 KB
/
example.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include "Python.h"
#include <algorithm>
#include <vector>
template<typename T>
void reverse_it_impl(std::vector<T>& in){
std::reverse(in.begin(), in.end());
}
/* Reverse a bytes array and extract the data with buffer interface */
static PyObject *
reverse_it_fast(PyObject *self, PyObject *args){
PyObject* incoming;
if(!PyArg_ParseTuple(args, "O", &incoming)){
return NULL;
}
if(!PyObject_CheckBuffer(incoming)){
/* TODO: perhaps raise BufferError via PyExc_BufferError? */
return NULL;
}
Py_buffer view;
int buffer_acquisition_result;
buffer_acquisition_result = PyObject_GetBuffer(incoming, &view, PyBUF_SIMPLE);
/* Fill the std::vector with the buffer contents */
std::vector<char> original_data;
char* buffer_start = static_cast<char*>((&view.buf)[0]);
original_data.assign(buffer_start, buffer_start + view.len);
PyBuffer_Release(&view);
reverse_it_impl(original_data);
return Py_BuildValue("y#", original_data.data(), original_data.size());
}
/* Reverse a sequence and extract the data with a copy */
static PyObject *
reverse_it(PyObject *self, PyObject *args){
PyObject* incoming;
if(!PyArg_ParseTuple(args, "O", &incoming)){
return NULL;
}
char* data;
Py_ssize_t len;
PyBytes_AsStringAndSize(incoming, &data, &len);
std::vector<char> original_data(data, data+len);
reverse_it_impl(original_data);
return Py_BuildValue("y#", original_data.data(), original_data.size());
}
/* Methods for our reverser object*/
static PyMethodDef ReverserMethods[] = {
{"reverse_it_fast", reverse_it_fast, METH_VARARGS, "Reverses the bytes array using buffer interface"},
{"reverse_it", reverse_it, METH_VARARGS, "Reverses the bytes array"},
{NULL, NULL, 0, NULL} /* End of methods sential value */
};
/* define the module */
static struct PyModuleDef reverser_module = {
PyModuleDef_HEAD_INIT,
"reverser_module",
"Reverser module, written in c++",
-1,
ReverserMethods,
NULL,
NULL,
NULL,
NULL
};
/* Init code */
PyMODINIT_FUNC
PyInit_reverser(void){
return PyModule_Create(&reverser_module);
}