java - C# SortedDictionary 中 Java 的 SortedMap.tailMap 的等价物

标签 java c#

我有使用 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);

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

还有其他想法吗?

最佳答案

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

  1. 创建并填充 SortedList<K,V>它可以通过索引访问(与 SortedDictionary 不同,这就是为什么你不能在这里使用它)
  2. 使用 suitable*) BinarySearch methodIList<K> SortedList.Keys
  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。 ,并且可以直接为任何需要它们的人添加 TryGetKey 和 TryGetEntry。

*) 不幸的是,.Net 自己的实现仅适用于 List s,不在 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):

关于java - C# SortedDictionary 中 Java 的 SortedMap.tailMap 的等价物,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26079025/

相关文章:

java - Libgdx某些字体字母不出现

java - Spring Rest Controller 字符串响应

c# - Windows 窗体 : What's the right way to allow overlapping Controls?

C# - Resources.GetDrawable 在 Android Xamarin 中已过时

c# - -TargetDatabase 标志是否已在 EF 4.3 迁移中重命名?

C#将一条记录从一个字典复制到另一个

java - 如何用选定的颜色填充网格上选定的单元格?

java - 各种小HashSet和1个大HashSet的搜索差异是什么?

java - 显示图像标签的SRC属性为相对路径的图像

c# - 银行开户申请