Skip to content

Commit

Permalink
Fixe OneToRange mutation and add some text to README
Browse files Browse the repository at this point in the history
  • Loading branch information
Tokazama committed Dec 17, 2019
1 parent 1d73032 commit e7558cd
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 40 deletions.
26 changes: 22 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -369,6 +375,12 @@ UnitMRange(2:20)

julia> first(mr)
2

julia> mr.start = 3
3

julia> mr
UnitMRange(3:10)
```

### set_last
Expand All @@ -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
Expand Down
1 change: 0 additions & 1 deletion src/StaticRanges.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
9 changes: 9 additions & 0 deletions src/onetorange.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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

33 changes: 0 additions & 33 deletions src/paramcheck.jl

This file was deleted.

3 changes: 1 addition & 2 deletions src/pop.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
5 changes: 5 additions & 0 deletions test/onetorange.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
5 changes: 5 additions & 0 deletions test/step_tests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)),
Expand Down

0 comments on commit e7558cd

Please sign in to comment.