-
-
Notifications
You must be signed in to change notification settings - Fork 58
/
utils.jl
347 lines (299 loc) · 9.08 KB
/
utils.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
"""
```julia
recursivecopy(a::Union{AbstractArray{T, N}, AbstractVectorOfArray{T,N}})
```
A recursive `copy` function. Acts like a `deepcopy` on arrays of arrays, but
like `copy` on arrays of scalars.
"""
function recursivecopy(a)
deepcopy(a)
end
function recursivecopy(a::Union{StaticArraysCore.SVector, StaticArraysCore.SMatrix,
StaticArraysCore.SArray, Number})
copy(a)
end
function recursivecopy(a::AbstractArray{T, N}) where {T <: Number, N}
copy(a)
end
function recursivecopy(a::AbstractArray{T, N}) where {T <: AbstractArray, N}
if ArrayInterface.ismutable(a)
b = similar(a)
map!(recursivecopy, b, a)
else
ArrayInterface.restructure(a, map(recursivecopy, a))
end
end
function recursivecopy(a::AbstractVectorOfArray)
b = copy(a)
b.u = recursivecopy.(a.u)
return b
end
"""
```julia
recursivecopy!(b::AbstractArray{T, N}, a::AbstractArray{T, N})
```
A recursive `copy!` function. Acts like a `deepcopy!` on arrays of arrays, but
like `copy!` on arrays of scalars.
"""
function recursivecopy! end
function recursivecopy!(b::AbstractArray{T, N}, a::AbstractArray{T2, N}) where {T <: StaticArraysCore.StaticArray,
T2 <: StaticArraysCore.StaticArray,
N}
@inbounds for i in eachindex(a)
# TODO: Check for `setindex!`` and use `copy!(b[i],a[i])` or `b[i] = a[i]`, see #19
b[i] = copy(a[i])
end
end
function recursivecopy!(b::AbstractArray{T, N},
a::AbstractArray{T2, N}) where {T <: Enum, T2 <: Enum, N}
copyto!(b, a)
end
function recursivecopy!(b::AbstractArray{T, N},
a::AbstractArray{T2, N}) where {T <: Number, T2 <: Number, N}
copyto!(b, a)
end
function recursivecopy!(b::AbstractArray{T, N},
a::AbstractArray{T2, N}) where {T <: Union{AbstractArray, AbstractVectorOfArray},
T2 <: Union{AbstractArray, AbstractVectorOfArray}, N}
if ArrayInterface.ismutable(T)
@inbounds for i in eachindex(b, a)
recursivecopy!(b[i], a[i])
end
else
copyto!(b, a)
end
return b
end
function recursivecopy!(b::AbstractVectorOfArray, a::AbstractVectorOfArray)
if ArrayInterface.ismutable(eltype(b.u))
@inbounds for i in eachindex(b.u, a.u)
recursivecopy!(b.u[i], a.u[i])
end
else
copyto!(b.u, a.u)
end
return b
end
"""
```julia
recursivefill!(b::AbstractArray{T, N}, a)
```
A recursive `fill!` function.
"""
function recursivefill! end
function recursivefill!(b::AbstractArray{T, N},
a::T2) where {T <: StaticArraysCore.StaticArray,
T2 <: StaticArraysCore.StaticArray, N}
@inbounds for i in eachindex(b)
b[i] = copy(a)
end
end
function recursivefill!(bs::AbstractVectorOfArray{T, N},
a::T2) where {T <: StaticArraysCore.StaticArray,
T2 <: StaticArraysCore.StaticArray, N}
@inbounds for b in bs, i in eachindex(b)
b[i] = copy(a)
end
end
function recursivefill!(b::AbstractArray{T, N},
a::T2) where {T <: StaticArraysCore.SArray,
T2 <: Union{Number, Bool}, N}
@inbounds for i in eachindex(b)
b[i] = fill(a, typeof(b[i]))
end
end
function recursivefill!(bs::AbstractVectorOfArray{T, N},
a::T2) where {T <: StaticArraysCore.SArray,
T2 <: Union{Number, Bool}, N}
@inbounds for b in bs, i in eachindex(b)
b[i] = fill(a, typeof(b[i]))
end
end
for type in [AbstractArray, AbstractVectorOfArray]
@eval function recursivefill!(b::$type{T, N}, a::T2) where {T <: Enum, T2 <: Enum, N}
fill!(b, a)
end
@eval function recursivefill!(b::$type{T, N},
a::T2) where {T <: Union{Number, Bool}, T2 <: Union{Number, Bool}, N
}
fill!(b, a)
end
for type2 in [Any, StaticArraysCore.StaticArray]
@eval function recursivefill!(b::$type{T, N}, a::$type2) where {T <: StaticArraysCore.MArray, N}
@inbounds for i in eachindex(b)
if isassigned(b, i)
recursivefill!(b[i], a)
else
b[i] = zero(eltype(b))
recursivefill!(b[i], a)
end
end
end
end
@eval function recursivefill!(b::$type{T, N}, a) where {T <: AbstractArray, N}
@inbounds for i in eachindex(b)
recursivefill!(b[i], a)
end
return b
end
end
# Deprecated
function vecvec_to_mat(vecvec)
mat = Matrix{eltype(eltype(vecvec))}(undef, length(vecvec), length(vecvec[1]))
for i in 1:length(vecvec)
mat[i, :] = vecvec[i]
end
mat
end
"""
```julia
vecvecapply(f::Base.Callable, v)
```
Calls `f` on each element of a vecvec `v`.
"""
function vecvecapply(f, v)
sol = Vector{eltype(eltype(v))}()
for i in eachindex(v)
for j in eachindex(v[i])
push!(sol, v[i][j])
end
end
f(sol)
end
function vecvecapply(f, v::Array{T}) where {T <: Number}
f(v)
end
function vecvecapply(f, v::T) where {T <: Number}
f(v)
end
"""
```julia
copyat_or_push!{T}(a::AbstractVector{T}, i::Int, x)
```
If `i<length(x)`, it's simply a `recursivecopy!` to the `i`th element. Otherwise, it will
`push!` a `deepcopy`.
"""
function copyat_or_push!(a::AbstractVector{T}, i::Int, x, perform_copy = true) where {T}
@inbounds if length(a) >= i
if !ArrayInterface.ismutable(T) || !perform_copy
# TODO: Check for `setindex!`` if T <: StaticArraysCore.StaticArray and use `copy!(b[i],a[i])`
# or `b[i] = a[i]`, see https://github.com/JuliaDiffEq/RecursiveArrayTools.jl/issues/19
a[i] = x
else
if length(a[i]) == length(x)
recursivecopy!(a[i], x)
else
a[i] = recursivecopy(x)
end
end
else
if perform_copy
push!(a, recursivecopy(x))
else
push!(a, x)
end
end
nothing
end
function copyat_or_push!(a::AbstractVector{T}, i::Int, x,
nc::Type{Val{perform_copy}}) where {T, perform_copy}
copyat_or_push!(a, i, x, perform_copy)
end
"""
```julia
recursive_one(a)
```
Calls `one` on the bottom container to get the "true element one type".
"""
recursive_one(a) = recursive_one(a[1])
recursive_one(a::T) where {T <: Number} = one(a)
recursive_bottom_eltype(a) = a == eltype(a) ? a : recursive_bottom_eltype(eltype(a))
"""
```julia
recursive_unitless_bottom_eltype(a)
```
Grabs the unitless element type at the bottom of the chain. For example, if
ones has a `Array{Array{Float64,N},N}`, this will return `Float64`.
"""
recursive_unitless_bottom_eltype(a) = recursive_unitless_bottom_eltype(typeof(a))
recursive_unitless_bottom_eltype(a::Type{Any}) = Any
function recursive_unitless_bottom_eltype(a::Type{T}) where {T}
recursive_unitless_bottom_eltype(eltype(a))
end
function recursive_unitless_bottom_eltype(a::Type{T}) where {T <: AbstractArray}
recursive_unitless_bottom_eltype(eltype(a))
end
function recursive_unitless_bottom_eltype(a::Type{T}) where {T <: Number}
eltype(a) == Number ? Float64 : typeof(one(eltype(a)))
end
recursive_unitless_bottom_eltype(::Type{<:Enum{T}}) where {T} = T
"""
```julia
recursive_unitless_eltype(a)
```
Grabs the unitless element type. For example, if
ones has a `Array{Array{Float64,N},N}`, this will return `Array{Float64,N}`.
"""
recursive_unitless_eltype(a) = recursive_unitless_eltype(eltype(a))
recursive_unitless_eltype(a::Type{Any}) = Any
function recursive_unitless_eltype(a::Type{T}) where {T <: StaticArraysCore.StaticArray}
StaticArraysCore.similar_type(a, recursive_unitless_eltype(eltype(a)))
end
function recursive_unitless_eltype(a::Type{T}) where {T <: Array}
Array{recursive_unitless_eltype(eltype(a)), ndims(a)}
end
recursive_unitless_eltype(a::Type{T}) where {T <: Number} = typeof(one(eltype(a)))
recursive_unitless_eltype(::Type{<:Enum{T}}) where {T} = T
recursive_mean(x...) = mean(x...)
function recursive_mean(vecvec::Vector{T}) where {T <: AbstractArray}
out = zero(vecvec[1])
for i in eachindex(vecvec)
out += vecvec[i]
end
out / length(vecvec)
end
# From Iterators.jl. Moved here since Iterators.jl is not precompile safe anymore.
# Concatenate the output of n iterators
struct Chain{T <: Tuple}
xss::T
end
# iteratorsize method defined at bottom because of how @generated functions work in 0.6 now
"""
chain(xs...)
Iterate through any number of iterators in sequence.
```jldoctest
julia> for i in chain(1:3, ['a', 'b', 'c'])
@show i
end
i = 1
i = 2
i = 3
i = 'a'
i = 'b'
i = 'c'
```
"""
chain(xss...) = Chain(xss)
Base.length(it::Chain{Tuple{}}) = 0
Base.length(it::Chain) = sum(length, it.xss)
Base.eltype(::Type{Chain{T}}) where {T} = typejoin([eltype(t) for t in T.parameters]...)
function Base.iterate(it::Chain)
i = 1
xs_state = nothing
while i <= length(it.xss)
xs_state = iterate(it.xss[i])
xs_state !== nothing && return xs_state[1], (i, xs_state[2])
i += 1
end
return nothing
end
function Base.iterate(it::Chain, state)
i, xs_state = state
xs_state = iterate(it.xss[i], xs_state)
while xs_state == nothing
i += 1
i > length(it.xss) && return nothing
xs_state = iterate(it.xss[i])
end
return xs_state[1], (i, xs_state[2])
end