From 7b8fb7233e985af03e8ad56cc82e59ed6b2fb919 Mon Sep 17 00:00:00 2001 From: Joel Dice Date: Fri, 9 Nov 2007 08:08:35 -0700 Subject: [PATCH] add PersistentSet.toString(), fix a bug when removing the last element, and add a couple sanity checks --- classpath/java/util/PersistentSet.java | 96 ++++++-------------------- 1 file changed, 22 insertions(+), 74 deletions(-) diff --git a/classpath/java/util/PersistentSet.java b/classpath/java/util/PersistentSet.java index 74cf5f4a5c..a8062d6c78 100644 --- a/classpath/java/util/PersistentSet.java +++ b/classpath/java/util/PersistentSet.java @@ -1,8 +1,5 @@ package java.util; -import java.util.Comparator; -import java.lang.Iterable; - public class PersistentSet implements Iterable { private static final Node NullNode = new Node(null); @@ -33,6 +30,19 @@ public class PersistentSet implements Iterable { this.size = size; } + public String toString() { + StringBuilder sb = new StringBuilder(); + sb.append("{"); + for (java.util.Iterator it = iterator(); it.hasNext();) { + sb.append(it.next()); + if (it.hasNext()) { + sb.append(","); + } + } + sb.append("}"); + return sb.toString(); + } + public Comparator comparator() { return comparator; } @@ -168,6 +178,13 @@ public class PersistentSet implements Iterable { } private PersistentSet remove(Path p) { + if (size == 1) { + if (p.node != root) { + throw new IllegalArgumentException(); + } + return new PersistentSet(NullNode, comparator, 0); + } + Node new_ = p.node; Node newRoot = p.root.root; Cell> ancestors = p.ancestors; @@ -424,77 +441,6 @@ public class PersistentSet implements Iterable { return new Path(false, s.value, p.root, s.next); } } - -// public void dump(java.io.PrintStream out) { -// dump(root, out, 0); -// } - -// private static void indent(java.io.PrintStream out, int level) { -// for (int i = 0; i < level; ++i) out.print(" "); -// } - -// private static void dump(Node n, java.io.PrintStream out, int level) { -// indent(out, level); -// out.print(n == NullNode ? null : n.value); -// out.println(n == NullNode ? "" : n.red ? " (red)" : " (black)"); -// if (n.left != NullNode || n.right != NullNode) { -// dump(n.left, out, level + 1); -// dump(n.right, out, level + 1); -// } -// } - -// private static int[] randomSet(java.util.Random r, int size) { -// int[] data = new int[size]; -// for (int i = size - 1; i >= 0; --i) { -// data[i] = i + 1; -// } - -// for (int i = size - 1; i >= 0; --i) { -// int n = r.nextInt(size); -// int tmp = data[i]; -// data[i] = data[n]; -// data[n] = tmp; -// } - -// return data; -// } - -// public static void main(String[] args) { -// java.util.Random r = new java.util.Random(Integer.parseInt(args[0])); -// int size = 18; -// PersistentSet[] sets = new PersistentSet[size]; -// PersistentSet s = new PersistentSet(); - -// int[] data = randomSet(r, size); - -// for (int i = 0; i < size; ++i) { -// System.out.println("-- add " + data[i] + " -- "); -// sets[i] = s = s.add(data[i]); -// dump(s.root, System.out, 0); -// } - -// System.out.println("\npersistence:\n"); -// for (int i = 0; i < size; ++i) { -// dump(sets[i].root, System.out, 0); -// System.out.println("--"); -// } - -// data = randomSet(r, size); - -// System.out.println("\nremoval:\n"); -// for (int i = 0; i < size; ++i) { -// System.out.println("-- remove " + data[i] + " -- "); -// sets[i] = s = s.remove(data[i]); -// dump(s.root, System.out, 0); -// } - -// System.out.println("\npersistence:\n"); -// for (int i = 0; i < size; ++i) { -// dump(sets[i].root, System.out, 0); -// System.out.println("--"); -// } -// } - private static class Node { public T value; public Node left; @@ -543,6 +489,8 @@ public class PersistentSet implements Iterable { } public PersistentSet remove() { + if (fresh) throw new IllegalStateException(); + return root.remove(this); }