Skip to content

Commit

Permalink
Merge pull request #6326 from JuliaLang/jb/newrange1
Browse files Browse the repository at this point in the history
ordinal range implementation, fixes #5585
  • Loading branch information
JeffBezanson committed Apr 1, 2014
2 parents 3cd9b68 + ff28fbb commit c402ce9
Show file tree
Hide file tree
Showing 37 changed files with 418 additions and 400 deletions.
25 changes: 25 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,22 @@ Library improvements
* Faster performance on `fill!` and `copy!` for array types not supporting
efficient linear indexing ([#5671], [#5387])

* Changes to range types ([#5585])

* `Range` is now the abstract range type, instead of `Ranges`

* New function `range` for constructing ranges by length

* `Range` is now `StepRange`, and `Range1` is now `UnitRange`. Their
constructors accept end points instead of lengths. Both are subtypes of a
new abstract type `OrdinalRange`.

* Ranges now support `BigInt` and general ordinal types.

* Very large ranges (e.g. `0:typemax(Int)`) can now be constructed, but some
operations (e.g. `length`) will raise an `OverflowError`.


Deprecated or removed
---------------------

Expand Down Expand Up @@ -268,6 +284,11 @@ Deprecated or removed

* `infs` and `nans` are deprecated in favor of the more general `fill`.

* `*` and `div` are no longer supported for `Char`.

* `Range` is renamed `StepRange` and `Range1` is renamed `UnitRange`.
`Ranges` is renamed `Range`.

[#4042]: https://github.com/JuliaLang/julia/issues/4042
[#5164]: https://github.com/JuliaLang/julia/issues/5164
[#4026]: https://github.com/JuliaLang/julia/issues/4026
Expand Down Expand Up @@ -342,9 +363,13 @@ Deprecated or removed
[#6169]: https://github.com/JuliaLang/julia/issues/6169
[#5970]: https://github.com/JuliaLang/julia/issues/5970
[#6197]: https://github.com/JuliaLang/julia/pull/6197
<<<<<<< HEAD
[#5387]: https://github.com/JuliaLang/julia/pull/5387
[#5671]: https://github.com/JuliaLang/julia/pull/5671
[#5380]: https://github.com/JuliaLang/julia/pull/5380
=======
[#5585]: https://github.com/JuliaLang/julia/issues/5585
>>>>>>> remove * and div for Char
Julia v0.2.0 Release Notes
==========================
Expand Down
27 changes: 16 additions & 11 deletions base/abstractarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ end
checkbounds(sz::Int, i::Int) = 1 <= i <= sz || throw(BoundsError())
checkbounds(sz::Int, i::Real) = checkbounds(sz, to_index(i))
checkbounds(sz::Int, I::AbstractVector{Bool}) = length(I) == sz || throw(BoundsError())
checkbounds(sz::Int, r::Ranges{Int}) = isempty(r) || (minimum(r) >= 1 && maximum(r) <= sz) || throw(BoundsError())
checkbounds{T<:Real}(sz::Int, r::Ranges{T}) = checkbounds(sz, to_index(r))
checkbounds(sz::Int, r::Range{Int}) = isempty(r) || (minimum(r) >= 1 && maximum(r) <= sz) || throw(BoundsError())
checkbounds{T<:Real}(sz::Int, r::Range{T}) = checkbounds(sz, to_index(r))

function checkbounds{T <: Real}(sz::Int, I::AbstractArray{T})
for i in I
Expand Down Expand Up @@ -202,7 +202,7 @@ end
copy(a::AbstractArray) = copy!(similar(a), a)
copy(a::AbstractArray{None}) = a # cannot be assigned into so is immutable

function copy!{R,S}(B::AbstractMatrix{R}, ir_dest::Ranges{Int}, jr_dest::Ranges{Int}, A::AbstractMatrix{S}, ir_src::Ranges{Int}, jr_src::Ranges{Int})
function copy!{R,S}(B::AbstractMatrix{R}, ir_dest::Range{Int}, jr_dest::Range{Int}, A::AbstractMatrix{S}, ir_src::Range{Int}, jr_src::Range{Int})
if length(ir_dest) != length(ir_src) || length(jr_dest) != length(jr_src)
error("source and destination must have same size")
end
Expand All @@ -220,7 +220,7 @@ function copy!{R,S}(B::AbstractMatrix{R}, ir_dest::Ranges{Int}, jr_dest::Ranges{
return B
end

function copy_transpose!{R,S}(B::AbstractMatrix{R}, ir_dest::Ranges{Int}, jr_dest::Ranges{Int}, A::AbstractVecOrMat{S}, ir_src::Ranges{Int}, jr_src::Ranges{Int})
function copy_transpose!{R,S}(B::AbstractMatrix{R}, ir_dest::Range{Int}, jr_dest::Range{Int}, A::AbstractVecOrMat{S}, ir_src::Range{Int}, jr_src::Range{Int})
if length(ir_dest) != length(jr_src) || length(jr_dest) != length(ir_src)
error("source and destination must have same size")
end
Expand Down Expand Up @@ -321,8 +321,13 @@ full(x::AbstractArray) = x

for fn in _numeric_conversion_func_names
@eval begin
$fn(r::Range ) = Range($fn(r.start), $fn(r.step), r.len)
$fn(r::Range1) = Range1($fn(r.start), r.len)
$fn(r::StepRange) = $fn(r.start):$fn(r.step):$fn(last(r))
$fn(r::UnitRange) = $fn(r.start):$fn(last(r))
end
end

for fn in (:float,:float16,:float32,:float64)
@eval begin
$fn(r::FloatRange) = FloatRange($fn(r.start), $fn(r.step), r.len, $fn(r.divisor))
end
end
Expand Down Expand Up @@ -451,21 +456,21 @@ end

## get (getindex with a default value) ##

typealias RangeVecIntList{A<:AbstractVector{Int}} Union((Union(Ranges, AbstractVector{Int})...), AbstractVector{Range1{Int}}, AbstractVector{Range{Int}}, AbstractVector{A})
typealias RangeVecIntList{A<:AbstractVector{Int}} Union((Union(Range, AbstractVector{Int})...), AbstractVector{UnitRange{Int}}, AbstractVector{Range{Int}}, AbstractVector{A})

get(A::AbstractArray, i::Integer, default) = in_bounds(length(A), i) ? A[i] : default
get(A::AbstractArray, I::(), default) = similar(A, typeof(default), 0)
get(A::AbstractArray, I::Dims, default) = in_bounds(size(A), I...) ? A[I...] : default

function get!{T}(X::AbstractArray{T}, A::AbstractArray, I::Union(Ranges, AbstractVector{Int}), default::T)
function get!{T}(X::AbstractArray{T}, A::AbstractArray, I::Union(Range, AbstractVector{Int}), default::T)
ind = findin(I, 1:length(A))
X[ind] = A[I[ind]]
X[1:first(ind)-1] = default
X[last(ind)+1:length(X)] = default
X
end

get(A::AbstractArray, I::Ranges, default) = get!(similar(A, typeof(default), length(I)), A, I, default)
get(A::AbstractArray, I::Range, default) = get!(similar(A, typeof(default), length(I)), A, I, default)

function get!{T}(X::AbstractArray{T}, A::AbstractArray, I::RangeVecIntList, default::T)
fill!(X, default)
Expand Down Expand Up @@ -823,7 +828,7 @@ function isequal(A::AbstractArray, B::AbstractArray)
if size(A) != size(B)
return false
end
if isa(A,Ranges) != isa(B,Ranges)
if isa(A,Range) != isa(B,Range)
return false
end
for i = 1:length(A)
Expand All @@ -847,7 +852,7 @@ function (==)(A::AbstractArray, B::AbstractArray)
if size(A) != size(B)
return false
end
if isa(A,Ranges) != isa(B,Ranges)
if isa(A,Range) != isa(B,Range)
return false
end
for i = 1:length(A)
Expand Down
24 changes: 12 additions & 12 deletions base/array.jl
Original file line number Diff line number Diff line change
Expand Up @@ -118,11 +118,11 @@ end
getindex(T::(Type...)) = Array(T,0)

# T[a:b] and T[a:s:b] also contruct typed ranges
function getindex{T<:Number}(::Type{T}, r::Ranges)
function getindex{T<:Number}(::Type{T}, r::Range)
copy!(Array(T,length(r)), r)
end

function getindex{T<:Number}(::Type{T}, r1::Ranges, rs::Ranges...)
function getindex{T<:Number}(::Type{T}, r1::Range, rs::Range...)
a = Array(T,length(r1)+sum(length,rs))
o = 1
copy!(a, o, r1)
Expand Down Expand Up @@ -248,8 +248,8 @@ getindex(A::Array, i0::Real, i1::Real, i2::Real, i3::Real, i4::Real, i5::Real)
getindex(A::Array, i0::Real, i1::Real, i2::Real, i3::Real, i4::Real, i5::Real, I::Real...) =
arrayref(A,to_index(i0),to_index(i1),to_index(i2),to_index(i3),to_index(i4),to_index(i5),to_index(I)...)

# Fast copy using copy! for Range1
function getindex(A::Array, I::Range1{Int})
# Fast copy using copy! for UnitRange
function getindex(A::Array, I::UnitRange{Int})
lI = length(I)
X = similar(A, lI)
if lI > 0
Expand All @@ -261,10 +261,10 @@ end
function getindex{T<:Real}(A::Array, I::AbstractVector{T})
return [ A[i] for i in to_index(I) ]
end
function getindex{T<:Real}(A::Ranges, I::AbstractVector{T})
function getindex{T<:Real}(A::Range, I::AbstractVector{T})
return [ A[i] for i in to_index(I) ]
end
function getindex(A::Ranges, I::AbstractVector{Bool})
function getindex(A::Range, I::AbstractVector{Bool})
checkbounds(A, I)
return [ A[i] for i in to_index(I) ]
end
Expand Down Expand Up @@ -316,7 +316,7 @@ function setindex!{T<:Real}(A::Array, x, I::AbstractVector{T})
return A
end

function setindex!{T}(A::Array{T}, X::Array{T}, I::Range1{Int})
function setindex!{T}(A::Array{T}, X::Array{T}, I::UnitRange{Int})
if length(X) != length(I)
throw_setindex_mismatch(X, (I,))
end
Expand Down Expand Up @@ -545,7 +545,7 @@ function deleteat!(a::Vector, i::Integer)
return _deleteat!(a, i, 1)
end

function deleteat!{T<:Integer}(a::Vector, r::Range1{T})
function deleteat!{T<:Integer}(a::Vector, r::UnitRange{T})
n = length(a)
f = first(r)
l = last(r)
Expand Down Expand Up @@ -599,7 +599,7 @@ function splice!(a::Vector, i::Integer, ins::AbstractArray=_default_splice)
return v
end

function splice!{T<:Integer}(a::Vector, r::Range1{T}, ins::AbstractArray=_default_splice)
function splice!{T<:Integer}(a::Vector, r::UnitRange{T}, ins::AbstractArray=_default_splice)
v = a[r]
m = length(ins)
if m == 0
Expand Down Expand Up @@ -716,7 +716,7 @@ for f in (:+, :-, :div, :mod, :&, :|, :$)
return F
end
# interaction with Ranges
function ($f){S,T<:Real}(A::StridedArray{S}, B::Ranges{T})
function ($f){S,T<:Real}(A::StridedArray{S}, B::Range{T})
F = similar(A, promote_type(S,T), promote_shape(size(A),size(B)))
i = 1
for b in B
Expand All @@ -725,7 +725,7 @@ for f in (:+, :-, :div, :mod, :&, :|, :$)
end
return F
end
function ($f){S<:Real,T}(A::Ranges{S}, B::StridedArray{T})
function ($f){S<:Real,T}(A::Range{S}, B::StridedArray{T})
F = similar(B, promote_type(S,T), promote_shape(size(A),size(B)))
i = 1
for a in A
Expand Down Expand Up @@ -1157,7 +1157,7 @@ function indexin{T}(a::AbstractArray{T}, b::AbstractArray{T})
end

# findin (the index of intersection)
function findin(a, b::Range1)
function findin(a, b::UnitRange)
ind = Array(Int, 0)
f = first(b)
l = last(b)
Expand Down
2 changes: 1 addition & 1 deletion base/ascii.jl
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ getindex(s::ASCIIString, i::Int) = (x=s.data[i]; x < 0x80 ? char(x) : '\ufffd')
sizeof(s::ASCIIString) = sizeof(s.data)

getindex(s::ASCIIString, r::Vector) = ASCIIString(getindex(s.data,r))
getindex(s::ASCIIString, r::Range1{Int}) = ASCIIString(getindex(s.data,r))
getindex(s::ASCIIString, r::UnitRange{Int}) = ASCIIString(getindex(s.data,r))
getindex(s::ASCIIString, indx::AbstractVector{Int}) = ASCIIString(s.data[indx])
search(s::ASCIIString, c::Char, i::Integer) = c < 0x80 ? search(s.data,uint8(c),i) : 0
rsearch(s::ASCIIString, c::Char, i::Integer) = c < 0x80 ? rsearch(s.data,uint8(c),i) : 0
Expand Down
6 changes: 3 additions & 3 deletions base/bitarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -707,7 +707,7 @@ function deleteat!(B::BitVector, i::Integer)
return _deleteat!(B, i)
end

function deleteat!(B::BitVector, r::Range1{Int})
function deleteat!(B::BitVector, r::UnitRange{Int})
n = length(B)
i_f = first(r)
i_l = last(r)
Expand Down Expand Up @@ -787,7 +787,7 @@ splice!(B::BitVector, i::Integer, ins::AbstractVector{Bool}) = splice!(B, i, bit

const _default_bit_splice = BitVector(0)

function splice!(B::BitVector, r::Range1{Int}, ins::BitVector = _default_bit_splice)
function splice!(B::BitVector, r::UnitRange{Int}, ins::BitVector = _default_bit_splice)
n = length(B)
i_f = first(r)
i_l = last(r)
Expand Down Expand Up @@ -829,7 +829,7 @@ function splice!(B::BitVector, r::Range1{Int}, ins::BitVector = _default_bit_spl

return v
end
splice!(B::BitVector, r::Range1{Int}, ins::AbstractVector{Bool}) = splice!(B, r, bitpack(ins))
splice!(B::BitVector, r::UnitRange{Int}, ins::AbstractVector{Bool}) = splice!(B, r, bitpack(ins))

function empty!(B::BitVector)
ccall(:jl_array_del_end, Void, (Any, Uint), B.chunks, length(B.chunks))
Expand Down
2 changes: 0 additions & 2 deletions base/char.jl
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ promote_rule(::Type{Char}, ::Type{Uint128}) = Uint128

# numeric operations
+(x::Char , y::Char ) = int(x)+int(y)
*(x::Char , y::Char ) = int(x)*int(y)
div(x::Char , y::Char ) = div(int(x),int(y))

# ordinal operations
+(x::Char , y::Integer) = char(int(x)+int(y))
Expand Down
4 changes: 2 additions & 2 deletions base/combinatorics.jl
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ immutable Combinations{T}
end

eltype(c::Combinations) = typeof(c.a)
eltype{T}(c::Combinations{Range1{T}}) = Array{T,1}
eltype{T}(c::Combinations{UnitRange{T}}) = Array{T,1}
eltype{T}(c::Combinations{Range{T}}) = Array{T,1}

length(c::Combinations) = binomial(length(c.a),c.t)
Expand Down Expand Up @@ -231,7 +231,7 @@ immutable Permutations{T}
end

eltype(c::Permutations) = typeof(c.a)
eltype{T}(c::Permutations{Range1{T}}) = Array{T,1}
eltype{T}(c::Permutations{UnitRange{T}}) = Array{T,1}
eltype{T}(c::Permutations{Range{T}}) = Array{T,1}

length(c::Permutations) = factorial(length(c.a))
Expand Down
2 changes: 1 addition & 1 deletion base/constants.jl
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ const golden = φ
for T in (MathConst, Rational, Integer, Number)
^(::MathConst{:e}, x::T) = exp(x)
end
for T in (Ranges, BitArray, SparseMatrixCSC, StridedArray, AbstractArray)
for T in (Range, BitArray, SparseMatrixCSC, StridedArray, AbstractArray)
.^(::MathConst{:e}, x::T) = exp(x)
end
^(::MathConst{:e}, x::AbstractMatrix) = expm(x)
Expand Down
14 changes: 7 additions & 7 deletions base/darray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ type DArray{T,N,A} <: AbstractArray{T,N}
pmap::Vector{Int}

# indexes held by piece i
indexes::Array{NTuple{N,Range1{Int}},N}
indexes::Array{NTuple{N,UnitRange{Int}},N}
# cuts[d][i] = first index of chunk i in dimension d
cuts::Vector{Vector{Int}}

Expand Down Expand Up @@ -104,7 +104,7 @@ end
function chunk_idxs(dims, chunks)
cuts = map(defaultdist, dims, chunks)
n = length(dims)
idxs = Array(NTuple{n,Range1{Int}},chunks...)
idxs = Array(NTuple{n,UnitRange{Int}},chunks...)
cartesianmap(tuple(chunks...)) do cidx...
idxs[cidx...] = ntuple(n, i->(cuts[i][cidx[i]]:cuts[i][cidx[i]+1]-1))
end
Expand Down Expand Up @@ -184,7 +184,7 @@ end
function convert{S,T,N}(::Type{Array{S,N}}, s::SubDArray{T,N})
I = s.indexes
d = s.parent
if isa(I,(Range1{Int}...)) && S<:T && T<:S
if isa(I,(UnitRange{Int}...)) && S<:T && T<:S
l = locate(d, map(first, I)...)
if isequal(d.indexes[l...], I)
# SubDArray corresponds to a chunk
Expand Down Expand Up @@ -243,14 +243,14 @@ function getindex{T}(d::DArray{T}, I::(Int...))
end

getindex(d::DArray) = d[1]
getindex(d::DArray, I::Union(Int,Range1{Int})...) = sub(d,I)
getindex(d::DArray, I::Union(Int,UnitRange{Int})...) = sub(d,I)

copy(d::SubOrDArray) = d

# local copies are obtained by convert(Array, ) or assigning from
# a SubDArray to a local Array.

function setindex!(a::Array, d::DArray, I::Range1{Int}...)
function setindex!(a::Array, d::DArray, I::UnitRange{Int}...)
n = length(I)
@sync begin
for i = 1:length(d.chunks)
Expand All @@ -261,7 +261,7 @@ function setindex!(a::Array, d::DArray, I::Range1{Int}...)
a
end

function setindex!(a::Array, s::SubDArray, I::Range1{Int}...)
function setindex!(a::Array, s::SubDArray, I::UnitRange{Int}...)
n = length(I)
d = s.parent
J = s.indexes
Expand Down Expand Up @@ -293,7 +293,7 @@ end
# to disambiguate
setindex!(a::Array{Any}, d::SubOrDArray, i::Int) = arrayset(a, d, i)

setindex!(a::Array, d::SubOrDArray, I::Union(Int,Range1{Int})...) =
setindex!(a::Array, d::SubOrDArray, I::Union(Int,UnitRange{Int})...) =
setindex!(a, d, [isa(i,Int) ? (i:i) : i for i in I ]...)

## higher-order functions ##
Expand Down
18 changes: 12 additions & 6 deletions base/deprecated.jl
Original file line number Diff line number Diff line change
Expand Up @@ -246,13 +246,13 @@ export ComplexPair

# superseded sorting API

@deprecate select(v::AbstractVector,k::Union(Int,Range1),o::Ordering) select(v,k,order=o)
@deprecate select(v::AbstractVector,k::Union(Int,Range1),f::Function) select(v,k,lt=f)
@deprecate select(f::Function,v::AbstractVector,k::Union(Int,Range1)) select(v,k,lt=f)
@deprecate select(v::AbstractVector,k::Union(Int,UnitRange),o::Ordering) select(v,k,order=o)
@deprecate select(v::AbstractVector,k::Union(Int,UnitRange),f::Function) select(v,k,lt=f)
@deprecate select(f::Function,v::AbstractVector,k::Union(Int,UnitRange)) select(v,k,lt=f)

# @deprecate select!(v::AbstractVector,k::Union(Int,Range1),o::Ordering) select!(v,k,order=o)
@deprecate select!(v::AbstractVector,k::Union(Int,Range1),f::Function) select!(v,k,lt=f)
@deprecate select!(f::Function,v::AbstractVector,k::Union(Int,Range1)) select!(v,k,lt=f)
# @deprecate select!(v::AbstractVector,k::Union(Int,UnitRange),o::Ordering) select!(v,k,order=o)
@deprecate select!(v::AbstractVector,k::Union(Int,UnitRange),f::Function) select!(v,k,lt=f)
@deprecate select!(f::Function,v::AbstractVector,k::Union(Int,UnitRange)) select!(v,k,lt=f)

@deprecate sort(v::AbstractVector,o::Ordering) sort(v,order=o)
@deprecate sort(v::AbstractVector,a::Algorithm) sort(v,alg=a)
Expand Down Expand Up @@ -388,6 +388,12 @@ const Stat = StatStruct
export CharString
const CharString = UTF32String

export Ranges
const Ranges = Range

export Range1
const Range1 = UnitRange

@deprecate set_rounding(r::RoundingMode) set_rounding(Float64,r)
@deprecate get_rounding() get_rounding(Float64)
@deprecate with_rounding(f::Function, r::RoundingMode) with_rounding(f::Function, Float64, r)
Expand Down
Loading

0 comments on commit c402ce9

Please sign in to comment.