KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tctest > performance > http > load > IntList


1 /*
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright
3  * notice. All rights reserved.
4  */

5 package com.tctest.performance.http.load;
6
7 import java.util.ArrayList JavaDoc;
8 import java.util.List JavaDoc;
9
10 /**
11  * A space efficient, growable list of ints -- not very fancy ;-)
12  */

13 public class IntList {
14   private static final int BLOCK = 4096;
15   private final List JavaDoc arrays = new ArrayList JavaDoc();
16   private int[] current;
17   private int currentIndex = 0;
18   private int size;
19
20   public IntList() {
21     next();
22   }
23
24   public void add(int i) {
25     if (currentIndex == current.length) {
26       next();
27     }
28
29     current[currentIndex++] = i;
30     size++;
31   }
32
33   public int size() {
34     return size;
35   }
36
37   public int get(int index) {
38     int whichArray = index == 0 ? 0 : index / BLOCK;
39     return ((int[]) arrays.get(whichArray))[index % BLOCK];
40   }
41
42   private void next() {
43     current = new int[BLOCK];
44     currentIndex = 0;
45     arrays.add(current);
46   }
47
48   public static void main(String JavaDoc args[]) {
49     int num = 5000000;
50     IntList il = new IntList();
51
52     for (int i = 0; i < num; i++) {
53       il.add(i);
54
55     }
56
57     if (il.size() != num) { throw new AssertionError JavaDoc("wrong size reported: " + il.size()); }
58
59     for (int i = 0; i < num; i++) {
60       int val = il.get(i);
61       if (val != i) { throw new AssertionError JavaDoc("Expected " + i + " got " + val); }
62     }
63   }
64
65 }
66
Popular Tags