]> git.proxmox.com Git - ceph.git/blobdiff - ceph/src/boost/libs/python/src/numpy/ndarray.cpp
update sources to v12.2.3
[ceph.git] / ceph / src / boost / libs / python / src / numpy / ndarray.cpp
index 710e3d499393c31de4f5177c9c71727b8d5facd1..af09ecc3381378a84d58959b76d75bbb59faa739 100644 (file)
@@ -22,20 +22,20 @@ namespace detail
 ndarray::bitflag numpy_to_bitflag(int const f)
 {
   ndarray::bitflag r = ndarray::NONE;
-  if (f & NPY_C_CONTIGUOUS) r = (r | ndarray::C_CONTIGUOUS);
-  if (f & NPY_F_CONTIGUOUS) r = (r | ndarray::F_CONTIGUOUS);
-  if (f & NPY_ALIGNED) r = (r | ndarray::ALIGNED);
-  if (f & NPY_WRITEABLE) r = (r | ndarray::WRITEABLE);
+  if (f & NPY_ARRAY_C_CONTIGUOUS) r = (r | ndarray::C_CONTIGUOUS);
+  if (f & NPY_ARRAY_F_CONTIGUOUS) r = (r | ndarray::F_CONTIGUOUS);
+  if (f & NPY_ARRAY_ALIGNED) r = (r | ndarray::ALIGNED);
+  if (f & NPY_ARRAY_WRITEABLE) r = (r | ndarray::WRITEABLE);
   return r;
 }
 
 int bitflag_to_numpy(ndarray::bitflag f)
 {
   int r = 0;
-  if (f & ndarray::C_CONTIGUOUS) r |= NPY_C_CONTIGUOUS;
-  if (f & ndarray::F_CONTIGUOUS) r |= NPY_F_CONTIGUOUS;
-  if (f & ndarray::ALIGNED) r |= NPY_ALIGNED;
-  if (f & ndarray::WRITEABLE) r |= NPY_WRITEABLE;
+  if (f & ndarray::C_CONTIGUOUS) r |= NPY_ARRAY_C_CONTIGUOUS;
+  if (f & ndarray::F_CONTIGUOUS) r |= NPY_ARRAY_F_CONTIGUOUS;
+  if (f & ndarray::ALIGNED) r |= NPY_ARRAY_ALIGNED;
+  if (f & ndarray::WRITEABLE) r |= NPY_ARRAY_WRITEABLE;
   return r;
 }
 
@@ -119,10 +119,10 @@ ndarray from_data_impl(void * data,
   }
   int itemsize = dt.get_itemsize();
   int flags = 0;
-  if (writeable) flags |= NPY_WRITEABLE;
-  if (is_c_contiguous(shape, strides, itemsize)) flags |= NPY_C_CONTIGUOUS;
-  if (is_f_contiguous(shape, strides, itemsize)) flags |= NPY_F_CONTIGUOUS;
-  if (is_aligned(strides, itemsize)) flags |= NPY_ALIGNED;
+  if (writeable) flags |= NPY_ARRAY_WRITEABLE;
+  if (is_c_contiguous(shape, strides, itemsize)) flags |= NPY_ARRAY_C_CONTIGUOUS;
+  if (is_f_contiguous(shape, strides, itemsize)) flags |= NPY_ARRAY_F_CONTIGUOUS;
+  if (is_aligned(strides, itemsize)) flags |= NPY_ARRAY_ALIGNED;
   ndarray r(python::detail::new_reference
     (PyArray_NewFromDescr(&PyArray_Type,
                          incref_dtype(dt),
@@ -138,6 +138,30 @@ ndarray from_data_impl(void * data,
 
 } // namespace detail
 
+namespace {
+    int normalize_index(int n,int nlim) // wraps [-nlim:nlim) into [0:nlim), throw IndexError otherwise
+    {
+        if (n<0)
+            n += nlim; // negative indices work backwards from end
+        if (n < 0 || n >= nlim)
+        {
+            PyErr_SetObject(PyExc_IndexError, Py_None);
+            throw_error_already_set();
+        }
+        return n;
+    }
+}
+
+Py_intptr_t ndarray::shape(int n) const
+{
+    return get_shape()[normalize_index(n,get_nd())];
+}
+
+Py_intptr_t ndarray::strides(int n) const
+{
+    return get_strides()[normalize_index(n,get_nd())];
+}
+
 ndarray ndarray::view(dtype const & dt) const
 {
   return ndarray(python::detail::new_reference
@@ -170,7 +194,7 @@ python::object ndarray::get_base() const
 void ndarray::set_base(object const & base) 
 {
   Py_XDECREF(get_struct()->base);
-  if (base != object()) 
+  if (base.ptr())
   {
     Py_INCREF(base.ptr());
     get_struct()->base = base.ptr();
@@ -243,13 +267,13 @@ ndarray empty(int nd, Py_intptr_t const * shape, dtype const & dt)
 ndarray array(python::object const & obj) 
 {
   return ndarray(python::detail::new_reference
-    (PyArray_FromAny(obj.ptr(), NULL, 0, 0, NPY_ENSUREARRAY, NULL)));
+    (PyArray_FromAny(obj.ptr(), NULL, 0, 0, NPY_ARRAY_ENSUREARRAY, NULL)));
 }
 
 ndarray array(python::object const & obj, dtype const & dt) 
 {
   return ndarray(python::detail::new_reference
-    (PyArray_FromAny(obj.ptr(), detail::incref_dtype(dt), 0, 0, NPY_ENSUREARRAY, NULL)));
+    (PyArray_FromAny(obj.ptr(), detail::incref_dtype(dt), 0, 0, NPY_ARRAY_ENSUREARRAY, NULL)));
 }
 
 ndarray from_object(python::object const & obj, dtype const & dt, int nd_min, int nd_max, ndarray::bitflag flags)