Page 1 of 1

Evil Algorithm, applying transformation vector in-place?

Posted: Fri 02 Jan, 2009 11:54 pm
by King Harold
This is quite annoying, and google isn't cooperating.

Suppose we have an array of data, called "data", and an array of incides called "indices" (very original..)
All indices [0 .. n-1] are present, and only once.

The array "result" is such that for every n, result[n] equals data[indices[n]].

Can the result be generated without copying the items of "data" into a new array? If so, how?
In other words, can "data" be modified in-place to have the property of "result"? (of course in that case the "data" used at the right hand side is taken from the old array, but that's just a technicality)

I really feel like it ought to be possible..

Re: Evil Algorithm, applying transformation vector in-place?

Posted: Sat 03 Jan, 2009 2:02 am
by benryves
I can't think of any way that wouldn't require some additional storage where the amount of storage relates to the number of elements, sorry. :(

Re: Evil Algorithm, applying transformation vector in-place?

Posted: Sat 03 Jan, 2009 4:45 am
by calc84maniac
What language? Methinks you failed to specify...

Re: Evil Algorithm, applying transformation vector in-place?

Posted: Sat 03 Jan, 2009 2:39 pm
by King Harold
Doesn't matter, all I need it an algorithm
But, please don't use that as an excuse to use something exotic ;)

edit: ok, Universe, you have won. Until I find a better way, I will just copy all data to a secondary array. :(

Re: Evil Algorithm, applying transformation vector in-place?

Posted: Sat 31 Jan, 2009 8:34 pm
by CoBB
King Harold wrote:Doesn't matter, all I need it an algorithm
But, please don't use that as an excuse to use something exotic ;)

edit: ok, Universe, you have won. Until I find a better way, I will just copy all data to a secondary array. :(
http://www.jjj.de/fxt/

Have a look at the permutation functions of the lib, you might find something. Or just google for in-place permutation algorithms...

Re: Evil Algorithm, applying transformation vector in-place?

Posted: Tue 03 Feb, 2009 3:21 pm
by King Harold
Cool! thanks :)