.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 中没有找到类似的东西。我猜你已经知道我打算使用这个类来管理数据库有序记录集操作:) 谢谢。
I'm in need of very specific class, I would really like to know if there is existing one, so I don't have to re-implement it.
I have a set of items. Each item has a numeric value associated whit it - weight. Weight of each item is unique within set. Items must be sorted by weight. Weight can be modified for each item, but the operation to change weight is extremely expensive. There is an operation, which executes on set frequently - move range of items within set, by modifying item's weight.
So I need a List kind class, but with built-in logic to manage items weights. Weight sequence must be sparse to minimize weight collisions on move operations and improve performance by minimizing weight change operations.
Class interface should look like that:
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.
}
Haven't found anything similar in framework or PowerCollections. I guess you already figured out that I intend to use this class to manage database ordered record set operations :)
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以在内部使用
SortedList
。它不允许您修改键,但是当您想要修改某个项目的重量时,您可以按旧重量删除该项目,然后按新重量再次插入。我不确定这是否会对性能造成太大影响。You could internally use a
SortedList<int, T>
. It doesn't allow you to modify the keys, but when you want to modify an item's weight, you could remove the item at the old weight and insert it again at the new weight. I'm not sure if that would cause too much of a performance hit though.