forked from shapely/shapely
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vector.c
43 lines (34 loc) · 1.12 KB
/
vector.c
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
#define PY_SSIZE_T_CLEAN
#define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION
#include <Python.h>
#include <structmember.h>
#define NO_IMPORT_ARRAY
#define PY_ARRAY_UNIQUE_SYMBOL shapely_ARRAY_API
#include <numpy/arrayobject.h>
#include <numpy/ndarraytypes.h>
#include <numpy/npy_3kcompat.h>
#include "kvec.h"
#include "vector.h"
/* Copy values from arr of indexes to a new numpy integer array.
*
* Parameters
* ----------
* arr: dynamic vector array to convert to ndarray
*/
PyArrayObject* index_vec_to_npy_arr(index_vec_t* arr) {
Py_ssize_t i;
npy_intp size = kv_size(*arr);
npy_intp dims[1] = {size};
// the following raises a compiler warning based on how the macro is defined
// in numpy. There doesn't appear to be anything we can do to avoid it.
PyArrayObject* result = (PyArrayObject*)PyArray_SimpleNew(1, dims, NPY_INTP);
if (result == NULL) {
PyErr_SetString(PyExc_RuntimeError, "could not allocate numpy array");
return NULL;
}
for (i = 0; i < size; i++) {
// assign value into numpy array
*(npy_intp*)PyArray_GETPTR1(result, i) = kv_A(*arr, i);
}
return (PyArrayObject*)result;
}