Skip to content
Closed
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
30 changes: 21 additions & 9 deletions base/array.jl
Original file line number Diff line number Diff line change
Expand Up @@ -466,20 +466,21 @@ function push!(a::Array{Any,1}, item::ANY)
return a
end

function append!{T}(a::Array{T,1}, items::AbstractVector)
if is(T,None)
error(_grow_none_errmsg)
end
function append!{T}(a::Array{T,1}, items::AbstractVector{T})
n = length(items)
ccall(:jl_array_grow_end, Void, (Any, Uint), a, n)
copy!(a, length(a)-n+1, items, 1, n)
return a
end

function prepend!{T}(a::Array{T,1}, items::AbstractVector)
function append!{T}(a::Array{T,1}, items::AbstractVector)
if is(T,None)
error(_grow_none_errmsg)
end
append!(a, convert(Array{T,1}, items))
end

function prepend!{T}(a::Array{T,1}, items::AbstractVector{T})
n = length(items)
ccall(:jl_array_grow_beg, Void, (Any, Uint), a, n)
if a === items
Expand All @@ -490,6 +491,13 @@ function prepend!{T}(a::Array{T,1}, items::AbstractVector)
return a
end

function prepend!{T}(a::Array{T,1}, items::AbstractVector)
if is(T,None)
error(_grow_none_errmsg)
end
prepend!(a, convert(Array{T,1}, items))
end

function resize!(a::Vector, nl::Integer)
l = length(a)
if nl > l
Expand Down Expand Up @@ -589,9 +597,7 @@ function deleteat!(a::Vector, inds)
return a
end

const _default_splice = []

function splice!(a::Vector, i::Integer, ins::AbstractArray=_default_splice)
function splice!{T}(a::Array{T,1}, i::Integer, ins::AbstractVector{T}=T[])
v = a[i]
m = length(ins)
if m == 0
Expand All @@ -607,7 +613,10 @@ function splice!(a::Vector, i::Integer, ins::AbstractArray=_default_splice)
return v
end

function splice!{T<:Integer}(a::Vector, r::UnitRange{T}, ins::AbstractArray=_default_splice)
splice!{T}(a::Array{T,1}, i::Integer, ins::AbstractVector) =
splice!(a, i, convert(Array{T,1}, ins))

function splice!{T,N<:Integer}(a::Array{T,1}, r::UnitRange{N}, ins::AbstractVector{T}=T[])
v = a[r]
m = length(ins)
if m == 0
Expand Down Expand Up @@ -642,6 +651,9 @@ function splice!{T<:Integer}(a::Vector, r::UnitRange{T}, ins::AbstractArray=_def
return v
end

splice!{T,N<:Integer}(a::Array{T,1}, r::UnitRange{N}, ins::AbstractVector) =
splice!(a, r, convert(Array{T,1}, ins))

function empty!(a::Vector)
ccall(:jl_array_del_end, Void, (Any, Uint), a, length(a))
return a
Expand Down
27 changes: 27 additions & 0 deletions test/arrayops.jl
Original file line number Diff line number Diff line change
Expand Up @@ -893,3 +893,30 @@ function i7197()
ind2sub(size(S), 5)
end
@test i7197() == (2,2)

# issue #7642
A = [1,2,3]
B = ["hey"]
@test_throws MethodError append!(A,B)
@test length(A) == 3

@test_throws MethodError prepend!(A,B)
@test length(A) == 3

@test_throws MethodError splice!(A,1,B)
@test length(A) == 3
@test_throws MethodError splice!(A,2,B)
@test length(A) == 3
@test_throws MethodError splice!(A,3,B)
@test length(A) == 3

@test_throws MethodError splice!(A,1:3,B)
@test length(A) == 3

B = [1.0, 1.2, 1.5]
@test_throws InexactError append!(A,B)
@test length(A) == 3
@test_throws InexactError prepend!(A,B)
@test length(A) == 3
@test_throws InexactError splice!(A,1:3,B)
@test length(A) == 3