Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package sonc.quad;
- import java.util.HashMap;
- import java.util.Map;
- import java.util.Set;
- class NodeTrie<T extends HasPoint> extends Trie<T>
- {
- HashMap<Quadrant, Trie<T>> tries = new HashMap<>();
- // Constructor
- NodeTrie(double topLeftX, double topLeftY, double bottomRightX, double bottomRightY)
- {
- super(topLeftX, topLeftY, bottomRightX, bottomRightY);
- tries.put(Quadrant.NE, new LeafTrie<T>(this.topLeftX + ((this.bottomRightX - topLeftX) / 2), this.topLeftY, this.bottomRightX, this.topLeftY + ((this.bottomRightY - this.topLeftY) / 2)));
- tries.put(Quadrant.NW, new LeafTrie<T>(this.topLeftX, this.topLeftY, this.topLeftX + ((this.bottomRightX - this.topLeftX) / 2), this.topLeftY + ((this.bottomRightY - this.topLeftY) / 2)));
- tries.put(Quadrant.SE, new LeafTrie<T>(this.topLeftX + ((this.bottomRightX - this.topLeftX) / 2), this.topLeftY + ((this.bottomRightY - this.topLeftY) / 2), this.bottomRightX, this.bottomRightY));
- tries.put(Quadrant.SW, new LeafTrie<T>(this.topLeftX + ((this.bottomRightX - this.topLeftX) / 2), this.topLeftY + ((this.bottomRightY - this.topLeftY) / 2), this.bottomRightX, this.bottomRightY));
- }
- void collectAll(Set<T> nodes)
- {
- for (Map.Entry<Quadrant, Trie<T>> entry : tries.entrySet())
- {
- entry.getValue().collectAll(nodes);
- }
- }
- void collectNear(double x, double y, double radius, Set<T> nodes)
- {
- for (Map.Entry<Quadrant, Trie<T>> entry : tries.entrySet())
- {
- entry.getValue().collectNear(x, y, radius, nodes);
- }
- }
- void delete(T point)
- {
- tries.get(this.quadrantOf(point)).delete(point);
- }
- T find(T point)
- {
- Quadrant quadrant = this.quadrantOf(point);
- if (quadrant != null)
- return tries.get(quadrant).find(point);
- else
- return null;
- }
- Trie<T> insert(T point)
- {
- Trie<T> t_aux = tries.get(this.quadrantOf(point));
- Quadrant q_aux = quadrantOf(point);
- if(q_aux!=null) {
- tries.remove(q_aux);
- tries.put(q_aux, t_aux.insert(point));
- }
- return this;
- }
- Trie<T> insertReplace(T point)
- {
- try
- {
- Trie<T> t_aux = tries.get(this.quadrantOf(point));
- Quadrant q_aux = quadrantOf(point);
- tries.remove(q_aux);
- tries.put(q_aux, t_aux.insertReplace(point));
- }
- catch (PointOutOfBoundException e)
- {
- e.printStackTrace();
- }
- return this;
- }
- Quadrant quadrantOf(T point)
- {
- if (tries.get(Quadrant.NE).contains(point))
- return Quadrant.NE;
- System.out.print("aqui: " + tries.get(Quadrant.NW).contains(point));
- if (tries.get(Quadrant.NW).contains(point))
- return Quadrant.NW;
- else if (tries.get(Quadrant.SE).contains(point))
- return Quadrant.SE;
- else if (tries.get(Quadrant.SW).contains(point))
- return Quadrant.SW;
- //else
- //throw new PointOutOfBoundException("quadrantOf: Point not find in quadrant");
- return null;
- }
- boolean contains(T point)
- {
- return tries.get(Quadrant.NE).contains(point) || tries.get(Quadrant.NW).contains(point) || tries.get(Quadrant.SE).contains(point) || tries.get(Quadrant.SW).contains(point);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement