Skip to content

Commit

Permalink
dispatch operation on LongLongUInt{1} to UInt (#56)
Browse files Browse the repository at this point in the history
  • Loading branch information
ArrogantGao authored Aug 13, 2024
1 parent 76b798d commit 241d6a8
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 1 deletion.
33 changes: 32 additions & 1 deletion src/longlonguint.jl
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,35 @@ function Base.mod(x::LongLongUInt{C}, D::Int) where {C}
D == 2 ? mod(x.content[end], 2) : error("mod only supports 2")
end

function Base.:(>>)(x::LongLongUInt{1}, y::Int)
return LongLongUInt{1}(x.content[1] >> y)
end
function Base.:(<<)(x::LongLongUInt{1}, y::Int)
return LongLongUInt{1}(x.content[1] << y)
end

function readbit(x::LongLongUInt{1}, loc::Int)
return readbit(x.content[1], loc)
end
function indicator(::Type{LongLongUInt{1}}, i::Int)
return LongLongUInt{1}(indicator(UInt, i))
end

function Base.:(<)(val1::LongLongUInt{1}, val2::LongLongUInt{1})
return val1.content[1] < val2.content[1]
end

function Base.:(<)(val1::LongLongUInt{C}, val2::LongLongUInt{C}) where{C}
for i in 1:C
if val1.content[i] < val2.content[i]
return true
elseif val1.content[i] > val2.content[i]
return false
end
end
return false
end

function Base.:(>>)(x::LongLongUInt{C}, y::Int) where {C}
y < 0 && return x << -y
nshift = y ÷ bsizeof(UInt)
Expand Down Expand Up @@ -103,4 +132,6 @@ function longinttype(n::Int, D::Int)
C = (N-1) ÷ bsizeof(UInt) + 1
return LongLongUInt{C}
end
Base.hash(x::LongLongUInt) = hash(x.content)

Base.hash(x::LongLongUInt{1}) = hash(x.content[1])
Base.hash(x::LongLongUInt{C}) where{C} = hash(x.content)
34 changes: 34 additions & 0 deletions test/longlonguint.jl
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ using Test, BitBasis
end

@testset "shift" begin
x = LongLongUInt((3, ))
@test x << 0 == LongLongUInt((3, ))
@test x << 1 == LongLongUInt((3 << 1, ))
@test x >> 1 == LongLongUInt((3 >> 1, ))

x = LongLongUInt((3, 6))
@test x << 0 == LongLongUInt((3, 6))
@test x << 64 == LongLongUInt((6, 0))
Expand Down Expand Up @@ -70,6 +75,12 @@ end
end

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

x = LongLongUInt((3, 6))
@test readbit(x, 1) == 0
@test readbit(x, 2) == 1
Expand All @@ -86,4 +97,27 @@ end
@testset "bmask" begin
@test bmask(LongLongUInt{1}, 1:1) == LongLongUInt((UInt64(1),))
@test bmask(LongLongUInt{2}, 1:65) == LongLongUInt((UInt64(1), typemax(UInt64)))
end

@testset "isless" begin
x = LongLongUInt((3,))
y = LongLongUInt((5,))
@test x < y
@test y > x

x = LongLongUInt((3, 6))
y = LongLongUInt((3, 7))
@test x < y
@test y > x

x = LongLongUInt((3, 6))
y = LongLongUInt((3, 6))
@test !(x < y)
@test !(y < x)
@test x y
@test y x

xs = [LongLongUInt((3, 6)), LongLongUInt((3, 7)), LongLongUInt((2, 4))]
sorted_xs = sort(xs)
@test sorted_xs == [LongLongUInt((2, 4)), LongLongUInt((3, 6)), LongLongUInt((3, 7))]
end

0 comments on commit 241d6a8

Please sign in to comment.