@@ -129,6 +129,11 @@ struct npy_api {
129129 NPY_STRING_, NPY_UNICODE_, NPY_VOID_
130130 };
131131
132+ typedef struct {
133+ Py_intptr_t *ptr;
134+ int len;
135+ } PyArray_Dims;
136+
132137 static npy_api& get () {
133138 static npy_api api = lookup ();
134139 return api;
@@ -158,6 +163,7 @@ struct npy_api {
158163 Py_ssize_t *, PyObject **, PyObject *);
159164 PyObject *(*PyArray_Squeeze_)(PyObject *);
160165 int (*PyArray_SetBaseObject_)(PyObject *, PyObject *);
166+ PyObject* (*PyArray_Resize_)(PyObject*, PyArray_Dims*, int , int );
161167private:
162168 enum functions {
163169 API_PyArray_Type = 2 ,
@@ -166,6 +172,7 @@ struct npy_api {
166172 API_PyArray_DescrFromType = 45 ,
167173 API_PyArray_DescrFromScalar = 57 ,
168174 API_PyArray_FromAny = 69 ,
175+ API_PyArray_Resize = 80 ,
169176 API_PyArray_NewCopy = 85 ,
170177 API_PyArray_NewFromDescr = 94 ,
171178 API_PyArray_DescrNewFromType = 9 ,
@@ -192,6 +199,7 @@ struct npy_api {
192199 DECL_NPY_API (PyArray_DescrFromType);
193200 DECL_NPY_API (PyArray_DescrFromScalar);
194201 DECL_NPY_API (PyArray_FromAny);
202+ DECL_NPY_API (PyArray_Resize);
195203 DECL_NPY_API (PyArray_NewCopy);
196204 DECL_NPY_API (PyArray_NewFromDescr);
197205 DECL_NPY_API (PyArray_DescrNewFromType);
@@ -647,6 +655,21 @@ class array : public buffer {
647655 return reinterpret_steal<array>(api.PyArray_Squeeze_ (m_ptr));
648656 }
649657
658+ // / Resize array to given shape
659+ // / If refcheck is true and more that one reference exist to this array
660+ // / then resize will succeed only if it makes a reshape, i.e. original size doesn't change
661+ void resize (ShapeContainer new_shape, bool refcheck = true ) {
662+ detail::npy_api::PyArray_Dims d = {
663+ new_shape->data (), int (new_shape->size ())
664+ };
665+ // try to resize, set ordering param to -1 cause it's not used anyway
666+ object new_array = reinterpret_steal<object>(
667+ detail::npy_api::get ().PyArray_Resize_ (m_ptr, &d, int (refcheck), -1 )
668+ );
669+ if (!new_array) throw error_already_set ();
670+ if (isinstance<array>(new_array)) { *this = std::move (new_array); }
671+ }
672+
650673 // / Ensure that the argument is a NumPy array
651674 // / In case of an error, nullptr is returned and the Python error is cleared.
652675 static array ensure (handle h, int ExtraFlags = 0 ) {
0 commit comments