简体   繁体   English

寻找.Net排序的集合,可以访问上一个和下一个元素

[英]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: 我正在实现Bentley-Ottman算法 ,该算法要求扫描线(SL)具有以下属性的数据结构:

  • maintain a sorted collection of T , where T is IComparable<T> , 维护T的排序集合,其中TIComparable<T>
  • insertion of elements should be O(log count) , and should return whether the element is already inserted, 元素的插入应为O(log count) ,并应返回元素是否已插入,
  • deletion of elements should be O(log count) , 删除元素应为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. 对于给定的元素e (无论是否已经在集合中),我需要按排序顺序在e旁边的集合的上一个和下一个元素。

SortedList<TKey, TValue> has an O(count) at insertion and deletion, since it has to move all consecutive elements in the list. SortedList<TKey, TValue>在插入和删除时具有O(count) ,因为它必须移动列表中的所有连续元素。 However, I could index it, and therefore get the previous and next elements in O(1) , once I know the index of e . 但是,我可以索引它,因此一旦知道e的索引,就可以获取O(1)的上一个和下一个元素。

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. SortedDictionary<TKey, TValue>SortedSet<T>具有O(log count)插入和删除,但是我找不到任何迭代器可以为我提供下一个和上一个元素。

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. LinkedList<T>不允许二进制搜索。 List<T> still has the O(count) insertion/deletion. List<T>仍然具有O(count)插入/删除。 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 例如, TreeDictionary中的C5收集库的NuGetgithub上有一个

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; 如果存在k的前置变量,则bool TryPredecessor(K k,out of KeyValuePair res)返回true,在这种情况下,将前任绑定到res; otherwise returns false and binds the default value of KeyValuePair to res. 否则返回false并将KeyValuePair的默认值绑定到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. 根据密钥比较器,k的前身是排序字典中的最大密钥严格小于k的条目。 Throws NoSuchItemException if k does not have a predecessor entry; 如果k没有前置项,则抛出NoSuchItemException;否则,抛出NoSuchItemException。 that is, no key is less than k. 即,任何密钥都不小于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; 如果存在k的后继,则bool TrySuccessor(K k,out of KeyValuePair res)返回true,在这种情况下,将后继绑定到res; otherwise returns false and binds the default value of KeyValuePair to res. 否则返回false并将KeyValuePair的默认值绑定到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. k的后继项是排序字典中的条目,根据密钥比较器,其最小密钥严格大于k。 Throws NoSuchItemException if k does not have a successor; 如果k没有后继,则抛出NoSuchItemException;否则,抛出NoSuchItemException。 that is, no entry in the dictionary has a key that is greater than k. 也就是说,字典中的任何条目都不具有大于k的键。

and should have nearly everything you need. 并应具备您所需的几乎所有东西。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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