Mercurial > hg > octave-nkf
changeset 11507:c3ad80f4ce36
Array.h, Array.cc: more constructor fixes
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Thu, 13 Jan 2011 06:01:08 -0500 |
parents | 964b7fd379f1 |
children | 41866901ec62 |
files | liboctave/Array.cc liboctave/Array.h liboctave/ChangeLog |
diffstat | 3 files changed, 74 insertions(+), 69 deletions(-) [+] |
line wrap: on
line diff
--- a/liboctave/Array.cc +++ b/liboctave/Array.cc @@ -48,7 +48,7 @@ template <class T> Array<T>::Array (const Array<T>& a, const dim_vector& dv) - : rep (a.rep), dimensions (dv), + : dimensions (dv), rep (a.rep), slice_data (a.slice_data), slice_len (a.slice_len) { if (dimensions.safe_numel () != a.numel ()) @@ -69,7 +69,7 @@ template <class T> Array<T>::Array (const Array<T>& a, octave_idx_type nr, octave_idx_type nc) - : rep (a.rep), dimensions (nr, nc), + : dimensions (nr, nc), rep (a.rep), slice_data (a.slice_data), slice_len (a.slice_len) { if (dimensions.safe_numel () != a.numel ()) @@ -317,20 +317,23 @@ // Helper class for multi-d dimension permuting (generalized transpose). class rec_permute_helper { - octave_idx_type *dim, *stride; + // STRIDE occupies the last half of the space allocated for dim to + // avoid a double allocation. + + int n; + int top; + octave_idx_type *dim; + octave_idx_type *stride; bool use_blk; - int top; public: rec_permute_helper (const dim_vector& dv, const Array<octave_idx_type>& perm) + + : n (dv.length ()), top (0), dim (new octave_idx_type [2*n]), + stride (dim + n), use_blk (false) { - int n = dv.length (); assert (n == perm.length ()); - dim = new octave_idx_type [2*n]; - // A hack to avoid double allocation - stride = dim + n; - // Get cumulative dimensions. OCTAVE_LOCAL_BUFFER (octave_idx_type, cdim, n+1); cdim[0] = 1; @@ -345,7 +348,6 @@ } // Reduce contiguous runs. - top = 0; for (int k = 1; k < n; k++) { if (stride[k] == stride[top]*dim[top]) @@ -522,28 +524,30 @@ return retval; } -// Helper class for multi-d index reduction and recursive indexing/indexed assignment. -// Rationale: we could avoid recursion using a state machine instead. However, using -// recursion is much more amenable to possible parallelization in the future. +// Helper class for multi-d index reduction and recursive +// indexing/indexed assignment. Rationale: we could avoid recursion +// using a state machine instead. However, using recursion is much +// more amenable to possible parallelization in the future. // Also, the recursion solution is cleaner and more understandable. + class rec_index_helper { - octave_idx_type *dim, *cdim; + // CDIM occupies the last half of the space allocated for dim to + // avoid a double allocation. + + int n; + int top; + octave_idx_type *dim; + octave_idx_type *cdim; idx_vector *idx; - int top; public: rec_index_helper (const dim_vector& dv, const Array<idx_vector>& ia) + : n (ia.length ()), top (0), dim (new octave_idx_type [2*n]), + cdim (dim + n), idx (new idx_vector [n]) { - int n = ia.length (); assert (n > 0 && (dv.length () == std::max (n, 2))); - dim = new octave_idx_type [2*n]; - // A hack to avoid double allocation - cdim = dim + n; - idx = new idx_vector [n]; - top = 0; - dim[0] = dv(0); cdim[0] = 1; idx[0] = ia(0); @@ -579,8 +583,8 @@ dest += idx[0].index (src, dim[0], dest); else { - octave_idx_type n = idx[lev].length (dim[lev]), d = cdim[lev]; - for (octave_idx_type i = 0; i < n; i++) + octave_idx_type nn = idx[lev].length (dim[lev]), d = cdim[lev]; + for (octave_idx_type i = 0; i < nn; i++) dest = do_index (src + d*idx[lev].xelem (i), dest, lev-1); } @@ -595,8 +599,8 @@ src += idx[0].assign (src, dim[0], dest); else { - octave_idx_type n = idx[lev].length (dim[lev]), d = cdim[lev]; - for (octave_idx_type i = 0; i < n; i++) + octave_idx_type nn = idx[lev].length (dim[lev]), d = cdim[lev]; + for (octave_idx_type i = 0; i < nn; i++) src = do_assign (src, dest + d*idx[lev].xelem (i), lev-1); } @@ -611,8 +615,8 @@ idx[0].fill (val, dim[0], dest); else { - octave_idx_type n = idx[lev].length (dim[lev]), d = cdim[lev]; - for (octave_idx_type i = 0; i < n; i++) + octave_idx_type nn = idx[lev].length (dim[lev]), d = cdim[lev]; + for (octave_idx_type i = 0; i < nn; i++) do_fill (val, dest + d*idx[lev].xelem (i), lev-1); } } @@ -640,11 +644,14 @@ // once (apart from reinitialization) class rec_resize_helper { - octave_idx_type *cext, *sext, *dext; + octave_idx_type *cext; + octave_idx_type *sext; + octave_idx_type *dext; int n; public: rec_resize_helper (const dim_vector& ndv, const dim_vector& odv) + : cext (0), sext (0), dext (0), n (0) { int l = ndv.length (); assert (odv.length () == l);
--- a/liboctave/Array.h +++ b/liboctave/Array.h @@ -125,9 +125,9 @@ protected: - typename Array<T>::ArrayRep *rep; + dim_vector dimensions; - dim_vector dimensions; + typename Array<T>::ArrayRep *rep; // Rationale: // slice_data is a pointer to rep->data, denoting together with slice_len the @@ -142,11 +142,9 @@ // slice constructor Array (const Array<T>& a, const dim_vector& dv, octave_idx_type l, octave_idx_type u) - : rep(a.rep), dimensions (dv) + : dimensions (dv), rep(a.rep), slice_data (a.slice_data+l), slice_len (u-l) { rep->count++; - slice_data = a.slice_data + l; - slice_len = u - l; dimensions.chop_trailing_singletons (); } @@ -165,57 +163,49 @@ // Empty ctor (0x0). Array (void) - : rep (nil_rep ()), dimensions () + : dimensions (), rep (nil_rep ()), slice_data (rep->data), + slice_len (rep->len) { rep->count++; - slice_data = rep->data; - slice_len = rep->len; } // Obsolete 1D ctor (there are no 1D arrays). explicit Array (octave_idx_type n) GCC_ATTR_DEPRECATED - : rep (new typename Array<T>::ArrayRep (n)), dimensions (n, 1) - { - slice_data = rep->data; - slice_len = rep->len; - } + : dimensions (n, 1), rep (new typename Array<T>::ArrayRep (n)), + slice_data (rep->data), slice_len (rep->len) + { } // 2D uninitialized ctor. explicit Array (octave_idx_type m, octave_idx_type n) - : rep (), dimensions (m, n) - { - rep = new typename Array<T>::ArrayRep (dimensions.safe_numel ()); - slice_data = rep->data; - slice_len = rep->len; - } + : dimensions (m, n), + rep (new typename Array<T>::ArrayRep (dimensions.safe_numel ())), + slice_data (rep->data), slice_len (rep->len) + { } // 2D initialized ctor. explicit Array (octave_idx_type m, octave_idx_type n, const T& val) - : rep (), dimensions (m, n) + : dimensions (m, n), + rep (new typename Array<T>::ArrayRep (dimensions.safe_numel ())), + slice_data (rep->data), slice_len (rep->len) { - rep = new typename Array<T>::ArrayRep (dimensions.safe_numel ()); - slice_data = rep->data; - slice_len = rep->len; fill (val); } // nD uninitialized ctor. explicit Array (const dim_vector& dv) - : rep (new typename Array<T>::ArrayRep (dv.safe_numel ())), - dimensions (dv) + : dimensions (dv), + rep (new typename Array<T>::ArrayRep (dv.safe_numel ())), + slice_data (rep->data), slice_len (rep->len) { - slice_data = rep->data; - slice_len = rep->len; dimensions.chop_trailing_singletons (); } // nD initialized ctor. explicit Array (const dim_vector& dv, const T& val) - : rep (new typename Array<T>::ArrayRep (dv.safe_numel ())), - dimensions (dv) + : dimensions (dv), + rep (new typename Array<T>::ArrayRep (dv.safe_numel ())), + slice_data (rep->data), slice_len (rep->len) { - slice_data = rep->data; - slice_len = rep->len; fill (val); dimensions.chop_trailing_singletons (); } @@ -228,20 +218,17 @@ // Type conversion case. template <class U> Array (const Array<U>& a) - : rep (new typename Array<T>::ArrayRep (a.data (), a.length ())), - dimensions (a.dims ()) - { - slice_data = rep->data; - slice_len = rep->len; - } + : dimensions (a.dims ()), + rep (new typename Array<T>::ArrayRep (a.data (), a.length ())), + slice_data (rep->data), slice_len (rep->len) + { } // No type conversion case. Array (const Array<T>& a) - : rep (a.rep), dimensions (a.dimensions) + : dimensions (a.dimensions), rep (a.rep), slice_data (a.slice_data), + slice_len (a.slice_len) { rep->count++; - slice_data = a.slice_data; - slice_len = a.slice_len; } public:
--- a/liboctave/ChangeLog +++ b/liboctave/ChangeLog @@ -1,3 +1,14 @@ +2011-01-13 John W. Eaton <jwe@octave.org> + + * Array.cc (rec_permute_helper::rec_permute_helper, + rec_index_helper::rec_index_helper, + rec_resize_helper::rec_resize_helper): Initialize all data + members in initialization lists. + + * Array.h, Array.cc (Array::Array): Initialize slice_data and + slice_len in memeber initialization lists. + (Array::~Array): Now virtual. + 2011-01-13 John W. Eaton <jwe@octave.org> * oct-locbuf.h (octave_chunk_buffer::~octave_chunk_buffer):