Skip to content

Commit

Permalink
bitarray: small fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
carlobaldassi committed May 6, 2012
1 parent 0310315 commit 7f37047
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 9 deletions.
20 changes: 12 additions & 8 deletions extras/bitarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -198,19 +198,22 @@ bitones(args...) = fill!(BitArray(Int, args...), 1)
bitfalses(args...) = bitzeros(Bool, args...)
bittrues(args...) = bitones(Bool, args...)

biteye(n::Integer) = biteye(n, n)
function biteye(m::Integer, n::Integer)
a = bitzeros(m,n)
biteye{T}(::Type{T}, n::Integer) = biteye(T, n, n)
function biteye{T}(::Type{T}, m::Integer, n::Integer)
a = bitzeros(T, m, n)
for i = 1:min(m,n)
a[i,i] = 1
a[i,i] = one(T)
end
return a
end
biteye(n::Integer) = biteye(Int, n)
biteye(m::Integer, n::Integer) = biteye(Int, m, n)

function one{T}(x::BitMatrix{T})
m, n = size(x)
a = bitzeros(T,size(x))
for i = 1:min(m,n)
a[i,i] = 1
a[i,i] = one(T)
end
return a
end
Expand Down Expand Up @@ -1266,13 +1269,14 @@ end

findn(B::BitVector) = find(B)

function findn(B::BitMatrix)
function findn{T<:Integer}(B::BitMatrix{T})
nnzB = nnz(B)
I = Array(Int, nnzB)
J = Array(Int, nnzB)
z = zero(T)
count = 1
for j=1:size(B,2), i=1:size(B,1)
if B[i,j] != 0
if B[i,j] != z
I[count] = i
J[count] = j
count += 1
Expand Down Expand Up @@ -1305,7 +1309,7 @@ function findn(B::BitArray)
end

gen_cartesian_map(findn_cache, findn_one, ranges,
(:B, :I, :count, :z), B,I,1, 0)
(:B, :I, :count, :z), B, I, 1, 0)
return I
end
end
Expand Down
2 changes: 1 addition & 1 deletion test/bitarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@ for m = 0 : v1
end

b1 = bitrand(T, v1)
for m = [randi(v1) 0 1 63 64 65 191 192 193 v1-1]
for m = [randi(v1)-1 0 1 63 64 65 191 192 193 v1-1]
@assert isequal(b1 << m, [ b1[m+1:end]; bitzeros(T, m) ])
@assert isequal(b1 >>> m, [ bitzeros(T, m); b1[1:end-m] ])
@assert isequal(rotl(b1, m), [ b1[m+1:end]; b1[1:m] ])
Expand Down

0 comments on commit 7f37047

Please sign in to comment.