使用 Array.BinarySearch() 返回第一个值 <= 查找值?

发布于 2024-09-13 18:09:17 字数 679 浏览 10 评论 0 原文

我正在尝试创建一个“查找”列,该列将返回等于或小于正在查找的值的数组值的索引。这是我的尝试,看起来效果很好,但我想知道是否有更干净的方法?

// Sorted
float[] ranges = new float[]
  {
     0.8f,
     1.1f,
     2.7f,
     3.9f,
     4.5f,
     5.1f,
  };


private int GetIndex(float lookupValue)
{
    int position = Array.BinarySearch(ranges, lookupValue);
    if (position < 0)
    {
        // Find the highest available value that does not
        // exceed the value being looked up.
        position = ~position - 1;
    }

    // If position is still negative => all values in array 
    // are greater than lookupValue, return 0
    return position < 0 ? 0 : position;
}

谢谢。

I'm trying to create a "lookup" column that would return the index of the array value that is equal to or less than the value being looked up. So this is my attempt, which seems to work fine, but I was wondering if there is a cleaner way of doing it ?

// Sorted
float[] ranges = new float[]
  {
     0.8f,
     1.1f,
     2.7f,
     3.9f,
     4.5f,
     5.1f,
  };


private int GetIndex(float lookupValue)
{
    int position = Array.BinarySearch(ranges, lookupValue);
    if (position < 0)
    {
        // Find the highest available value that does not
        // exceed the value being looked up.
        position = ~position - 1;
    }

    // If position is still negative => all values in array 
    // are greater than lookupValue, return 0
    return position < 0 ? 0 : position;
}

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

素衣风尘叹 2024-09-20 18:09:17

不,我认为这是一个非常好的方法。

我唯一可能改变的是使其成为数组的扩展方法,而不是引用类变量的私有函数。然后它就变得通用/不依赖于一个类,并且语法也更清晰: ranges.GetIndex(...)

像这样的东西:

public static class Extensions
{
    public static int GetIndex<T>(this T[] ranges, T lookupValue)
    {
        // your code here
    }
}

当然,你必须记住这只适用于已排序的数组...

Nope, I think this is a pretty good approach.

The only thing I might change is to make it an extension method on arrays, instead of a private function referring to a class variable. Then it becomes general / not tied to one class, and the syntax is cleaner too: ranges.GetIndex(...)

Something like this:

public static class Extensions
{
    public static int GetIndex<T>(this T[] ranges, T lookupValue)
    {
        // your code here
    }
}

Of course, you'll have to remember this only works on sorted arrays...

可遇━不可求 2024-09-20 18:09:17

您可以使用普通的 for 循环(假设您的数据是有序的)。不确定它是否更干净,但对于大量数据肯定没有那么有效。就我个人而言,我会选择你拥有的 BinarySearch。

int GetIndex(IList<float> ranges, float target)
{
    for (int i = 0; i < ranges.Count; i++)
    {
        if(ranges[i] < target) continue;
        if (ranges[i] >= target) return i;
    }
    return 0;
}

You could use a normal for loop (assuming your data is ordered). Not sure if it's cleaner, but certainly not as effective on lots of data. Personally I would go for the BinarySearch you have.

int GetIndex(IList<float> ranges, float target)
{
    for (int i = 0; i < ranges.Count; i++)
    {
        if(ranges[i] < target) continue;
        if (ranges[i] >= target) return i;
    }
    return 0;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文