From c4cabc73be50ef2ea1a31004575fe13fb7f5cf85 Mon Sep 17 00:00:00 2001 From: Jishnu Bhattacharya Date: Thu, 22 Aug 2024 22:39:12 +0530 Subject: [PATCH 1/2] Unique may return an AbstractFill --- src/FillArrays.jl | 2 +- test/runtests.jl | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/FillArrays.jl b/src/FillArrays.jl index 170e07d3..0c183d64 100644 --- a/src/FillArrays.jl +++ b/src/FillArrays.jl @@ -607,7 +607,7 @@ diff(x::AbstractFillVector{T}) where T = Zeros{T}(length(x)-1) # unique ######### -unique(x::AbstractFill{T}) where T = isempty(x) ? T[] : T[getindex_value(x)] +unique(x::AbstractFill{T}) where T = fillsimilar(x, Int(!isempty(x))) allunique(x::AbstractFill) = length(x) < 2 ######### diff --git a/test/runtests.jl b/test/runtests.jl index 0163df8a..63262d8d 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -1245,7 +1245,7 @@ end @testset "unique" begin @test unique(Fill(12, 20)) == unique(fill(12, 20)) @test unique(Fill(1, 0)) == [] - @test unique(Zeros(0)) isa Vector{Float64} + @test unique(Zeros(0)) == Zeros(0) @test !allunique(Fill("a", 2)) @test allunique(Ones(0)) end From 024250297b50181638d86447ad3a191ec8e723be Mon Sep 17 00:00:00 2001 From: Jishnu Bhattacharya Date: Thu, 22 Aug 2024 23:05:34 +0530 Subject: [PATCH 2/2] unique for OneElement --- src/FillArrays.jl | 2 +- src/oneelement.jl | 8 ++++++++ test/runtests.jl | 19 +++++++++++++++++++ 3 files changed, 28 insertions(+), 1 deletion(-) diff --git a/src/FillArrays.jl b/src/FillArrays.jl index 0c183d64..5f4bb1bf 100644 --- a/src/FillArrays.jl +++ b/src/FillArrays.jl @@ -607,7 +607,7 @@ diff(x::AbstractFillVector{T}) where T = Zeros{T}(length(x)-1) # unique ######### -unique(x::AbstractFill{T}) where T = fillsimilar(x, Int(!isempty(x))) +unique(x::AbstractFill) = fillsimilar(x, Int(!isempty(x))) allunique(x::AbstractFill) = length(x) < 2 ######### diff --git a/src/oneelement.jl b/src/oneelement.jl index 01cdea64..fee7707f 100644 --- a/src/oneelement.jl +++ b/src/oneelement.jl @@ -432,6 +432,14 @@ permutedims(o::OneElementMatrix) = OneElement(o.val, reverse(o.ind), reverse(o.a permutedims(o::OneElementVector) = reshape(o, (1, length(o))) permutedims(o::OneElement, dims) = OneElement(o.val, _permute(o.ind, dims), _permute(o.axes, dims)) +# unique +function unique(O::OneElement) + v = getindex_value(O) + len = iszero(v) ? 1 : min(2, length(O)) + OneElement(getindex_value(O), len, len) +end +allunique(O::OneElement) = length(O) <= 1 || (length(O) < 3 && !iszero(getindex_value(O))) + # show _maybesize(t::Tuple{Base.OneTo{Int}, Vararg{Base.OneTo{Int}}}) = size.(t,1) _maybesize(t) = t diff --git a/test/runtests.jl b/test/runtests.jl index 63262d8d..f41dc245 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -2699,6 +2699,25 @@ end B = OneElement(2, (1, 2), (Base.IdentityUnitRange(1:1), Base.IdentityUnitRange(2:2))) @test repr(B) == "OneElement(2, (1, 2), (Base.IdentityUnitRange(1:1), Base.IdentityUnitRange(2:2)))" end + + @testset "unique" begin + @testset for n in 1:3 + O = OneElement(5, 2, n) + @test unique(O) == unique(Array(O)) + @test allunique(O) == allunique(Array(O)) + O = OneElement(0, 2, n) + @test unique(O) == unique(Array(O)) + @test allunique(O) == allunique(Array(O)) + @testset for m in 1:4 + O2 = OneElement(2, (2,1), (m,n)) + @test unique(O2) == unique(Array(O2)) + @test allunique(O2) == allunique(Array(O2)) + O2 = OneElement(0, (2,1), (m,n)) + @test unique(O2) == unique(Array(O2)) + @test allunique(O2) == allunique(Array(O2)) + end + end + end end @testset "repeat" begin