Skip to content

Commit 1b83965

Browse files
authored
More specific interfaces (#142)
1 parent f9b971b commit 1b83965

File tree

5 files changed

+39
-33
lines changed

5 files changed

+39
-33
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "BlockSparseArrays"
22
uuid = "2c9a651f-6452-4ace-a6ac-809f4280fbb4"
33
authors = ["ITensor developers <[email protected]> and contributors"]
4-
version = "0.7.7"
4+
version = "0.7.8"
55

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

src/BlockArraysExtensions/BlockArraysExtensions.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ Base.view(S::BlockIndices, i) = S[i]
111111
# @view b[Block(1, 1)[1:2, 2:2]]
112112
# ```
113113
# This is similar to the definition:
114-
# @interface BlockSparseArrayInterface() to_indices(a, inds, I::Tuple{UnitRange{<:Integer},Vararg{Any}})
114+
# @interface interface(a) to_indices(a, inds, I::Tuple{UnitRange{<:Integer},Vararg{Any}})
115115
function Base.getindex(
116116
a::NonBlockedVector{<:Integer,<:BlockIndices}, I::UnitRange{<:Integer}
117117
)

src/abstractblocksparsearray/views.jl

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -39,21 +39,21 @@ function Base.view(
3939
},
4040
I::Block{N},
4141
) where {N}
42-
return @interface BlockSparseArrayInterface() view(a, I)
42+
return @interface interface(a) view(a, I)
4343
end
4444
function Base.view(
4545
a::SubArray{
4646
<:Any,N,<:AnyAbstractBlockSparseArray,<:Tuple{Vararg{BlockSlice{<:BlockRange{1}},N}}
4747
},
4848
I::Vararg{Block{1},N},
4949
) where {N}
50-
return @interface BlockSparseArrayInterface() view(a, I...)
50+
return @interface interface(a) view(a, I...)
5151
end
5252
function Base.view(
5353
V::SubArray{<:Any,1,<:AnyAbstractBlockSparseArray,<:Tuple{BlockSlice{<:BlockRange{1}}}},
5454
I::Block{1},
5555
)
56-
return @interface BlockSparseArrayInterface() view(a, I)
56+
return @interface interface(a) view(a, I)
5757
end
5858

