Skip to content

Commit 024f3f5

Browse files
committed
AxisArrays features: support eltype, convert, minimum, maximum, etc
1 parent 60b0ce1 commit 024f3f5

File tree

3 files changed

+44
-8
lines changed

3 files changed

+44
-8
lines changed

src/IntervalSets.jl

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,24 @@ module IntervalSets
44

55
# package code goes here
66

7-
import Base: show, in, length, isempty, isequal, issubset, ==, union, intersect
7+
using Base: @pure
8+
import Base: eltype, convert, show, in, length, isempty, isequal, issubset, ==, hash, union, intersect, minimum, maximum
89

9-
export ClosedInterval, , .., ±, ordered
10+
export AbstractInterval, ClosedInterval, , .., ±, ordered
11+
12+
abstract AbstractInterval{T}
1013

1114
include("closed.jl")
1215

16+
eltype{T}(::Type{AbstractInterval{T}}) = T
17+
@pure eltype{I<:AbstractInterval}(::Type{I}) = eltype(supertype(I))
18+
19+
convert{I<:AbstractInterval}(::Type{I}, i::I) = i
20+
function convert{I<:AbstractInterval}(::Type{I}, i::AbstractInterval)
21+
T = eltype(I)
22+
I(convert(T, i.left), convert(T, i.right))
23+
end
24+
1325
ordered{T}(a::T, b::T) = ifelse(a < b, (a, b), (b, a))
1426
ordered(a, b) = ordered(promote(a, b)...)
1527

src/closed.jl

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
"""
2-
A `ClosedInterval(left, right)` is an interval set that includes both its upper and lower bounds. In
2+
A `ClosedInterval(left, right)` is an interval set that includes both its upper and lower bounds. In
33
mathematical notation, the constructed range is `[left, right]`.
44
"""
5-
immutable ClosedInterval{T}
5+
immutable ClosedInterval{T} <: AbstractInterval{T}
66
left::T
77
right::T
88

@@ -20,6 +20,9 @@ function ClosedInterval(left, right)
2020
ClosedInterval{T}(checked_conversion(T, left, right)...)
2121
end
2222

23+
ClosedInterval(i::AbstractInterval) = convert(ClosedInterval{eltype(i)}, i)
24+
(::Type{ClosedInterval{T}}){T}(i::AbstractInterval) = convert(ClosedInterval{T}, i)
25+
2326
..(x, y) = ClosedInterval(x, y)
2427

2528
±(x, y) = ClosedInterval(x - y, x + y)
@@ -28,13 +31,21 @@ end
2831
show(io::IO, I::ClosedInterval) = print(io, I.left, "..", I.right)
2932

3033
in(v, I::ClosedInterval) = I.left <= v <= I.right
34+
in(a::ClosedInterval, b::ClosedInterval) = (b.left <= a.left) & (a.right <= b.right)
3135

3236
isempty(A::ClosedInterval) = A.left > A.right
3337

3438
isequal(A::ClosedInterval, B::ClosedInterval) = (isequal(A.left, B.left) & isequal(A.right, B.right)) | (isempty(A) & isempty(B))
3539

3640
==(A::ClosedInterval, B::ClosedInterval) = (A.left == B.left && A.right == B.right) || (isempty(A) && isempty(B))
3741

42+
const _closed_interval_hash = UInt == UInt64 ? 0x1588c274e0a33ad4 : 0x1e3f7252
43+
44+
hash(I::ClosedInterval, h::UInt) = hash(I.left, hash(I.right, hash(_closed_interval_hash, h)))
45+
46+
minimum(I::ClosedInterval) = I.left
47+
maximum(I::ClosedInterval) = I.right
48+
3849
function intersect(A::ClosedInterval, B::ClosedInterval)
3950
left = max(A.left, B.left)
4051
right = min(A.right, B.right)

test/runtests.jl

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,25 @@ using Base.Test
1111
@test_throws ArgumentError :a .. "b"
1212
I = 0..3
1313
print(io, I)
14-
@test String(io) == "0..3"
14+
@test takebuf_string(io) == "0..3"
1515
J = 3..2
1616
K = 5..4
1717
L = 3 ± 2
1818
M = ClosedInterval(2, 5.0)
19-
takebuf_array(io)
2019
print(io, M)
21-
@test String(io) == "2.0..5.0"
20+
@test takebuf_string(io) == "2.0..5.0"
2221
N = ClosedInterval(UInt8(255), 300)
2322
O = CartesianIndex(1, 2, 3, 4) ± 2
2423
@test O == (-1..3, 0..4, 1..5, 2..6)
2524

25+
@test eltype(I) == Int
26+
@test eltype(M) == Float64
27+
@test convert(ClosedInterval{Float64}, I) === 0.0..3.0
28+
@test !(convert(ClosedInterval{Float64}, I) === 0..3)
29+
@test ClosedInterval{Float64}(1,3) === 1.0..3.0
30+
@test ClosedInterval(0.5..2.5) === 0.5..2.5
31+
@test ClosedInterval{Int}(1.0..3.0) === 1..3
32+
2633
@test !isempty(I)
2734
@test isempty(J)
2835
@test J == K
@@ -34,9 +41,13 @@ using Base.Test
3441
@test isequal(J, K)
3542

3643
@test typeof(M.left) == typeof(M.right) && typeof(M.left) == Float64
37-
@test typeof(N.left) == typeof(N.right) && typeof(N.left) == Int
44+
@test typeof(N.left) == typeof(N.right) && typeof(N.left) == Int
45+
46+
@test maximum(I) === 3
47+
@test minimum(I) === 0
3848

3949
@test 2 in I
50+
@test 1..2 in 0.5..2.5
4051

4152
@test I L == ClosedInterval(0, 5)
4253
@test I L == ClosedInterval(1, 3)
@@ -60,5 +71,7 @@ using Base.Test
6071
@test (ClosedInterval(7, 9) I) == false
6172
@test I I
6273
@test I ClosedInterval(1, 2)
74+
75+
@test hash(1..3) == hash(1.0..3.0)
6376
end
6477
end

0 commit comments

Comments
 (0)