From c9ade2ac6eef32de4569bb8475e68e7874f206a4 Mon Sep 17 00:00:00 2001 From: Alex Arslan Date: Thu, 10 Aug 2017 16:21:24 -0700 Subject: [PATCH] Preserve the input element type in unique Previously the element type of the output was the smallest type that would fit the union of the input's individual element types. Now the output has an identical element type to the input. Fixes #22696. --- NEWS.md | 5 +++++ base/set.jl | 12 +++++++----- test/sets.jl | 4 +++- 3 files changed, 15 insertions(+), 6 deletions(-) diff --git a/NEWS.md b/NEWS.md index 4f001ae743fec..2f86431226db9 100644 --- a/NEWS.md +++ b/NEWS.md @@ -143,6 +143,10 @@ This section lists changes that do not have deprecation warnings. * Worker-worker connections are setup lazily for an `:all_to_all` topology. Use keyword arg `lazy=false` to force all connections to be setup during a `addprocs` call. ([#22814]) + * The element type of the input is now preserved in `unique`. Previously the element type + of the output was shrunk to fit the union of the type of each element in the input. + ([#22696]) + Library improvements -------------------- @@ -1150,6 +1154,7 @@ Command-line option changes [#22588]: https://github.com/JuliaLang/julia/issues/22588 [#22605]: https://github.com/JuliaLang/julia/issues/22605 [#22666]: https://github.com/JuliaLang/julia/issues/22666 +[#22696]: https://github.com/JuliaLang/julia/issues/22696 [#22703]: https://github.com/JuliaLang/julia/issues/22703 [#22712]: https://github.com/JuliaLang/julia/issues/22712 [#22718]: https://github.com/JuliaLang/julia/issues/22718 diff --git a/base/set.jl b/base/set.jl index f5ced5e45d779..18d200a948a55 100644 --- a/base/set.jl +++ b/base/set.jl @@ -240,7 +240,8 @@ const ⊆ = issubset Return an array containing only the unique elements of collection `itr`, as determined by [`isequal`](@ref), in the order that the first of each -set of equivalent elements originally appears. +set of equivalent elements originally appears. The element type of the +input is preserved. # Examples ```jldoctest @@ -249,6 +250,11 @@ julia> unique([1, 2, 6, 2]) 1 2 6 + +julia> unique(Real[1, 1.0, 2]) +2-element Array{Real,1}: + 1 + 2 ``` """ function unique(itr) @@ -260,10 +266,6 @@ function unique(itr) return out end x, i = next(itr, i) - if !isleaftype(T) - S = typeof(x) - return _unique_from(itr, S[x], Set{S}((x,)), i) - end push!(seen, x) push!(out, x) return unique_from(itr, out, seen, i) diff --git a/test/sets.jl b/test/sets.jl index 93f6022d53e8e..cc14883698cc6 100644 --- a/test/sets.jl +++ b/test/sets.jl @@ -232,7 +232,9 @@ u = unique([1,1,2]) @test unique(n->n % 3, [5,1,8,9,3,4,10,7,2,6]) == [5,1,9] # issue 20105 @test @inferred(unique(x for x in 1:1)) == [1] -@test unique(x for x in Any[1,1.0])::Vector{Real} == [1] +@test unique(x for x in Any[1,1.0])::Vector{Any} == [1] +@test unique(x for x in Real[1,1.0])::Vector{Real} == [1] +@test unique(Integer[1,1,2])::Vector{Integer} == [1,2] # unique! @testset "unique!" begin