|
45 | 45 |
|
46 | 46 |
|
47 | 47 | function leafnodes(geoms; nodecapacity=10)
|
48 |
| - extents_indices = [(GI.extent(geoms[i]), i) for i in eachindex(geoms)] |
49 |
| - perm = sortperm(extents_indices; by=(v -> ((v[1][1][1] + v[1][1][2]) / 2))) # [extent/index][dim][min/max] sort by x |
50 |
| - sorted_extents = extents_indices[perm] |
51 |
| - r = length(sorted_extents) |
| 48 | + ext1 = GI.extent(first(geoms)) |
| 49 | + extents_indices = Tuple{typeof(ext1),Int}[(GI.extent(geoms[i]), i) for i in eachindex(geoms)] |
| 50 | + # Use the same scratch space for all sorts |
| 51 | + scratch = similar(extents_indices) |
| 52 | + sort!(extents_indices; by=v -> (v[1][1][1] + v[1][1][2]) / 2, scratch) # [extent/index][dim][min/max] sort by x |
| 53 | + r = length(extents_indices) |
52 | 54 | P = ceil(Int, r / nodecapacity)
|
53 | 55 | S = ceil(Int, sqrt(P))
|
54 |
| - x_splits = Iterators.partition(sorted_extents, S * nodecapacity) |
| 56 | + x_splits = Iterators.partition(extents_indices, S * nodecapacity) |
55 | 57 |
|
56 | 58 | nodes = STRLeafNode{Vector{typeof(extents_indices[1][1])}}[]
|
57 | 59 | for x_split in x_splits
|
58 |
| - perm = sortperm(x_split; by=(v -> ((v[1][2][1] + v[1][2][2]) / 2))) # [extent/index][dim][min/max] sort by y |
59 |
| - sorted_split = x_split[perm] |
60 |
| - y_splits = Iterators.partition(sorted_split, nodecapacity) |
| 60 | + sort!(x_split; |
| 61 | + by=v -> (v[1][2][1] + v[1][2][2]) / 2, # [extent/index][dim][min/max] sort by y |
| 62 | + scratch=resize!(scratch, length(x_split)), |
| 63 | + ) |
| 64 | + y_splits = Iterators.partition(x_split, nodecapacity) |
61 | 65 | for y_split in y_splits
|
62 |
| - push!(nodes, STRLeafNode(getindex.(y_split,1), getindex.(y_split,2))) |
| 66 | + exts = first.(y_split)::Vector{typeof(ext1)} |
| 67 | + inds = last.(y_split)::Vector{Int} |
| 68 | + push!(nodes, STRLeafNode(exts, inds)) |
63 | 69 | end
|
64 | 70 | end
|
65 | 71 | return nodes
|
|
68 | 74 |
|
69 | 75 | # a bit of duplication...
|
70 | 76 | function parentnodes(nodes; nodecapacity=10)
|
71 |
| - extents_indices = [(GI.extent(node), node) for node in nodes] |
72 |
| - perm = sortperm(extents_indices; by=(v -> ((v[1][1][1] + v[1][1][2]) / 2))) # [extent/node][dim][min/max] sort by x |
73 |
| - sorted_extents = extents_indices[perm] |
74 |
| - r = length(sorted_extents) |
| 77 | + n1 = first(nodes) |
| 78 | + ext1 = GI.extent(n1) |
| 79 | + extents_indices = Tuple{typeof(ext1),typeof(n1)}[(GI.extent(node), node) for node in nodes] |
| 80 | + scratch = similar(extents_indices) |
| 81 | + sort!(extents_indices; by=v -> (v[1][1][1] + v[1][1][2]) / 2, scratch) # [extent/node][dim][min/max] sort by x |
| 82 | + r = length(extents_indices) |
75 | 83 | P = ceil(Int, r / nodecapacity)
|
76 | 84 | S = ceil(Int, sqrt(P))
|
77 |
| - x_splits = Iterators.partition(sorted_extents, S * nodecapacity) |
| 85 | + x_splits = Iterators.partition(extents_indices, S * nodecapacity) |
78 | 86 |
|
79 | 87 | T = typeof(extents_indices[1][1])
|
80 | 88 | N = Vector{typeof(extents_indices[1][2])}
|
81 |
| - nodes = STRNode{T, N}[] |
| 89 | + outnodes = STRNode{T, N}[] |
82 | 90 | for x_split in x_splits
|
83 |
| - perm = sortperm(x_split; by=(v -> ((v[1][2][1] + v[1][2][2]) / 2))) # [extent/index][dim][min/max] sort by y |
84 |
| - sorted_split = x_split[perm] |
85 |
| - y_splits = Iterators.partition(sorted_split, nodecapacity) |
| 91 | + sort!(x_split; |
| 92 | + by=v -> (v[1][2][1] + v[1][2][2]) / 2, # [extent/index][dim][min/max] sort by y |
| 93 | + scratch=resize!(scratch, length(x_split)), |
| 94 | + ) |
| 95 | + y_splits = Iterators.partition(x_split, nodecapacity) |
86 | 96 | for y_split in y_splits
|
87 |
| - push!(nodes, STRNode(foldl(Extents.union, getindex.(y_split,1)), getindex.(y_split,2))) |
| 97 | + # Alloc free union over the extents |
| 98 | + ext = foldl(y_split; init=y_split[1][1]) do u, (ext, _) |
| 99 | + Extents.union(u, ext) |
| 100 | + end |
| 101 | + y_splitnodes = last.(y_split)::Vector{eltype(nodes)} |
| 102 | + push!(outnodes, STRNode(ext, y_splitnodes)) |
88 | 103 | end
|
89 | 104 | end
|
90 |
| - return nodes |
| 105 | + return outnodes |
91 | 106 | end
|
92 | 107 |
|
93 | 108 |
|
|
0 commit comments