From 51dc670d0715d1812db1dd88bce52c1f2037df25 Mon Sep 17 00:00:00 2001 From: mtfishman Date: Fri, 20 Dec 2024 18:07:43 -0500 Subject: [PATCH] Add support for linear indexing --- Project.toml | 2 +- src/abstractarrayinterface.jl | 14 ++++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/Project.toml b/Project.toml index b009e02..a4ff79e 100644 --- a/Project.toml +++ b/Project.toml @@ -1,7 +1,7 @@ name = "Derive" uuid = "a07dfc7f-7d04-4eb5-84cc-a97f051f655a" authors = ["ITensor developers and contributors"] -version = "0.3.5" +version = "0.3.6" [deps] Adapt = "79e6a3ab-5dfb-504d-930d-738a2a938a0e" diff --git a/src/abstractarrayinterface.jl b/src/abstractarrayinterface.jl index 485f6ab..72a790e 100644 --- a/src/abstractarrayinterface.jl +++ b/src/abstractarrayinterface.jl @@ -44,6 +44,13 @@ end return @interface interface getindex(a, Tuple(I)...) end +# Linear indexing. +@interface interface ::AbstractArrayInterface function Base.getindex( + a::AbstractArray, I::Int +) + return @interface interface getindex(a, CartesianIndices(a)[I]) +end + # TODO: Use `MethodError`? @interface ::AbstractArrayInterface function Base.setindex!( a::AbstractArray{<:Any,N}, value, I::Vararg{Int,N} @@ -51,6 +58,13 @@ end return error("Not implemented.") end +# Linear indexing. +@interface interface ::AbstractArrayInterface function Base.setindex!( + a::AbstractArray, value, I::Int +) + return @interface interface setindex!(a, value, CartesianIndices(a)[I]) +end + # TODO: Make this more general, use `Base.to_index`. @interface interface::AbstractArrayInterface function Base.setindex!( a::AbstractArray{<:Any,N}, value, I::CartesianIndex{N}