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

remove parametric type for counters #51

Merged
merged 1 commit into from
Aug 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
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
50 changes: 25 additions & 25 deletions src/distances/qgram.jl
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ function (dist::AbstractQGramDistance)(s1, s2)
((s1 === missing) | (s2 === missing)) && return missing
counter = newcounter(dist)
for (n1, n2) in _count(qgrams(s1, dist.q), qgrams(s2, dist.q))
count!(counter, n1, n2)
count!(dist, counter, n1, n2)
end
calculate(dist, counter)
end
Expand All @@ -115,15 +115,15 @@ struct QGram <: AbstractQGramDistance
q::Int
end

mutable struct SingleCounter{QD<:AbstractQGramDistance} <: AbstractQGramMatchCounter
mutable struct SingleCounter <: AbstractQGramMatchCounter
shared::Int
end

newcounter(d::QGram) = SingleCounter{QGram}(0)
@inline function count!(c::SingleCounter{QGram}, n1::Integer, n2::Integer)
newcounter(::QGram) = SingleCounter(0)
@inline function count!(::QGram, c::SingleCounter, n1::Integer, n2::Integer)
c.shared += abs(n1 - n2)
end
calculate(dist::QGram, c::SingleCounter{QGram}) = c.shared
calculate(::QGram, c::SingleCounter) = c.shared

"""
Cosine(q::Int)
Expand All @@ -141,19 +141,19 @@ struct Cosine <: AbstractQGramDistance
q::Int
end

mutable struct ThreeCounters{QD<:AbstractQGramDistance} <: AbstractQGramMatchCounter
mutable struct ThreeCounters <: AbstractQGramMatchCounter
left::Int
right::Int
shared::Int
end

newcounter(d::Cosine) = ThreeCounters{Cosine}(0, 0, 0)
@inline function count!(c::ThreeCounters{Cosine}, n1::Integer, n2::Integer)
newcounter(::Cosine) = ThreeCounters(0, 0, 0)
@inline function count!(::Cosine, c::ThreeCounters, n1::Integer, n2::Integer)
c.left += n1^2
c.right += n2^2
c.shared += n1 * n2
end
calculate(d::Cosine, c::ThreeCounters{Cosine}) =
calculate(::Cosine, c::ThreeCounters) =
1.0 - c.shared / (sqrt(c.left) * sqrt(c.right))

"""
Expand All @@ -170,13 +170,13 @@ where ``Q(s, q)`` denotes the set of q-grams of length n for the string s
struct Jaccard <: AbstractQGramDistance
q::Int
end
newcounter(d::Jaccard) = ThreeCounters{Jaccard}(0, 0, 0)
@inline function count!(c::ThreeCounters{Jaccard}, n1::Integer, n2::Integer)
newcounter(::Jaccard) = ThreeCounters(0, 0, 0)
@inline function count!(::Jaccard, c::ThreeCounters, n1::Integer, n2::Integer)
c.left += (n1 > 0)
c.right += (n2 > 0)
c.shared += (n1 > 0) & (n2 > 0)
end
calculate(d::Jaccard, c::ThreeCounters{Jaccard}) =
calculate(::Jaccard, c::ThreeCounters) =
1.0 - c.shared / (c.left + c.right - c.shared)

"""
Expand All @@ -193,13 +193,13 @@ where ``Q(s, q)`` denotes the set of q-grams of length n for the string s
struct SorensenDice <: AbstractQGramDistance
q::Int
end
newcounter(d::SorensenDice) = ThreeCounters{SorensenDice}(0, 0, 0)
@inline function count!(c::ThreeCounters{SorensenDice}, n1::Integer, n2::Integer)
newcounter(::SorensenDice) = ThreeCounters(0, 0, 0)
@inline function count!(::SorensenDice, c::ThreeCounters, n1::Integer, n2::Integer)
c.left += (n1 > 0)
c.right += (n2 > 0)
c.shared += (n1 > 0) & (n2 > 0)
end
calculate(d::SorensenDice, c::ThreeCounters{SorensenDice}) =
calculate(::SorensenDice, c::ThreeCounters) =
1.0 - 2.0 * c.shared / (c.left + c.right)

"""
Expand All @@ -216,13 +216,13 @@ where ``Q(s, q)`` denotes the set of q-grams of length n for the string s
struct Overlap <: AbstractQGramDistance
q::Int
end
newcounter(d::Overlap) = ThreeCounters{Overlap}(0, 0, 0)
@inline function count!(c::ThreeCounters{Overlap}, n1::Integer, n2::Integer)
newcounter(::Overlap) = ThreeCounters(0, 0, 0)
@inline function count!(::Overlap, c::ThreeCounters, n1::Integer, n2::Integer)
c.left += (n1 > 0)
c.right += (n2 > 0)
c.shared += (n1 > 0) & (n2 > 0)
end
calculate(d::Overlap, c::ThreeCounters{Overlap}) =
calculate(::Overlap, c::ThreeCounters) =
1.0 - c.shared / min(c.left, c.right)

