Skip to content

Commit

Permalink
Add support for linear indexing (#19)
Browse files Browse the repository at this point in the history
  • Loading branch information
mtfishman authored Dec 20, 2024
1 parent 5c52fbf commit 1e7cd77
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "Derive"
uuid = "a07dfc7f-7d04-4eb5-84cc-a97f051f655a"
authors = ["ITensor developers <[email protected]> and contributors"]
version = "0.3.5"
version = "0.3.6"

[deps]
Adapt = "79e6a3ab-5dfb-504d-930d-738a2a938a0e"
Expand Down
14 changes: 14 additions & 0 deletions src/abstractarrayinterface.jl
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,27 @@ 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}
) where {N}
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}
Expand Down

0 comments on commit 1e7cd77

Please sign in to comment.