Skip to content

Commit

Permalink
update (#43)
Browse files Browse the repository at this point in the history
  • Loading branch information
GiggleLiu authored Jul 1, 2022
1 parent 0c0285f commit 6b6964c
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 5 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ docs/Manifest.toml
docs/build
docs/src/assets/indigo.css
.vscode
*.swp
13 changes: 8 additions & 5 deletions src/bit_operations.jl
Original file line number Diff line number Diff line change
Expand Up @@ -307,14 +307,17 @@ end
const IntIterator{T} = Union{NTuple{<:Any,T},Vector{T},T,UnitRange{T}} where {T<:Integer}

"""
controller(cbits, cvals) -> Function
controller([T=Int, ]cbits, cvals) -> Function
Return a function that checks whether a basis at `cbits` takes specific value `cvals`.
"""
function controller(cbits::IntIterator{Int}, cvals::IntIterator{Int})
do_mask = bmask(cbits)
function controller(cbits::IntIterator, cvals::IntIterator)
controller(Int, cbits, cvals)
end
function controller(::Type{T}, cbits::IntIterator, cvals::IntIterator) where T
do_mask = bmask(T, cbits...)
target =
length(cvals) == 0 ? 0 :
mapreduce(xy -> (xy[2] == 1 ? 1 << (xy[1] - 1) : 0), |, zip(cbits, cvals))
length(cvals) == 0 ? zero(T) :
mapreduce(xy -> (xy[2] == 1 ? one(T) << T(xy[1] - 1) : zero(T)), |, zip(cbits, cvals))
return b -> ismatch(b, do_mask, target)
end
6 changes: 6 additions & 0 deletions test/bit_operations.jl
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,9 @@ end
@testset "random" begin
@test rand(BitStr64{5}) isa BitStr64{5}
end

@testset "controller" begin
c = controller(Int128, [2,3], [0, 1])
@test c(Int128(3)) == false
@test c(Int128(0b101)) == true
end

0 comments on commit 6b6964c

Please sign in to comment.