Skip to content

Commit

Permalink
Implemented clamp(t, i::ClosedInterval)
Browse files Browse the repository at this point in the history
  • Loading branch information
jagot committed Apr 14, 2021
1 parent 4e5e2e9 commit 3109820
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 2 deletions.
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

0 comments on commit 3109820

Please sign in to comment.