Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/274.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# 非精确二进制搜索:给定一个值,查找元素位置的上下索引_C#_Binary Search - Fatal编程技术网

C# 非精确二进制搜索:给定一个值,查找元素位置的上下索引

C# 非精确二进制搜索:给定一个值,查找元素位置的上下索引,c#,binary-search,C#,Binary Search,我有一个列表,列表按KeyValuePair.Key排序,因此可以进行二进制搜索。我有一个double对象。现在,我的任务是找到double对象的索引。以下是适用的条件: 如果该double对象在指定公差内与KeyValuePair.Key中的一个匹配,则应返回相应的KeyValuePair.Value 如果double对象超出了KeyValuePair.Key的最大和最小范围,则应返回0 如果double对象落在KeyValuePair.Key的最大最小值范围内,但未在指定公差范围内匹配任何K

我有一个
列表
列表按
KeyValuePair.Key
排序,因此可以进行二进制搜索。我有一个
double
对象。现在,我的任务是找到
double
对象的索引。以下是适用的条件:

  • 如果该
    double
    对象在指定公差内与
    KeyValuePair.Key
    中的一个匹配,则应返回相应的
    KeyValuePair.Value
  • 如果
    double
    对象超出了
    KeyValuePair.Key
    的最大和最小范围,则应返回0
  • 如果
    double
    对象落在
    KeyValuePair.Key
    的最大最小值范围内,但未在指定公差范围内匹配任何
    KeyValuePair.Key
    ,则获取最接近的上限和最接近的下限
    KeyValuePair.Value
    (由
    KeyValuePair.Key
    测量)的平均值

  • 我知道C#中提供了二进制搜索实现,但它并不完全适合我的需要。我想问,是否有任何已经满足我需求的实施方案?我不想花几个小时来编写和调试其他人已经编写、调试和完善的代码

    列表周围使用比较器和一个小包装器可以非常容易地完成此操作。BinarySearch

    static double Search(List<KeyValuePair<double, double>> list, double key) {
        int index = list.BinarySearch(
            new KeyValuePair<double, double>(key, 0), 
            new Comparer());
    
         // Case 1
         if (index >= 0)
             return list[index].Value;
    
         // NOTE: if the search fails, List<T>.BinarySearch returns the 
         // bitwise complement of the insertion index that would be used
         // to keep the list sorted.
         index = ~index;
    
         // Case 2
         if (index == 0 || index == list.Count)
            return 0;
    
         // Case 3
         return (list[index - 1].Value + list[index].Value) / 2;
     }
    
     class Comparer : IComparer<KeyValuePair<double, double>> {
         public int Compare(
             KeyValuePair<double, double> x, 
             KeyValuePair<double, double> y) 
         {
             if (Math.abs(x.Key - y.Key) < TOLERANCE)
                 return 0;
    
             return x.Key.CompareTo(y.Key);
         }
     }
    
    静态双重搜索(列表,双键){
    int index=list.BinarySearch(
    新的KeyValuePair(键,0),
    新比较器());
    //案例1
    如果(索引>=0)
    返回列表[索引]。值;
    //注意:如果搜索失败,List.BinarySearch将返回
    //将使用的插入索引的按位补码
    //使列表保持有序。
    索引=~index;
    //案例2
    if(index==0 | | index==list.Count)
    返回0;
    //案例3
    返回(list[index-1].Value+list[index].Value)/2;
    }
    类比较器:IComparer{
    公共整数比较(
    键值对x,
    键值(y)
    {
    if(数学绝对值(x键-y键)<公差)
    返回0;
    返回x.Key.CompareTo(y.Key);
    }
    }
    
    您可能需要注意的唯一一件事是列表包含重复的id:如果列表包含多个具有相同值的元素,则该方法仅返回一个实例,并且可能返回任何一个实例,而不一定是第一个。很好的一个。。。我没有意识到,
    BinarySearch
    实际上返回插入索引的位补码。。。谢谢@Mitch,对于我的应用程序,所有的
    键都是唯一的,所以没有这种担心。@Mitch非常好!另外,感谢您指出,我在最初的解决方案中忘记了处理公差要求。