Skip to content

Commit

Permalink
use a separate parameter for the hyper rectangle (#174)
Browse files Browse the repository at this point in the history
  • Loading branch information
KristofferC committed Dec 27, 2023
1 parent dc04c19 commit 3eca4e8
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 12 deletions.
2 changes: 1 addition & 1 deletion src/hyperrectangles.jl
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ function compute_bbox(data::AbstractVector{V}) where {V <: AbstractVector}
maxes[j] = dim_max
mins[j] = dim_min
end
return HyperRectangle(V(mins), V(maxes))
return HyperRectangle(SVector(mins), SVector(maxes))
end


Expand Down
13 changes: 2 additions & 11 deletions src/kd_tree.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ struct KDNode{T}
split_dim::Int16 # The dimension the hyper rectangle was split at
end

struct KDTree{V <: AbstractVector, M <: MinkowskiMetric, T} <: NNTree{V,M}
struct KDTree{V <: AbstractVector, M <: MinkowskiMetric, T, TH} <: NNTree{V,M}
data::Vector{V}
hyper_rec::HyperRectangle{V}
hyper_rec::HyperRectangle{TH}
indices::Vector{Int}
metric::M
nodes::Vector{KDNode{T}}
Expand All @@ -31,7 +31,6 @@ function KDTree(data::AbstractVector{V},
reorder = !isempty(reorderbuffer) || (storedata ? reorder : false)

tree_data = TreeData(data, leafsize)
n_d = length(V)
n_p = length(data)

indices = collect(1:n_p)
Expand Down Expand Up @@ -77,7 +76,6 @@ function KDTree(data::AbstractVector{V},
end
end

backup_rec = HyperRectangle(copy(hyper_rec.mins), copy(hyper_rec.maxes))
KDTree(storedata ? data : similar(data, 0), hyper_rec, indices, metric, nodes, tree_data, reorder)
end

Expand All @@ -99,13 +97,6 @@ end
reorderbuffer = reorderbuffer_points)
end

Base.@propagate_inbounds function setindex(s::StaticArray, v, i)
return StaticArrays.setindex(s, v, i)
end
Base.@propagate_inbounds function setindex(s::V, v, i) where {V <: AbstractArray}
return V(setindex(SVector{length(V)}(s), v, i))
end

function build_KDTree(index::Int,
data::AbstractVector{V},
data_reordered::Vector{V},
Expand Down
8 changes: 8 additions & 0 deletions test/test_knn.jl
Original file line number Diff line number Diff line change
Expand Up @@ -116,3 +116,11 @@ import Tensors
nn(stree, vpoints) == nn(stree, spoints)
end
end


@testset "subarray" begin
dynamic_view(X) = [NearestNeighbors.SizedVector{length(v)}(v) for v in eachslice(X; dims = ndims(X))]
data = randn(1000, 1000)
view = dynamic_view(data)
@test KDTree(view) isa KDTree
end

0 comments on commit 3eca4e8

Please sign in to comment.