1 23 24 package com.sun.enterprise.util.collection; 25 26 import java.util.*; 27 28 public class LongArray 29 { 30 31 long[] data; 32 int size; 33 double growFactor; 34 35 public LongArray() { 36 this(6, 0.5); 37 } 38 39 public LongArray(int initialCapacity) { 40 this(initialCapacity, 0.5); 41 } 42 43 public LongArray(int initialCapacity, double growFactor) { 44 data = new long[initialCapacity]; 45 this.growFactor = growFactor; 46 } 47 48 public void add(int index, long val) { 49 if (size == data.length) { 50 makeRoom(1); 51 } 52 53 System.arraycopy(data, index, data, index+1, size - index); 54 data[index] = val; 55 size++; 56 57 } 58 59 public boolean add(long val) { 60 if (size == data.length) { 61 makeRoom(1); 62 } 63 64 data[size++] = val; 65 return true; 66 } 67 68 public boolean addAll(LongArray coll) { 69 int collSize = coll.size(); 70 makeRoom(collSize); 71 LongIterator longIter = coll.longIterator(); 72 while (longIter.hasNext()) { 73 data[size++] = longIter.nextLong(); 74 } 75 return (collSize > 0); 76 } 77 78 public boolean addAll(int index, LongArray coll) { 79 int collSize = coll.size(); 80 makeRoom(collSize); 81 System.arraycopy(data, index, data, index+collSize, size - index); 82 LongIterator longIter = coll.longIterator(); 83 while (longIter.hasNext()) { 84 data[index++] = longIter.nextLong(); 85 } 86 size += collSize; 87 return (collSize > 0); 88 } 89 90 public void clear() { 91 size = 0; 92 data = new long[6]; 93 } 94 95 public boolean contains(long val) { 96 return (indexOf(val) >= 0); 97 } 98 99 public boolean containsAll(LongArray coll) { 100 LongIterator iter = coll.longIterator(); 101 while (iter.hasNext()) { 102 if (contains(iter.nextLong()) == false) { 103 return false; 104 } 105 } 106 return true; 107 } 108 109 public boolean containsAll(long[] array) { 110 for (int i=array.length; i > 0; ) { 111 if (contains(array[--i]) == false) { 112 return false; 113 } 114 } 115 return true; 116 } 117 118 public boolean equals(Object o) { 119 return this == o; 121 } 122 123 public long get(int index) { 124 return data[index]; 125 } 126 127 public int hashCode() { 128 long hashCode = 1; 129 for (int i=0; i<size; i++) { 130 hashCode = 31*hashCode + data[i]; 131 } 132 return (int) hashCode; 133 } 134 135 public int indexOf(long val) { 136 for (int i=0; i<size; i++) { 137 if (data[i] == val) { 138 return i; 139 } 140 } 141 return -1; 142 } 143 144 public boolean isEmpty() { 145 return size == 0; 146 } 147 148 public int lastIndexOf(long val) { 149 for (int i=size-1; i >= 0; i--) { 150 if (data[i] == val) { 151 return i; 152 } 153 } 154 return -1; 155 } 156 157 public ListIterator listIterator() { 158 return new LongIterator(0); 159 } 160 161 public ListIterator listIterator(int index) { 162 return new LongIterator(index); 163 } 164 165 public long remove(int index) { 166 long val = data[index]; 167 if (index < size - 1) { 168 System.arraycopy(data, index+1, data, index, size - index); 169 } 170 size--; 171 return val; 172 } 173 174 public boolean remove(long val) { 175 for (int i=0; i<size; i++) { 176 if (data[i] == val) { 177 if (i < size - 1) { 178 System.arraycopy(data, i+1, data, i, size - i); 179 size--; 180 return true; 181 } 182 } 183 } 184 return false; 185 } 186 187 public boolean removeAll(LongArray coll) { 188 boolean removed = false; 189 LongIterator iter = coll.longIterator(); 190 while (iter.hasNext()) { 191 if (remove(iter.nextLong()) == true) { 192 removed = true; 193 } 194 } 195 return removed; 196 } 197 198 public boolean removeAll(long[] array) { 199 boolean removed = false; 200 for (int i=0; i<array.length; i++) { 201 if (remove(array[i]) == true) { 202 removed = true; 203 } 204 } 205 return removed; 206 } 207 208 209 public boolean retainAll(LongArray coll) { 210 return false; 211 } 212 213 public boolean retainAll(long[] array) { 214 return false; 215 } 216 217 218 public Object set(int index, Object o) { 219 long oldVal = data[index]; 220 data[index] = ((Long ) o).longValue(); 221 return new Long (oldVal); 222 } 223 224 public long set(int index, long val) { 225 long oldVal = data[index]; 226 data[index] = val; 227 return oldVal; 228 } 229 230 231 public int size() { 232 return size; 233 } 234 235 public long[] toArray() { 236 return data; 237 } 238 239 240 protected void makeRoom(int space) { 241 242 if (space + size >= data.length) { 243 long[] oldData = data; 244 245 int newSize = size + space + ((int) (size * growFactor)) + 1; 246 data = new long[newSize]; 247 248 System.arraycopy(oldData, 0, data, 0, oldData.length); 249 } 250 } 251 252 253 public Iterator iterator() { 254 return new LongIterator(0); 255 } 256 257 public LongIterator longIterator() { 258 return new LongIterator(0); 259 } 260 261 public void print() { 262 System.out.print("Data (size: " + size + "): "); 263 for (int i=0; i<size; i++) { 264 System.out.print(" " + data[i]); 265 } 266 System.out.println(); 267 } 268 269 public void printStat() { 270 System.out.println("Size: " + size); 271 System.out.println("Length: " + data.length); 272 } 273 274 275 public static void main(String [] args) { 276 int count = 1000000; 277 278 ArrayList al = new ArrayList(count); 279 LongArray la = new LongArray(count, 0.1); 280 LongArrayList lal = new LongArrayList(count, 0.1); 281 282 long t1, t2; 283 284 t1 = System.currentTimeMillis(); 285 for (int i=0; i<count; i++) { 286 al.add(new Long (i)); 287 } 288 t2 = System.currentTimeMillis(); 289 System.out.println("ArrayList took: " + ((t2 - t1) / 1000.0) + " sec."); 290 al.clear(); 291 292 t1 = System.currentTimeMillis(); 293 for (int i=0; i<count; i++) { 294 la.add(i); 295 } 296 t2 = System.currentTimeMillis(); 297 System.out.println("LongArray took: " + ((t2 - t1) / 1000.0) + " sec."); 298 la.printStat(); 299 la.clear(); 300 301 t1 = System.currentTimeMillis(); 302 for (int i=0; i<count; i++) { 303 lal.add(i); 304 } 305 t2 = System.currentTimeMillis(); 306 System.out.println("LongArrayList took: " + ((t2 - t1) / 1000.0) + " sec."); 307 lal.clear(); 308 309 for (int i=0; i<count; i++) { 310 la.add(i); 311 } 312 313 for (int i=0; i<10; i++) { 314 for (int j=0; j<count; j++) { 315 if (la.get(j) != j) { 316 System.out.println("data["+j+"]: " + la.get(j)); 317 } 318 } 319 } 320 } 321 322 323 private class LongIterator 324 implements ListIterator 325 { 326 int startIndex; 327 int index = 0; 328 329 LongIterator(int startAt) { 330 startIndex = startAt; 331 } 332 333 public void add(Object o) { 334 ; 335 } 336 337 public boolean hasPrevious() { 338 return index > startIndex; 339 } 340 341 public boolean hasNext() { 342 return index < size; 343 } 344 345 public Object previous() { 346 return new Long (data[--index]); 347 } 348 349 public int previousIndex() { 350 return index; 351 } 352 353 public Object next() { 354 return new Long (data[index++]); 355 } 356 357 public int nextIndex() { 358 return index; 359 } 360 361 public long previousLong() { 362 return data[--index]; 363 } 364 365 public long nextLong() { 366 return data[index++]; 367 } 368 369 public void remove() { 370 } 372 373 public void set(Object o) { 374 ; 375 } 376 } 377 378 } 379 | Popular Tags |