3
我需要非常特定的类,我真的很想知道是否存在现成的类,所以我不必重新实现它。 我有一套物品。每个项目都有一个与数值相关联的数值 - 重量。每个项目的重量在集合内是唯一的。物品必须按重量分类。重量可以为每个项目修改,但改变重量的操作是非常昂贵的。有一个操作,频繁执行 - 通过修改项目的重量来移动项目内的项目范围。 所以我需要一个List类,但内置逻辑来管理项目的权重。重量序列必须是稀疏的,以减少移动操作中的重量冲突,并通过最小化重量变化操作来提高性能。 类接口看起来应该:.NET的稀疏排序数字序列类
public abstract class SparsedSequence<T> : IList<T>
{
// Weight increment for new items.
private int WeightIncrement = 10000;
protected abstract void OnWeightChanged(int weight, T item);
public void MoveRange(int offset, int count, int amount)
{
// There must be fancy weight management logic.
}
public void MoveRange(T[] range, int amount)
{
// Cut T[] to set of calls to MoveRange(int, int, int)
}
public int ConstraintAmount(int offset, int count, int amount)
{
// Returns amount constrainded by sequence size and 0,
// so that moved block will remain within proper range.
// If returns 0 - block unmovable in that direcion.
}
public int ConstraintAmount(T[] range, int amount)
{
// ----- " -----
}
public void Add(T newItem)
{
// Add to sequnce end.
// Defines new weight and calls OnWeightChanged.
// NewWeight = Weights[Count - 1] + WeightIncrement.
}
public void Add(T item, int weight)
{
// Adds item with forced weight.
}
public T this[int index]
{
// Get item
get { ... }
}
public IList<int> Weights
{
// Get items weights
get { ... }
}
public KeyValuePair<int, T> this[int index]
{
// Get item and weight
get { ... }
}
// Remove, clear, insert, indexof etc.
}
没有找到框架或PowerCollections类似的事情。我想你已经知道我打算使用这个类来管理数据库有序记录集操作:) 谢谢。
不是很好的主意,因为排序(不知道是否有这样的词)不是主要要求。主要 - 是高效的MoveRange操作实现。在SortedList只能移动范围的情况下 - 就是调用Add/Remove两次移动元素的数量。 – 2010-11-02 11:08:07
我这种情况下,你可能会想使用某种链接列表,但使用内置的'LinkedList'可能无法工作,因为你一次只能删除一个节点。如果你想移除节点块并重新定位它们,你可能必须实现一个自定义集合类。 –
2010-11-02 13:32:23
集合中的元素的对齐不是问题。我会在内部使用T []和Array.XXX方法。问题是 - 我真的不想实现体重管理逻辑 - 它需要沉重的数学和统计优化来减少重量冲突。基本逻辑可以分为方法AllocateRange(int startIndex,int EndIndex,int amount)以确保所需的自由范围权重的数字和RelocateRange(int rangeStart,int amount)来移动元素权重。那么只需按重量来衡量所有的序列,这就是全部。 AllocateRange是一个难以实现的方法。 – 2010-11-10 13:01:40