# HG changeset patch # User Jaroslav Hajek # Date 1238826656 -7200 # Node ID 5d6d0bc9aeb4fdbfa6d360e060383135bdfa8ecd # Parent b7210faa3ed0d9b52d730309c88c46668d1cb836 optimize simple stack operations on arrays diff --git a/liboctave/Array.cc b/liboctave/Array.cc --- a/liboctave/Array.cc +++ b/liboctave/Array.cc @@ -53,14 +53,6 @@ { --rep->count; rep = new ArrayRep (slice_data, slice_len, true); - slice_data = rep->data; - } - else if (slice_len != rep->len) - { - // Possibly economize here. - ArrayRep *new_rep = new ArrayRep (slice_data, slice_len, true); - delete rep; - rep = new_rep; slice_data = rep->data; } } @@ -1072,7 +1064,36 @@ else { octave_idx_type nx = numel (); - if (n != nx) + if (n == nx - 1 && n > 0) + { + // Stack "pop" operation. + if (rep->count == 1) + slice_data[slice_len-1] = T (); + slice_len--; + dimensions = dv; + } + else if (n == nx + 1 && nx > 0) + { + // Stack "push" operation. + if (rep->count == 1 && slice_data + slice_len < rep->data + rep->len) + { + slice_data[slice_len++] = rfv; + dimensions = dv; + } + else + { + static const octave_idx_type max_stack_chunk = 1024; + octave_idx_type nn = n + std::min (nx, max_stack_chunk); + Array tmp (Array (nn), dv, 0, n); + T *dest = tmp.fortran_vec (); + + std::copy (data (), data () + nx, dest); + dest[nx] = rfv; + + *this = tmp; + } + } + else if (n != nx) { Array tmp = Array (dv); T *dest = tmp.fortran_vec (); @@ -1489,9 +1510,13 @@ else if (i.length (n) != 0) { octave_idx_type l, u; - bool col_vec = ndims () == 2 && columns () == 1 && rows () != 1; - if (i.is_cont_range (n, l, u)) + if (i.is_scalar () && i(0) == n-1) + { + // Stack "pop" operation. + resize (n-1); + } + else if (i.is_cont_range (n, l, u)) { // Special case deleting a contiguous range. octave_idx_type m = n + l - u; diff --git a/liboctave/ChangeLog b/liboctave/ChangeLog --- a/liboctave/ChangeLog +++ b/liboctave/ChangeLog @@ -0,0 +1,8 @@ +2009-04-04 Jaroslav Hajek + + * Array.cc (Array::make_unique): Don't economize when unique. + (Array::resize_fill (octave_idx_type, const T&)): Optimize push & + pop operations. + (Array::delete_elements (const idx_vector&)): Do pop operation + using resize. +