Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add keytype+valtype method for AbstractArray and AbstractVector #27749

Merged
merged 13 commits into from
Apr 4, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ Standard library changes
* A no-argument construct to `Ptr{T}` has been added which constructs a null pointer ([#30919])
* `strip` now accepts a function argument in the same manner as `lstrip` and `rstrip` ([#31211])
* `mktempdir` now accepts a `prefix` keyword argument to customize the file name ([#31230], [#22922])
* `keytype` and `valtype` now work on `AbstractArray`, and return the `eltype` of `keys(...)` and
`values(...)` respectively ([#27749]).
* `nextfloat(::BigFloat)` and `prevfloat(::BigFloat)` now returns a value with the same precision
as their argument, which means that (in particular) `nextfloat(prevfloat(x)) == x` whereas
previously this could result in a completely different value with a different precision ([#31310])
Expand Down
45 changes: 45 additions & 0 deletions base/abstractarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,51 @@ unsafe_indices(r::AbstractRange) = (OneTo(unsafe_length(r)),) # Ranges use check
keys(a::AbstractArray) = CartesianIndices(axes(a))
keys(a::AbstractVector) = LinearIndices(a)

"""
keytype(T::Type{<:AbstractArray})
keytype(A::AbstractArray)

Return the key type of an array. This is equal to the
`eltype` of the result of `keys(...)`, and is provided
mainly for compatibility with the dictionary interface.

# Examples
```jldoctest
julia> keytype([1, 2, 3]) == Int
true

julia> keytype([1 2; 3 4])
CartesianIndex{2}
```

!!! compat "Julia 1.2"
For arrays, this function requires at least Julia 1.2.
"""
keytype(a::AbstractArray) = keytype(typeof(a))

keytype(A::Type{<:AbstractArray}) = CartesianIndex{ndims(A)}
keytype(A::Type{<:AbstractVector}) = Int

valtype(a::AbstractArray) = valtype(typeof(a))

"""
valtype(T::Type{<:AbstractArray})
valtype(A::AbstractArray)

Return the value type of an array. This is identical to `eltype` and is
provided mainly for compatibility with the dictionary interface.

# Examples
```jldoctest
julia> valtype(["one", "two", "three"])
String
```

!!! compat "Julia 1.2"
For arrays, this function requires at least Julia 1.2.
"""
valtype(A::Type{<:AbstractArray}) = eltype(A)

prevind(::AbstractArray, i::Integer) = Int(i)-1
nextind(::AbstractArray, i::Integer) = Int(i)+1

Expand Down
3 changes: 3 additions & 0 deletions test/abstractarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -864,6 +864,9 @@ for A in (rand(2), rand(2,3))
@test A[i] == v
end
@test Array(values(A)) == A

@test keytype(A) == eltype(keys(A))
@test valtype(A) == eltype(values(A))
end

# nextind and prevind
Expand Down
31 changes: 31 additions & 0 deletions test/ranges.jl
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,37 @@ end
@test length(Char(0):Char(0x001fffff)) == 2097152
@test length(typemax(UInt64)//one(UInt64):1:typemax(UInt64)//one(UInt64)) == 1
end
@testset "keys/values" begin
keytype_is_correct(r) = keytype(r) == eltype(keys(r))
valtype_is_correct(r) = valtype(r) == eltype(values(r))
@test keytype_is_correct(1:3)
@test keytype_is_correct(1:.3:4)
@test keytype_is_correct(.1:.1:.3)
@test keytype_is_correct(Int8(1):Int8(5))
@test keytype_is_correct(Int16(1):Int8(5))
@test keytype_is_correct(Int16(1):Int8(3):Int8(5))
@test keytype_is_correct(Int8(1):Int16(3):Int8(5))
@test keytype_is_correct(Int8(1):Int8(3):Int16(5))
@test keytype_is_correct(Int64(1):Int64(5))
@test keytype_is_correct(Int64(1):Int64(5))
@test keytype_is_correct(Int128(1):Int128(5))
@test keytype_is_correct(Base.OneTo(4))
@test keytype_is_correct(Base.OneTo(Int32(4)))

@test valtype_is_correct(1:3)
@test valtype_is_correct(1:.3:4)
@test valtype_is_correct(.1:.1:.3)
@test valtype_is_correct(Int8(1):Int8(5))
@test valtype_is_correct(Int16(1):Int8(5))
@test valtype_is_correct(Int16(1):Int8(3):Int8(5))
@test valtype_is_correct(Int8(1):Int16(3):Int8(5))
@test valtype_is_correct(Int8(1):Int8(3):Int16(5))
@test valtype_is_correct(Int64(1):Int64(5))
@test valtype_is_correct(Int64(1):Int64(5))
@test valtype_is_correct(Int128(1):Int128(5))
@test valtype_is_correct(Base.OneTo(4))
@test valtype_is_correct(Base.OneTo(Int32(4)))
end
@testset "findall(::Base.Fix2{typeof(in)}, ::Array)" begin
@test findall(in(3:20), [5.2, 3.3]) == findall(in(Vector(3:20)), [5.2, 3.3])

Expand Down