Skip to content
This repository was archived by the owner on Nov 22, 2023. It is now read-only.

Fix and test behavior of offset integers #187

Merged
merged 1 commit into from
Feb 25, 2020
Merged
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
8 changes: 4 additions & 4 deletions src/faces.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import Base: +, -, abs, *, /, div, convert, ==, <=, >=, <, >, show, to_index, sub_with_overflow

function show(io::IO, oi::OffsetInteger{O, T}) where {O, T}
print(io, "|$(raw(oi)) (indexes as $(O >= 0 ? raw(oi) - O : raw(oi) + -O))|")
print(io, "|$(raw(oi)) (indexes as $(value(oi))|")
end

Base.eltype(::Type{OffsetInteger{O, T}}) where {O, T} = T
@@ -12,7 +12,7 @@ OffsetInteger{O1, T1}(x::OffsetInteger{O2, T2}) where {O1, O2, T1 <: Integer, T2

OffsetInteger{O}(x::Integer) where {O} = OffsetInteger{O, eltype(x)}(x)
OffsetInteger{O}(x::OffsetInteger) where {O} = OffsetInteger{O, eltype(x)}(x)
(::Type{IT})(x::OffsetInteger{O, T}) where {IT <: Integer, O, T <: Integer} = IT(raw(x) - O)
(::Type{IT})(x::OffsetInteger{O, T}) where {IT <: Integer, O, T <: Integer} = IT(value(x))

Base.@pure pure_max(x1, x2) = x1 > x2 ? x1 : x2
Base.promote_rule(::Type{T1}, ::Type{OffsetInteger{O, T2}}) where {T1 <: Integer, O, T2} = T1
@@ -24,12 +24,12 @@ to_index(I::OffsetInteger{0}) = raw(I)

# basic operators
for op in (:(-), :abs)
@eval $(op)(x::T) where {T <: OffsetInteger} = T($(op)(x.i))
@eval $(op)(x::T) where {T <: OffsetInteger} = T($(op)(value(x)))
end
for op in (:(+), :(-), :(*), :(/), :div)
@eval begin
@inline function $(op)(x::OffsetInteger{O}, y::OffsetInteger{O}) where O
OffsetInteger{O}($op(x.i, y.i))
OffsetInteger{O}($op(value(x), value(y)))
end
end
end
2 changes: 2 additions & 0 deletions src/types.jl
Original file line number Diff line number Diff line change
@@ -78,6 +78,8 @@ end

raw(x::OffsetInteger) = x.i
raw(x::Integer) = x
value(x::OffsetInteger{O, T}) where {O, T} = raw(x) - O
value(x::Integer) = x

"""
A `HyperRectangle` is a generalization of a rectangle into N-dimensions.
12 changes: 12 additions & 0 deletions test/faces.jl
Original file line number Diff line number Diff line change
@@ -44,4 +44,16 @@
@test o1 < o2
@test sort([o3, o1, o2]) == [o1, o2, o3]
end

@testset "operations on offsets" begin
o1 = OffsetInteger{-2, Int}(-1)
o2 = OffsetInteger{-2, Int}(0)
o3 = OffsetInteger{-2, Int}(1)
@test abs(o2) === o2
@test abs(o1) === o3
@test o1 + o3 === o2
@test -o1 === o3
@test -o3 === o1
@test -o2 === o2
end
end