GlenKPeterson / Paguro

Generic, Null-safe, Immutable Collections and Functional Transformations for the JVM
Other
312 stars 24 forks source link

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

Closed axkr closed 2 weeks ago

axkr commented 2 years ago

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 commented 2 years ago

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 commented 2 years ago

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.