1 19 20 25 26 27 package soot.util; 28 29 import soot.util.*; 30 import java.util.*; 31 32 37 public class DeterministicHashMap extends HashMap 38 { 39 Set keys = new TrustingMonotonicArraySet(); 40 41 42 public DeterministicHashMap(int initialCapacity) 43 { 44 super(initialCapacity); 45 } 46 47 48 public DeterministicHashMap(int initialCapacity, float loadFactor) 49 { 50 super(initialCapacity, loadFactor); 51 } 52 53 54 public Object put(Object key, Object value) 55 { 56 if(!containsKey(key)) 57 keys.add(key); 58 59 return super.put(key, value); 60 } 61 62 63 public Collection entries() 64 { 65 throw new UnsupportedOperationException (); 66 } 67 68 69 public Object remove(Object obj) 70 { 71 throw new UnsupportedOperationException (); 72 } 73 74 75 public Set keySet() 76 { 77 return keys; 78 } 79 } 80 81 83 84 class TrustingMonotonicArraySet extends AbstractSet 85 { 86 private static final int DEFAULT_SIZE = 8; 87 88 private int numElements; 89 private int maxElements; 90 private Object [] elements; 91 92 public TrustingMonotonicArraySet() 93 { 94 maxElements = DEFAULT_SIZE; 95 elements = new Object [DEFAULT_SIZE]; 96 numElements = 0; 97 } 98 99 102 103 public TrustingMonotonicArraySet(Object [] elements) 104 { 105 this(); 106 107 for(int i = 0; i < elements.length; i++) 108 add(elements[i]); 109 } 110 111 public void clear() 112 { 113 numElements = 0; 114 } 115 116 public boolean contains(Object obj) 117 { 118 for(int i = 0; i < numElements; i++) 119 if(elements[i].equals(obj)) 120 return true; 121 122 return false; 123 } 124 125 public boolean add(Object e) 126 { 127 if(numElements == maxElements) 129 doubleCapacity(); 130 131 elements[numElements++] = e; 133 return true; 134 } 135 136 public int size() 137 { 138 return numElements; 139 } 140 141 public Iterator iterator() 142 { 143 return new ArrayIterator(); 144 } 145 146 private class ArrayIterator implements Iterator 147 { 148 int nextIndex; 149 150 ArrayIterator() 151 { 152 nextIndex = 0; 153 } 154 155 public boolean hasNext() 156 { 157 return nextIndex < numElements; 158 } 159 160 public Object next() throws NoSuchElementException 161 { 162 if(!(nextIndex < numElements)) 163 throw new NoSuchElementException(); 164 165 return elements[nextIndex++]; 166 } 167 168 public void remove() throws NoSuchElementException 169 { 170 if(nextIndex == 0) 171 throw new NoSuchElementException(); 172 else 173 { 174 removeElementAt(nextIndex - 1); 175 nextIndex = nextIndex - 1; 176 } 177 } 178 } 179 180 private void removeElementAt(int index) 181 { 182 throw new UnsupportedOperationException (); 183 194 } 195 196 197 private void doubleCapacity() 198 { 199 int newSize = maxElements * 2; 200 201 Object [] newElements = new Object [newSize]; 202 203 System.arraycopy(elements, 0, newElements, 0, numElements); 204 elements = newElements; 205 maxElements = newSize; 206 } 207 208 public Object [] toArray() 209 { 210 Object [] array = new Object [numElements]; 211 212 System.arraycopy(elements, 0, array, 0, numElements); 213 return array; 214 } 215 } 216 217 218 | Popular Tags |