简体   繁体   中英

Looking for .Net sorted collection with access to previous and next elements

I'm implementing the Bentley-Ottman algorithm which requires for the sweep line (SL) a data structure with the following properties:

  • maintain a sorted collection of T , where T is IComparable<T> ,
  • insertion of elements should be O(log count) , and should return whether the element is already inserted,
  • deletion of elements should be O(log count) ,
  • for a given element e (whether already in the collection or not) I need the previous and next element of the collection next to e in the sort order.

SortedList<TKey, TValue> has an O(count) at insertion and deletion, since it has to move all consecutive elements in the list. However, I could index it, and therefore get the previous and next elements in O(1) , once I know the index of e .

SortedDictionary<TKey, TValue> and SortedSet<T> have O(log count) insertion and deletion, but I cannot find any iterator that gives me the next and previous elements.

Is there any implementation that gives me the full functionality?

And if not, what would be the fastest way to implement it? LinkedList<T> does not allow a binary search. List<T> still has the O(count) insertion/deletion. Do I really have to implement my own balanced tree?

For example the TreeDictionary of the c5 collection library and nuget and github has a

bool TryPredecessor(K k, out KeyValuePair res) returns true if there is a precedessor of k and in that case binds the predecessor to res; otherwise returns false and binds the default value of KeyValuePair to res. The predecessor of k is the entry in the sorted dictionary with the greatest key strictly less than k according to the key comparer. Throws NoSuchItemException if k does not have a predecessor entry; that is, no key is less than k.

and a

bool TrySuccessor(K k, out KeyValuePair res) returns true if there is a successor of k and in that case binds the successor to res; otherwise returns false and binds the default value of KeyValuePair to res. The successor of k is the entry in the sorted dictionary with the least key strictly greater than k according to the key comparer. Throws NoSuchItemException if k does not have a successor; that is, no entry in the dictionary has a key that is greater than k.

and should have nearly everything you need.

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