代码之家  ›  专栏  ›  技术社区  ›  Oliver

如何提高排序后列表<myitem>中项目与当前项目前后项目的比较?

  •  0
  • Oliver  · 技术社区  · 15 年前

    有人知道完成这项任务的好方法吗?

    目前,我正在做更多或更少的这种方式,但我对这段代码感到不满意,无法说我可以立即改进什么。

    所以如果有人 更聪明的 做这份工作的方式我很乐意知道。

    private bool Check(List<MyItem> list)
    {
        bool result = true;
        //MyItem implements IComparable<MyItem>
        list.Sort();
    
        for (int pos = 0; pos < list.Count - 1; pos++)
        {
            bool previousCheckOk = true;
            if (pos != 0)
            {
                if (!CheckCollisionWithPrevious(pos))
                {
                    MarkAsFailed(pos);
                    result = false;
                    previousCheckOk = false;
                }
                else
                {
                    MarkAsGood(pos);
                }
            }
    
            if (previousCheckOk && pos != list.Count - 1)
            {
                if (!CheckCollisionWithFollowing(pos))
                {
                    MarkAsFailed(pos);
                    result = false;
                }
                else
                {
                    MarkAsGood(pos);
                }
            }
        }
        return result;
    }
    
    private bool CheckCollisionWithPrevious(int pos)
    {
        bool checkOk = false;
        var previousItem = _Item[pos - 1];
    
        // Doing some checks ...
    
        return checkOk;
    }
    
    private bool CheckCollisionWithFollowing(int pos)
    {
        bool checkOk = false;
        var followingItem = _Item[pos + 1];
    
        // Doing some checks ...
    
        return checkOk;
    }
    

    更新

    在阅读了来自Aaronaught的答案和一个小周末来重新充满精神力量之后,我想出了下面的解决方案,现在看起来好多了(和我从Aaronaught得到的几乎一样):

    public bool Check(DataGridView dataGridView)
    {
        bool result = true;
        _Items.Sort();
    
        for (int pos = 1; pos < _Items.Count; pos++)
        {
            var previousItem = _Items[pos - 1];
            var currentItem = _Items[pos];
    
            if (previousItem.CollidesWith(currentItem))
            {
                dataGridView.Rows[pos].ErrorText = "Offset collides with item named " + previousItem.Label;
                result = false;
                sb.AppendLine("Line " + pos);
            }
        }
    
        dataGridView.Refresh();
        return result;
    }
    
    1 回复  |  直到 15 年前
        1
  •  1
  •   Aaronaught    15 年前

    当然可以减少重复:

    private bool Check(List<MyItem> list)
    {
        list.Sort();
    
        for (int pos = 1; pos < list.Count; pos++)
        {
            if (!CheckCollisionWithPrevious(list, pos))
            {
                MarkAsFailed();
                return false;
            }
            MarkAsGood();
        }
        return true;
    }
    
    private bool CheckCollisionWithPrevious(List<MyItem> list, int pos)
    {
        bool checkOk = false;
        var previousItem = list[pos - 1];
    
        // Doing some checks ...
    
        return checkOk;
    }
    

    假设 CheckCollisionWithPrevious CheckCollisionWithFollowing 执行基本上相同的比较,然后用更少的代码执行相同的函数。

    我还添加了 list 作为第二个函数的参数;在第一个函数中将其作为参数,但在其调用的函数中引用硬编码成员是没有意义的。如果你要取一个参数,那么把这个参数传下去。

    不过,就性能而言,每次发生这种情况时,您都要对列表进行排序;如果这种情况经常发生,那么最好从使用排序的集合开始。

    编辑:如果 整点 这个密码是 只是 如果你想查一些重复的钥匙 方式 最好使用一个数据结构来防止这种情况,比如 Dictionary<TKey, TValue> .

    推荐文章