2

I am looking for a list-like data structure with the following properties:

  1. Generic, i.e., DataStructure<T>, where T is generic
  2. Multiple different instances of T which are considered equal by IComparable<T> must be allowed to be present in the list at the same time; their order can be arbitrary (even change), but they must come after "smaller" instances of T and before "larger" instances of T
  3. O(log(n)) insertion time complexity (or faster)
  4. O(log(n)) retrieval time complexity (or faster)
  5. Possibility to access the first/last element with O(1) time complexity
  6. Possibility to access the predecessor/successor of an element with O(1) time complexity
  7. Available without the use of additional libraries

I do not care about removal time complexity since elements only need to be deleted very rarely. I also do not care about space complexity.

I know that there is no data structure which fulfills all properties, but the ones from the BCL (which look like they could do what I need at first glace) seem to have too many disadvantages:

  • SortedSet<T> does not allow multiple instances which are considered the same by IComparable<T> (2) and does not have predecessor/successor functions (6)
  • SortedSet<T, List<T>> (with only one "representative" indexing instance of T) would require quite some additional (ugly) code, and there is still no predecessor/successor function (6)
  • SortedList<T, T> is too slow for insertion (3)
  • SortedDictionary<T, T> does not allow multiple instances which are considered the same by IComparable<T> (2) and does not allow accessing the first/last element directly (5), nor does it have predecessor/successor functions (6)
  • Inheriting from List<T> and keeping it sorted would possibly be an option, but I'd rather use something that is built-in (7) due to the implementation effort and the potentially poor performance

Am I overlooking something? There seem to be no other relevant data structures for my use case (I looked here and here, among others). Is there another data structure which I did not consider?

Community
  • 1
  • 1

1 Answers1

0

If you're willing to relax 5 and 6 then you might be able to use a TreeMap

pgpb.padilla
  • 2,318
  • 2
  • 20
  • 44