Skip to content

Commit e214e32

Browse files
authored
Updates for Julia 0.6 and 0.7 (#25)
1 parent d13b0fa commit e214e32

File tree

6 files changed

+31
-38
lines changed

6 files changed

+31
-38
lines changed

.travis.yml

-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ os:
44
- linux
55
- osx
66
julia:
7-
- 0.5
87
- 0.6
98
- nightly
109
notifications:

REQUIRE

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
julia 0.5
2-
Compat 0.17
1+
julia 0.6
2+
Compat 0.49

appveyor.yml

-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
environment:
22
matrix:
3-
- JULIA_URL: "https://julialang-s3.julialang.org/bin/winnt/x86/0.5/julia-0.5-latest-win32.exe"
4-
- JULIA_URL: "https://julialang-s3.julialang.org/bin/winnt/x64/0.5/julia-0.5-latest-win64.exe"
53
- JULIA_URL: "https://julialang-s3.julialang.org/bin/winnt/x86/0.6/julia-0.6-latest-win32.exe"
64
- JULIA_URL: "https://julialang-s3.julialang.org/bin/winnt/x64/0.6/julia-0.6-latest-win64.exe"
75
- JULIA_URL: "https://julialangnightlies-s3.julialang.org/bin/winnt/x86/julia-latest-win32.exe"

src/IntervalSets.jl

+13-16
Original file line numberDiff line numberDiff line change
@@ -2,41 +2,38 @@ __precompile__()
22

33
module IntervalSets
44

5-
# package code goes here
6-
75
using Base: @pure
8-
import Base: eltype, convert, show, in, length, isempty, isequal, issubset, ==, hash, union, intersect, minimum, maximum, extrema, range
9-
if isdefined(Main, :)
10-
import Base:
11-
end
6+
import Base: eltype, convert, show, in, length, isempty, isequal, issubset, ==, hash,
7+
union, intersect, minimum, maximum, extrema, range,
128

139
using Compat
10+
using Compat.Dates
1411

1512
export AbstractInterval, ClosedInterval, , .., ±, ordered, width
1613

17-
@compat abstract type AbstractInterval{T} end
14+
abstract type AbstractInterval{T} end
1815

1916
include("closed.jl")
2017

21-
eltype{T}(::Type{AbstractInterval{T}}) = T
22-
@pure eltype{I<:AbstractInterval}(::Type{I}) = eltype(supertype(I))
18+
eltype(::Type{AbstractInterval{T}}) where {T} = T
19+
@pure eltype(::Type{I}) where {I<:AbstractInterval} = eltype(supertype(I))
2320

24-
convert{I<:AbstractInterval}(::Type{I}, i::I) = i
25-
function convert{I<:AbstractInterval}(::Type{I}, i::AbstractInterval)
21+
convert(::Type{I}, i::I) where {I<:AbstractInterval} = i
22+
function convert(::Type{I}, i::AbstractInterval) where I<:AbstractInterval
2623
T = eltype(I)
2724
I(convert(T, i.left), convert(T, i.right))
2825
end
29-
function convert{I<:AbstractInterval}(::Type{I}, r::Range)
26+
function convert(::Type{I}, r::AbstractRange) where I<:AbstractInterval
3027
T = eltype(I)
3128
I(convert(T, minimum(r)), convert(T, maximum(r)))
3229
end
3330

34-
ordered{T}(a::T, b::T) = ifelse(a < b, (a, b), (b, a))
31+
ordered(a::T, b::T) where {T} = ifelse(a < b, (a, b), (b, a))
3532
ordered(a, b) = ordered(promote(a, b)...)
3633

37-
checked_conversion{T}(::Type{T}, a, b) = _checked_conversion(T, convert(T, a), convert(T, b))
38-
_checked_conversion{T}(::Type{T}, a::T, b::T) = a, b
34+
checked_conversion(::Type{T}, a, b) where {T} = _checked_conversion(T, convert(T, a), convert(T, b))
35+
_checked_conversion(::Type{T}, a::T, b::T) where {T} = a, b
3936
_checked_conversion(::Type{Any}, a, b) = throw(ArgumentError("$a and $b promoted to type Any"))
40-
_checked_conversion{T}(::Type{T}, a, b) = throw(ArgumentError("$a and $b are not both of type $T"))
37+
_checked_conversion(::Type{T}, a, b) where {T} = throw(ArgumentError("$a and $b are not both of type $T"))
4138

4239
end # module

src/closed.jl

+11-14
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,15 @@
22
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} <: AbstractInterval{T}
5+
struct ClosedInterval{T} <: AbstractInterval{T}
66
left::T
77
right::T
88

9-
(::Type{ClosedInterval{T}}){T}(l::T, r::T) = new{T}(l, r)
9+
ClosedInterval{T}(l::T, r::T) where {T} = new{T}(l, r)
10+
ClosedInterval{T}(l, r) where {T} = ((a, b) = checked_conversion(T, l, r); new{T}(a, b))
11+
ClosedInterval{T}(i::AbstractInterval) where {T} = convert(ClosedInterval{T}, i)
1012
end
1113

