1 package gov.nasa.jpf.util; 20 21 39 40 public class DynamicObjectArray { 41 final static int DEFAULT_CHUNKSIZE = 256; 42 final static int INIT_CHUNKS = 16; 43 44 int chunkSize; 45 Object [][] data; 46 int length; 47 48 public DynamicObjectArray () { 49 this( DEFAULT_CHUNKSIZE); 50 } 51 52 public DynamicObjectArray (int chunkSize) { 53 this.chunkSize = chunkSize; 54 55 data = new Object [INIT_CHUNKS][]; 56 } 57 58 public boolean isEmpty () { 59 return (length == 0); 60 } 61 62 public Object get (int index) { 63 int i = index / chunkSize; 64 int j = index % chunkSize; 65 66 if (data[i] == null) { 67 return null; 68 } else { 69 return data[i][j]; 70 } 71 } 72 73 public void set (int index, Object value) { 74 int i = index / chunkSize; 75 int j = index % chunkSize; 76 77 if (i >= data.length) { 78 Object [][] newChunk = new Object [i+1][]; 79 System.arraycopy(data, 0, newChunk, 0, data.length); 80 data = newChunk; 81 } 82 if (data[i] == null) { 83 data[i] = new Object [chunkSize]; 84 } 85 86 data[i][j] = value; 87 88 if (index >= length) { 89 length = index+1; 90 } 91 } 92 93 public int size() { 94 return length; 95 } 96 97 public String toString() { 98 int i; 99 StringBuffer sb = new StringBuffer (length*4); 100 101 sb.append('{'); 102 int l = length-1; 103 for (i=0; i<l; i++) { 104 sb.append(get(i)); 105 sb.append(','); 106 } 107 sb.append(get(i)); 108 sb.append('}'); 109 110 return sb.toString(); 111 } 112 113 public Object [] toArray (Object [] buf) { 114 if (buf.length < length) { 115 buf = new Object [length]; 116 } 117 for (int i=0; i<length; i++) { 118 buf[i] = get(i); 119 } 120 121 return buf; 122 } 123 124 } 125 126 | Popular Tags |