相关疑难解决方法(0)

SortedList和SortedDictionary有什么区别?

a SortedList<TKey,TValue>和a 之间是否有任何实际的区别SortedDictionary<TKey,TValue>?在任何情况下你会专门使用一个而不是另一个吗?

.net c# generics sortedlist sorteddictionary

251
推荐指数
6
解决办法
9万
查看次数

为什么SortedList <TKey,TValue>不使用指针值?

所以我正在通过执行SortedList<TKey, TValue>和实现Add(Insert下面显示的调用)真的让我感到惊讶.

Add方法进行明显的二元搜索以确定KVP应该进入的索引,但Insert 似乎可以显着改进(尽管在更大的范围内):

private void Insert(int index, TKey key, TValue value)
{
  if (this._size == this.keys.Length)
    this.EnsureCapacity(this._size + 1);
  if (index < this._size)
  {
    Array.Copy((Array) this.keys, index, (Array) this.keys, index + 1, this._size - index);
    Array.Copy((Array) this.values, index, (Array) this.values, index + 1, this._size - index);
  }
  this.keys[index] = key;
  this.values[index] = value;
  ++this._size;
  ++this.version;
}
Run Code Online (Sandbox Code Playgroud)

如果我正确地阅读这个,并且我保留在任何时候都是错的权利,这是一个O(2n)操作.

在我看来,应该用指针实现.像那种LinkedList …

.net c# data-structures

10
推荐指数
1
解决办法
235
查看次数

如何从SortedDictionary获取最接近我的键的项目?

目前我正在使用SortedList<T,U>针对特定数字的二进制搜索,如果它不存在,我将获得最接近的下限键项.

我看到插入未分类数据的速度相当慢,我正在做很多事情.

有没有办法做类似的事情SortedDictionary,或者我应该坚持我的SortedList

.net c# collections performance

7
推荐指数
1
解决办法
2991
查看次数

C#合并两个SortedLists(Union?)

我正在寻找加速合并两个代码的代码SortedLists.

C#4.0通用SortedList:http://msdn.microsoft.com/en-us/library/ms132319( v = vs.100).aspx

public Trait getTrait(decimal thisValue)
{       
    if (ParentStructure != null && ParentStructure.RankedTraits.Count > 0)
    {
        SortedList<decimal, Trait> tempTraits = this.RankedTraits;

        // Improve here (union?)
        foreach (KeyValuePair<decimal, Trait> kvp in (ParentStructure.RankedTraits))
        {
            if (!tempTraits.ContainsKey(kvp.Key)) 
            { 
                tempTraits.Add(kvp.Key, kvp.Value); 
            }
        }
        return _getTrait(tempTraits, thisValue);
        }
    }
    return _getTrait(_rankTraits, thisValue);
}
Run Code Online (Sandbox Code Playgroud)

我认为联合而不是foreach循环会更快,但我不知道如何实现一个联合SortedList.如果有人可以帮助我,我会很感激.

此外,如果有更好的方法来做到这一点,我愿意接受建议.

c# sortedlist c#-4.0

6
推荐指数
1
解决办法
4412
查看次数

LINQ:获取具有两个/多个值中最高的元素

我有一个列表,其中每个元素包含两个值(V1和V2).我需要的是具有最高V1和最高V2(优先级为V1)的元素.

我尝试了两种方法:

  1. OrderByDescending和ThenByDescending,然后取第一个元素:

    list.OrderByDescending(e => e.V1).ThenByDescending(e => e.V2).First();
    
    Run Code Online (Sandbox Code Playgroud)
  2. 选择具有最大V1的元素,然后从该枚举中选择具有最大V2的第一个元素:

    var maxV1 = l.Where(e => e.V1 == l.Max(e => e.V1));
    maxV1.First(e => e.V2 == maxV1.Max(e1 => e1.V2));
    
    Run Code Online (Sandbox Code Playgroud)

两者(在我的用例中)需要相当长的时间,我对我的任何一种解决方案都不满意.

列表本身不包含很多元素,不超过100个.但是它们有很多.

还有另一个,最好是更有效的解决方案,而不是我已经尝试过的解决方案吗?或者我是否需要重新考虑整个架构?

编辑:我忘了提到每个元素中有更多变量可用于选择最高值.使用哪一个取决于参数.因此,使用已排序集合进行预排序并不会带来任何好处

c# linq sorting optimization

5
推荐指数
1
解决办法
1355
查看次数