我有使用SortedMap.tailMap
的java代码。在我的移植代码中,我有SortedMap
= Dictionary<IComparable, value>
。我需要一种在C#中复制/模仿tailMap的方法。
我认为如下:
myDictionary.Where(p => p.Key.CompareTo(value) >= 0).ToDictionary
这会返回Dictionary
,我需要返回SortedDictionary
。我可以从SortedDictionary
创建一个Dictionary
,但我觉得应该有一个更优雅和性能更好的方法,因为它已经排序了。
另一个想法是做类似
的事情var newSD = new SortedDictionary<k,v>();
foreach (var p in oldDictionary.Where(p => p.Key.CompareTo(value) >= 0))
newSD.Add(p.Key, p.Value);
这应该有效,我不确定在构建该列表时,排序顺序中的值的添加将如何影响插入的时间。
还有其他想法吗?
答案 0 :(得分:0)
TreeMap有尾贴图。那是你真正想要的吗?
答案 1 :(得分:0)
我过去曾经多次需要这个功能,而最简单的方法就是
IEnumerable<KeyValuePair<K, V>> Tail(this SortedList<K, V> list, K fromKey, bool inclusive = true)
Head
和Tail
并附加了代码 - 请参阅下文。另外,我添加了TryGetCeiling / Floor / Higher / LowerValue方法 - 从Java的NavigableMap派生的名称,并且可以直接为需要它们的人添加TryGet Key和TryGet 条目。 *)不幸的是,.Net自己的实现仅适用于List
,而不适用于IList
。多么浪费...另外,请确保使用一个返回~x
以防止找不到该项目的项目,例如我链接的项目。
public static class SortedListEx
{
public static IEnumerable<KeyValuePair<K, V>> Head<K, V>(
this SortedList<K, V> list, K toKey, bool inclusive = true)
{
https://stackoverflow.com/a/2948872/709537 BinarySearch
var binarySearchResult = list.Keys.BinarySearch(toKey);
if (binarySearchResult < 0)
binarySearchResult = ~binarySearchResult;
else if (inclusive)
binarySearchResult++;
return System.Linq.Enumerable.Take(list, binarySearchResult);
}
public static IEnumerable<KeyValuePair<K, V>> Tail<K, V>(
this SortedList<K, V> list, K fromKey, bool inclusive = true)
{
https://stackoverflow.com/a/2948872/709537 BinarySearch
var binarySearchResult = list.Keys.BinarySearch(fromKey);
if (binarySearchResult < 0)
binarySearchResult = ~binarySearchResult;
else if (!inclusive)
binarySearchResult++;
return new ListOffsetEnumerable<K, V>(list, binarySearchResult);
}
public static bool TryGetCeilingValue<K, V>(
this SortedList<K, V> list, K key, out V value)
{
var binarySearchResult = list.Keys.BinarySearch(key);
if (binarySearchResult < 0)
binarySearchResult = ~binarySearchResult;
if (binarySearchResult >= list.Count)
{
value = default(V);
return false;
}
value = list.Values[binarySearchResult];
return true;
}
public static bool TryGetHigherValue<K, V>(
this SortedList<K, V> list, K key, out V value)
{
var binarySearchResult = list.Keys.BinarySearch(key);
if (binarySearchResult < 0)
binarySearchResult = ~binarySearchResult;
else
binarySearchResult++;
if (binarySearchResult >= list.Count)
{
value = default(V);
return false;
}
value = list.Values[binarySearchResult];
return true;
}
public static bool TryGetFloorValue<K, V>(
this SortedList<K, V> list, K key, out V value)
{
var binarySearchResult = list.Keys.BinarySearch(key);
if (binarySearchResult < 0)
binarySearchResult = ~binarySearchResult;
else
binarySearchResult++;
if (binarySearchResult >= list.Count)
{
value = default(V);
return false;
}
value = list.Values[binarySearchResult];
return true;
}
public static bool TryGetLowerValue<K, V>(
this SortedList<K, V> list, K key, out V value)
{
var binarySearchResult = list.Keys.BinarySearch(key);
if (binarySearchResult < 0)
binarySearchResult = ~binarySearchResult;
if (binarySearchResult >= list.Count)
{
value = default(V);
return false;
}
value = list.Values[binarySearchResult];
return true;
}
class ListOffsetEnumerable<K, V> : IEnumerable<KeyValuePair<K, V>>
{
private readonly SortedList<K, V> _sortedList;
private readonly int _offset;
public ListOffsetEnumerable(SortedList<K, V> sortedList, int offset)
{
_sortedList = sortedList;
_offset = offset;
}
public IEnumerator<KeyValuePair<K, V>> GetEnumerator()
{
return new ListOffsetEnumerator<K, V>(_sortedList, _offset);
}
IEnumerator IEnumerable.GetEnumerator() { return GetEnumerator(); }
}
class ListOffsetEnumerator<K, V> : IEnumerator<KeyValuePair<K, V>>
{
private readonly SortedList<K, V> _sortedList;
private int _index;
public ListOffsetEnumerator(SortedList<K, V> sortedList, int offset)
{
_sortedList = sortedList;
_index = offset - 1;
}
public bool MoveNext()
{
if (_index >= _sortedList.Count)
return false;
_index++;
return _index < _sortedList.Count;
}
public KeyValuePair<K, V> Current
{
get
{
return new KeyValuePair<K, V>(
_sortedList.Keys[_index],
_sortedList.Values[_index]);
}
}
object IEnumerator.Current { get { return Current; } }
public void Dispose() { }
public void Reset() { throw new NotSupportedException(); }
}
}
这是一个简单的测试
SortedList<int, int> l =
new SortedList<int, int> { { 1, 1 }, { 3, 3 }, { 4, 4 } };
for (int i = 0; i <= 5; i++)
{
Console.WriteLine("{ { 1, 1 }, { 3, 3 }, { 4, 4 } }.Head( " +i+ ", true): "
+ string.Join(", ", l.Head(i, true)));
Console.WriteLine("{ { 1, 1 }, { 3, 3 }, { 4, 4 } }.Head( " +i+ ", false): "
+ string.Join(", ", l.Head(i, false)));
}
for (int i = 0; i <= 5; i++)
{
Console.WriteLine("{ { 1, 1 }, { 3, 3 }, { 4, 4 } }.Tail( " +i+ ", true): "
+ string.Join(", ", l.Tail(i, true)));
Console.WriteLine("{ { 1, 1 }, { 3, 3 }, { 4, 4 } }.Tail( " +i+ ", false): "
+ string.Join(", ", l.Tail(i, false)));
}
打印以下内容:
{ { 1, 1 }, { 3, 3 }, { 4, 4 } }.Head( 0, true):
{ { 1, 1 }, { 3, 3 }, { 4, 4 } }.Head( 0, false):
{ { 1, 1 }, { 3, 3 }, { 4, 4 } }.Head( 1, true): [1, 1]
{ { 1, 1 }, { 3, 3 }, { 4, 4 } }.Head( 1, false):
{ { 1, 1 }, { 3, 3 }, { 4, 4 } }.Head( 2, true): [1, 1]
{ { 1, 1 }, { 3, 3 }, { 4, 4 } }.Head( 2, false): [1, 1]
{ { 1, 1 }, { 3, 3 }, { 4, 4 } }.Head( 3, true): [1, 1], [3, 3]
{ { 1, 1 }, { 3, 3 }, { 4, 4 } }.Head( 3, false): [1, 1]
{ { 1, 1 }, { 3, 3 }, { 4, 4 } }.Head( 4, true): [1, 1], [3, 3], [4, 4]
{ { 1, 1 }, { 3, 3 }, { 4, 4 } }.Head( 4, false): [1, 1], [3, 3]
{ { 1, 1 }, { 3, 3 }, { 4, 4 } }.Head( 5, true): [1, 1], [3, 3], [4, 4]
{ { 1, 1 }, { 3, 3 }, { 4, 4 } }.Head( 5, false): [1, 1], [3, 3], [4, 4]
{ { 1, 1 }, { 3, 3 }, { 4, 4 } }.Tail( 0, true): [1, 1], [3, 3], [4, 4]
{ { 1, 1 }, { 3, 3 }, { 4, 4 } }.Tail( 0, false): [1, 1], [3, 3], [4, 4]
{ { 1, 1 }, { 3, 3 }, { 4, 4 } }.Tail( 1, true): [1, 1], [3, 3], [4, 4]
{ { 1, 1 }, { 3, 3 }, { 4, 4 } }.Tail( 1, false): [3, 3], [4, 4]
{ { 1, 1 }, { 3, 3 }, { 4, 4 } }.Tail( 2, true): [3, 3], [4, 4]
{ { 1, 1 }, { 3, 3 }, { 4, 4 } }.Tail( 2, false): [3, 3], [4, 4]
{ { 1, 1 }, { 3, 3 }, { 4, 4 } }.Tail( 3, true): [3, 3], [4, 4]
{ { 1, 1 }, { 3, 3 }, { 4, 4 } }.Tail( 3, false): [4, 4]
{ { 1, 1 }, { 3, 3 }, { 4, 4 } }.Tail( 4, true): [4, 4]
{ { 1, 1 }, { 3, 3 }, { 4, 4 } }.Tail( 4, false):
{ { 1, 1 }, { 3, 3 }, { 4, 4 } }.Tail( 5, true):
{ { 1, 1 }, { 3, 3 }, { 4, 4 } }.Tail( 5, false):