-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathsequences.jl
72 lines (68 loc) · 1.52 KB
/
sequences.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
# Copied from TypedPolynomials
module Sequences
import MutableArithmetics
const MA = MutableArithmetics
function merge_sorted!(
result,
v1::AbstractArray,
v2::AbstractArray,
compare,
combine,
filter = x -> !iszero(x),
)
i = firstindex(result)
i1 = firstindex(v1)
i2 = firstindex(v2)
while i1 <= lastindex(v1) && i2 <= lastindex(v2)
x1 = v1[i1]
x2 = v2[i2]
Δ = compare(x1, x2)
if Δ < 0
if filter(x1)
result[i] = x1
i += 1
end
i1 += 1
elseif Δ > 0
if filter(x2)
result[i] = x2
i += 1
end
i2 += 1
else
c = combine(x1, x2)
if filter(c)
result[i] = combine(x1, x2)
i += 1
end
i1 += 1
i2 += 1
end
end
for j in i1:lastindex(v1)
if filter(v1[j])
result[i] = v1[j]
i += 1
end
end
for j in i2:lastindex(v2)
if filter(v2[j])
result[i] = v2[j]
i += 1
end
end
resize!(result, i - 1)
return result
end
function merge_sorted(
v1::AbstractArray,
v2::AbstractArray,
compare,
combine = Base.:+,
filter = x -> !iszero(x),
)
T = MA.promote_operation(combine, eltype(v1), eltype(v2))
result = Vector{T}(undef, length(v1) + length(v2))
return merge_sorted!(result, v1, v2, compare, combine, filter)
end
end