diff liboctave/array/Array.h @ 30227:b00ff462e0f2

improve efficiency of mxArray -> octave_value conversion * Array.h (Array<T>::ArrayRep, Array<T>::Array): New constructor that accepts externally allocated data array. * Sparse.h (Sparse<T>::SparseRep, Sparse<T>::Sparse): New constructor that accepts externally allocated data and index arrays. * mex.cc (maybe_disown_ptr): New function. (mxArray_interleaved_full::fp_to_ov): Use new Array constructor to pass allocated data from mxArray object to Array object without copying data. Call maybe_disown_ptr on data pointer to give up ownership. (mxArray_interleaved_sparse::to_ov): Use new Array constructor to pass allocated data from mxArray object to Array object without copying data. Call maybe_disown_ptr on data pointer to give up ownership. (mxArray_separate_full::to_ov): New template. (mxArray_separate_flll::as_octave_value): Use it.
author John W. Eaton <jwe@octave.org>
date Fri, 01 Oct 2021 15:25:51 -0400
parents 4c88a452519c
children a2936935c7c8
line wrap: on
line diff
--- a/liboctave/array/Array.h	Wed Sep 29 16:43:08 2021 -0400
+++ b/liboctave/array/Array.h	Fri Oct 01 15:25:51 2021 -0400
@@ -167,6 +167,10 @@
       std::fill_n (m_data, n, val);
     }
 
+    explicit ArrayRep (T *ptr, const dim_vector& dv)
+      : m_data (ptr), m_len (dv.safe_numel ()), m_count (1)
+    { }
+
     ArrayRep (const ArrayRep& a)
       : m_data (new T [a.m_len]), m_len (a.m_len), m_count (1)
     {
@@ -271,6 +275,20 @@
     m_dimensions.chop_trailing_singletons ();
   }
 
+  // Construct an Array from a pointer to an externally allocated array
+  // of values.  PTR must be allocated with operator new.  The Array
+  // object takes ownership of PTR and will delete it when the Array
+  // object is deleted.  The dimension vector DV must be consistent with
+  // the size of the allocated PTR array.
+
+  explicit Array (T *ptr, const dim_vector& dv)
+    : m_dimensions (dv),
+      m_rep (new typename Array<T>::ArrayRep (ptr, dv)),
+      m_slice_data (m_rep->m_data), m_slice_len (m_rep->m_len)
+  {
+    m_dimensions.chop_trailing_singletons ();
+  }
+
   //! Reshape constructor.
   OCTARRAY_API Array (const Array<T>& a, const dim_vector& dv);