Skip to content

Commit

Permalink
nearly finish OrdinalRange
Browse files Browse the repository at this point in the history
still not clear what to do with floating point in StepRange
  • Loading branch information
JeffBezanson committed Apr 1, 2014
1 parent 5ba25ea commit e733cf1
Show file tree
Hide file tree
Showing 15 changed files with 179 additions and 181 deletions.
16 changes: 8 additions & 8 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 @@ -321,7 +321,7 @@ 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::StepRange) = StepRange($fn(r.start), $fn(r.step), $fn(last(r)))
$fn(r::Range1) = Range1($fn(r.start), $fn(last(r)))
$fn(r::FloatRange) = FloatRange($fn(r.start), $fn(r.step), r.len, $fn(r.divisor))
end
Expand Down Expand Up @@ -451,21 +451,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{Range1{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 +823,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 +847,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
12 changes: 6 additions & 6 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 @@ -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 @@ -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
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
3 changes: 2 additions & 1 deletion base/exports.jl
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ export
Matrix,
MergeSort,
ObjectIdDict,
OrdinalRange,
PollingFileWatcher,
ProcessGroup,
QR,
Expand All @@ -78,7 +79,6 @@ export
Range,
Range1,
RangeIndex,
Ranges,
Rational,
Regex,
RegexMatch,
Expand All @@ -97,6 +97,7 @@ export
SharedArray,
SparseMatrixCSC,
StatStruct,
StepRange,
StridedArray,
StridedMatrix,
StridedVecOrMat,
Expand Down
4 changes: 2 additions & 2 deletions base/linalg/dense.jl
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,9 @@ end

function diagind(m::Integer, n::Integer, k::Integer=0)
if 0 < k < n
return Range(k*m+1,m+1,min(m,n-k))
return (k*m+1):(m+1):(k*m + min(m,n-k))
elseif 0 <= -k <= m
return Range(1-k,m+1,min(m+k,n))
return (1-l):(m+1):(-k + min(m+k,n))
end
throw(BoundsError())
end
Expand Down
4 changes: 2 additions & 2 deletions base/linalg/tridiag.jl
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ end
# Allocation-free variants
# Note that solve is non-aliasing, so you can use the same array for
# input and output
function solve!{T<:BlasFloat}(x::AbstractArray{T}, xrng::Ranges{Int}, M::Tridiagonal{T}, rhs::AbstractArray{T}, rhsrng::Ranges{Int})
function solve!{T<:BlasFloat}(x::AbstractArray{T}, xrng::Range{Int}, M::Tridiagonal{T}, rhs::AbstractArray{T}, rhsrng::Range{Int})
d = M.d
N = length(d)
if length(xrng) != N || length(rhsrng) != N
Expand Down Expand Up @@ -319,7 +319,7 @@ end
\(M::Tridiagonal, rhs::StridedVecOrMat) = solve(M, rhs)

# Tridiagonal multiplication
function mult(x::AbstractArray, xrng::Ranges{Int}, M::Tridiagonal, v::AbstractArray, vrng::Ranges{Int})
function mult(x::AbstractArray, xrng::Range{Int}, M::Tridiagonal, v::AbstractArray, vrng::Range{Int})
dl = M.dl
d = M.d
du = M.du
Expand Down
2 changes: 1 addition & 1 deletion base/linalg/woodbury.jl
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ det(W::Woodbury)=det(W.A)*det(W.C)/det(W.Cp)

# Allocation-free solver for arbitrary strides (requires that W.A has a
# non-aliasing "solve" routine, e.g., is Tridiagonal)
function solve!(x::AbstractArray, xrng::Ranges{Int}, W::Woodbury, rhs::AbstractArray, rhsrng::Ranges{Int})
function solve!(x::AbstractArray, xrng::Range{Int}, W::Woodbury, rhs::AbstractArray, rhsrng::Range{Int})
solve!(W.tmpN1, 1:length(W.tmpN1), W.A, rhs, rhsrng)
A_mul_B!(W.tmpk1, W.V, W.tmpN1)
A_mul_B!(W.tmpk2, W.Cp, W.tmpk1)
Expand Down
4 changes: 1 addition & 3 deletions base/multidimensional.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@
nothing
end


unsafe_getindex(v::Real, ind::Int) = v
unsafe_getindex(v::Ranges, ind::Int) = first(v) + (ind-1)*step(v)
unsafe_getindex(v::Range, ind::Int) = first(v) + (ind-1)*step(v)
unsafe_getindex(v::BitArray, ind::Int) = Base.unsafe_bitgetindex(v.chunks, ind)
unsafe_getindex(v::AbstractArray, ind::Int) = v[ind]
unsafe_getindex(v, ind::Real) = unsafe_getindex(v, to_index(ind))
Expand All @@ -16,7 +15,6 @@ unsafe_setindex!{T}(v::AbstractArray{T}, x::T, ind::Int) = (v[ind] = x; v)
unsafe_setindex!(v::BitArray, x::Bool, ind::Int) = (Base.unsafe_bitsetindex!(v.chunks, x, ind); v)
unsafe_setindex!{T}(v::AbstractArray{T}, x::T, ind::Real) = unsafe_setindex!(v, x, to_index(ind))


# Version that uses cartesian indexing for src
@ngenerate N typeof(dest) function _getindex!(dest::Array, src::AbstractArray, I::NTuple{N,Union(Int,AbstractVector)}...)
checksize(dest, I...)
Expand Down
8 changes: 4 additions & 4 deletions base/random.jl
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ function rand{T<:Integer,U<:Unsigned}(g::RandIntGen{T,U})
end

rand{T<:Union(Signed,Unsigned,Bool,Char)}(r::Range1{T}) = rand(RandIntGen(r))
rand{T<:Real}(r::Ranges{T}) = convert(T, first(r) + rand(0:(length(r)-1)) * step(r))
rand{T<:Real}(r::Range{T}) = convert(T, first(r) + rand(0:(length(r)-1)) * step(r))

function rand!(g::RandIntGen, A::AbstractArray)
for i = 1 : length(A)
Expand All @@ -179,7 +179,7 @@ end

rand!{T<:Union(Signed,Unsigned,Bool,Char)}(r::Range1{T}, A::AbstractArray) = rand!(RandIntGen(r), A)

function rand!{T<:Real}(r::Ranges{T}, A::AbstractArray)
function rand!{T<:Real}(r::Range{T}, A::AbstractArray)
g = RandIntGen(0:(length(r)-1))
f = first(r)
s = step(r)
Expand All @@ -195,8 +195,8 @@ function rand!{T<:Real}(r::Ranges{T}, A::AbstractArray)
return A
end

rand{T<:Real}(r::Ranges{T}, dims::Dims) = rand!(r, Array(T, dims))
rand(r::Ranges, dims::Int...) = rand(r, dims)
rand{T<:Real}(r::Range{T}, dims::Dims) = rand!(r, Array(T, dims))
rand(r::Range, dims::Int...) = rand(r, dims)


## random Bools
Expand Down
Loading

0 comments on commit e733cf1

Please sign in to comment.