rbush icon indicating copy to clipboard operation
rbush copied to clipboard

Feature request: clone method

Open danvk opened this issue 8 years ago • 6 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.)

danvk avatar Jan 03 '17 18:01 danvk

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)?

mourner avatar Jan 03 '17 19:01 mourner

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

danvk avatar Jan 03 '17 19:01 danvk

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

mourner avatar Jan 03 '17 19:01 mourner

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 avatar Jan 14 '19 06:01 Pyrolistical

@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.

mourner avatar Jan 14 '19 10:01 mourner

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.

Pyrolistical avatar Jan 14 '19 17:01 Pyrolistical