runtime icon indicating copy to clipboard operation
runtime copied to clipboard

Provide ImmutableSortedSetArray<T> an ImmutableSortedSet based on array not on tree

Open fabricefou opened this issue 8 years ago • 3 comments

Rational

ImmutableSortedSet is based on an AVL Tree also:

  • property Item[Int32] has compexity in O(Log(N))
  • We can't have an "equivalent" ImmutableArray without make a copy

In the ImmutableCollections framework, we already have the same type of choice for our data structures. I think to the distinction between ImmutableArray vs ImmutableList (based on tree data structure)

With this structure we could have:

  • property Item[Int32] with O(1) complexity
  • We can have an property ToImmutable without any copy make (call of the internal ImmutableArray constructor).

Usage

Very ofen we want to have the garanty (by type) than the incomming Array T[] is sorted to call safely BinarySearch. Of course we could used the ImmutableSortedSet but this structure isn't efficient when working with indexes.

Another use case come when we have to rely on native API (eg Interpolation API) when we want a sorted Array double* in input. With an ImmutableSortedSetArray we can without copy have an double* via a Span-based API or some other magic trick.

Could you provide an ImmutableSortedSet based on a Array ?

fabricefou avatar Dec 26 '17 10:12 fabricefou

:memo: Several of the use cases for this would be partially mitigated by dotnet/runtime#14477.

sharwell avatar Dec 26 '17 12:12 sharwell

After discussing this with @ianhays we think that it is not valuable adding a new API which would be a copy of an existing API but with a different implementation. First of all this would confuse costumers, second of all any one could implement this by using our interfaces. So we really think that what is suggested here is to change the implementation to have a better performance, something like proposed in dotnet/runtime#14477.

safern avatar Jan 29 '18 19:01 safern

Thanks for the answer 😀. But with this kind of thought we'd never have had the 2 distincts implementations ImmutableArray and ImmutableList. Both implement the same contract. I think we would never have one single implementation efficient for all use case (building and reading). Another example is the existence of SortedDictionary<Key, Value> and SortedList<Key, Value>, two distinct implementations with equivalent contract but with different performance trade off. I really think you will help your costumers by adding this kind of implementations.

fabricefou avatar Jan 29 '18 22:01 fabricefou