Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature request: clone method #65

Open
danvk opened this issue Jan 3, 2017 · 6 comments
Open

Feature request: clone method #65

danvk opened this issue Jan 3, 2017 · 6 comments

Comments

@danvk
Copy link
Contributor

danvk commented Jan 3, 2017

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

@mourner
Copy link
Owner

mourner commented Jan 3, 2017

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

@danvk
Copy link
Contributor Author

danvk commented Jan 3, 2017

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

@mourner
Copy link
Owner

mourner commented Jan 3, 2017

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

@Pyrolistical
Copy link

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

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

@mourner
Copy link
Owner

mourner commented Jan 14, 2019

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

@Pyrolistical
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants