diff --git a/README.md b/README.md index 94cb1f1..a7b0f45 100755 --- a/README.md +++ b/README.md @@ -333,6 +333,12 @@ julia> find_all(>(4) & <(8), fr) ``` ## Mutation +There are options for in place mutations and corresponding non mutationg +operations. These allow safe mutation of ranges by avoiding states that are +typically prohibited at time of construction. For example, `OneToMRange` cannot +have a negative value for it's `stop` field. These methods are also called +whenever `setproperty!` is used. + ### set_length ```julia @@ -369,6 +375,12 @@ UnitMRange(2:20) julia> first(mr) 2 + +julia> mr.start = 3 +3 + +julia> mr +UnitMRange(3:10) ``` ### set_last @@ -380,14 +392,20 @@ julia> r = 1:10 julia> set_last(r, 5) 1:5 -julia> mr = UnitMRange(1, 10) -UnitMRange(1:10) +julia> mr = OneToMRange(10) +OneToMRange(10) -julia> set_last!(r, 5) -UnitMRange(1:5) +julia> set_last!(mr, 5) +UnitMRange(5) julia> last(mr) 5 + +julia> mr.stop = -1 +-1 + +julia> mr +OneToMRange(0) ``` ### set_step diff --git a/src/StaticRanges.jl b/src/StaticRanges.jl index d838f24..64ba3d1 100755 --- a/src/StaticRanges.jl +++ b/src/StaticRanges.jl @@ -95,7 +95,6 @@ const MRange{T} = Union{OneToMRange{T},UnitMRange{T},StepMRange{T},LinMRange{T}, const UnionRange{T} = Union{SRange{T},MRange{T}} const FRange{T} = Union{OneTo{T},UnitRange{T},StepRange{T},LinRange{T}, StepRangeLen{T}} - include("staticness.jl") include("checkindex.jl") include("filter.jl") diff --git a/src/onetorange.jl b/src/onetorange.jl index c2d3b45..4290bd8 100755 --- a/src/onetorange.jl +++ b/src/onetorange.jl @@ -69,3 +69,12 @@ OneToMRange(r::AbstractRange{T}) where {T<:Integer} = OneToMRange{T}(r) Base.AbstractUnitRange{T}(r::OneToSRange) where {T} = OneToSRange{T}(r) Base.AbstractUnitRange{T}(r::OneToMRange) where {T} = OneToMRange{T}(r) + +function Base.setproperty!(r::OneToMRange, s::Symbol, val) + if s === :stop + return set_last!(r, val) + else + error("type $(typeof(r)) has no property $s") + end +end + diff --git a/src/paramcheck.jl b/src/paramcheck.jl deleted file mode 100755 index ddb9760..0000000 --- a/src/paramcheck.jl +++ /dev/null @@ -1,33 +0,0 @@ - - -#= -Switch traits are used for to pass information that can't be known based on -an objects type. For example, a method may require paramter checks for -construction that may have been previously completed incidently. - -A concrete example is the LengthCheck trait - - -=# -abstract type ParamCheck{B} end - -is_checked(::P) where {P} = is_checked(P) -is_checked(::Type{<:ParamCheck{B}}) where {B} = B - - -function checkparams(p::P, args...; kwargs...) where {P<:ParamCheck} - return is_checked(P) ? nothing : p(args...; kwargs...) -end - -(p::ParamCheck{true})(args...; kwargs...) = nothing -(p::ParamCheck{false})(args...; kwargs...) = execute_check(p, args...; kwargs...) - - - - - - - - - - diff --git a/src/pop.jl b/src/pop.jl index 3b2a6be..8a365b4 100755 --- a/src/pop.jl +++ b/src/pop.jl @@ -8,13 +8,12 @@ end StaticArrays.pop(r::Union{OneTo,OneToRange}) = similar_type(r)(last(r) - one(eltype(r))) -# FIXME this should be defined somewhere +# FIXME this should be defined somewhere else function StaticArrays.popfirst(v::AbstractVector) isempty(v) && error("array must be non-empty") return length(v) == 1 ? empty!(v) : @inbounds(v[2:end]) end -### function Base.pop!(r::StepMRangeLen) isempty(r) && error("array must be non-empty") l = last(r) diff --git a/test/onetorange.jl b/test/onetorange.jl index 20b1687..be50aa5 100755 --- a/test/onetorange.jl +++ b/test/onetorange.jl @@ -41,6 +41,7 @@ for (frange, oneto) in ((mrange, OneToMRange),(srange ,OneToSRange)) str = String(take!(io)) @test str == "$(oneto)(3)" @test in(1, r) == true + end @test oneto{Int}(oneto(10)) == oneto(10) @@ -65,4 +66,8 @@ for (frange, oneto) in ((mrange, OneToMRange),(srange ,OneToSRange)) @test oneto{Int16}(3.0) == oneto{Int16}(3) @test_throws InexactError(:Int16, Int16, 3.2) oneto{Int16}(3.2) end + + r = OneToMRange(10) + r.stop = -3 + @test last(r) == 0 end diff --git a/test/step_tests.jl b/test/step_tests.jl index ef25459..5e4ca11 100755 --- a/test/step_tests.jl +++ b/test/step_tests.jl @@ -29,6 +29,11 @@ end end end + + @test set_step(StepSRangeLen(1,1,4), 2) == StepSRangeLen(1,2,4) + @test set_step(StepRangeLen(1,1,4), 2) == StepRangeLen(1,2,4) + + for (r,b) in ((OneToMRange(10), OneTo(10)), (OneToSRange(UInt(10)), OneTo(UInt(10))), (UnitMRange(1, 10), UnitRange(1, 10)),