简体   繁体   English

如何查找某个点是否在一组间隔内?

[英]How to find if a point is within a set of intervals?

I am looking for the fastest way to decide whether or not a point on a line is within a subset of this line. 我正在寻找最快的方法来决定一条线上的一个点是否在这一行的一个子集内。 I am given an integer Point, and I also have a "list" of either: 我给了一个整数Point,我也有一个“列表”:

  1. Points, represented by an integer ( 3, 10, 1000, etc) 点,由整数(3,10,1000等)表示
  2. Intervals, that I represent by 2 integers ( 2:10 is all integers from 2 to 10 inluded, 50:60, etc) 间隔,我用2个整数表示(2:10是2到10的所有整数,50:60等)

In this example, if the value of my point is 5, then I return true because it is included in an interval, same for 55. If my point is equal to 1000, I also return true because it matches the list of points. 在这个例子中,如果我的点的值是5,那么我返回true,因为它包含在一个区间中,对于55也是如此。如果我的点等于1000,我也返回true,因为它匹配点列表。

I am looking for a fast way (quicker than linear) to check for this condition, WITHOUT having to instanciate as many integer as there are possible points (ie, for a 1:1000 interval I don't want to instanciate 1000 integers). 我正在寻找一种快速的方法(比线性更快)来检查这种情况,而不必像可能的点那样实现尽可能多的整数(即,对于1:1000的间隔,我不想实现1000个整数)。 Can this be done in a logarithmic time? 这可以在对数时间内完成吗?

Thanks 谢谢

edit : you can consider that any time taken to pre-process the list of data is equal to 0, because once my initial intervals are processed I need to apply this test to 10k points 编辑:你可以认为任何时候预处理数据列表等于0,因为一旦我的初始间隔被处理,我需要将这个测试应用到10k点

If you have the integers ranges sorted and the ranges are non-overlapping, you can perform binary search to find the correct range in logarithmic time. 如果排序的整数范围和范围不重叠,则可以执行二进制搜索以在对数时间内找到正确的范围。

Are there any constraint on the range? 该范围是否有任何限制? Based on that you can probably come up with hashing function to search in constant time. 基于此,你可以提出散列函数来恒定搜索。 But this depends on how your constraints are. 但这取决于你的约束方式。

After reflexion, I think that the following code should work in logarithmic time, excluding the time needed to build the map: 在反思之后,我认为以下代码应该在对数时间内工作,不包括构建映射所需的时间:

enum pointType {
    point,
    open,
    close
};
std::map<long int, pointType> mapPoints;

mapPoints.insert(std::pair<long int, pointType>(3, point));

//create the 5:10 interval:
mapPoints.insert(std::pair<long int, pointType>(5, open));
mapPoints.insert(std::pair<long int, pointType>(10, close));

int number = 4;
bool inside = false;
std::map<long int, pointType>::iterator it1 = mapPoints.lower_bound(number);

if(it1->first == number || it1->second == close) {
    inside = true;
}

I think this should work as long as the map is filled properly with non-overlapping intervals 我认为只要地图以非重叠的间隔正确填充,这就应该有效

First check a hash_map of points. 首先检查点的hash_map。 That's the simple check. 这是简单的检查。

Then simply order a map of intervals by the first coordinate and then find lower_bound of the point. 然后,只需按第一个坐标排序间隔图,然后找到该点的lower_bound。

Then check if you are contained in the element returned. 然后检查您是否包含在返回的元素中。 If you aren't in that, you aren't in any. 如果你不在,那你就不在。

You could do that in sublinear time GIVEN a tree data structure (I'd recommend a B-tree),if you don't count the time taken to build the tree (most trees take n log n or similar time to build). 您可以在次线性时间内执行此操作GIVEN树数据结构(我建议使用B树),如果您不计算构建树所花费的时间(大多数树需要n log n或类似的时间来构建)。

If you just have a plain list, then you cannot do better than linear because in the worst case you potentially have to check all points and intervals. 如果你只有一个简单的列表,那么你不能比线性更好,因为在最坏的情况下你可能需要检查所有的点和间隔。

You can use a Bloom Filter to test a point and see if it's not in an interval, in linear O(1) time. 您可以使用布隆过滤器来测试点,看看它不是在一个时间间隔,以线性O(1)时间。 If it passes that test you must use another method such as a binary search to see if it's definitely part of an interval, in O(log n) time. 如果它通过了该测试,你必须使用另一种方法,例如二进制搜索,以查看它是否是O(log n)时间内的间隔的一部分。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 找出点是否位于矩形内 - Find if point lies within a rectangle 如何在无组织点云中的一个点周围的特定半径的球形空间中找到一组点 - How to find a set of points in a spherical space with specific radius around one point in a unorganized point cloud 如何使用Qt / QwtPlot设置固定轴间隔? - How to set fixed axis intervals with Qt/QwtPlot? 找到最窄间隔的算法,其中m将覆盖一组数字 - Algorithm to find the narrowest intervals, m of which will cover a set of numbers 如何查找点是否在VTK中由2D非结构化网格定义的轮廓内? - How to find if a point is within a contour defined by a 2D unstructured grid in VTK? 如何计算一个点是否在一个圆内 - How to calculate if a point is within a circle 查找所有重叠一点的时间间隔 - Finding All Intervals That Overlap a Point 如何在qcustomplot中找到交点? - How to find point of intersection in qcustomplot? 处理设定间隔的算法 - Algorithm to deal with set intervals 查找到图中某个点一定距离内的所有lineegments = edges,如何将boost-graph与boost-geometry结合起来? - Find all linesegments=edges within a certain distance to a point in a graph, how to combine boost-graph with boost-geometry?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM