Skip to content
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
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name = "IntervalSets"
uuid = "8197267c-284f-5f27-9208-e0e47529a953"
version = "0.5.3"
version = "0.5.4"

[deps]
Dates = "ade2ca70-3891-5945-98fb-dc099432e06a"
Expand Down
10 changes: 9 additions & 1 deletion src/IntervalSets.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module IntervalSets

using Base: @pure
import Base: eltype, convert, show, in, length, isempty, isequal, issubset, ==, hash,
union, intersect, minimum, maximum, extrema, range, ⊇
union, intersect, minimum, maximum, extrema, range, clamp,

using Statistics
import Statistics: mean
Expand Down Expand Up @@ -270,6 +270,14 @@ range(i::TypedEndpointsInterval{:closed,:open}; length::Integer) =
range(leftendpoint(i); step=width(i)/length, length=length)
range(i::TypedEndpointsInterval{:closed,:open}, len::Integer) = range(i; length=len)

"""
clamp(t, i::ClosedInterval)

Clamp the scalar `t` such that the result is in the interval `i`.
"""
clamp(t, i::TypedEndpointsInterval{:closed,:closed}) =
clamp(t, leftendpoint(i), rightendpoint(i))


"""
duration(iv)
Expand Down
7 changes: 7 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -705,6 +705,13 @@ struct IncompleteInterval <: AbstractInterval{Int} end
@test range(Interval{:closed,:open}(0..1); length=10) == range(0; step=1/10, length=10)
end

@testset "clamp" begin
@test clamp(1, 0..3) == 1
@test clamp(1.0, 1.5..3) == 1.5
@test clamp(1.0, 0..0.5) == 0.5
@test clamp.([pi, 1.0, big(10.)], Ref(2..9.)) == [big(pi), 2, 9]
end

@testset "IteratorSize" begin
@test Base.IteratorSize(ClosedInterval) == Base.SizeUnknown()
end
Expand Down