在C#SortedDictionary中等效于Java的SortedMap.tailMap

Pre*_*ott 5 c# java

我有使用的Java代码SortedMap.tailMap。在我的移植代码中,我有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);
Run Code Online (Sandbox Code Playgroud)

那应该行得通,我不确定在构建列表时按排序顺序添加值将如何影响插入的时间。

还有其他想法吗?

Eug*_*sky 1

我过去曾多次需要此功能,据我所知,最简单的方法是

  1. 创建并填充可通过索引访问的SortedList<K,V>(与 SortedDictionary 相比,这就是您不能在此处使用它的原因)
  2. IList<K> SortedList.Keys上使用合适的*) BinarySearch 方法
  3. 通过IList<V> SortedList.Values访问值
  4. 将 2. 和 3. 包装到扩展方法中IEnumerable<KeyValuePair<K, V>> Tail(this SortedList<K, V> list, K fromKey, bool inclusive = true)
  5. 将其作为答案发布在这里,以便我可以复制并粘贴它,从此我刚刚实现HeadTail附加了代码 - 见下文。另外,我添加了 TryGetCeiling/Floor/Higher/LowerValue 方法 - 名称源自 Java 的NavigableMap ,并且为需要它们的任何人添加 TryGet Key 和 TryGet Entry都很简单。

*) 不幸的是,.Net 自己的实现仅适用于Lists,而不适用于ILists。太浪费了……另外,一定要使用一个可以返回的东西,~x以防找不到该项目,就像我链接到的那样。

public static class SortedListEx
{
    public static IEnumerable<KeyValuePair<K, V>> Head<K, V>(
        this SortedList<K, V> list, K toKey, bool inclusive = true)
    {
        /sf/answers/206421071/ 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)
    {
        /sf/answers/206421071/ 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(); }
    }
}
Run Code Online (Sandbox Code Playgroud)

这是一个简单的测试

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)));
}
Run Code Online (Sandbox Code Playgroud)

打印以下内容:

{ { 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):
Run Code Online (Sandbox Code Playgroud)