Skip to content

Commit

Permalink
Merge pull request #329 from JuliaLang/teh/IndexStyle
Browse files Browse the repository at this point in the history
Support IndexStyle, IndexLinear, IndexCartesian
  • Loading branch information
andreasnoack authored Feb 18, 2017
2 parents fa0053b + d0a74f7 commit 9a95537
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 0 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ Currently, the `@compat` macro supports the following syntaxes:
* `@compat A{T} = B{T}` or `@compat const A{T} = B{T}` to declare type alias with free
parameters. [#20500]. Use `const A = B` for type alias without free parameters.

* `@compat Base.IndexStyle(::Type{<:MyArray}) = IndexLinear()` and `@compat Base.IndexStyle(::Type{<:MyArray}) = IndexCartesian()` to define traits for abstract arrays, replacing the former `Base.linearindexing{T<:MyArray}(::Type{T}) = Base.LinearFast()` and `Base.linearindexing{T<:MyArray}(::Type{T}) = Base.LinearSlow()`, respectively.

## Module Aliases

* In 0.6, some 0.5 iterator functions have been moved to the `Base.Iterators`
Expand Down
27 changes: 27 additions & 0 deletions src/Compat.jl
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,12 @@ function is_quote_symbol(ex::ANY, val::Symbol)
return false
end

is_index_style(ex::Expr) = ex == :(Compat.IndexStyle) || ex == :(Base.IndexStyle) ||
(ex.head == :(.) && (ex.args[1] == :Compat || ex.args[1] == :Base) &&
ex.args[2] == Expr(:quote, :IndexStyle))

is_index_style(arg) = false

# rewrites accesses to IOContext dicts
function rewrite_iocontext!(expr::Expr)
args = expr.args
Expand Down Expand Up @@ -463,6 +469,18 @@ function _compat(ex::Expr)
return _compat(Expr(:comparison, ex.args[1], :.=, Expr(:call, Symbol(shead[1:end-1]), ex.args...)))
end
end
if VERSION < v"0.6.0-dev.2840"
if ex.head == :(=) && isa(ex.args[1], Expr) && ex.args[1].head == :call
a = ex.args[1].args[1]
if is_index_style(a)
ex.args[1].args[1] = :(Base.linearindexing)
elseif isa(a, Expr) && a.head == :curly
if is_index_style(a.args[1])
ex.args[1].args[1].args[1] = :(Base.linearindexing)
end
end
end
end
return Expr(ex.head, map(_compat, ex.args)...)
end
function _compat(ex::Symbol)
Expand Down Expand Up @@ -1384,6 +1402,15 @@ else
using Base: Iterators
end

if VERSION < v"0.6.0-dev.2840"
export IndexStyle, IndexLinear, IndexCartesian
eval(Expr(:typealias, :IndexStyle, :(Base.LinearIndexing)))
eval(Expr(:typealias, :IndexLinear, :(Base.LinearFast)))
eval(Expr(:typealias, :IndexCartesian, :(Base.LinearSlow)))
IndexStyle{T}(::Type{T}) = Base.linearindexing(T)
IndexStyle(args...) = Base.linearindexing(args...)
end

include("to-be-deprecated.jl")

end # module Compat
18 changes: 18 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1745,4 +1745,22 @@ f20500_2() = A20500_2
@inferred f20500()
@inferred f20500_2()

module CompatArray
using Compat
immutable CartesianArray{T,N} <: AbstractArray{T,N}
parent::Array{T,N}
end
immutable LinearArray{T,N} <: AbstractArray{T,N}
parent::Array{T,N}
end
@compat Base.IndexStyle(::Type{<:LinearArray}) = IndexLinear()
end
@test IndexStyle(Array{Float32,2}) === IndexLinear()
@test IndexStyle(CompatArray.CartesianArray{Float32,2}) === IndexCartesian()
@test IndexStyle(CompatArray.LinearArray{Float32,2}) === IndexLinear()
let a = CompatArray.CartesianArray(rand(2,3)), b = CompatArray.LinearArray(rand(2,3))
@test IndexStyle(a) === IndexCartesian()
@test IndexStyle(b) === IndexLinear()
end

include("to-be-deprecated.jl")

0 comments on commit 9a95537

Please sign in to comment.