diff --git a/.gitignore b/.gitignore index 8c960ec..3f02ca7 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ *.jl.cov *.jl.*.cov *.jl.mem +Manifest.toml diff --git a/src/group.jl b/src/group.jl index 96d0a55..6ad3c82 100644 --- a/src/group.jl +++ b/src/group.jl @@ -52,7 +52,8 @@ function group(groups, values) out = Dictionary{I, Vector{T}}() @inbounds for (group, value) in zip(groups, values) - push!(get!(Vector{T}, out, group), value) + tmp = get!(() -> T[value], out, group) + last(tmp) == value || push!(tmp, value) end return out @@ -295,8 +296,8 @@ function groupfind(container) out = Dictionary{I, Vector{T}}() @inbounds for i in keys(container) - tmp = get!(Vector{T}, out, container[i]) - push!(tmp, i) + tmp = get!(() -> T[i], out, container[i]) + last(tmp) == i || push!(tmp, i) end return out end @@ -307,8 +308,8 @@ function groupfind(inds::AbstractIndices) out = Dictionary{I, Vector{T}}() for i in inds - tmp = get!(Vector{T}, out, i) - push!(tmp, i) + tmp = get!(() -> T[i], out, container[i]) + last(tmp) == i || push!(tmp, i) end return out end diff --git a/test/group.jl b/test/group.jl index 0881a73..a808a8d 100644 --- a/test/group.jl +++ b/test/group.jl @@ -5,6 +5,8 @@ @test group(iseven, x -> x*2, 1:10)::Dictionary == dictionary([false => [2,6,10,14,18], true => [4,8,12,16,20]]) @test group((x,y) -> iseven(x+y), (x,y) -> x, 1:10, [1,3,4,2,5,6,4,2,3,9])::Dictionary == dictionary([true => [1,4,5,6,8,9], false => [2,3,7,10]]) + + @test group(isnothing, [1, 2, 3, nothing, 4, 5, nothing])::Dictionary == dictionary([false => [1, 2, 3, 4, 5], true => [nothing, nothing]]) end @testset "grouponly" begin