"""
Expand All @@ -248,13 +248,13 @@ struct NMD <: AbstractQGramDistance
q::Int
end

newcounter(d::NMD) = ThreeCounters{NMD}(0, 0, 0)
@inline function count!(c::ThreeCounters{NMD}, n1::Integer, n2::Integer)
newcounter(::NMD) = ThreeCounters(0, 0, 0)
@inline function count!(::NMD, c::ThreeCounters, n1::Integer, n2::Integer)
c.left += n1
c.right += n2
c.shared += max(n1, n2)
end
calculate(d::NMD, c::ThreeCounters{NMD}) =
calculate(::NMD, c::ThreeCounters) =
(c.shared - min(c.left, c.right)) / max(c.left, c.right)


Expand All @@ -279,22 +279,22 @@ struct MorisitaOverlap <: AbstractQGramDistance
q::Int
end

mutable struct FiveCounters{QD<:AbstractQGramDistance} <: AbstractQGramMatchCounter
mutable struct FiveCounters <: AbstractQGramMatchCounter
leftsum::Int # sum(m(s1))
rightsum::Int # sum(m(s2))
leftsq::Int # sum(m(s1).^2)
rightsq::Int # sum(m(s2).^2)
shared::Int # sum(m(s1) .* m(s2))
end

newcounter(d::MorisitaOverlap) = FiveCounters{MorisitaOverlap}(0, 0, 0, 0, 0)
@inline function count!(c::FiveCounters{MorisitaOverlap}, n1::Integer, n2::Integer)
newcounter(::MorisitaOverlap) = FiveCounters(0, 0, 0, 0, 0)
@inline function count!(::MorisitaOverlap, c::FiveCounters, n1::Integer, n2::Integer)
c.leftsum += n1
c.rightsum += n2
c.leftsq += (n1^2)
c.rightsq += (n2^2)
c.shared += (n1 * n2)
end
calculate(d::MorisitaOverlap, c::FiveCounters{MorisitaOverlap}) =
calculate(::MorisitaOverlap, c::FiveCounters) =
1.0 - ((2 * c.shared) / (c.leftsq*c.rightsum/c.leftsum + c.rightsq*c.leftsum/c.rightsum))

16 changes: 8 additions & 8 deletions src/distances/qgram_preprocessed.jl
Original file line number Diff line number Diff line change
Expand Up @@ -56,15 +56,15 @@ function (dist::AbstractQGramDistance)(qc1::QGramDict, qc2::QGramDict)
for (k1, c1) in d1
index = Base.ht_keyindex2!(d2, k1)
if index > 0
count!(counter, c1, d2.vals[index])
count!(dist, counter, c1, d2.vals[index])
else
count!(counter, c1, 0)
count!(dist, counter, c1, 0)
end
end
for (k2, c2) in d2
index = Base.ht_keyindex2!(d1, k2)
if index <= 0
count!(counter, 0, c2)
count!(dist, counter, 0, c2)
end
end
calculate(dist, counter)
Expand Down Expand Up @@ -125,26 +125,26 @@ function (dist::AbstractQGramDistance)(qc1::QGramSortedVector, qc2::QGramSortedV
# length can be zero
if i2 > length(d2)
for i in i1:length(d1)
@inbounds count!(counter, d1[i][2], 0)
@inbounds count!(dist, counter, d1[i][2], 0)
end
break
elseif i1 > length(d1)
for i in i2:length(d2)
@inbounds count!(counter, 0, d2[i][2])
@inbounds count!(dist, counter, 0, d2[i][2])
end
break
end
@inbounds k1, n1 = d1[i1]
@inbounds k2, n2 = d2[i2]
cmpval = Base.cmp(k1, k2)
if cmpval == -1 # k1 < k2
count!(counter, n1, 0)
count!(dist, counter, n1, 0)
i1 += 1
elseif cmpval == +1 # k2 < k1
count!(counter, 0, n2)
count!(dist, counter, 0, n2)
i2 += 1
else
count!(counter, n1, n2)
count!(dist, counter, n1, n2)
i1 += 1
i2 += 1
end
Expand Down