@@ -42,7 +42,7 @@ from ..utils._heap cimport heap_push
42
42
from ..utils._sorting cimport simultaneous_sort
43
43
from ..utils._openmp_helpers cimport _openmp_thread_num
44
44
from ..utils._typedefs cimport ITYPE_t, DTYPE_t
45
- from ..utils._typedefs cimport ITYPECODE, DTYPECODE
45
+ from ..utils._vector_sentinel cimport vector_to_nd_array
46
46
47
47
from numbers import Integral, Real
48
48
from typing import List
@@ -76,70 +76,6 @@ ctypedef fused vector_vector_DITYPE_t:
76
76
vector[vector[DTYPE_t]]
77
77
78
78
79
- cdef class StdVectorSentinel:
80
- """ Wraps a reference to a vector which will be deallocated with this object.
81
-
82
- When created, the StdVectorSentinel swaps the reference of its internal
83
- vectors with the provided one (vec_ptr), thus making the StdVectorSentinel
84
- manage the provided one's lifetime.
85
- """
86
- pass
87
-
88
-
89
- # We necessarily need to define two extension types extending StdVectorSentinel
90
- # because we need to provide the dtype of the vector but can't use numeric fused types.
91
- cdef class StdVectorSentinelDTYPE(StdVectorSentinel):
92
- cdef vector[DTYPE_t] vec
93
-
94
- @staticmethod
95
- cdef StdVectorSentinel create_for(vector[DTYPE_t] * vec_ptr):
96
- # This initializes the object directly without calling __init__
97
- # See: https://cython.readthedocs.io/en/latest/src/userguide/extension_types.html#instantiation-from-existing-c-c-pointers # noqa
98
- cdef StdVectorSentinelDTYPE sentinel = StdVectorSentinelDTYPE.__new__ (StdVectorSentinelDTYPE)
99
- sentinel.vec.swap(deref(vec_ptr))
100
- return sentinel
101
-
102
-
103
- cdef class StdVectorSentinelITYPE(StdVectorSentinel):
104
- cdef vector[ITYPE_t] vec
105
-
106
- @staticmethod
107
- cdef StdVectorSentinel create_for(vector[ITYPE_t] * vec_ptr):
108
- # This initializes the object directly without calling __init__
109
- # See: https://cython.readthedocs.io/en/latest/src/userguide/extension_types.html#instantiation-from-existing-c-c-pointers # noqa
110
- cdef StdVectorSentinelITYPE sentinel = StdVectorSentinelITYPE.__new__ (StdVectorSentinelITYPE)
111
- sentinel.vec.swap(deref(vec_ptr))
112
- return sentinel
113
-
114
-
115
- cdef np.ndarray vector_to_nd_array(vector_DITYPE_t * vect_ptr):
116
- """ Create a numpy ndarray given a C++ vector.
117
-
118
- The numpy array buffer is the one of the C++ vector.
119
- A StdVectorSentinel is registered as the base object for the numpy array,
120
- freeing the C++ vector it encapsulates when the numpy array is freed.
121
- """
122
- typenum = DTYPECODE if vector_DITYPE_t is vector[DTYPE_t] else ITYPECODE
123
- cdef:
124
- np.npy_intp size = deref(vect_ptr).size()
125
- np.ndarray arr = np.PyArray_SimpleNewFromData(1 , & size, typenum,
126
- deref(vect_ptr).data())
127
- StdVectorSentinel sentinel
128
-
129
- if vector_DITYPE_t is vector[DTYPE_t]:
130
- sentinel = StdVectorSentinelDTYPE.create_for(vect_ptr)
131
- else :
132
- sentinel = StdVectorSentinelITYPE.create_for(vect_ptr)
133
-
134
- # Makes the numpy array responsible of the life-cycle of its buffer.
135
- # A reference to the StdVectorSentinel will be stolen by the call to
136
- # `PyArray_SetBaseObject` below, so we increase its reference counter.
137
- # See: https://docs.python.org/3/c-api/intro.html#reference-count-details
138
- Py_INCREF(sentinel)
139
- np.PyArray_SetBaseObject(arr, sentinel)
140
- return arr
141
-
142
-
143
79
cdef np.ndarray[object , ndim= 1 ] coerce_vectors_to_nd_arrays(
144
80
shared_ptr[vector_vector_DITYPE_t] vecs
145
81
):
0 commit comments