1 35 37 package com.google.gwt.dev.js.rhino; 38 39 import java.io.Serializable ; 40 import java.io.IOException ; 41 import java.io.ObjectInputStream ; 42 import java.io.ObjectOutputStream ; 43 44 47 48 public class ObjArray implements Serializable { 49 50 public ObjArray() { } 51 52 public ObjArray(int capacityHint) { 53 if (capacityHint < 0) throw new IllegalArgumentException (); 54 if (capacityHint > FIELDS_STORE_SIZE) { 55 data = new Object [capacityHint - FIELDS_STORE_SIZE]; 56 } 57 } 58 59 public final boolean isEmpty() { 60 return size == 0; 61 } 62 63 public final int size() { 64 return size; 65 } 66 67 public final void setSize(int newSize) { 68 if (newSize < 0) throw new IllegalArgumentException (); 69 int N = size; 70 if (newSize < N) { 71 for (int i = newSize; i != N; ++i) { 72 setImpl(i, null); 73 } 74 }else if (newSize > N) { 75 if (newSize > FIELDS_STORE_SIZE) { 76 ensureCapacity(newSize); 77 } 78 } 79 size = newSize; 80 } 81 82 public final Object get(int index) { 83 if (!(0 <= index && index < size)) throw invalidIndex(index, size); 84 return getImpl(index); 85 } 86 87 public final void set(int index, Object value) { 88 if (!(0 <= index && index < size)) throw invalidIndex(index, size); 89 setImpl(index, value); 90 } 91 92 private Object getImpl(int index) { 93 switch (index) { 94 case 0: return f0; 95 case 1: return f1; 96 case 2: return f2; 97 case 3: return f3; 98 case 4: return f4; 99 case 5: return f5; 100 } 101 return data[index - FIELDS_STORE_SIZE]; 102 } 103 104 private void setImpl(int index, Object value) { 105 switch (index) { 106 case 0: f0 = value; break; 107 case 1: f1 = value; break; 108 case 2: f2 = value; break; 109 case 3: f3 = value; break; 110 case 4: f4 = value; break; 111 case 5: f5 = value; break; 112 default: data[index - FIELDS_STORE_SIZE] = value; 113 } 114 115 } 116 117 public int indexOf(Object obj) { 118 int N = size; 119 for (int i = 0; i != N; ++i) { 120 Object current = getImpl(i); 121 if (current == obj || (current != null && current.equals(obj))) { 122 return i; 123 } 124 } 125 return -1; 126 } 127 128 public int lastIndexOf(Object obj) { 129 for (int i = size; i != 0;) { 130 --i; 131 Object current = getImpl(i); 132 if (current == obj || (current != null && current.equals(obj))) { 133 return i; 134 } 135 } 136 return -1; 137 } 138 139 public final Object peek() { 140 int N = size; 141 if (N == 0) throw invalidEmptyStackAccess(); 142 return getImpl(N - 1); 143 } 144 145 public final Object pop() { 146 int N = size; 147 --N; 148 Object top; 149 switch (N) { 150 case -1: throw invalidEmptyStackAccess(); 151 case 0: top = f0; f0 = null; break; 152 case 1: top = f1; f1 = null; break; 153 case 2: top = f2; f2 = null; break; 154 case 3: top = f3; f3 = null; break; 155 case 4: top = f4; f4 = null; break; 156 case 5: top = f5; f5 = null; break; 157 default: 158 top = data[N - FIELDS_STORE_SIZE]; 159 data[N - FIELDS_STORE_SIZE] = null; 160 } 161 size = N; 162 return top; 163 } 164 165 public final void push(Object value) { 166 add(value); 167 } 168 169 public final void add(Object value) { 170 int N = size; 171 if (N >= FIELDS_STORE_SIZE) { 172 ensureCapacity(N + 1); 173 } 174 size = N + 1; 175 setImpl(N, value); 176 } 177 178 public final void add(int index, Object value) { 179 Object tmp; 180 int N = size; 181 if (!(0 <= index && index <= N)) throw invalidIndex(index, N + 1); 182 switch (index) { 183 case 0: 184 if (N == 0) { f0 = value; break; } 185 tmp = f0; f0 = value; value = tmp; 186 case 1: 187 if (N == 1) { f1 = value; break; } 188 tmp = f1; f1 = value; value = tmp; 189 case 2: 190 if (N == 2) { f2 = value; break; } 191 tmp = f2; f2 = value; value = tmp; 192 case 3: 193 if (N == 3) { f3 = value; break; } 194 tmp = f3; f3 = value; value = tmp; 195 case 4: 196 if (N == 4) { f4 = value; break; } 197 tmp = f4; f4 = value; value = tmp; 198 case 5: 199 if (N == 5) { f5 = value; break; } 200 tmp = f5; f5 = value; value = tmp; 201 202 index = FIELDS_STORE_SIZE; 203 default: 204 ensureCapacity(N + 1); 205 if (index != N) { 206 System.arraycopy(data, index - FIELDS_STORE_SIZE, 207 data, index - FIELDS_STORE_SIZE + 1, 208 N - index); 209 } 210 data[index - FIELDS_STORE_SIZE] = value; 211 } 212 size = N + 1; 213 } 214 215 public final void remove(int index) { 216 int N = size; 217 if (!(0 <= index && index < N)) throw invalidIndex(index, N); 218 --N; 219 switch (index) { 220 case 0: 221 if (N == 0) { f0 = null; break; } 222 f0 = f1; 223 case 1: 224 if (N == 1) { f1 = null; break; } 225 f1 = f2; 226 case 2: 227 if (N == 2) { f2 = null; break; } 228 f2 = f3; 229 case 3: 230 if (N == 3) { f3 = null; break; } 231 f3 = f4; 232 case 4: 233 if (N == 4) { f4 = null; break; } 234 f4 = f5; 235 case 5: 236 if (N == 5) { f5 = null; break; } 237 f5 = data[0]; 238 239 index = FIELDS_STORE_SIZE; 240 default: 241 if (index != N) { 242 System.arraycopy(data, index - FIELDS_STORE_SIZE + 1, 243 data, index - FIELDS_STORE_SIZE, 244 N - index); 245 } 246 data[N - FIELDS_STORE_SIZE] = null; 247 } 248 size = N; 249 } 250 251 public final void clear() { 252 int N = size; 253 for (int i = 0; i != N; ++i) { 254 setImpl(i, null); 255 } 256 size = 0; 257 } 258 259 public final Object [] toArray() { 260 Object [] array = new Object [size]; 261 toArray(array, 0); 262 return array; 263 } 264 265 public final void toArray(Object [] array) { 266 toArray(array, 0); 267 } 268 269 public final void toArray(Object [] array, int offset) { 270 int N = size; 271 switch (N) { 272 default: 273 System.arraycopy(data, 0, array, offset + FIELDS_STORE_SIZE, 274 N - FIELDS_STORE_SIZE); 275 case 6: array[offset + 5] = f5; 276 case 5: array[offset + 4] = f4; 277 case 4: array[offset + 3] = f3; 278 case 3: array[offset + 2] = f2; 279 case 2: array[offset + 1] = f1; 280 case 1: array[offset + 0] = f0; 281 case 0: break; 282 } 283 } 284 285 private void ensureCapacity(int minimalCapacity) { 286 int required = minimalCapacity - FIELDS_STORE_SIZE; 287 if (required <= 0) throw new IllegalArgumentException (); 288 if (data == null) { 289 int alloc = FIELDS_STORE_SIZE * 2; 290 if (alloc < required) { 291 alloc = required; 292 } 293 data = new Object [alloc]; 294 } else { 295 int alloc = data.length; 296 if (alloc < required) { 297 if (alloc <= FIELDS_STORE_SIZE) { 298 alloc = FIELDS_STORE_SIZE * 2; 299 } else { 300 alloc *= 2; 301 } 302 if (alloc < required) { 303 alloc = required; 304 } 305 Object [] tmp = new Object [alloc]; 306 if (size > FIELDS_STORE_SIZE) { 307 System.arraycopy(data, 0, tmp, 0, 308 size - FIELDS_STORE_SIZE); 309 } 310 data = tmp; 311 } 312 } 313 } 314 315 private static RuntimeException invalidIndex(int index, int upperBound) { 316 String msg = index+" \u2209 [0, "+upperBound+')'; 318 return new IndexOutOfBoundsException (msg); 319 } 320 321 private static RuntimeException invalidEmptyStackAccess() { 322 throw new RuntimeException ("Empty stack"); 323 } 324 325 private void writeObject(ObjectOutputStream os) throws IOException { 326 os.defaultWriteObject(); 327 int N = size; 328 for (int i = 0; i != N; ++i) { 329 Object obj = getImpl(i); 330 os.writeObject(obj); 331 } 332 } 333 334 private void readObject(ObjectInputStream is) 335 throws IOException , ClassNotFoundException 336 { 337 is.defaultReadObject(); int N = size; 339 if (N > FIELDS_STORE_SIZE) { 340 data = new Object [N - FIELDS_STORE_SIZE]; 341 } 342 for (int i = 0; i != N; ++i) { 343 Object obj = is.readObject(); 344 setImpl(i, obj); 345 } 346 } 347 348 static final long serialVersionUID = 7448768847663119705L; 349 350 private int size; 352 353 private static final int FIELDS_STORE_SIZE = 6; 354 private transient Object f0, f1, f2, f3, f4, f5; 355 private transient Object [] data; 356 } 357 | Popular Tags |