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

Best practice for "inplace" sort() method on MutRrbt #48

Open
axkr opened this issue Dec 24, 2021 · 2 comments
Open

Best practice for "inplace" sort() method on MutRrbt #48

axkr opened this issue Dec 24, 2021 · 2 comments

Comments

@axkr
Copy link

axkr commented Dec 24, 2021

Sorting a MutRrbt "inplace" is a very common process in my project.
What is the "best practice" for sorting a MutRrbt data structure (before making it immutable())?

Should something simple like the following snippet directly be included in MutRrbt or can this be done in a more "clever" way?

  public static MutRrbt<Object> sort(MutRrbt<Object> rrbTree, int fromIndex, int toIndex,
      Comparator<Object> comparator) {
    int size = rrbTree.size();
    Object[] a = new Object[size];
    rrbTree.toArray(a);
    Arrays.sort(a, fromIndex, toIndex, comparator);
    return StaticImports.mutableRrb(a);
  }
@GlenKPeterson
Copy link
Owner

This question deserves careful thought and I'm busy with holiday stuff right now. The quick answer is that if you don't have duplicates, use the PersistentTreeSet or sortedSet() instead of an RRB-Tree.

Otherwise, your solution looks correct. That's the right place to start. We can try to improve efficiency later.

@axkr
Copy link
Author

axkr commented Dec 24, 2021

The quick answer is that if you don't have duplicates, use the PersistentTreeSet or sortedSet() instead of an RRB-Tree.

I have duplicates and another data structure is not an option.

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

2 participants