This is interesting thanks. I didn't realise you could re-assign j inside
the index like that. A very neat notational trick. But I agree that looking
forward the filter! option is probably best. I'll adjust my code
accordingly, many thanks.

As an aside, every time I post code to this list I get useful suggestions
that make me a better Julia coder. I really appreciate it!

Cheers,

Colin

On 4 June 2016 at 07:32, Steven G. Johnson <[email protected]> wrote:

>
>
> On Thursday, June 2, 2016 at 11:42:32 PM UTC-4, [email protected] wrote:
>>
>> function Base.filter!{T}(x::AbstractVector{T}, r::BasicInterval{T})
>>     for n = length(x):-1:1
>>         !in(x[n], r) && deleteat!(x, n)
>>     end
>>     return(x)
>> end
>>
>
> I'm pretty sure this implementation has O(n^2) complexity, because
> deleteat! for an array has to actually move all of the elements to fill the
> hole.
>
> To get an O(n) algorithm, you could do something like:
>
> j = 0
> for i = 1:length(x)
>     if x[i] in r
>         x[j += 1] = x[i]
>     end
> end
> return resize!(x, j)
>
> Or you could just do filter!(x -> x in r, x), which is fast in Julia 0.5
>

Reply via email to