12-
ClosedInterval{T}(left::T, right::T) = ClosedInterval{T}(left, right)
13-
(::Type{ClosedInterval{T}}){T}(left, right) =
14-
ClosedInterval{T}(checked_conversion(T, left, right)...)
15-
1614
function ClosedInterval(left, right)
1715
# Defining this as ClosedInterval(promote(left, right)...) has one problem:
1816
# if left and right do not promote to a common type, it triggers a StackOverflow.
@@ -21,7 +19,6 @@ function ClosedInterval(left, right)
2119
end
2220

2321
ClosedInterval(i::AbstractInterval) = convert(ClosedInterval{eltype(i)}, i)
24-
(::Type{ClosedInterval{T}}){T}(i::AbstractInterval) = convert(ClosedInterval{T}, i)
2522

2623
"""
2724
iv = l..r
@@ -37,7 +34,7 @@ Construct a ClosedInterval `iv` spanning the region from
3734
`center - halfwidth` to `center + halfwidth`.
3835
"""
3936
±(x, y) = ClosedInterval(x - y, x + y)
40-
±(x::CartesianIndex, y) = map(ClosedInterval, (x - y).I, (x + y).I)
37+
±(x::CartesianIndex, y) = (xy = y * one(x); map(ClosedInterval, (x - xy).I, (x + xy).I))
4138

4239
show(io::IO, I::ClosedInterval) = print(io, I.left, "..", I.right)
4340

@@ -64,7 +61,7 @@ function intersect(A::ClosedInterval, B::ClosedInterval)
6461
ClosedInterval(left, right)
6562
end
6663

67-
function union{T<:AbstractFloat}(A::ClosedInterval{T}, B::ClosedInterval{T})
64+
function union(A::ClosedInterval{T}, B::ClosedInterval{T}) where T<:AbstractFloat
6865
max(A.left, B.left) <= nextfloat(min(A.right, B.right)) || throw(ArgumentError("Cannot construct union of disjoint sets."))
6966
_union(A, B)
7067
end
@@ -90,19 +87,19 @@ issubset(A::ClosedInterval, B::ClosedInterval) = ((A.left in B) && (A.right in B
9087
Calculate the width (max-min) of interval `iv`. Note that for integers
9188
`l` and `r`, `width(l..r) = length(l:r) - 1`.
9289
"""
93-
function width{T}(A::ClosedInterval{T})
90+
function width(A::ClosedInterval{T}) where T
9491
_width = A.right - A.left
9592
max(zero(_width), _width) # this works when T is a Date
9693
end
9794

98-
length{T <: Integer}(A::ClosedInterval{T}) = max(0, Int(A.right - A.left) + 1)
95+
length(A::ClosedInterval{T}) where {T<:Integer} = max(0, Int(A.right - A.left) + 1)
9996

10097
length(A::ClosedInterval{Date}) = max(0, Dates.days(A.right - A.left) + 1)
10198

102-
function convert{R<:AbstractUnitRange,I<:Integer}(::Type{R}, i::ClosedInterval{I})
99+
function convert(::Type{R}, i::ClosedInterval{I}) where {R<:AbstractUnitRange,I<:Integer}
103100
R(minimum(i), maximum(i))
104101
end
105102

106-
range{I<:Integer}(i::ClosedInterval{I}) = convert(UnitRange{I}, i)
103+
range(i::ClosedInterval{I}) where {I<:Integer} = convert(UnitRange{I}, i)
107104

108-
Base.promote_rule{T1,T2}(::Type{ClosedInterval{T1}}, ::Type{ClosedInterval{T2}}) = ClosedInterval{promote_type(T1, T2)}
105+
Base.promote_rule(::Type{ClosedInterval{T1}}, ::Type{ClosedInterval{T2}}) where {T1,T2} = ClosedInterval{promote_type(T1, T2)}

test/runtests.jl

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
using IntervalSets
2-
using Base.Test
2+
using Compat
3+
using Compat.Test
4+
using Compat.Dates
35

46
@testset "IntervalSets" begin
57
@test ordered(2, 1) == (1, 2)
@@ -77,8 +79,8 @@ using Base.Test
7779
@test hash(1..3) == hash(1.0..3.0)
7880

7981
let A = Date(1990, 1, 1), B = Date(1990, 3, 1)
80-
@test width(ClosedInterval(A, B)) == Base.Dates.Day(59)
81-
@test width(ClosedInterval(B, A)) == Base.Dates.Day(0)
82+
@test width(ClosedInterval(A, B)) == Dates.Day(59)
83+
@test width(ClosedInterval(B, A)) == Dates.Day(0)
8284
@test isempty(ClosedInterval(B, A))
8385
@test length(ClosedInterval(A, B)) 60
8486
@test length(ClosedInterval(B, A)) 0

0 commit comments

Comments
 (0)