Skip to content

Commit

Permalink
BitSet: use _div64 & _mod64 from bitarray.jl
Browse files Browse the repository at this point in the history
  • Loading branch information
rfourquet committed Dec 12, 2017
1 parent f9e0282 commit 13f97db
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 11 deletions.
2 changes: 1 addition & 1 deletion base/bitarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ IndexStyle(::Type{<:BitArray}) = IndexLinear()
## aux functions ##

const _msk64 = ~UInt64(0)
@inline _div64(l) = l >>> 6
@inline _div64(l) = l >> 6
@inline _mod64(l) = l & 63
@inline _msk_end(l::Integer) = _msk64 >>> _mod64(-l)
@inline _msk_end(B::BitArray) = _msk_end(length(B))
Expand Down
15 changes: 5 additions & 10 deletions base/bitset.jl
Original file line number Diff line number Diff line change
Expand Up @@ -43,22 +43,17 @@ eltype(s::BitSet) = Int

sizehint!(s::BitSet, n::Integer) = sizehint!(s.bits, n)

# given an integer i, return the chunk which stores it
chk_indice(i::Int) = i >> 6
# return the bit offset of i within chk_indice(i)
chk_offset(i::Int) = i & 63

function _bits_getindex(b::Bits, n::Int, offset::Int)
ci = chk_indice(n) - offset + 1
ci = _div64(n) - offset + 1
1 <= ci <= length(b) || return false
@inbounds r = (b[ci] & (one(UInt64) << chk_offset(n))) != 0
@inbounds r = (b[ci] & (one(UInt64) << _mod64(n))) != 0
r
end

function _bits_findnext(b::Bits, start::Int)
# start is 0-based
# @assert start >= 0
chk_indice(start) + 1 > length(b) && return -1
_div64(start) + 1 > length(b) && return -1
unsafe_bitfindnext(b, start+1) - 1
end

Expand All @@ -71,7 +66,7 @@ end

# An internal function for setting the inclusion bit for a given integer
@inline function _setint!(s::BitSet, idx::Int, b::Bool)
cidx = chk_indice(idx)
cidx = _div64(idx)
len = length(s.bits)
diff = cidx - s.offset
if diff >= len
Expand All @@ -92,7 +87,7 @@ end
s.offset += diff
diff = 0
end
_unsafe_bitsetindex!(s.bits, b, diff+1, chk_offset(idx))
_unsafe_bitsetindex!(s.bits, b, diff+1, _mod64(idx))
s
end

Expand Down

0 comments on commit 13f97db

Please sign in to comment.