-
Notifications
You must be signed in to change notification settings - Fork 78
/
product.jl
68 lines (60 loc) · 1.89 KB
/
product.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
# ------------------------------------------------------------------
# Licensed under the MIT License. See LICENSE in the project root.
# ------------------------------------------------------------------
"""
ProductPartition(p₁, p₂)
A method for partitioning spatial objects using the product of two
partitioning methods `p₁` and `p₂`.
"""
struct ProductPartition{P1,P2} <: PartitionMethod
p₁::P1
p₂::P2
end
# general case
function partitioninds(rng::AbstractRNG, domain::Domain, method::ProductPartition)
# individual partition results
s₁, _ = partitioninds(rng, domain, method.p₁)
s₂, _ = partitioninds(rng, domain, method.p₂)
# label-based representation
l₁ = Vector{Int}(undef, nelements(domain))
l₂ = Vector{Int}(undef, nelements(domain))
for (i, inds) in enumerate(s₁)
l₁[inds] .= i
end
for (i, inds) in enumerate(s₂)
l₂[inds] .= i
end
# product of labels
counter = 0
d = Dict{Tuple{Int,Int},Int}()
l = Vector{Int}(undef, nelements(domain))
for i in 1:nelements(domain)
t = (l₁[i], l₂[i])
if t ∉ keys(d)
counter += 1
d[t] = counter
end
l[i] = d[t]
end
# return partition using label predicate
pred(i, j) = l[i] == l[j]
partitioninds(rng, domain, PredicatePartition(pred))
end
# predicate partition method
function partitioninds(
rng::AbstractRNG,
domain::Domain,
method::ProductPartition{P1,P2}
) where {P1<:PredicatePartitionMethod,P2<:PredicatePartitionMethod}
pred(i, j) = method.p₁(i, j) * method.p₂(i, j)
partitioninds(rng, domain, PredicatePartition(pred))
end
# spatial predicate partition method
function partitioninds(
rng::AbstractRNG,
domain::Domain,
method::ProductPartition{P1,P2}
) where {P1<:SPredicatePartitionMethod,P2<:SPredicatePartitionMethod}
pred(x, y) = method.p₁(x, y) * method.p₂(x, y)
partitioninds(rng, domain, SpatialPredicatePartition(pred))
end