栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

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

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

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

  1. 创建并填充一个可通过索引访问的SortedList (与SortedDictionary不同,这就是为什么您不能在此处使用它)
  2. 在IList SortedList.Keys上使用合适的*)BinarySearch方法
  3. 通过IList SortedList.Values访问该值
  4. 将2.和3.包装成扩展方法
    IEnumerable<KeyValuePair<K, V>> Tail(this SortedList<K, V> list, K fromKey, bool inclusive = true)
  5. 张贴在这里的答案,所以我可以复制和粘贴从此以后 我只是执行
    Head
    Tail
    和附加的代码-见下文。此外,我还添加了TryGetCeiling / Floor / Higher / LowerValue方法-从Java的NavigableMap派生的名称,为需要它们的任何人添加TryGet Key和TryGet Entry都非常简单。

*)不幸的是,.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):


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/428668.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号