Skip to content

Commit

Permalink
Added convert/outer constructor method (#23)
Browse files Browse the repository at this point in the history
  • Loading branch information
Ben J. Ward authored Feb 21, 2017
1 parent ac09625 commit 1f5b320
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/IntervalTrees.jl
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ immutable Interval{T} <: AbstractInterval{T}
first::T
last::T
end

Base.convert{T}(::Type{Interval{T}}, range::Range{T}) = Interval(first(range), last(range))
Interval{T}(range::Range{T}) = convert(Interval{T}, range)
Base.first{T}(i::Interval{T}) = i.first
Base.last{T}(i::Interval{T}) = i.last

Expand All @@ -47,6 +48,7 @@ immutable IntervalValue{K, V} <: AbstractInterval{K}
last::K
value::V
end
IntervalValue{K, V}(range::Range{K}, value::V) = IntervalValue(first(range), last(range), value)

Base.first{K, V}(i::IntervalValue{K, V}) = i.first
Base.last{K, V}(i::IntervalValue{K, V}) = i.last
Expand Down
11 changes: 11 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,17 @@ end
using IntervalTrees
import IntervalTrees: Slice, InternalNode, LeafNode, Interval, IntervalBTree

# Convert
@testset "Convert and constructors" begin
@test Interval(1:5) == Interval(1, 5)
@test Interval(4.2:9.2) == Interval(4.2, 9.2)
@test first(IntervalValue(1:5, "Hi!")) == 1
@test last(IntervalValue(1:5, "Hi!")) == 5
@test value(IntervalValue(1:5, "Hi!")) == "Hi!"
@test first(IntervalValue(4.2:9.2, "Bye!")) == 4.2
@test last(IntervalValue(4.2:9.2, "Bye!")) == 9.2
@test value(IntervalValue(4.2:9.2, "Bye!")) == "Bye!"
end

# Getters
@testset "Getters" begin
Expand Down

0 comments on commit 1f5b320

Please sign in to comment.