Skip to content

Commit

Permalink
Improve readbit (#51)
Browse files Browse the repository at this point in the history
* update

* fix tests
  • Loading branch information
GiggleLiu authored May 24, 2024
1 parent ed98a5b commit ca29f11
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 1 deletion.
2 changes: 2 additions & 0 deletions src/DitStr.jl
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,8 @@ One can specify the value of the nonzero entry by inputing a pair.
onehot(::Type{T}, n::DitStr{D,N,T1}; nbatch=nothing) where {D,T, N,T1} = _onehot(T, D^N, buffer(n)+1; nbatch)
onehot(n::DitStr; nbatch=nothing) = onehot(ComplexF64, n; nbatch)

readbit(x::DitStr{D, N, LongLongUInt{C}}, loc::Int) where {D, N, C} = readbit(x.buf, loc)

########## @dit_str macro ##############
"""
@dit_str -> DitStr64
Expand Down
6 changes: 5 additions & 1 deletion src/longlonguint.jl
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ Base.UInt(x::LongLongUInt{1}) = x.content[1]
Base.zero(::Type{LongLongUInt{C}}) where {C} = LongLongUInt{C}(ntuple(_->UInt(0), Val{C}()))
Base.zero(::LongLongUInt{C}) where {C} = zero(LongLongUInt{C})
# convert from integers
LongLongUInt{C}(x::T) where {C, T<:Integer} = LongLongUInt{C}(ntuple(i->i==1 ? UInt(x) : zero(UInt), Val{C}()))
LongLongUInt{C}(x::T) where {C, T<:Integer} = LongLongUInt{C}(ntuple(i->i==C ? UInt(x) : zero(UInt), Val{C}()))
Base.promote_type(::Type{LongLongUInt{C}}, ::Type{Int}) where {C} = LongLongUInt{C}
Base.promote_type(::Type{LongLongUInt{C}}, ::Type{UInt}) where {C} = LongLongUInt{C}
function Base.mod(x::LongLongUInt{C}, D::Int) where {C}
Expand Down Expand Up @@ -51,6 +51,10 @@ function Base.:(<<)(x::LongLongUInt{C}, y::Int) where C
end
)
end
function readbit(x::LongLongUInt{C}, loc::Int) where {C}
k = (loc-1) ÷ bsizeof(UInt)
return readbit(x.content[C-k], loc - k*bsizeof(UInt))
end
function indicator(::Type{LongLongUInt{C}}, i::Int) where C
k = (i-1) ÷ bsizeof(UInt)
LongLongUInt{C}(ntuple(j->j==C-k ? indicator(UInt, i-k*bsizeof(UInt)) : zero(UInt), Val{C}()))
Expand Down
15 changes: 15 additions & 0 deletions test/longlonguint.jl
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ using Test, BitBasis
@test ~x == LongLongUInt((~UInt(3), ~UInt(6)))

y = LongLongUInt((5, 7))
@test one(y) == LongLongUInt((0, 1))
@test x & y == LongLongUInt((1, 6))
@test x | y == LongLongUInt((7, 7))
@test x y == LongLongUInt((6, 1))
Expand Down Expand Up @@ -62,4 +63,18 @@ end
@test indicator(LongLongUInt{1}, 64) == LongLongUInt((UInt(1)<<63,))
@test indicator(LongLongUInt{2}, 64) == LongLongUInt((zero(UInt), UInt(1)<<63))
@test indicator(LongLongUInt{2}, 65) == LongLongUInt((1, 0))
end

@testset "takebit" begin
x = LongLongUInt((3, 6))
@test readbit(x, 1) == 0
@test readbit(x, 2) == 1
@test readbit(x, 3) == 1
@test readbit(x, 4) == 0

@test readbit(x, 64) == 0
@test readbit(x, 65) == 1
@test readbit(x, 66) == 1
@test readbit(x, 67) == 0
@test readbit(BitStr{78}(x), 66) == 1
end

0 comments on commit ca29f11

Please sign in to comment.