[C++-sig] Re: Optimize vector_indexing_suite set_slice

Ralf W. Grosse-Kunstleve rwgk at yahoo.com
Tue Aug 10 19:46:24 CEST 2004


--- "Neal D. Becker" <ndbecker2 at verizon.net> wrote:
> 3. This limitation (lack of optimization to operate directly on slices) may
> not be very important in many cases, you could just do:
>   Suppose we have a a vector v, and want to modify a slice of v using alg F:
> 
>   w = F (v[2:4]) # compute result from slice
>   v[2:4] = w #splice in result
> 
> 4. But it's even worse than it looks, because the "splice in result" will
> actually call erase and insert, instead of just copy.

FWIW:

I've solved this problem in a different way. I am using two types of
"selections" which may be understood as generalization of slices:

  - type 1: selection array of bools, same size as data array
  - type 2: selection array of std::size_t

Generalized slicing works like this:

  v.select(selection_array)

And now the other way (without using erase and insert):

  v.set_selected(selection_array, data_array_of_suitable_size)

I find that I don't use regular slicing very much, but the generalized slicing
is used all over the place in our apps.

I am carefully avoiding "slice references" because it is a Pandora's box and
can easily backfire performance-wise. If I had an exceptional case where
copying a slice is prohibitive I'd code up a specialized C++ function and wrap
it with Boost.Python.

Ralf



		
__________________________________
Do you Yahoo!?
Yahoo! Mail - 50x more storage than other providers!
http://promotions.yahoo.com/new_mail



More information about the Cplusplus-sig mailing list