From fb1acd1fcbadc83eb754e1f1d279edcb13d27cfc Mon Sep 17 00:00:00 2001 From: Nathan Daly Date: Thu, 25 Jun 2020 11:19:20 -0400 Subject: [PATCH 1/3] Add Base.copy,Base.copymutable for Sorted containers --- src/sorted_dict.jl | 5 +++++ src/sorted_multi_dict.jl | 5 +++++ src/sorted_set.jl | 5 +++++ test/test_sorted_containers.jl | 6 ++++++ 4 files changed, 21 insertions(+) diff --git a/src/sorted_dict.jl b/src/sorted_dict.jl index 836c18b0d..278cc13a8 100644 --- a/src/sorted_dict.jl +++ b/src/sorted_dict.jl @@ -535,6 +535,11 @@ function mergetwo!(m::SortedDict{K,D,Ord}, end end +# Standard copy functions use packcopy - that is, they retain elements but not +# the identical structure. +Base.copymutable(m::SortedDict) = packcopy(m) +Base.copy(m::SortedDict) = packcopy(m) + """ packcopy(sc) diff --git a/src/sorted_multi_dict.jl b/src/sorted_multi_dict.jl index a55b8f7a6..02e5937b2 100644 --- a/src/sorted_multi_dict.jl +++ b/src/sorted_multi_dict.jl @@ -385,6 +385,11 @@ function mergetwo!(m::SortedMultiDict{K,D,Ord}, end end +# Standard copy functions use packcopy - that is, they retain elements but not +# the identical structure. +Base.copymutable(m::SortedMultiDict) = packcopy(m) +Base.copy(m::SortedMultiDict) = packcopy(m) + """ packcopy(sc) diff --git a/src/sorted_set.jl b/src/sorted_set.jl index 7d9d31c7f..4bcf265a5 100644 --- a/src/sorted_set.jl +++ b/src/sorted_set.jl @@ -519,6 +519,11 @@ function issubset(iterable, m2::SortedSet) return true end +# Standard copy functions use packcopy - that is, they retain elements but not +# the identical structure. +Base.copymutable(m::SortedSet) = packcopy(m) +Base.copy(m::SortedSet) = packcopy(m) + """ packcopy(sc) diff --git a/test/test_sorted_containers.jl b/test/test_sorted_containers.jl index 67b08cd00..2b2a679dc 100644 --- a/test/test_sorted_containers.jl +++ b/test/test_sorted_containers.jl @@ -1717,4 +1717,10 @@ end @test pop!(s,50, nothing) == nothing @test isempty(s) + # Test AbstractSet/AbstractDict interface + for m in [SortedSet([1,2]), SortedDict(1=>2, 2=>3), SortedMultiDict(1=>2, 1=>3)] + # copy() + @test isequal(copy(m), m) + @test isequal(Base.copymutable(m), m) + end end From 0f5bd4a0950e3f249b4032c7d3778ecd14451f5d Mon Sep 17 00:00:00 2001 From: Nathan Daly Date: Thu, 25 Jun 2020 11:42:37 -0400 Subject: [PATCH 2/3] Bump patch version number when adding Base.copy() overloads --- Project.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Project.toml b/Project.toml index e64c3da00..60608952c 100644 --- a/Project.toml +++ b/Project.toml @@ -1,6 +1,6 @@ name = "DataStructures" uuid = "864edb3b-99cc-5e75-8d2d-829cb0a9cfe8" -version = "0.17.18" +version = "0.17.19" [deps] InteractiveUtils = "b77e0a4c-d291-57a0-90e8-8db25a27a240" From d9d3901aee2e21f62a530314590adb241ec73050 Mon Sep 17 00:00:00 2001 From: Nathan Daly Date: Thu, 25 Jun 2020 11:44:36 -0400 Subject: [PATCH 3/3] Add test that copy/copymutable preserve type --- test/test_sorted_containers.jl | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/test/test_sorted_containers.jl b/test/test_sorted_containers.jl index 2b2a679dc..846d1f915 100644 --- a/test/test_sorted_containers.jl +++ b/test/test_sorted_containers.jl @@ -1720,7 +1720,13 @@ end # Test AbstractSet/AbstractDict interface for m in [SortedSet([1,2]), SortedDict(1=>2, 2=>3), SortedMultiDict(1=>2, 1=>3)] # copy() - @test isequal(copy(m), m) - @test isequal(Base.copymutable(m), m) + let m1 = copy(m) + @test isequal(m1, m) + @test typeof(m1) === typeof(m) + end + let m1 = Base.copymutable(m) + @test isequal(m1, m) + @test typeof(m1) === typeof(m) + end end end