Skip to content

Commit 1e7cd77

Browse files
authored
Add support for linear indexing (#19)
1 parent 5c52fbf commit 1e7cd77

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "Derive"
22
uuid = "a07dfc7f-7d04-4eb5-84cc-a97f051f655a"
33
authors = ["ITensor developers <[email protected]> and contributors"]
4-
version = "0.3.5"
4+
version = "0.3.6"
55

66
[deps]
77
Adapt = "79e6a3ab-5dfb-504d-930d-738a2a938a0e"

src/abstractarrayinterface.jl

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,27 @@ end
4444
return @interface interface getindex(a, Tuple(I)...)
4545
end
4646

47+
# Linear indexing.
48+
@interface interface ::AbstractArrayInterface function Base.getindex(
49+
a::AbstractArray, I::Int
50+
)
51+
return @interface interface getindex(a, CartesianIndices(a)[I])
52+
end
53+
4754
# TODO: Use `MethodError`?
4855
@interface ::AbstractArrayInterface function Base.setindex!(
4956
a::AbstractArray{<:Any,N}, value, I::Vararg{Int,N}
5057
) where {N}
5158
return error("Not implemented.")
5259
end
5360

61+
# Linear indexing.
62+
@interface interface ::AbstractArrayInterface function Base.setindex!(
63+
a::AbstractArray, value, I::Int
64+
)
65+
return @interface interface setindex!(a, value, CartesianIndices(a)[I])
66+
end
67+
5468
# TODO: Make this more general, use `Base.to_index`.
5569
@interface interface::AbstractArrayInterface function Base.setindex!(
5670
a::AbstractArray{<:Any,N}, value, I::CartesianIndex{N}

0 commit comments

Comments
 (0)