/* This file is part of the algoviz@vt collection of algorithm visualizations. This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this distribution. If not, see . */ package bst; import java.util.*; /** * A virtual state holds the exact positions of all the nodes (as VNodes) * and pointers (as Lines) at a certain point in time of the tree. * * @author Andy Street, alstreet@vt.edu * @version Feb 11, 2009 * */ public class VirtualState { private HashMap nodes = new HashMap(); private HashMap lines = new HashMap(); private boolean nodesMoving = false; public VNode getNode(int id) { return nodes.get(id); } public void addNode(VNode v) { nodes.put(v.id, v); } /** * Returns the VNode created */ public VNode addNode(Node n) { VNode v = new VNode(); GraphicalNode g = n.value; Location l = g.getLocation(); v.id = g.hashCode(); v.x = l.x; v.y = l.y; v.size = GraphicalNode.NODE_WIDTH; v.outline = g.getBorderColor(); v.fill = g.getNodeColor(); v.textColor = g.getTextColor(); v.text = n.key.toString(); v.height = n.height; v.isSelected = g.isSelected(); addNode(v); return v; } public Line getLine(int id) { return lines.get(id); } public void addLine(Line l) { lines.put(l.id, l); } /** * Returns the Line created. The hash should be negative the parent's hashcode * if this is a left pointer, positive otherwise. */ public Line addLine(int hash, VNode parent, VNode child) { Line ret = new Line(parent.x, parent.y, child.x, child.y); ret.id = hash; lines.put(hash, ret); return ret; } public Line removeLine(int hash) { return lines.remove(hash); } public Set nodeIds() { return nodes.keySet(); } public Set lineIds() { return lines.keySet(); } public Collection getNodes() { return nodes.values(); } public Collection getLines() { return lines.values(); } /** * Whether nodes will be moving when animating to and from this state * * @return animateLines */ public boolean getNodesMoving() { return nodesMoving; } public void setNodesMoving(boolean nodesMoving) { this.nodesMoving = nodesMoving; } /** * Creates a shallow copy of this state */ public VirtualState copy() { VirtualState ret = new VirtualState(); for(VNode n : nodes.values()) ret.addNode(n); for(Line l : lines.values()) ret.addLine(l); return ret; } }