aldanor:

Is there an efficient method to remove elements with multiple (compile-time-unknown) indices from an array? I currently do something like

                if (!index.empty)
                        foreach (i; index.sort.reverse)
                                a = a.remove(i);

... which looks a bit awkward.

Do not forget to add the () after the sort, otherwise you call the deprecated, buggy and slow built-in sort.

reverse is another deprecated built-in, so use "retro".

The first "if" is not much useful, trying to sort an empty array will not wast much time.

foreach (i; index.sort().retro)
    a = a.remove(i);

In alternative you can also invert the sorting cmp (untested):

foreach (i; index.sort!q{a > b})
    a = a.remove(i);

If you have to remove just few items in-place, that's the way to do it. In future I hope we'll have a better remove function in Phobos (but it's not hard to write).

Another solution is to filter the array a. If you have the indexes, you can zip a with the indexes, and then filter, map to extract just the items and use copy() to work in place, but it's not nice-looking code.

Bye,
bearophile

Reply via email to