Skip to content

Commit

Permalink
Minor cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
mbauman committed Feb 4, 2015
1 parent edee4ee commit c465dce
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 11 deletions.
22 changes: 13 additions & 9 deletions src/intset.jl
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@ function copy!(to::IntSet, from::IntSet)
to
end
eltype(s::IntSet) = Int
sizehint!(s::IntSet, sz::Integer) = (_resize0!(s.bits, sz+1); s)
sizehint!(s::IntSet, n::Integer) = (_resize0!(s.bits, n+1); s)

# An internal function for setting the inclusion bit for a given integer n >= 0
@inline function _setint!(s::IntSet, n::Integer, b::Bool)
idx = n+1
if idx > length(s.bits)
!b && return s # setting a bit to zero outside the set's bits is a no-op
newlen = idx + idx÷2 # This operation may overflow
newlen = idx + idx>>1 # This operation may overflow; we want saturation
_resize0!(s.bits, ifelse(newlen<0, typemax(Int), newlen))
end
unsafe_setindex!(s.bits, b, idx) # Use @inbounds once available
Expand All @@ -30,7 +30,7 @@ end

# An internal function to resize a bitarray and ensure the newly allocated
# elements are zeroed (will become unnecessary if this behavior changes)
@inline function _resize0!(b::BitArray, newlen::Integer)
@inline function _resize0!(b::BitVector, newlen::Integer)
len = length(b)
resize!(b, newlen)
len < newlen && unsafe_setindex!(b, false, len+1:newlen) # resize! gives dirty memory
Expand All @@ -46,7 +46,7 @@ function _matchlength!(b::BitArray, newlen::Integer)
return BitVector(0)
end

const _intset_bounds_err_msg = string("elements of IntSet must be between 0 and ", typemax(Int)-1)
const _intset_bounds_err_msg = "elements of IntSet must be between 0 and typemax(Int)-1"

function push!(s::IntSet, n::Integer)
0 <= n < typemax(Int) || throw(ArgumentError(_intset_bounds_err_msg))
Expand Down Expand Up @@ -146,10 +146,11 @@ function symdiff!(s1::IntSet, s2::IntSet)
end

function in(n::Integer, s::IntSet)
if 0 <= n < length(s.bits)
unsafe_getindex(s.bits, n+1) != s.inverse
idx = n+1
if 1 <= idx <= length(s.bits)
unsafe_getindex(s.bits, idx) != s.inverse
else
ifelse((n < 0) | (n >= typemax(Int)), false, s.inverse)
ifelse((idx <= 0) | (idx > typemax(Int)), false, s.inverse)
end
end

Expand Down Expand Up @@ -211,11 +212,14 @@ function ==(s1::IntSet, s2::IntSet)
# If the lengths are the same, simply punt to bitarray comparison
l1 == l2 && return s1.bits == s2.bits
# Otherwise check the last bit. If equal, we only need to check up to l2
return findprev(s1.bits, l1) == findprev(s2.bits, l2) && s1.bits[1:l2] == s2.bits
return findprev(s1.bits, l1) == findprev(s2.bits, l2) &&
unsafe_getindex(s1.bits, 1:l2) == s2.bits
else
# one complement, one not. Could feasibly be true on 32 bit machines
# Only if all non-overlapping bits are set and overlaps are inverted
return l1 == typemax(Int) && map!(!, s1.bits[1:l2]) == s2.bits && (l1 == l2 || all(s1.bits[l2+1:end]))
return l1 == typemax(Int) &&
map!(!, unsafe_getindex(s1.bits, 1:l2)) == s2.bits &&
(l1 == l2 || all(unsafe_getindex(s1.bits, l2+1:l1)))
end
end

Expand Down
6 changes: 4 additions & 2 deletions test/perfplots.jl
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ for (fname, param) in (("perf_density", :density),
for d in groupby(bdf, :test)
p = plot(d, x=param, y=:median, ymin=:min, ymax=:p95, color=:module,
Geom.line, Geom.ribbon, Scale.x_log10, Scale.y_log10,
Guide.Title(string(d[:test][1])), Guide.YLabel("time (s)"))
Guide.Title(string(d[:test][1])), Guide.YLabel("time (s)"),
Theme(background_color=Color.color("white")))
draw(PNG(joinpath(fname, string(d[:test][1], ".png")), 4inch, 3inch), p)
end
# Relative plots
Expand All @@ -25,7 +26,8 @@ for (fname, param) in (("perf_density", :density),
end
for d in groupby(rdf, :test)
p = plot(d, x=param, y=:min, Geom.line, Scale.x_log10,
Guide.Title(string(d[:test][1])), Guide.YLabel("min relative to base"))
Guide.Title(string(d[:test][1])), Guide.YLabel("min relative to base"),
Theme(background_color=Color.color("white")))
draw(PNG(joinpath(fname, string(d[:test][1], "_rel.png")), 4inch, 3inch), p)
end
end

0 comments on commit c465dce

Please sign in to comment.