1
- macro implement_array_methods (t)
2
- t = esc (t)
3
- quote
4
- Base. Array (a:: $t ) = $ _Array (a)
5
- Base. collect (a:: $t ) = $ _Array (a)
6
- Base. copyto! (dest:: $t , source:: AbstractArray ) = $ _copyto! (dest, source)
7
- Base. copyto! (dest:: AbstractArray , source:: $t ) = $ _copyto! (dest, source)
8
- Base. copyto! (dest:: $t , source:: $t ) = $ _copyto! (dest, source)
9
- function Base. copyto! (
10
- dest:: $t , Rdest:: CartesianIndices , src:: AbstractArray , Rsrc:: CartesianIndices
11
- )
12
- return $ _copyto! (dest, Rdest, src, Rsrc)
13
- end
14
- function Base. copyto! (
15
- dest:: AbstractArray , Rdest:: CartesianIndices , src:: $t , Rsrc:: CartesianIndices
16
- )
17
- return $ _copyto! (dest, Rdest, src, Rsrc)
18
- end
19
- function Base. copyto! (
20
- dest:: $t , Rdest:: CartesianIndices , src:: $t , Rsrc:: CartesianIndices
21
- )
22
- return $ _copyto! (dest, Rdest, src, Rsrc)
23
- end
24
- # For ambiguity
25
- Base. copyto! (dest:: PermutedDimsArray , src:: $t ) = DiskArrays. _copyto! (dest, src)
26
- function Base. copyto! (dest:: PermutedDimsArray{T,N} , src:: $t{T,N} ) where {T,N}
27
- return $ _copyto! (dest, src)
28
- end
29
-
30
- Base. reverse (a:: $t ; dims= :) = $ _reverse (a, dims)
31
- Base. reverse (a:: $t{<:Any,1} ) = $ _reverse1 (a)
32
- Base. reverse (a:: $t{<:Any,1} , start:: Integer , stop:: Integer = lastindex (a)) = $ _reverse1 (a, start, stop)
33
-
34
- # Here we extend the unexported `_replace` method, but we replicate
35
- # much less Base functionality by extending it rather than `replace`.
36
- function Base. _replace! (new:: Base.Callable , res:: AbstractArray , A:: $t , count:: Int )
37
- return $ _replace! (new, res, A, count)
38
- end
39
- function Base. _replace! (new:: Base.Callable , res:: $t , A:: AbstractArray , count:: Int )
40
- return $ _replace! (new, res, A, count)
41
- end
42
- function Base. _replace! (new:: Base.Callable , res:: $t , A:: $t , count:: Int )
43
- return $ _replace! (new, res, A, count)
44
- end
45
- end
46
- end
47
-
48
1
# Use broadcast to copy to a new Array
49
- _Array (a:: AbstractArray{T,N} ) where {T,N} = a[ntuple (_ -> :, Val {N} ())... ]
50
- _Array (a:: AbstractArray{T,0} ) where {T} = fill (a[])
2
+ _disk_collect (a:: AbstractArray{T,N} ) where {T,N} = a[ntuple (_ -> :, Val {N} ())... ]
3
+ _disk_collect (a:: AbstractArray{T,0} ) where {T} = fill (a[])
51
4
52
5
# Use broadcast to copy
53
- function _copyto ! (dest:: AbstractArray{<:Any,N} , source:: AbstractArray{<:Any,N} ) where {N}
6
+ function _disk_copyto ! (dest:: AbstractArray{<:Any,N} , source:: AbstractArray{<:Any,N} ) where {N}
54
7
return dest .= source
55
8
end
56
- function _copyto ! (dest:: AbstractArray , source:: AbstractArray )
9
+ function _disk_copyto ! (dest:: AbstractArray , source:: AbstractArray )
57
10
# TODO make this more specific so we are reshaping the Non-DiskArray more often.
58
11
reshape (dest, size (source)) .= source
59
12
return dest
60
13
end
61
-
62
- function _copyto! (dest, Rdest, src, Rsrc)
14
+ function _disk_copyto! (dest, Rdest, src, Rsrc)
63
15
if size (Rdest) != size (Rsrc)
64
16
throw (ArgumentError (" source and destination must have same size (got $(size (Rsrc)) and $(size (Rdest)) )" ))
65
17
end
@@ -70,26 +22,77 @@ function _copyto!(dest, Rdest, src, Rsrc)
70
22
end
71
23
view (dest, Rdest) .= view (src, Rsrc)
72
24
end
25
+
73
26
# Use a view for lazy reverse
74
- _reverse (a, :: Colon ) = _reverse (a, ntuple (identity, ndims (a)))
75
- _reverse (a, dims:: Int ) = _reverse (a, (dims,))
76
- function _reverse (A, dims:: Tuple )
27
+ _disk_reverse (a, :: Colon ) = _disk_reverse (a, ntuple (identity, ndims (a)))
28
+ _disk_reverse (a, dims:: Int ) = _disk_reverse (a, (dims,))
29
+ function _disk_reverse (A, dims:: Tuple )
77
30
rev_axes = map (ntuple (identity, ndims (A)), axes (A)) do d, a
78
31
ax = StepRange (a)
79
32
d in dims ? reverse (ax) : ax
80
33
end
81
34
return view (A, rev_axes... )
82
35
end
83
- _reverse1 (a) = _reverse (a, 1 )
84
- function _reverse1 (a, start:: Int , stop:: Int )
36
+
37
+ _disk_reverse1 (a) = _disk_reverse (a, 1 )
38
+ function _disk_reverse1 (a, start:: Int , stop:: Int )
85
39
inds = [firstindex (a): start- 1 ; stop: - 1 : start; stop+ 1 : lastindex (a)]
86
40
return view (a, inds)
87
41
end
88
42
89
43
# Use broadcast instead of a loop.
90
44
# The `count` argument is disallowed as broadcast is not sequential.
91
- function _replace ! (new, res:: AbstractArray , A:: AbstractArray , count:: Int )
45
+ function _disk_replace ! (new, res:: AbstractArray , A:: AbstractArray , count:: Int )
92
46
count < length (res) &&
93
47
throw (ArgumentError (" `replace` on DiskArrays objects cannot use a count value" ))
94
48
return broadcast! (new, res, A)
95
49
end
50
+
51
+ macro implement_array_methods (t)
52
+ t = esc (t)
53
+ quote
54
+ Base. Array (a:: $t ) = $ _disk_collect (a)
55
+ Base. collect (a:: $t ) = $ _disk_collect (a)
56
+ Base. copyto! (dest:: $t , source:: AbstractArray ) = $ _disk_copyto! (dest, source)
57
+ Base. copyto! (dest:: AbstractArray , source:: $t ) = $ _disk_copyto! (dest, source)
58
+ Base. copyto! (dest:: $t , source:: $t ) = $ _disk_copyto! (dest, source)
59
+ function Base. copyto! (
60
+ dest:: $t , Rdest:: CartesianIndices , src:: AbstractArray , Rsrc:: CartesianIndices
61
+ )
62
+ return $ _disk_copyto! (dest, Rdest, src, Rsrc)
63
+ end
64
+ function Base. copyto! (
65
+ dest:: AbstractArray , Rdest:: CartesianIndices , src:: $t , Rsrc:: CartesianIndices
66
+ )
67
+ return $ _disk_copyto! (dest, Rdest, src, Rsrc)
68
+ end
69
+ function Base. copyto! (
70
+ dest:: $t , Rdest:: CartesianIndices , src:: $t , Rsrc:: CartesianIndices
71
+ )
72
+ return $ _disk_copyto! (dest, Rdest, src, Rsrc)
73
+ end
74
+ # For ambiguity
75
+ Base. copyto! (dest:: PermutedDimsArray , src:: $t ) = DiskArrays. _copyto! (dest, src)
76
+ function Base. copyto! (dest:: PermutedDimsArray{T,N} , src:: $t{T,N} ) where {T,N}
77
+ return $ _disk_copyto! (dest, src)
78
+ end
79
+
80
+ Base. reverse (a:: $t ; dims= :) = $ _disk_reverse (a, dims)
81
+ Base. reverse (a:: $t{<:Any,1} ) = $ _disk_reverse1 (a)
82
+ Base. reverse (a:: $t{<:Any,1} , start:: Integer , stop:: Integer = lastindex (a)) =
83
+ $ _disk_reverse1 (a, start, stop)
84
+
85
+ # Here we extend the unexported `_replace` method, but we replicate
86
+ # much less Base functionality by extending it rather than `replace`.
87
+ function Base. _replace! (new:: Base.Callable , res:: AbstractArray , A:: $t , count:: Int )
88
+ return $ _disk_replace! (new, res, A, count)
89
+ end
90
+ function Base. _replace! (new:: Base.Callable , res:: $t , A:: AbstractArray , count:: Int )
91
+ return $ _disk_replace! (new, res, A, count)
92
+ end
93
+ function Base. _replace! (new:: Base.Callable , res:: $t , A:: $t , count:: Int )
94
+ return $ _disk_replace! (new, res, A, count)
95
+ end
96
+ end
97
+ end
98
+
0 commit comments