.NET 的稀疏排序数字序列类

发布于 2024-09-29 03:20:15 字数 1830 浏览 1 评论 0原文

我需要非常具体的课程,我真的很想知道是否存在现有的课程,所以我不必重新实现它。 我有一套物品。每个项目都有一个与其关联的数值 - 重量。每件物品的重量在套装中都是唯一的。物品必须按重量分类。可以修改每个项目的重量,但是改变重量的操作是极其昂贵的。有一个操作经常在集合上执行 - 通过修改项目的重量来移动集合内项目的范围。 所以我需要一个列表类,但具有内置逻辑来管理项目权重。权重序列必须稀疏,以最大程度地减少移动操作中的权重冲突,并通过最小化权重更改操作来提高性能。 类接口应该如下所示:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

请远离我 2024-10-06 03:20:15

您可以在内部使用 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文