Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 是否有一种方法可以将(log(n))项快速插入任何ILST<;T>;_C#_List_Performance_Generics - Fatal编程技术网

C# 是否有一种方法可以将(log(n))项快速插入任何ILST<;T>;

C# 是否有一种方法可以将(log(n))项快速插入任何ILST<;T>;,c#,list,performance,generics,C#,List,Performance,Generics,在WPF中,我希望保持我的ObservableCollection排序。项目是随机添加的,不是批量添加的。我不想每次都对整个IList进行排序。我想在每个插页的正确位置插入 是否有一种方法可以将项目快速(log(n))插入T的任何IList 有人为它编写代码吗 注意:是的,在调用此方法之前,应该已经对列表进行了排序 注2:有时您必须使用IList,不能用SortedList替换它,并且不希望维护2个集合:例如:在WPF中使用ObservableCollection 更新,是的,有办法做到这一点。

在WPF中,我希望保持我的ObservableCollection排序。项目是随机添加的,不是批量添加的。我不想每次都对整个IList进行排序。我想在每个插页的正确位置插入

是否有一种方法可以将项目快速(log(n))插入T的任何IList

有人为它编写代码吗

注意:是的,在调用此方法之前,应该已经对列表进行了排序

注2:有时您必须使用IList,不能用SortedList替换它,并且不希望维护2个集合:例如:在WPF中使用ObservableCollection


更新,是的,有办法做到这一点。通过使用二分法。我问这个问题是为了节省时间。我正在编写它,并将在调试后立即显示它。

好的。。。在有人结束我的问题之前,我就这么做了(这几乎发生在2票结束之前)

虽然有两个人投票决定结束,但我认为这是一个好问题,我也认为代码值得保留

我花了大约5个小时来做和调试。它似乎工作得很好。为了节省5个小时。。。这就是我问这个问题的原因。为了节省时间

/// <summary>
/// Insert item in list in log(n). The list should already be sorted.
/// Item will be inserted and there will be duplicate if comparer return 0.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="list"></param>
/// <param name="item"></param>
/// <param name="comparer"></param>
/// <returns></returns>
public static int InsertInSortedList<T>(this IList<T> list, T item, Func<T, T, int> comparer = null)
{
    if (comparer == null)
    {
        comparer = Comparer<T>.Default.Compare;
    }

    int first = 0;
    int last = list.Count;
    int middle = 0;
    int compareResult = 0;

    if (last > 0)
    {
        while (true)
        {
            middle = first + ((last - first) / 2);

            compareResult = comparer(item, list[middle]);

            if (compareResult > 0)
            {
                first = middle + 1;

                if (first >= last)
                {
                    middle++;
                    break;
                } 

                continue;
            }

            if (compareResult < 0)
            {
                last = middle;

                if (first == last)
                {
                    break;
                }

                continue;
            }

            break;
        }
    }

    if (middle == list.Count)
    {
        list.Add(item);
    }
    else
    {
        list.Insert(middle, item);
    }

    return middle;

}
//
///在日志(n)的列表中插入项目。应该已经对列表进行了排序。
///将插入项,如果比较器返回0,则将有重复项。
/// 
/// 
/// 
/// 
/// 
/// 
公共静态int insertedList(此IList列表,T项,Func comparer=null)
{
if(比较器==null)
{
comparer=comparer.Default.Compare;
}
int first=0;
int last=list.Count;
int-middle=0;
int compareResult=0;
如果(上次>0)
{
while(true)
{
中间=第一+((最后-第一)/2);
compareResult=比较器(项目,列表[中间]);
如果(比较结果>0)
{
第一个=中间+1;
如果(第一个>=最后一个)
{
中级++;
打破
} 
继续;
}
如果(比较结果<0)
{
最后=中间;
如果(第一个==最后一个)
{
打破
}
继续;
}
打破
}
}
if(中间==list.Count)
{
列表。添加(项目);
}
其他的
{
列表。插入(中间,项目);
}
返回中间;
}

好的。。。在有人结束我的问题之前,我就这么做了(这几乎发生在2票结束之前)

