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

Copy both indices and values #101

Closed
wants to merge 2 commits into from
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
6 changes: 2 additions & 4 deletions src/AbstractDictionary.jl
Original file line number Diff line number Diff line change
Expand Up @@ -475,18 +475,16 @@ function Random.randn(rng::AbstractRNG, ::Type{T}, dict::AbstractDictionary) whe
end


# Copying - note that this doesn't necessarily copy the indices! (`copy(keys(dict))` can do that)
"""
copy(dict::AbstractDictionary)
copy(dict::AbstractDictionary, ::Type{T})

Create a shallow copy of the values of `dict`. Note that `keys(dict)` is not copied, and
therefore care must be taken that inserting/deleting elements. A new element type `T` can
Create a shallow copy of the values of `dict`. A new element type `T` can
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we should say "values and indices of dict"?

optionally be specified.
"""
Base.copy(dict::AbstractDictionary) = copy(dict, eltype(dict))
function Base.copy(d::AbstractDictionary, ::Type{T}) where {T}
out = similar(d, T)
out = similar(copy(keys(d)), T)
copyto!(out, d)
return out
end
Expand Down
2 changes: 1 addition & 1 deletion src/ArrayDictionary.jl
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ function Base.similar(inds::ArrayIndices, ::Type{T}) where {T}
return ArrayDictionary(inds, similar(parent(inds), T))
end

Base.copy(dict::ArrayDictionary) = ArrayDictionary(dict.indices, copy(dict.values))
Base.copy(dict::ArrayDictionary) = ArrayDictionary(copy(dict.indices), copy(dict.values))

# insertable interface
isinsertable(::ArrayDictionary) = true # Need an array trait for this...
Expand Down
2 changes: 1 addition & 1 deletion src/Dictionary.jl
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ function Base.convert(::Type{Dictionary{I, T}}, dict::Dictionary) where {I, T}
end
Base.convert(::Type{T}, dict::T) where {T<:Dictionary} = dict

Base.copy(dict::Dictionary) = Dictionary(dict.indices, copy(dict.values))
Base.copy(dict::Dictionary) = Dictionary(copy(dict.indices), copy(dict.values))

"""
dictionary(iter)
Expand Down
8 changes: 7 additions & 1 deletion test/ArrayDictionary.jl
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@

dictcopy = copy(dict)
@test dict isa ArrayDictionary{Int64, String}
@test sharetokens(dict, dictcopy)
@test !sharetokens(dict, dictcopy)
if VERSION < v"1.6-"
io = IOBuffer(); show(io, MIME"text/plain"(), dict); @test String(take!(io)) == "2-element ArrayDictionary{Int64,String,ArrayIndices{Int64,Array{Int64,1}},Array{String,1}}\n 1 │ #undef\n 2 │ #undef"
else
Expand All @@ -94,6 +94,12 @@
@test all(!isassigned(dictcopy, i) for i in collect(keys(dictcopy)))
@test sharetokens(dict, ArrayDictionary{Int64, String}(dict))
@test pairs(dict) isa Dictionaries.PairDictionary{Int64, String}

dict = ArrayDictionary([1,2,3,4,5], [1,3,2,4,5])
dictcopy = copy(dict)
set!(dict, 6, 7)
@test length(dict) == 6
@test length(dictcopy) == 5
end

@testset "sort" begin
Expand Down
10 changes: 9 additions & 1 deletion test/Dictionary.jl
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@

dictcopy = copy(dict)
@test dict isa Dictionary{Int64, String}
@test sharetokens(dict, dictcopy)
@test !sharetokens(dict, dictcopy)
if VERSION < v"1.6-"
io = IOBuffer(); show(io, MIME"text/plain"(), dict); @test String(take!(io)) == "2-element Dictionary{Int64,String}\n 1 │ #undef\n 2 │ #undef"
else
Expand Down Expand Up @@ -261,6 +261,14 @@
end
end

@testset "copy" begin
dict = Dictionary([1,2,3,4,5], [1,3,2,4,5])
dictcopy = copy(dict)
set!(dict, 6, 7)
@test length(dict) == 6
@test length(dictcopy) == 5
end

@testset "dictionary" begin
res = Dictionary(['a','b','c'], [1,2,3])
@test isequal(dictionary(pairs(res)), res)
Expand Down