5959
# Specialized code for getting the view of a block.
@@ -63,7 +63,7 @@ function BlockArrays.viewblock(
6363
return viewblock(a, Tuple(block)...)
6464
end
6565

66-
# TODO: Define `@interface BlockSparseArrayInterface() viewblock`.
66+
# TODO: Define `@interface interface(a) viewblock`.
6767
function BlockArrays.viewblock(
6868
a::AbstractBlockSparseArray{<:Any,N}, block::Vararg{Block{1},N}
6969
) where {N}
@@ -192,9 +192,9 @@ end
192192

193193
# XXX: TODO: Distinguish if a sub-view of the block needs to be taken!
194194
# Define a new `SubBlockSlice` which is used in:
195-
# `@interface BlockSparseArrayInterface() to_indices(a, inds, I::Tuple{UnitRange{<:Integer},Vararg{Any}})`
195+
# `@interface interface(a) to_indices(a, inds, I::Tuple{UnitRange{<:Integer},Vararg{Any}})`
196196
# in `blocksparsearrayinterface/blocksparsearrayinterface.jl`.
197-
# TODO: Define `@interface BlockSparseArrayInterface() viewblock`.
197+
# TODO: Define `@interface interface(a) viewblock`.
198198
function BlockArrays.viewblock(
199199
a::SubArray{T,N,<:AbstractBlockSparseArray{T,N},<:Tuple{Vararg{BlockSliceCollection,N}}},
200200
block::Vararg{Block{1},N},
@@ -248,7 +248,7 @@ function BlockArrays.viewblock(
248248
return @view parent(a)[brs...]
249249
end
250250

251-
# TODO: Define `@interface BlockSparseArrayInterface() viewblock`.
251+
# TODO: Define `@interface interface() viewblock`.
252252
function BlockArrays.viewblock(
253253
a::SubArray{
254254
T,
@@ -295,7 +295,7 @@ end
295295
blockedslice_blocks(x::BlockSlice) = x.block
296296
blockedslice_blocks(x::BlockIndices) = x.blocks
297297

298-
# TODO: Define `@interface BlockSparseArrayInterface() viewblock`.
298+
# TODO: Define `@interface interface(a) viewblock`.
299299
function BlockArrays.viewblock(
300300
a::SubArray{
301301
T,N,<:AbstractBlockSparseArray{T,N},<:Tuple{Vararg{SubBlockSliceCollection,N}}
@@ -311,7 +311,7 @@ function BlockArrays.viewblock(
311311
end
312312
return @view parent(a)[brs...]
313313
end
314-
# TODO: Define `@interface BlockSparseArrayInterface() viewblock`.
314+
# TODO: Define `@interface interface(a) viewblock`.
315315
function BlockArrays.viewblock(
316316
a::SubArray{
317317
T,N,<:AbstractBlockSparseArray{T,N},<:Tuple{Vararg{SubBlockSliceCollection,N}}

src/abstractblocksparsearray/wrappedabstractblocksparsearray.jl

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -37,27 +37,27 @@ end
3737
function Base.to_indices(
3838
a::AnyAbstractBlockSparseArray, inds, I::Tuple{UnitRange{<:Integer},Vararg{Any}}
3939
)
40-
return @interface BlockSparseArrayInterface() to_indices(a, inds, I)
40+
return @interface interface(a) to_indices(a, inds, I)
4141
end
4242

4343
function Base.to_indices(
4444
a::AnyAbstractBlockSparseArray, inds, I::Tuple{AbstractArray{Bool},Vararg{Any}}
4545
)
46-
return @interface BlockSparseArrayInterface() to_indices(a, inds, I)
46+
return @interface interface(a) to_indices(a, inds, I)
4747
end
4848
# Fix ambiguity error with Base for logical indexing in Julia 1.10.
4949
# TODO: Delete this once we drop support for Julia 1.10.
5050
function Base.to_indices(
5151
a::AnyAbstractBlockSparseArray, inds, I::Union{Tuple{BitArray{N}},Tuple{Array{Bool,N}}}
5252
) where {N}
53-
return @interface BlockSparseArrayInterface() to_indices(a, inds, I)
53+
return @interface interface(a) to_indices(a, inds, I)
5454
end
5555

5656
# a[[Block(2), Block(1)], [Block(2), Block(1)]]
5757
function Base.to_indices(
5858
a::AnyAbstractBlockSparseArray, inds, I::Tuple{Vector{<:Block{1}},Vararg{Any}}
5959
)
60-
return @interface BlockSparseArrayInterface() to_indices(a, inds, I)
60+
return @interface interface(a) to_indices(a, inds, I)
6161
end
6262

6363
# a[BlockVector([Block(2), Block(1)], [2]), BlockVector([Block(2), Block(1)], [2])]
@@ -67,7 +67,7 @@ function Base.to_indices(
6767
inds,
6868
I::Tuple{AbstractBlockVector{<:Block{1}},Vararg{Any}},
6969
)
70-
return @interface BlockSparseArrayInterface() to_indices(a, inds, I)
70+
return @interface interface(a) to_indices(a, inds, I)
7171
end
7272

7373
# a[mortar([Block(1)[1:2], Block(2)[1:3]])]
@@ -76,7 +76,7 @@ function Base.to_indices(
7676
inds,
7777
I::Tuple{BlockVector{<:BlockIndex{1},<:Vector{<:BlockIndexRange{1}}},Vararg{Any}},
7878
)
79-
return @interface BlockSparseArrayInterface() to_indices(a, inds, I)
79+
return @interface interface(a) to_indices(a, inds, I)
8080
end
8181

8282
# a[[Block(1)[1:2], Block(2)[1:2]], [Block(1)[1:2], Block(2)[1:2]]]
@@ -88,15 +88,15 @@ end
8888

8989
# BlockArrays `AbstractBlockArray` interface
9090
function BlockArrays.blocks(a::AnyAbstractBlockSparseArray)
91-
@interface BlockSparseArrayInterface() blocks(a)
91+
@interface interface(a) blocks(a)
9292
end
9393

9494
# Fix ambiguity error with `BlockArrays`
9595
using BlockArrays: BlockSlice
9696
function BlockArrays.blocks(
9797
a::SubArray{<:Any,<:Any,<:AbstractBlockSparseArray,<:Tuple{Vararg{BlockSlice}}}
9898
)
99-
return @interface BlockSparseArrayInterface() blocks(a)
99+
return @interface interface(a) blocks(a)
100100
end
101101

102102
using TypeParameterAccessors: parenttype
@@ -129,7 +129,7 @@ function Base.getindex(a::AnyAbstractBlockSparseArray{<:Any,0})
129129
return ArrayLayouts.layout_getindex(a)
130130
end
131131

132-
# TODO: Define `@interface BlockSparseArrayInterface() isassigned`.
132+
# TODO: Define `@interface interface(a) isassigned`.
133133
function Base.isassigned(
134134
a::AnyAbstractBlockSparseArray{<:Any,N}, index::Vararg{Block{1},N}
135135
) where {N}
@@ -145,7 +145,7 @@ function Base.isassigned(a::AnyAbstractBlockSparseArray{<:Any,N}, index::Block{N
145145
return isassigned(a, Tuple(index)...)
146146
end
147147

148-
# TODO: Define `@interface BlockSparseArrayInterface() isassigned`.
148+
# TODO: Define `@interface interface(a) isassigned`.
149149
function Base.isassigned(
150150
a::AnyAbstractBlockSparseArray{<:Any,N}, index::Vararg{BlockIndex{1},N}
151151
) where {N}
@@ -157,13 +157,13 @@ function Base.setindex!(
157157
a::AnyAbstractBlockSparseArray{<:Any,N}, value, I::BlockIndex{N}
158158
) where {N}
159159
# TODO: Use `@interface interface(a) setindex!(...)`.
160-
@interface BlockSparseArrayInterface() setindex!(a, value, I)
160+
@interface interface(a) setindex!(a, value, I)
161161
return a
162162
end
163163
# Fixes ambiguity error with BlockArrays.jl
164164
function Base.setindex!(a::AnyAbstractBlockSparseArray{<:Any,1}, value, I::BlockIndex{1})
165165
# TODO: Use `@interface interface(a) setindex!(...)`.
166-
@interface BlockSparseArrayInterface() setindex!(a, value, I)
166+
@interface interface(a) setindex!(a, value, I)
167167
return a
168168
end
169169

@@ -269,29 +269,29 @@ end
269269
end
270270

271271
# Needed by `BlockArrays` matrix multiplication interface
272-
# TODO: Define a `@interface BlockSparseArrayInterface() similar` function.
272+
# TODO: Define a `@interface interface(a) similar` function.
273273
function Base.similar(
274274
arraytype::Type{<:AnyAbstractBlockSparseArray},
275275
elt::Type,
276276
axes::Tuple{Vararg{AbstractUnitRange{<:Integer}}},
277277
)
278-
return @interface BlockSparseArrayInterface() similar(arraytype, elt, axes)
278+
return @interface interface(arraytype) similar(arraytype, elt, axes)
279279
end
280280

281-
# TODO: Define a `@interface BlockSparseArrayInterface() similar` function.
281+
# TODO: Define a `@interface interface(a) similar` function.
282282
function Base.similar(
283283
a::AnyAbstractBlockSparseArray,
284284
elt::Type,
285285
axes::Tuple{Vararg{AbstractUnitRange{<:Integer}}},
286286
)
287287
# TODO: Use `@interface interface(a) similar(...)`.
288-
return @interface BlockSparseArrayInterface() similar(a, elt, axes)
288+
return @interface interface(a) similar(a, elt, axes)
289289
end
290290

291291
# Fixes ambiguity error.
292292
function Base.similar(a::AnyAbstractBlockSparseArray, elt::Type, axes::Tuple{})
293293
# TODO: Use `@interface interface(a) similar(...)`.
294-
return @interface BlockSparseArrayInterface() similar(a, elt, axes)
294+
return @interface interface(a) similar(a, elt, axes)
295295
end
296296

297297
# Fixes ambiguity error with `BlockArrays`.
@@ -303,7 +303,7 @@ function Base.similar(
303303
},
304304
)
305305
# TODO: Use `@interface interface(a) similar(...)`.
306-
return @interface BlockSparseArrayInterface() similar(a, elt, axes)
306+
return @interface interface(a) similar(a, elt, axes)
307307
end
308308

309309
# Fixes ambiguity error with `OffsetArrays`.
@@ -313,7 +313,7 @@ function Base.similar(
313313
axes::Tuple{AbstractUnitRange{<:Integer},Vararg{AbstractUnitRange{<:Integer}}},
314314
)
315315
# TODO: Use `@interface interface(a) similar(...)`.
316-
return @interface BlockSparseArrayInterface() similar(a, elt, axes)
316+
return @interface interface(a) similar(a, elt, axes)
317317
end
318318

319319
# Fixes ambiguity error with `BlockArrays`.
@@ -323,7 +323,7 @@ function Base.similar(
323323
axes::Tuple{AbstractBlockedUnitRange{<:Integer},Vararg{AbstractUnitRange{<:Integer}}},
324324
)
325325
# TODO: Use `@interface interface(a) similar(...)`.
326-
return @interface BlockSparseArrayInterface() similar(a, elt, axes)
326+
return @interface interface(a) similar(a, elt, axes)
327327
end
328328

329329
# Fixes ambiguity errors with BlockArrays.
@@ -337,15 +337,15 @@ function Base.similar(
337337
},
338338
)
339339
# TODO: Use `@interface interface(a) similar(...)`.
340-
return @interface BlockSparseArrayInterface() similar(a, elt, axes)
340+
return @interface interface(a) similar(a, elt, axes)
341341
end
342342

343343
# Fixes ambiguity error with `StaticArrays`.
344344
function Base.similar(
345345
a::AnyAbstractBlockSparseArray, elt::Type, axes::Tuple{Base.OneTo,Vararg{Base.OneTo}}
346346
)
347347
# TODO: Use `@interface interface(a) similar(...)`.
348-
return @interface BlockSparseArrayInterface() similar(a, elt, axes)
348+
return @interface interface(a) similar(a, elt, axes)
349349
end
350350

351351
struct BlockType{T} end

src/blocksparsearray/blocksparsearray.jl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,12 @@ Base.axes(a::BlockSparseArray) = a.axes
259259
@interface ::AbstractBlockSparseArrayInterface BlockArrays.blocks(a::BlockSparseArray) =
260260
a.blocks
261261

262+
function blocktype(
263+
arraytype::Type{<:BlockSparseArray{T,N,A}}
264+
) where {T,N,A<:AbstractArray{T,N}}
265+
return A
266+
end
267+
262268
# TODO: Use `TypeParameterAccessors`.
263269
function blockstype(
264270
arraytype::Type{<:BlockSparseArray{T,N,A,Blocks}}

0 commit comments

Comments
 (0)