Skip to content

Commit

Permalink
Unique may return an AbstractFill (#382)
Browse files Browse the repository at this point in the history
* Unique may return an AbstractFill

* unique for OneElement
  • Loading branch information
jishnub authored Aug 28, 2024
1 parent d498a3e commit 5b31642
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/FillArrays.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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) = fillsimilar(x, Int(!isempty(x)))
allunique(x::AbstractFill) = length(x) < 2

#########
Expand Down
8 changes: 8 additions & 0 deletions src/oneelement.jl
Original file line number Diff line number Diff line change
Expand Up @@ -436,6 +436,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
Expand Down
21 changes: 20 additions & 1 deletion test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -2708,6 +2708,25 @@ end
@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

@testset "sum" begin
@testset "OneElement($v, $ind, $sz)" for (v, ind, sz) in (
(Int8(2), 3, 4),
Expand Down

0 comments on commit 5b31642

Please sign in to comment.