public boolean add(T x) { int i, c = 0, ix = it.intValue(x); Node u = r; // 1 - search for ix until falling out of the trie for (i = 0; i < w; i++) { c = (ix >>> w - i - 1) & 1; if (u.child[c] == null) break; u = u.child[c]; } if (i == w) return false; // already contains x - abort Node pred = (c == right) ? u.jump : u.jump.child[0]; u.jump = null; // u will have two children shortly // 2 - add path to ix for (; i < w; i++) { c = (ix >>> w - i - 1) & 1; u.child[c] = newNode(); u.child[c].parent = u; u = u.child[c]; } u.x = x; // 3 - add u to linked list u.child[prev] = pred; u.child[next] = pred.child[next]; u.child[prev].child[next] = u; u.child[next].child[prev] = u; // 4 - walk back up, updating jump pointers Node v = u.parent; while (v != null) { if ((v.child[left] == null && (v.jump == null || it.intValue(v.jump.x) > ix)) || (v.child[right] == null && (v.jump == null || it.intValue(v.jump.x) < ix))) v.jump = u; v = v.parent; } n++; return true; }

public void addAfter(Object x) { Node p = new Node(); p.x = x; p.next = actual.next; actual.next = p; actual = p; length++; }

public T push(T x) { Node<T> node = new Node<T>(); node.x = x; node.next = head; head = node; if (n == 0) { tail = node; } n++; return x; }

public boolean add(T x) { Node<T> node = new Node<T>(); node.x = x; if (n == 0) { head = node; } else { tail.next = node; } tail = node; n++; return true; }

public CameraPlacementState getBestSuccessor() { this.setScore(); // Generate a successor, test successor, keep highest-scoring successor CameraPlacementState best = this; CameraPlacementState challenger = null; // Try all the spin possibilities: for (int c = 0; c < cameraLocations.size(); c++) { System.out.println("On rotation, camera " + c); for (int orient = 0; orient < 360; orient++) { Node newCam = cameraLocations.get(c).clone(); newCam.setOri(orient); List<Node> newCamList = new ArrayList<Node>(); for (int copy = 0; copy < cameraLocations.size(); copy++) { if (c != copy) { newCamList.add(cameraLocations.get(copy)); } } newCamList.add(newCam); challenger = new CameraPlacementState(plans, newCamList); challenger.setScore(); if (challenger.getScore() > best.getScore()) { best = challenger; System.out.println("New best: " + best.getScore()); } } } // Try all new location possibilities: List<Node> possibleMoves = this.calculatePossibleLocations(); for (int c = 0; c < cameraLocations.size(); c++) { System.out.println("On relocation, camera " + c); for (Node poss : possibleMoves) { Node newCam = cameraLocations.get(c).clone(); newCam.x = poss.x; newCam.y = poss.y; List<Node> newCamList = new ArrayList<Node>(); for (int copy = 0; copy < cameraLocations.size(); copy++) { if (c != copy) { newCamList.add(cameraLocations.get(copy)); } } newCamList.add(newCam); challenger = new CameraPlacementState(plans, newCamList); challenger.setScore(); if (challenger.getScore() > best.getScore()) { best = challenger; System.out.println("New best: " + best.getScore()); } } } if (best == this) { return null; } return best; }

private CameraPlacementState improveSingleCamera(Node camera) { // Motivation: We waste a lot of time calculating for cameras that are, for the moment, // "complete" // So, let's narrow the focus of the algorithm to one camera, so we skip unnecessary // calculations // We'll be using the Best-Choice algorithm to optimize this camera. this.setScore(); // Generate a successor, test successor, keep highest-scoring successor CameraPlacementState best = this; CameraPlacementState challenger = null; // Try all the spin possibilities: // System.out.println("On rotation, camera "+c); for (int orient = 0; orient < 360; orient++) { Node newCam = camera.clone(); newCam.setOri(orient); List<Node> newCamList = new ArrayList<Node>(); for (int copy = 0; copy < cameraLocations.size(); copy++) { if (!camera.equals(cameraLocations.get(copy))) { newCamList.add(cameraLocations.get(copy)); } } newCamList.add(newCam); challenger = new CameraPlacementState(plans, newCamList); challenger.setScore(); if (challenger.getScore() > best.getScore()) { best = challenger; System.out.println("New best: " + best.getScore()); // return best; } } // Try all new location possibilities: List<Node> possibleMoves = this.calculatePossibleLocations(); // System.out.println("On relocation, camera "+c); for (Node poss : possibleMoves) { Node newCam = camera.clone(); newCam.x = poss.x; newCam.y = poss.y; List<Node> newCamList = new ArrayList<Node>(); for (int copy = 0; copy < cameraLocations.size(); copy++) { if (!camera.equals(cameraLocations.get(copy))) { newCamList.add(cameraLocations.get(copy)); } } newCamList.add(newCam); challenger = new CameraPlacementState(plans, newCamList); challenger.setScore(); if (challenger.getScore() > best.getScore()) { best = challenger; System.out.println("New best: " + best.getScore()); // return best; } } if (best == this) { return null; } return best; }

public void mouseDragged() { if (selection != null) { selection.x = mouseX; selection.y = mouseY; } }

public void remove() { actual.x = actual.next.x; actual.next = actual.next.next; length--; }