Skip to content

Commit 057bee7

Browse files
committed
simplify load!()
- one `load!()` instread of 2 - one `convertel` function to convert `data` elements into `V` instead of 3 `getxxx`
1 parent fe01ebe commit 057bee7

4 files changed

Lines changed: 31 additions & 29 deletions

File tree

examples/pareto.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ open(joinpath(@__DIR__, "pareto3d_rtree_seq.html"), "w") do io
5858
end
5959

6060
bulk_pareto_tree = RTree{Float64, 3}(Int, String, leaf_capacity = 8, branch_capacity = 8)
61-
SI.load!(bulk_pareto_tree, collect(pareto_tree))
61+
SI.load!(bulk_pareto_tree, pareto_tree)
6262
SI.check(bulk_pareto_tree)
6363

6464
bulk_pareto_tree_plot = plot(bulk_pareto_tree);

examples/spiral.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ for (i, br) in enumerate(mbrs)
2020
end
2121
SI.check(seq_tree)
2222

23-
SI.load!(bulk_tree, enumerate(mbrs), getid=x->x[1], getmbr=x->x[2], getval=x->string(x[1]))
23+
SI.load!(bulk_tree, enumerate(mbrs), convertel -> eltype(bulk_tree)(x[2], x[1], string(x[1])))
2424
SI.check(bulk_tree)
2525

2626
include(joinpath(@__DIR__, "dataframe_utils.jl"))

src/rtree/bulk.jl

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,25 @@ function omt_branch_fill(tree::RTree; fill_factor::Number=1.0) where T<:Node
1010
end
1111

1212
"""
13-
Bulk-load the `data` into `tree`.
13+
load!(tree::RTree{T,N,V}, data::Any;
14+
convertel = identity, method = :OMT,
15+
leaf_fill = capacity(Leaf, tree),
16+
branch_fill::Tuple{Integer, Integer} = omt_branch_fill(tree)) where {T,N,V}
17+
18+
Bulk-load `data` into `tree`.
19+
* `tree`: an *empty* R-tree for storing elements of type `V`
20+
* `data`: iterable container with the elements to put into `tree`
21+
* `convertel`: function to convert elements of `data` to type `V`
22+
* `method`: bulk-loading method
23+
* `leaf_fill`: the average number of elements to store in R-tree leaves (1-level nodes)
24+
* `branch_fill`: the tuple of the number of slices and the number of subtrees per slice
25+
in the R-tree nodes (level ≥ 1).
26+
27+
The supported bulk-loading methods are:
28+
* `:OMT`: *Overlap Minimizing Top-down method* by Taewon Lee and Sukho Lee
1429
"""
1530
function load!(tree::RTree{T,N,V}, data::Any;
16-
getmbr = first,
17-
getid = idtrait(V) === HasNoID ? x -> nothing : x -> x[2],
18-
getval = idtrait(V) === HasNoID ? x -> x[2] : x -> x[3],
31+
convertel = identity,
1932
method::Symbol = :OMT,
2033
leaf_fill::Integer = capacity(Leaf, tree),
2134
branch_fill::Tuple{Integer, Integer} = omt_branch_fill(tree)) where {T, N, V}
@@ -25,27 +38,14 @@ function load!(tree::RTree{T,N,V}, data::Any;
2538
@warn "No bulk-load data provided"
2639
return tree
2740
end
28-
# FIXME kwargs... doesn't work for some reason
29-
return load!(tree, V[V(getmbr(x), getid(x), getval(x)) for x in data],
30-
method=method, leaf_fill=leaf_fill, branch_fill=branch_fill)
31-
end
32-
33-
function load!(tree::RTree{T,N,V}, data::AbstractVector{V};
34-
method::Symbol = :OMT,
35-
leaf_fill::Integer = capacity(Leaf, tree),
36-
branch_fill::Tuple{Integer, Integer} = omt_branch_fill(tree)) where {T,N,V}
37-
isempty(tree) || throw(ArgumentError("Cannot bulk-load into non-empty tree"))
38-
if isempty(data)
39-
@warn "No bulk-load data provided"
40-
return tree
41-
end
4241
0 < leaf_fill <= capacity(Leaf, tree) ||
4342
throw(ArgumentError("Leaf fill should be positive and not exceed leaf capacity"))
4443
prod(branch_fill) > 1 || throw(ArgumentError("Branch fill should be > 1"))
4544
if method == :OMT
46-
return load_omt!(tree, data, leaf_fill=leaf_fill, branch_fill=branch_fill)
45+
return load_omt!(tree, V[convertel(x) for x in data],
46+
leaf_fill=leaf_fill, branch_fill=branch_fill)
4747
else
48-
throw(ArgumentError("Unsupported bulk-loadaing method $method"))
48+
throw(ArgumentError("Unsupported bulk-loading method $method"))
4949
end
5050
end
5151

test/rtree.jl

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -223,18 +223,22 @@ end
223223
end
224224
end
225225

226-
@testset "bulk load" begin
226+
@testset "load!() (OMT bulk load)" begin
227227
tree = RTree{SI.dimtype(eltype(mbrs)), ndims(eltype(mbrs))}(Int, String,
228228
leaf_capacity = 20, branch_capacity = 20)
229229
@test tree === SI.load!(tree, enumerate(mbrs), method=:OMT,
230-
getid = x -> x[1], getmbr = x -> x[2], getval = x -> string(x[1]))
230+
convertel = x -> eltype(tree)(x[2], x[1], string(x[1])))
231231
@test SI.check(tree)
232232
@test length(tree) == length(mbrs)
233233
#@show SI.height(tree)
234234
#@show tree.nnodes_perlevel
235235
# cannot bulk-load into non-empty tree
236236
@test_throws ArgumentError SI.load!(tree, enumerate(mbrs), method=:OMT,
237-
getid = x -> x[1], getmbr = x -> x[2], getval = x -> string(x[1]))
237+
convertel = x -> eltype(tree)(x[2], x[1], string(x[1])))
238+
tree2 = similar(tree)
239+
# can load from tree into another tree
240+
@test SI.load!(tree2, tree) == tree2
241+
@test length(tree2) == length(tree)
238242

239243
@testset "findfirst()" begin
240244
# check that the elements can be found
@@ -257,9 +261,7 @@ end
257261
tree = RTree{Int, 2}(Int, String, leaf_capacity = 5, branch_capacity = 5)
258262
pts = [(0, 0), (1, 0), (2, 2), (2, 0), (0, 1), (1, 1), (-1, -1)]
259263
SI.load!(tree, enumerate(pts),
260-
getid = x -> x[1],
261-
getmbr = x -> SI.Rect(x[2], x[2]),
262-
getval = x -> string(x[1]))
264+
convertel = x -> eltype(tree)(SI.Rect(x[2], x[2]), x[1], string(x[1])))
263265
@test length(tree) == length(pts)
264266
@test SI.check(tree)
265267
rect = SI.Rect((0, 0), (1, 1))
@@ -280,7 +282,7 @@ end
280282
pts = [ntuple(_ -> 5. * randn(), 3) for _ in 1:1000]
281283
tree = RTree{Float64, 3}(Int, String, leaf_capacity = 10, branch_capacity = 10)
282284
SI.load!(tree, enumerate(pts),
283-
getid = x -> x[1], getmbr = x -> SI.Rect(x[2], x[2]), getval = x -> string(x[1]))
285+
convertel = x -> eltype(tree)(SI.Rect(x[2], x[2]), x[1], string(x[1])))
284286
@test length(tree) == length(pts)
285287
#@show tree.nelems tree.nnodes_perlevel
286288

0 commit comments

Comments
 (0)