New collection: SortedList

Yes. SortedList is just a name I borrowed (it’s not actually a list because you can’t insert elements at arbitrary position). Perhaps SortedIndexedSet or simply requiring all SortedSets to be indexable is more appropriate.

I see the discussion has already gone the way my first thought went: a SortedList that is not a List is confusing. There’s also a question of whether to have both immutable and mutable implementations.

The best algorithm would likely be to take the red-black tree and include child element counts in each node so an index can be reasonably efficiently looked up, and everything stays sorted. In fact, the existing mutable RedBlackTree already does contain the size and it would be relatively straightforward to do the lookup. (Edit: so does immutable; I just missed it. So there isn’t much point to this paragraph except that it agrees with the current strategy.)

As for duplicate elements, one could either just stuff them into the same tree or, more efficiently, chain them onto the same node.

The current implementation is not suitable because it calls nth which traverses n items in the tree, making indexed operations O(n) instead of O(log n) or O(1). (Edit: this paragraph is wrong, since the immutable implementation already has a stored count.)

Where did you get that idea? It’s obviously O(log n).

Sorry, I missed the count field in the abstract tree class!