At this point, is it still more efficient to use the HashMap (in consideration of generation and access) or should another method be used, such as a set of 2-D interval trees?
Nobody can tell except the one who did a measurement. It all depends on how often you need to update the data, how often they change, and how often they get queried.
And also on the size of a Room
. With a few points, the HashMap
wins in a typical scenario, with thousands of them, the overhead of updating the points dominates.
Can't you use an array for all Point
s? If so, then it'll be surely way faster then the HashMap
, especially for updating (unless it gets really huge).
This may or may not be useful.
Actually, not only each Room
has some Point
s, but also each Point
may have one Room
(n:1). How it gets implemented, it's a different question.
Given this, we have a standard problem: If you link bidirectionally, then you have to keep both links in sync. The simplest solution is to make the setters ("adders"/"removers") as private as possible and only called them from a method
void link(Room room, Point point) {
Room oldRoom = point.getRoom();
if (oldRoom!=null) oldRoom.removePoint(point);
room.addPoint(point);
point.setRoom(room);
}
Your linking from Point
to Room
is external, but this hardly changes anything (except for keeping Point
smaller and possibly cleaner and adding the tiny overhead of a HashMap to each access).
It's usually also the fastest as usually there are much more queries than updates. Your problem is that the update cost gets scaled up by the Room
size. Without you telling us some figures, no estimate is possible.
With too many points and rooms of not too widely varying size, you could round the point coordinates to a multiple of some n
and keep an array holding the set of all rooms intersecting the grid. You could proceed as before, but the update overhead would be lower by nearly a factor given by the room area(*) and the search overhead higher, as you'd have to select the really matching room (if any) from the set.
(*) For rooms not aligned to grid, the number of intersecting rectangles is obviously higher.