作者:123AJAgjt | 来源:互联网 | 2023-05-17 20:50
I'm trying to determine a fast way of storing a set of objects, each of which have an x and y coordinate value, such that I can quickly retrieve all objects within a certain rectangle or circle. For small sets of objects (~100) the naive approach of simply storing them in a list, and iterating through it, is relatively quick. However, for much larger groups, that is expectedly slow. I've tried storing them in a pair of TreeMaps as well, one sorted on the x coordinate, and one sorted on the y coordinate, using this code:
我正在尝试确定一种存储一组对象的快速方法,每个对象都有一个x和y坐标值,这样我就可以快速检索某个矩形或圆形内的所有对象。对于小型对象集(~100),简单地将它们存储在列表中并通过它迭代的简单方法相对较快。但是,对于规模更大的群体来说,这预计会很慢。我已经尝试将它们存储在一对TreeMaps中,一个在x坐标上排序,一个在y坐标上排序,使用以下代码:
xSubset = objectsByX.subSet( minX, maxX );
ySubset = objectsByY.subSet( minY, maxY );
result.addAll( xSubset );
result.retainAll( ySubset );
This also works, and is faster for larger sets of objects, but is still slower than I would like. Part of the problem is also that these objects move around, and need to be inserted back into this storage, which means removing them from and re-adding them to the trees/lists. I can't help but think there must be better solutions out there. I'm implementing this in Java, if it makes any difference, though I expect any solution will be more in the form of a useful pattern/algorithm.
这也适用,对于较大的对象集更快,但仍然比我想要的慢。部分问题还在于这些对象移动,需要插回到此存储中,这意味着将它们从树中删除并重新添加到树/列表中。我不禁想到那里必须有更好的解决方案。我在Java中实现这个,如果它有任何区别,尽管我希望任何解决方案都会以有用的模式/算法的形式出现。
6 个解决方案