Skip to content

Commit

Permalink
Add OneTo Array constructors
Browse files Browse the repository at this point in the history
  • Loading branch information
timholy committed Sep 30, 2017
1 parent 2be031f commit aa45b8e
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 0 deletions.
6 changes: 6 additions & 0 deletions base/array.jl
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,12 @@ const DenseVector{T} = DenseArray{T,1}
const DenseMatrix{T} = DenseArray{T,2}
const DenseVecOrMat{T} = Union{DenseVector{T}, DenseMatrix{T}}

## More constructors
Array{T,N}(inds::NTuple{N,<:OneTo}) where {T,N} = Array{T,N}(length.(inds))
Array{T}(inds::NTuple{N,<:OneTo}) where {T,N} = Array{T,N}(inds)
Array{T,N}(ind::OneTo, inds::Vararg{<:OneTo}) where {T,N} = Array{T,N}((ind, inds...))
Array{T}(ind::OneTo, inds::Vararg{<:OneTo}) where T = Array{T}((ind, inds...))

## Basic functions ##

"""
Expand Down
3 changes: 3 additions & 0 deletions base/boot.jl
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,8 @@ const NTuple{N,T} = Tuple{Vararg{T,N}}
# primitive array constructors
Array{T,N}(d::NTuple{N,Int}) where {T,N} =
ccall(:jl_new_array, Array{T,N}, (Any, Any), Array{T,N}, d)
Array{T,0}(::Tuple{}) where {T} =
ccall(:jl_new_array, Array{T,0}, (Any, Any), Array{T,0}, ())
Array{T,1}(d::NTuple{1,Int}) where {T} = Array{T,1}(getfield(d,1))
Array{T,2}(d::NTuple{2,Int}) where {T} = Array{T,2}(getfield(d,1), getfield(d,2))
Array{T,3}(d::NTuple{3,Int}) where {T} = Array{T,3}(getfield(d,1), getfield(d,2), getfield(d,3))
Expand All @@ -366,6 +368,7 @@ Array{T,3}(m::Int, n::Int, o::Int) where {T} =
ccall(:jl_alloc_array_3d, Array{T,3}, (Any, Int, Int, Int), Array{T,3}, m, n, o)

Array{T}(d::NTuple{N,Int}) where {T,N} = Array{T,N}(d)
Array{T}(::Tuple{}) where {T} = Array{T,0}(())
Array{T}(m::Int) where {T} = Array{T,1}(m)
Array{T}(m::Int, n::Int) where {T} = Array{T,2}(m, n)
Array{T}(m::Int, n::Int, o::Int) where {T} = Array{T,3}(m, n, o)
Expand Down
6 changes: 6 additions & 0 deletions test/arrayops.jl
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,12 @@ end
error("unexpected depwarn value")
end
@test_throws MethodError Array{Int,3}()

@test size(Array{Int}(Base.OneTo(3), Base.OneTo(2))) == (3,2)
@test size(Array{Int}((Base.OneTo(3), Base.OneTo(2)))) == (3,2)
@test size(Array{Int,2}(Base.OneTo(3), Base.OneTo(2))) == (3,2)
@test size(Array{Int,2}((Base.OneTo(3), Base.OneTo(2)))) == (3,2)
@test_throws MethodError Array{Int,3}((Base.OneTo(3), Base.OneTo(2)))
end
@testset "get" begin
A = reshape(1:24, 3, 8)
Expand Down

0 comments on commit aa45b8e

Please sign in to comment.