1 19 20 package jode.flow; 21 import jode.decompiler.LocalInfo; 22 import jode.util.ArrayEnum; 23 24 import java.util.Collection ; 25 import java.util.AbstractSet ; 26 import java.util.Iterator ; 27 28 35 public final class SlotSet extends AbstractSet implements Cloneable { 36 LocalInfo[] locals; 37 int count; 38 39 42 public SlotSet() { 43 locals = null; 44 count = 0; 45 } 46 47 50 public SlotSet(LocalInfo[] locals) { 51 count = locals.length; 52 this.locals = locals; 53 } 54 55 public final void grow(int size) { 56 if (locals != null) { 57 size += count; 58 if (size > locals.length) { 59 int nextSize = locals.length * 2; 60 LocalInfo[] newLocals 62 = new LocalInfo[nextSize > size ? nextSize : size]; 63 System.arraycopy(locals, 0, newLocals, 0, count); 64 locals = newLocals; 65 } 66 } else if (size > 0) 67 locals = new LocalInfo[size]; 68 } 69 70 73 public boolean add(Object o) { 74 LocalInfo li = (LocalInfo) o; 75 LocalInfo contained = findSlot(li.getSlot()); 76 if (contained != null) { 77 li.combineWith(contained); 78 return false; 79 } else { 80 grow(1); 81 locals[count++] = li; 82 return true; 83 } 84 } 85 86 public final boolean contains(Object o) { 87 return containsSlot(((LocalInfo)o).getSlot()); 88 } 89 90 93 public final boolean containsSlot(int slot) { 94 return findSlot(slot) != null; 95 } 96 97 100 public LocalInfo findSlot(int slot) { 101 for (int i=0; i<count;i++) 102 if (locals[i].getSlot() == slot) 103 return locals[i]; 104 return null; 105 } 106 107 110 public boolean remove(Object li) { 111 int slot = ((LocalInfo) li).getSlot(); 112 for (int i=0; i<count;i++) { 113 if (locals[i].getSlot() == slot) { 114 locals[i] = locals[--count]; 115 locals[count] = null; 116 return true; 117 } 118 } 119 return false; 120 } 121 122 public int size() { 123 return count; 124 } 125 126 public Iterator iterator() { 127 return new Iterator () { 128 int pos = 0; 129 130 public boolean hasNext() { 131 return pos < count; 132 } 133 134 public Object next() { 135 return locals[pos++]; 136 } 137 138 public void remove() { 139 if (pos < count) 140 System.arraycopy(locals, pos, 141 locals, pos-1, count - pos); 142 count--; 143 pos--; 144 } 145 }; 146 } 147 148 151 public void clear() { 152 locals = null; 153 count = 0; 154 } 155 156 public Object clone() { 157 try { 158 SlotSet other = (SlotSet) super.clone(); 159 if (count > 0) { 160 other.locals = new LocalInfo[count]; 161 System.arraycopy(locals, 0, other.locals, 0, count); 162 } 163 return other; 164 } catch (CloneNotSupportedException ex) { 165 throw new jode.AssertError("Clone?"); 166 } 167 } 168 169 175 public void merge(VariableSet vs) { 176 for (int i=0; i<count; i++) { 177 LocalInfo li = locals[i]; 178 int slot = li.getSlot(); 179 for (int j=0; j<vs.count; j++) { 180 if (li.getSlot() == vs.locals[j].getSlot()) 181 li.combineWith(vs.locals[j]); 182 } 183 } 184 } 185 186 193 public void mergeKill(SlotSet kill) { 194 grow(kill.size()); 195 big_loop: 196 for (Iterator i = kill.iterator(); i.hasNext(); ) { 197 LocalInfo li2 = (LocalInfo) i.next(); 198 if (!containsSlot(li2.getSlot())) 199 add(li2.getLocalInfo()); 200 } 201 } 202 } 203 | Popular Tags |