Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Store keys and values seperately #15

Merged
merged 6 commits into from
Dec 7, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 25 additions & 6 deletions src/Tally.jl
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ mutable struct TallyT{T} <: AbstractDict{T, Int}
by
equivalence

function TallyT(keys::Vector{T}, values::Vector{Int}, by = isequal, equivalence = identity) where {T}
function TallyT(keys::Vector{T}, values::Vector{Int}, by = identity, equivalence = isequal) where {T}
p = sortperm(values, rev = true)
return new{T}(keys[p], values[p], by, equivalence)
end
Expand Down Expand Up @@ -307,21 +307,40 @@ function Base.append!(T::TallyT, it)
end

function Base.get(T::TallyT, key, default)
ind = findfirst(==(key), T.keys)
if ind === nothing
fl, ind = _has_key(key, T.keys, T.by, T.equivalence)
if !fl
return default
else
return T.values[ind]
end
end

function Base.:+(T1::TallyT, T2::TallyT)
return TallyT(keys(T1), [T1.values[i] + T2[key] for (i, key) in enumerate(keys(T1))])
!(T1.by === T2.by && T1.equivalence === T2.equivalence) &&
error("Tallies incompatible")
K = copy(keys(T1))
for k in keys(T2)
if !_has_key(k, K, T1.by, T1.equivalence)[1]
push!(K, k)
end
end
# To get the right array type
V = empty!(vcat(T1.values, T2.values))
KK = eltype(K)[]
for k in K
v = get(T1, k, 0) + get(T2, k, 0)
if iszero(v)
continue
end
push!(KK, k)
push!(V, v)
end
return TallyT(KK, V)
end


function Base.:-(T1::TallyT, T2::TallyT)
return TallyT(keys(T1), [T1.values[i] - T2[key] for (i, key) in enumerate(keys(T1))])
T3 = TallyT(T2.keys, -T2.values)
return T1 + T3
end

################################################################################
Expand Down
19 changes: 18 additions & 1 deletion test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,9 @@ Base.hash(::A) = rand(UInt)
push!(T, 2)
@test T.keys == [2, 1]
@test T.values == [4, 3]
@test T[1] == 3
@test get(T, 3, 5) == 5
@test_throws KeyError T[3]

T = tally([2, 1, 1, 1, 2])
append!(T, [2, 1, 2, 2])
Expand All @@ -191,6 +194,10 @@ Base.hash(::A) = rand(UInt)
append!(T, [2, -2, 2])
@test T.keys == [2, -1]
@test T.values == [5, 4]
@test T[2] == 5
@test T[-2] == 5
@test_throws KeyError T[3]
@test get(T, 3, 5) == 5

# lazy tally
T = lazy_tally((rand(-1:1) for i in 1:100))
Expand All @@ -204,7 +211,17 @@ Base.hash(::A) = rand(UInt)
T1 = tally([2, 1, 1, 1, 2])
T2 = tally([2, 2, 1, 1, 2])
@test T1 + T2 == tally([1, 1, 1, 1, 1, 2, 2, 2, 2, 2])
@test T1 - T1 == Tally.TallyT([1, 2], [0, 0])
@test T1 - T1 == Tally.TallyT(Int[], Int[])
@test T1 - T2 == Tally.TallyT([1, 2], [1, -1])

T1 = tally([1, 1, 2])
T2 = tally([2, 3, 3])
@test T1 + T2 == tally([1, 1, 2, 2, 3, 3])
@test (T1 + T2) - T1 == T2

T1 = tally([], by = sin)
T2 = tally([])
@test_throws ErrorException T1 + T2
@test_throws ErrorException T1 - T2
end
end