Skip to content

keepat! and deleteat!: inconsistencies and possible bugs #42065

Closed
@bkamins

Description

@bkamins

This was tested under 1.7.0-beta4.

I start with what worries me with keepat!.

The first issue is:

julia> x = [1, 2, 3]
3-element Vector{Int64}:
 1
 2
 3

julia> keepat!(x, [true, false, false])
ERROR: ArgumentError: invalid index: true of type Bool

while the same as allowed for deleteat!:

julia> x = [1, 2, 3]
3-element Vector{Int64}:
 1
 2
 3

julia> deleteat!(x, [true, false, false])
2-element Vector{Int64}:
 2
 3

The second issue is that keepat! will corrupt a non-Vector or BitVector vector:

julia> x = [1, 2, 3]
3-element Vector{Int64}:
 1
 2
 3

julia> y = @view x[2:3]
2-element view(::Vector{Int64}, 2:3) with eltype Int64:
 2
 3

julia> keepat!(y, 2)
ERROR: MethodError: no method matching deleteat!(::SubArray{Int64, 1, Vector{Int64}, Tuple{UnitRange{Int64}}, true}, ::UnitRange{Int64})
Closest candidates are:
  deleteat!(::Vector, ::UnitRange{<:Integer}) at array.jl:1397
  deleteat!(::Vector, ::AbstractVector) at array.jl:1433
  deleteat!(::Vector, ::Any) at array.jl:1432
  ...
Stacktrace:
 [1] keepat!(a::SubArray{Int64, 1, Vector{Int64}, Tuple{UnitRange{Int64}}, true}, inds::Int64)
   @ Base .\abstractarray.jl:3079
 [2] top-level scope
   @ REPL[19]:1

julia> y
2-element view(::Vector{Int64}, 2:3) with eltype Int64:
 3
 3

julia> x
3-element Vector{Int64}:
 1
 3
 3

Maybe better define it only for Vector and BitVector and assume that if some custom vector implements deleteat! it must also implement keepat!?

Now to deleteat! issues.

The first is:

julia> x = [1, 2, 3]
3-element Vector{Int64}:
 1
 2
 3

julia> keepat!(x, true) # I think this is a correct behavior
ERROR: ArgumentError: invalid index: true of type Bool

julia> deleteat!(x, true) # and this is incorrect, unfortunately it is breaking
2-element Vector{Int64}:
 2
 3

The second issue is:

julia> x = falses(4)
4-element BitVector:
 0
 0
 0
 0

julia> y = collect(x)
4-element Vector{Bool}:
 0
 0
 0
 0

julia> deleteat!(x, [true, false, false, false]) # this is incorrect
ERROR: ArgumentError: indices must be unique and sorted

julia> deleteat!(y, [true, false, false, false]) # this is correct
3-element Vector{Bool}:
 0
 0
 0

I can implement the changes when there is a decision what behaviors in these four cases I have listed are expected. Thank you!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions