C
ctcgag
Good day, all.
I have a object which has sub-objects. The object only provides
one way to re-order sub-objects: a somewhat expensive swap operation.
I have code that does an indirect sort of the sub-objects:
my @x;
foreach my $i (1..$o->nofSub()) {
push @x, compute_value($o,$i);
};
my @idx = "dummy", sort {$x[$a]<=>$x[$b]} 0..$#x;
Now, I merely have to use @idx as a translation table so that I can access
sub-objects as if they were sorted. Works great for the most part, but
I've reached a point where that isn't good enough and I need to materialize
the sort into the object itself. So I'm trying to use the transformation
implied in @idx to apply a proper series of $o->swap($foo,$bar) operations
to put the sub-objects in order.
All the easy solutions I've come up with are about equivalent to selection
sort (O(N) swaps, O(N**2) comparisons). In this case, that will work
(because the nofSub never gets more than a few hundred), but its inelegance
and poor scaling offend me.
One solution is to add hooks into the object code to allow more efficient
re-organization, but I know that there must be a better, elegant way to do
it with just @idx and swap(). Any clues?
Thanks,
Xho
I have a object which has sub-objects. The object only provides
one way to re-order sub-objects: a somewhat expensive swap operation.
I have code that does an indirect sort of the sub-objects:
my @x;
foreach my $i (1..$o->nofSub()) {
push @x, compute_value($o,$i);
};
my @idx = "dummy", sort {$x[$a]<=>$x[$b]} 0..$#x;
Now, I merely have to use @idx as a translation table so that I can access
sub-objects as if they were sorted. Works great for the most part, but
I've reached a point where that isn't good enough and I need to materialize
the sort into the object itself. So I'm trying to use the transformation
implied in @idx to apply a proper series of $o->swap($foo,$bar) operations
to put the sub-objects in order.
All the easy solutions I've come up with are about equivalent to selection
sort (O(N) swaps, O(N**2) comparisons). In this case, that will work
(because the nofSub never gets more than a few hundred), but its inelegance
and poor scaling offend me.
One solution is to add hooks into the object code to allow more efficient
re-organization, but I know that there must be a better, elegant way to do
it with just @idx and swap(). Any clues?
Thanks,
Xho