Skip to content

Commit 3455bb2

Browse files
authored
Replace @wrappedtype with a more comprehensive @array_aliases (#4)
1 parent 8879d8a commit 3455bb2

File tree

5 files changed

+72
-14
lines changed

5 files changed

+72
-14
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.1.0"
4+
version = "0.2.0"
55

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

README.md

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ julia> Pkg.add("Derive")
4747
## Examples
4848

4949
````julia
50-
using Derive: Derive, @derive, @interface, interface
50+
using Derive: Derive, @array_aliases, @derive, @interface, interface
5151
using Test: @test
5252
````
5353

@@ -109,12 +109,18 @@ function setunstoredindex!(a::SparseArrayDOK, value, I::Int...)
109109
end
110110
````
111111

112-
Speficy the interface the type adheres to.
112+
Specify the interface the type adheres to.
113113

114114
````julia
115115
Derive.interface(::Type{<:SparseArrayDOK}) = SparseArrayInterface()
116116
````
117117

118+
Define aliases like `SparseMatrixDOK`, `AnySparseArrayDOK`, etc.
119+
120+
````julia
121+
@array_aliases SparseArrayDOK
122+
````
123+
118124
Derive the interface for the type.
119125

120126
````julia
@@ -129,6 +135,9 @@ a[1, 1] = 2
129135
@test a[2, 1] == 0
130136
@test a[1, 2] == 0
131137
@test a[2, 2] == 0
138+
139+
@test a isa SparseMatrixDOK
140+
@test a' isa AnySparseMatrixDOK
132141
````
133142

134143
Call the sparse array interface on a dense array.

examples/README.jl

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ julia> Pkg.add("Derive")
5252

5353
# ## Examples
5454

55-
using Derive: Derive, @derive, @interface, interface
55+
using Derive: Derive, @array_aliases, @derive, @interface, interface
5656
using Test: @test
5757

5858
# Define an interface.
@@ -104,9 +104,12 @@ function setunstoredindex!(a::SparseArrayDOK, value, I::Int...)
104104
return a
105105
end
106106

107-
# Speficy the interface the type adheres to.
107+
# Specify the interface the type adheres to.
108108
Derive.interface(::Type{<:SparseArrayDOK}) = SparseArrayInterface()
109109

110+
# Define aliases like `SparseMatrixDOK`, `AnySparseArrayDOK`, etc.
111+
@array_aliases SparseArrayDOK
112+
110113
# Derive the interface for the type.
111114
@derive (T=SparseArrayDOK,) begin
112115
Base.getindex(::T, ::Int...)
@@ -120,6 +123,9 @@ a[1, 1] = 2
120123
@test a[1, 2] == 0
121124
@test a[2, 2] == 0
122125

126+
@test a isa SparseMatrixDOK
127+
@test a' isa AnySparseMatrixDOK
128+
123129
# Call the sparse array interface on a dense array.
124130
isstored(a::AbstractArray, I::Int...) = true
125131
getstoredindex(a::AbstractArray, I::Int...) = getindex(a, I...)

src/abstractinterface.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ interface(::Type) = error("Interface unknown.")
88
function combine_interfaces(x1, x2, x_rest...)
99
return combine_interfaces(combine_interfaces(x1, x2), x_rest...)
1010
end
11-
combine_interfaces(x1, x2) = comine_interface_rule(interface(x1), interface(x2))
11+
combine_interfaces(x1, x2) = combine_interface_rule(interface(x1), interface(x2))
1212
combine_interfaces(x) = interface(x)
1313

1414
# Rules for combining interfaces.

src/wrappedarrays.jl

Lines changed: 51 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,57 @@
1-
using Adapt: WrappedArray
1+
function symbol_replace(symbol::Symbol, replacement::Pair{Symbol,Symbol})
2+
return Symbol(replace(String(symbol), String(replacement[1]) => String(replacement[2])))
3+
end
4+
function symbol_cat(symbol1::Symbol, symbol2::Symbol)
5+
return Symbol(symbol1, symbol2)
6+
end
7+
8+
vectype(type::Symbol) = symbol_replace(type, :Array => :Vector)
9+
mattype(type::Symbol) = symbol_replace(type, :Array => :Matrix)
10+
vecormattype(type::Symbol) = symbol_replace(type, :Array => :VecOrMat)
11+
anytype(type::Symbol) = symbol_cat(:Any, type)
12+
wrappedtype(type::Symbol) = symbol_cat(:Wrapped, type)
13+
14+
macro vecmat_aliases(type)
15+
return esc(vecmat_aliases(type))
16+
end
17+
18+
function vecmat_aliases(type::Symbol)
19+
return quote
20+
const $(vectype(type)){T} = $type{T,1}
21+
const $(mattype(type)){T} = $type{T,2}
22+
const $(vecormattype(type)){T} = Union{$(vectype(type)){T},$(mattype(type)){T}}
23+
end
24+
end
225

3-
macro wrappedtype(type)
4-
return esc(wrappedtype(type))
26+
using Adapt: Adapt, WrappedArray
27+
28+
# TODO: Define for types that are hardcoded to vector or matrix,
29+
# i.e. `Adjoint`, Transpose`, `Diagonal`, etc. Maybe call it
30+
# `wrapped_vec_aliases` and `wrapped_mat_aliases`.
31+
macro wrapped_aliases(type)
32+
return esc(wrapped_aliases(type))
533
end
634

7-
function wrappedtype(type::Symbol)
8-
wrappedtype = Symbol(:Wrapped, type)
9-
anytype = Symbol(:Any, type)
35+
function wrapped_aliases(type::Symbol)
1036
return quote
11-
const $wrappedtype{T,N} = $WrappedArray{T,N,$type,$type{T,N}}
12-
const $anytype{T,N} = Union{$type{T,N},$wrappedtype{T,N}}
37+
const $(wrappedtype(type)){T,N} = $(GlobalRef(Adapt, :WrappedArray)){
38+
T,N,$type,$type{T,N}
39+
}
40+
const $(anytype(type)){T,N} = Union{$type{T,N},$(wrappedtype(type)){T,N}}
1341
end
1442
end
43+
44+
macro array_aliases(type)
45+
return esc(array_aliases(type))
46+
end
47+
48+
function array_aliases(type::Symbol)
49+
# TODO: I tried to implement this by using `quote` and calling
50+
# out to the macros but I couldn't get it to work with `GlobalRef`.
51+
return Expr(
52+
:block,
53+
vecmat_aliases(type).args...,
54+
wrapped_aliases(type).args...,
55+
vecmat_aliases(anytype(type)).args...,
56+
)
57+
end

0 commit comments

Comments
 (0)