+ return ents;
+ }
+
+ private static void assignIndex(Ent[] ents) {
+ int index = 1;
+ for(int i=0;i<ents.length;i++) {
+ Ent ent = ents[i];
+ ent.n = index;
+ index += ent instanceof LongEnt ? 2 : 1;
+ }
+ }
+
+ public void stable() {
+ if(state != OPEN) return;
+ state = STABLE;
+ assignIndex(asArray());
+ }
+
+ private static final Sort.CompareFunc compareFunc = new Sort.CompareFunc() {
+ public int compare(Object a_, Object b_) {
+ return ((Ent)a_).n - ((Ent)b_).n;
+ }
+ };
+
+ private static final Sort.CompareFunc reverseCompareFunc = new Sort.CompareFunc() {
+ public int compare(Object a_, Object b_) {
+ return ((Ent)b_).n - ((Ent)a_).n;
+ }
+ };
+
+ public void optimize() {
+ if(state != OPEN) throw new IllegalStateException("can't optimize a stable constant pool");
+ Ent[] ents = asArray();
+ Sort.sort(ents,reverseCompareFunc);
+ for(int i=0;i<ents.length;i++)
+ System.err.println("" + i + " -> " + ents[i].debugToString() + " (" + ents[i].n + ")");
+ state = STABLE;
+ assignIndex(ents);
+ }
+
+ public void dump(DataOutput o) throws IOException {
+ Ent[] ents = asArray();