Skip to content

Commit

Permalink
Merge pull request #369 from timholy/teh/deepcopy_typestability
Browse files Browse the repository at this point in the history
Circumvent type-instability of deepcopy. Fixes #356
  • Loading branch information
timholy committed Oct 15, 2015
2 parents 8bdbeed + 3f5c0d3 commit 82df4e6
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
12 changes: 9 additions & 3 deletions src/core.jl
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ type ImageCmap{T<:Colorant,N,A<:AbstractArray} <: AbstractImageIndexed{T,N}
cmap::Vector{T}
properties::Dict{ASCIIString,Any}
end
ImageCmap(data::AbstractArray, cmap::AbstractVector, props::Dict) = ImageCmap{eltype(cmap),ndims(data),typeof(data)}(data, cmap, props)
ImageCmap{_,N}(data::AbstractArray{_,N}, cmap::AbstractVector, props::Dict) = ImageCmap{eltype(cmap),N,typeof(data)}(data, cmap, props)
ImageCmap(data::AbstractArray, cmap::AbstractVector; kwargs...) = ImageCmap(data, cmap, kwargs2dict(kwargs))

# Convenience constructors
Expand Down Expand Up @@ -76,8 +76,14 @@ ndims(img::AbstractImage) = ndims(img.data)

strides(img::AbstractImage) = strides(img.data)

copy(img::Image) = Image(copy(img.data), deepcopy(img.properties))
copy(img::ImageCmap) = ImageCmap(copy(img.data), copy(img.cmap), deepcopy(img.properties))
copy(img::Image) = Image(copy(img.data), dictcopy(img.properties))
copy(img::ImageCmap) = ImageCmap(copy(img.data), copy(img.cmap), dictcopy(img.properties))

function dictcopy(dct)
newkeys = [copy(key) for key in keys(dct)]
newvals = [copy(val) for val in values(dct)]
Dict{ASCIIString,Any}(zip(newkeys,newvals))
end

# Create a new "Image" (could be just an Array) copying the properties but replacing the data
copyproperties(img::AbstractArray, data::AbstractArray) = data
Expand Down
5 changes: 4 additions & 1 deletion test/core.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ if testing_units
end

facts("Core") do
a = rand(3,3)
@inferred(Image(a))

# support integer-valued types, but these are NOT recommended (use Ufixed)
B = rand(convert(UInt16, 1):convert(UInt16, 20), 3, 5)
# img, imgd, and imgds will be used in many more tests
Expand Down Expand Up @@ -127,7 +130,7 @@ facts("Core") do

context("Copy / similar") do
A = randn(3,5,3)
imgc = copy(img)
imgc = @inferred(copy(img))
@fact imgc.data --> img.data
imgc = copyproperties(imgd, A)
@fact imgc.data --> A
Expand Down

0 comments on commit 82df4e6

Please sign in to comment.