Skip to content

Commit 1aa1964

Browse files
committed
Refactor derived map functions
1 parent 4632408 commit 1aa1964

File tree

2 files changed

+20
-63
lines changed

2 files changed

+20
-63
lines changed

src/abstractsparsearrayinterface.jl

Lines changed: 0 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -92,39 +92,6 @@ end
9292
# TODO: Define `default_similartype` or something like that?
9393
return SparseArrayDOK{T}(undef, size)
9494
end
95-
96-
# map over a specified subset of indices of the inputs.
97-
function map_indices! end
98-
99-
@interface interface::AbstractArrayInterface function map_indices!(
100-
indices, f, a_dest::AbstractArray, as::AbstractArray...
101-
)
102-
for I in indices
103-
a_dest[I] = f(map(a -> a[I], as)...)
104-
end
105-
return a_dest
106-
end
107-
108-
# Only map the stored values of the inputs.
109-
function map_stored! end
110-
111-
@interface interface::AbstractArrayInterface function map_stored!(
112-
f, a_dest::AbstractArray, as::AbstractArray...
113-
)
114-
@interface interface map_indices!(eachstoredindex(as...), f, a_dest, as...)
115-
return a_dest
116-
end
117-
118-
# Only map all values, not just the stored ones.
119-
function map_all! end
120-
121-
@interface interface::AbstractArrayInterface function map_all!(
122-
f, a_dest::AbstractArray, as::AbstractArray...
123-
)
124-
@interface interface map_indices!(eachindex(as...), f, a_dest, as...)
125-
return a_dest
126-
end
127-
12895
using DerivableInterfaces: DerivableInterfaces, zero!
12996

13097
# `zero!` isn't defined in `Base`, but it is defined in `ArrayLayouts`
@@ -140,36 +107,6 @@ using DerivableInterfaces: DerivableInterfaces, zero!
140107
return @interface interface map_stored!(f, a, a)
141108
end
142109

143-
# Determines if a function preserves the stored values
144-
# of the destination sparse array.
145-
# The current code may be inefficient since it actually
146-
# accesses an unstored element, which in the case of a
147-
# sparse array of arrays can allocate an array.
148-
# Sparse arrays could be expected to define a cheap
149-
# unstored element allocator, for example
150-
# `get_prototypical_unstored(a::AbstractArray)`.
151-
function preserves_unstored(f, a_dest::AbstractArray, as::AbstractArray...)
152-
I = first(eachindex(as...))
153-
return iszero(f(map(a -> getunstoredindex(a, I), as)...))
154-
end
155-
156-
# @interface interface::AbstractSparseArrayInterface function Base.map!(
157-
# f, a_dest::AbstractArray, as::AbstractArray...
158-
# )
159-
# isempty(a_dest) && return a_dest # special case to avoid trying to access empty array
160-
# indices = if !preserves_unstored(f, a_dest, as...)
161-
# eachindex(a_dest)
162-
# elseif any(a -> a_dest !== a, as)
163-
# as = map(a -> Base.unalias(a_dest, a), as)
164-
# @interface interface zero!(a_dest)
165-
# eachstoredindex(as...)
166-
# else
167-
# eachstoredindex(a_dest)
168-
# end
169-
# @interface interface map_indices!(indices, f, a_dest, as...)
170-
# return a_dest
171-
# end
172-
173110
# `f::typeof(norm)`, `op::typeof(max)` used by `norm`.
174111
function reduce_init(f, op, as...)
175112
# TODO: Generalize this.

src/map.jl

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,26 @@ end
121121
identity, C, A
122122
)
123123

124+
# Only map the stored values of the inputs.
125+
function map_stored! end
126+
127+
@interface interface::AbstractArrayInterface function map_stored!(
128+
f, a_dest::AbstractArray, as::AbstractArray...
129+
)
130+
@interface interface map!(WeakPreserving(f), a_dest, as...)
131+
return a_dest
132+
end
133+
134+
# Only map all values, not just the stored ones.
135+
function map_all! end
136+
137+
@interface interface::AbstractArrayInterface function map_all!(
138+
f, a_dest::AbstractArray, as::AbstractArray...
139+
)
140+
@interface interface map!(NonPreserving(f), a_dest, as...)
141+
return a_dest
142+
end
143+
124144
# Utility functions
125145
# -----------------
126146
# shape check similar to checkbounds

0 commit comments

Comments
 (0)