虽然有两个人投票决定结束,但我认为这是一个好问题,我也认为代码值得保留

我花了大约5个小时来做和调试。它似乎工作得很好。为了节省5个小时。。。这就是我问这个问题的原因。为了节省时间

/// <summary>
/// Insert item in list in log(n). The list should already be sorted.
/// Item will be inserted and there will be duplicate if comparer return 0.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="list"></param>
/// <param name="item"></param>
/// <param name="comparer"></param>
/// <returns></returns>
public static int InsertInSortedList<T>(this IList<T> list, T item, Func<T, T, int> comparer = null)
{
    if (comparer == null)
    {
        comparer = Comparer<T>.Default.Compare;
    }

    int first = 0;
    int last = list.Count;
    int middle = 0;
    int compareResult = 0;

    if (last > 0)
    {
        while (true)
        {
            middle = first + ((last - first) / 2);

            compareResult = comparer(item, list[middle]);

            if (compareResult > 0)
            {
                first = middle + 1;

                if (first >= last)
                {
                    middle++;
                    break;
                } 

                continue;
            }

            if (compareResult < 0)
            {
                last = middle;

                if (first == last)
                {
                    break;
                }

                continue;
            }

            break;
        }
    }

    if (middle == list.Count)
    {
        list.Add(item);
    }
    else
    {
        list.Insert(middle, item);
    }

    return middle;

}
//
///在日志(n)的列表中插入项目。应该已经对列表进行了排序。
///将插入项,如果比较器返回0,则将有重复项。
/// 
/// 
/// 
/// 
/// 
/// 
公共静态int insertedList(此IList列表,T项,Func comparer=null)
{
if(比较器==null)
{
comparer=comparer.Default.Compare;
}
int first=0;
int last=list.Count;
int-middle=0;
int compareResult=0;
如果(上次>0)
{
while(true)
{
中间=第一+((最后-第一)/2);
compareResult=比较器(项目,列表[中间]);
如果(比较结果>0)
{
第一个=中间+1;
如果(第一个>=最后一个)
{
中级++;
打破
} 
继续;
}
如果(比较结果<0)
{
最后=中间;
如果(第一个==最后一个)
{
打破
}
继续;
}
打破
}
}
if(中间==list.Count)
{
列表。添加(项目);
}
其他的
{
列表。插入(中间,项目);
}
返回中间;
}

以下是一些用于分类收集的有用扩展方法(
SortedBinarySearch
SortedIndexOf
SortedInsert
SortedRemove
)。二进制搜索算法是从该方法的源代码中窃取的

///在已排序的
///指定项,并返回该项的从零开始的索引(如果找到);
///否则,将显示一个负数,该负数是
///下一个大于项目的项目,或者,如果没有更大的项目,
///的按位补码。
public static int SortedBinarySearch(此IList列表,T项,
IComparer比较器=空)
{
如果(list==null)抛出新的ArgumentNullException(nameof(list));
比较器=比较器??比较器。默认值;
int-lo=0;
int hi=list.Count-1;
而(lo>1);
int order=comparer.Compare(列表[i],项);
如果(订单==0)返回i;
如果(顺序<0)
{
lo=i+1;
}
其他的
{
hi=i-1;
}
}
返回~lo;
}
///在已排序的列表中搜索指定的项
///并返回该项的从零开始的索引
///如有发现,;否则,-1。
公共静态int-SortedIndexOf(此IList列表,T项,
IComparer比较器=空)
{
int index=SortedBinarySearch(列表、项目、比较器);
如果(指数<0)返回-1;
收益指数;
}
///将项目插入到已排序的列表中。
公共静态内部分拣机(此IList列表,T项,
IComparer比较器=空)
{
int index=SortedBinarySearch(列表、项目、比较器);
如果(索引<0)索引=~index;
列表。插入(索引,项目);
收益指数;
}
///从排序列表中删除项目并返回
///如果项目已成功删除,则为true;否则,错误。
公共静态bool SortedRemove(此IList l