Skip to content

Commit

Permalink
refactor controldo - fix a corner case (#41)
Browse files Browse the repository at this point in the history
* update

* fix tests

* fix tests
  • Loading branch information
GiggleLiu committed Jun 22, 2022
1 parent 0a009a4 commit a1e07e6
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 8 deletions.
14 changes: 6 additions & 8 deletions src/iterate_control.jl
Original file line number Diff line number Diff line change
Expand Up @@ -110,21 +110,19 @@ function group_shift!(nbits::Int, positions::AbstractVector{Int})
factors = Int[]
k_prv = 0
i = 0
m = length(positions)
# 2 456 9 ...
for (l,k) in enumerate(positions)
for k in positions
if k != k_prv + 1
push!(factors, 1<<(k_prv-i))
gap = k - k_prv-1
push!(masks, bmask(i+1:i+gap))
i += gap
end
k_prv = k
# the last block
if l == m && m != nbits
push!(factors, 1<<(k-i))
push!(masks, bmask(i+1:nbits))
end
end
# the last block
if i != nbits
push!(factors, 1<<(k_prv-i))
push!(masks, bmask(i+1:nbits))
end
return masks, factors
end
4 changes: 4 additions & 0 deletions test/iterate_control.jl
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ mulrow!(v::AbstractVector, i::Int, f) = (v[i] *= f; v)
@test group_shift!(5, [1, 2, 5]) == ([3, 28], [4, 8])
@test group_shift!(5, [2, 3]) == ([1, 30], [1, 4])
@test group_shift!(5, [1, 3, 5]) == ([1, 2, 28], [2, 4, 8])
@test group_shift!(3, [1, 2, 3]) == ([7], [8])
@test group_shift!(3, Int[]) == (Int[7], Int[1])
@test [itercontrol(3, [1, 2, 3], [0,1,0])...] == [2]
@test [itercontrol(3, Int[], Int[])...] == [0,1,2,3,4,5,6,7]
end

@testset "test IterControl" begin
Expand Down

0 comments on commit a1e07e6

Please sign in to comment.