Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/398.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
相当于Java';C#SortedDictionary中的s SortedMap.tailMap_Java_C# - Fatal编程技术网

相当于Java';C#SortedDictionary中的s SortedMap.tailMap

相当于Java';C#SortedDictionary中的s SortedMap.tailMap,java,c#,Java,C#,我有使用SortedMap.tailMap的java代码。在我的移植代码中,我有SortedMap=字典。我需要一种在C#中复制/模拟尾图的方法 我的想法如下: { { 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 },

我有使用
SortedMap.tailMap
的java代码。在我的移植代码中,我有
SortedMap
=
字典
。我需要一种在C#中复制/模拟尾图的方法

我的想法如下:

{ { 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):
myDictionary.Where(p=>p.Key.CompareTo(value)>=0.ToDictionary

这将返回一个
字典
,我需要返回一个
SortedDictionary
。我可以从
字典
中创建一个
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);
var newSD=newsorteddictionary();
foreach(旧字典中的var p.Where(p=>p.Key.CompareTo(value)>=0))
newSD.Add(p.Key,p.Value);
这应该行得通,我不确定在构建列表时,按排序顺序添加值会如何影响插入的计时

还有其他想法吗


    • 有一个尾部地图。这就是你真正想要的吗?

      我在过去几次需要这个功能,而且最简单的方法是

    • 创建并填充可通过索引访问的(与SortedDictionary相反,这就是为什么不能在此处使用它的原因)
    • 使用
    • 通过以下方式访问值:
    • 包裹2。三,。向上进入扩展方法
      IEnumerable Tail(此SortedList列表,K fromKey,bool inclusive=true)
    • 把它作为答案贴在这里,这样我就可以复制并粘贴它了。从今以后,我只是实现了
      Head
      Tail
      并附加了代码-见下文。此外,我还添加了TryGetCeiling/Floor/Higher/LowerValue方法——这些方法的名称源自Java,为需要它们的任何人添加TryGetKey和TryGetEntry都很简单
    • *)不幸的是,.Net自己的实现只能在
      List
      s上运行,而不能在
      IList
      s上运行。真是浪费。。。此外,确保使用返回
      ~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(); }
          }
      }
      

      抱歉,我正在寻找反面-C#中的tailMap(我的理解是TreeMap=>C#中的SortedDictionary,但遗憾的是,SortedDictionary似乎没有tailMap非常有用!谢谢
      { { 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):