ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

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

2019-10-10 22:00:02  阅读:287  来源: 互联网

标签:java c-2


我有使用SortedMap.tailMap的Java代码.在我的移植代码中,我有SortedMap = Dictionary< IComparable,value>.我需要一种在C#中复制/模仿tailMap的方法.

我已经想到了以下内容:

myDictionary.Where(p => p.Key.CompareTo(value)> = 0).ToDictionary

这将返回一个Dictionary,并且我需要返回SortedDictionary.我可以从Dictionary创建一个SortedDictionary,但是我觉得应该已经有一种更优雅,更完美的方法来做到这一点.

另一个想法是做类似的事情

var newSD = new SortedDictionary<k,v>();
foreach (var p in oldDictionary.Where(p => p.Key.CompareTo(value) >= 0))
    newSD.Add(p.Key, p.Value);

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

还有其他想法吗?

> Java SortedMap
> C# SortedDictionary

解决方法:

我过去几次需要此功能,而afaik做到这一点最简单的方法是

>创建并填充索引可访问的SortedList<K,V>(与SortedDictionary相反,这就是为什么您不能在此处使用它)
>在IList<K> SortedList.Keys上使用suitable*) BinarySearch method
>通过IList<V> SortedList.Values访问值
>将2和3.包装到扩展方法IEnumerable< KeyValuePair< K,V>中.尾巴(此SortedList< K,V>列表,K fromKey,包含布尔值= true)
>将其张贴在此处作为答案,这样我就可以复制并粘贴自此以后,我刚刚实现了Head and Tail并附加了代码-参见下文.另外,我添加了TryGetCeiling / Floor / Higher / LowerValue方法-从Java的NavigableMap派生的名称,为需要它们的任何人添加TryGetKey和TryGetEntry都很简单.

*)不幸的是,.Net自己的实现仅适用于列表,不适用于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)
    {
        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-2
来源: https://codeday.me/bug/20191010/1888459.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有