mourner / rbush

RBush — a high-performance JavaScript R-tree-based 2D spatial index for points and rectangles

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Feature request: clone method

danvk opened this issue · comments

I've implemented clone this way:

const newTree = rbush();
newTree.fromJSON(JSON.parse(JSON.stringify(tree.toJSON())));

but I imagine there might be a faster way for rbush itself to do this.

(My use case is that I'd like to create a new version of the tree with a few additional locations added.)

Thanks for the request! Would you expect RBush to clone the tree structure but don't clone the items from the original input array (and keep references instead)?

Yes, that's exactly what I'd want!

Great! If you want to tackle this, I'm happy to review a PR.

To keep the references, I believe current way to do it is:

function clone(tree) {
  const cloned = Rbush()
  cloned.load(tree.all())
  return cloned
}

@Pyrolistical this would be sub-optimal due to the need to perform loading again — in theory, this step could be avoided with selective tree cloning. But this probably won't be a bottleneck anyway.

Understood. I was just showing how you would do it. Its not great.

Maybe if you refactored to use a persistent data structure then you can have zero cost clone.