From 07c4e8a4254dc0acbda57c6f29a5096f0df149ce Mon Sep 17 00:00:00 2001 From: Tim Holy Date: Fri, 17 Feb 2017 04:56:44 -0600 Subject: [PATCH] Support IndexStyle, IndexLinear, IndexCartesian --- README.md | 2 ++ src/Compat.jl | 8 ++++++++ test/runtests.jl | 14 ++++++++++++++ 3 files changed, 24 insertions(+) diff --git a/README.md b/README.md index 3404cafa6..77eac58c9 100644 --- a/README.md +++ b/README.md @@ -101,6 +101,8 @@ Currently, the `@compat` macro supports the following syntaxes: * `bytestring` has been replaced in most cases with additional `String` construction methods; for 0.4 compatibility, the usage involves replacing `bytestring(args...)` with `Compat.String(args...)`. However, for converting a `Ptr{UInt8}` to a string, use the new `unsafe_string(...)` method to make a copy or `unsafe_wrap(String, ...)` to avoid a copy. +* In 0.6, older non-exported indexing traits were renamed and exported as `IndexStyle`, `IndexLinear`, and `IndexCartesian`. Any code specializing `Base.linearindexing(::Type{T})` should switch to `Compat.IndexStyle(::Type{T})`. + ## New functions, macros, and methods * `@views` takes an expression and converts all slices to views ([#20164]), while diff --git a/src/Compat.jl b/src/Compat.jl index 9a512ace4..7cf177a51 100644 --- a/src/Compat.jl +++ b/src/Compat.jl @@ -1384,6 +1384,14 @@ else using Base: Iterators end +if !isdefined(:IndexStyle) + 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) +end + include("to-be-deprecated.jl") end # module Compat diff --git a/test/runtests.jl b/test/runtests.jl index 0f6015794..6a2e6b29c 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -1745,4 +1745,18 @@ 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 Compat.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() + include("to-be-deprecated.jl")