Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dispatch operation on LongLongUInt{1} to UInt #56

Merged
merged 1 commit into from
Aug 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading