diff --git a/src/RegionTrees.jl b/src/RegionTrees.jl index ed92f90..e49fe54 100644 --- a/src/RegionTrees.jl +++ b/src/RegionTrees.jl @@ -22,6 +22,7 @@ export Cell, initial_data, allcells, allleaves, + allparents, adaptivesampling! include("twosarray.jl") diff --git a/src/cell.jl b/src/cell.jl index 0e73f6a..5974434 100644 --- a/src/cell.jl +++ b/src/cell.jl @@ -114,3 +114,17 @@ function allleaves(cell::Cell) end end end + +function allparents(cell::Cell) + Channel() do c + queue = [cell] + while !isempty(queue) + current = pop!(queue) + p = parent(current) + if ! (p === nothing) + put!(c, p) + push!(queue, p) + end + end + end +end diff --git a/test/cell.jl b/test/cell.jl index fecbb77..f944349 100644 --- a/test/cell.jl +++ b/test/cell.jl @@ -35,3 +35,24 @@ end @test findleaf(cell, SVector(-0.01, -0.01, -0.01)) === cell[1,1,1] @test findleaf(cell, SVector(0.01, -0.01, -0.01)) === cell[2,1,1] end + +@testset "find parents" begin + cell2D = Cell(SVector(0., 0), SVector(1., 1)) + split!(cell2D) + split!(cell2D[1,1]) + parents2D = [p for p in allparents(cell2D[1,1][1,2])] + + @test parents2D[1] === cell2D[1,1] + @test parents2D[2] === cell2D + + cell3D = Cell(SVector(-1., -2, -3), SVector(2., 4, 6), 0) + split!(cell3D) + split!(cell3D[2,2,2]) + parents3D = [p for p in allparents(cell3D[2,2,2][1,1,1])] + + @test parents3D[1] === cell3D[2,2,2] + @test parents3D[2] === cell3D + + noparents = [p for p in allparents(cell2D)] + @test length(noparents) == 0 +end \ No newline at end of file