简体   繁体   中英

Appropriate list-like sorted data structure

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

  1. Generic, ie, 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?

如果您愿意放松5和6,则可以使用TreeMap

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM