1 19 24 25 package org.netbeans.core.output2; 26 27 import java.util.Arrays ; 28 29 62 final class SparseIntList { 63 private int[] keys; 64 private int[] values; 65 private int used = 0; 66 private int lastAdded = Integer.MIN_VALUE; 67 private int lastIndex = Integer.MIN_VALUE; 68 69 70 SparseIntList(int capacity) { 71 allocArrays (capacity); 72 } 73 74 81 public synchronized void add (int value, int idx) { 82 if (value <= lastAdded) { 83 throw new IllegalArgumentException ("Contents must be presorted - " + "added value " + value + " is less than preceding " + "value " + lastAdded); } 87 if (idx <= lastIndex) { 88 throw new IllegalArgumentException ("Contents must be presorted - " + "added index " + idx + " is less than preceding " + "index " + lastIndex); } 92 if (used >= keys.length) { 93 growArrays(); 94 } 95 values[used] = value; 96 keys[used++] = idx; 97 lastAdded = value; 98 lastIndex = idx; 99 } 100 101 int lastAdded() { 102 return lastAdded; 103 } 104 105 int lastIndex() { 106 return lastIndex; 107 } 108 109 private void allocArrays (int size) { 110 keys = new int[size]; 111 values = new int[size]; 112 Arrays.fill(keys, Integer.MAX_VALUE); 115 Arrays.fill(values, -1); 116 } 117 118 121 private int lastGet = -1; 122 125 private int lastResult; 126 135 public synchronized int get(int index) { 136 if (index < 0) { 137 return 0; 138 } 139 140 if ((used == 0) || (used > 0 && index < keys[0])) { 141 return index; 142 } 143 144 if (index == lastGet) { 145 return lastResult; 146 } else { 147 lastGet = index; 148 } 149 150 int result; 151 int idx = Arrays.binarySearch(keys, index); 154 155 if (idx < 0) { 156 int nearest = findInRange(index, 0, used); 159 160 if (keys[nearest] > index) nearest--; 162 if (nearest == -1) { 163 result = index; 164 } else { 165 result = values[nearest] + (index - keys[nearest]); 168 } 169 } else { 170 result = idx == 0 ? index : values[idx-1] + (index - keys[idx-1]); 172 } 173 lastResult = result; 174 return result; 175 } 176 177 180 private int findInRange (int val, int start, int end) { 181 if (end - start <= 1) { 182 return start; 183 } 184 int midPoint = start + ((end - start) >> 1); 185 int idxAtMidpoint = keys[midPoint]; 186 if (idxAtMidpoint > val) { 187 return findInRange (val, start, start + ((end - start) >> 1)); 188 } else { 189 return findInRange (val, start + ((end - start) >> 1), end); 190 } 191 } 192 193 196 private void growArrays() { 197 int[] oldkeys = keys; 198 int[] oldvals = values; 199 allocArrays(Math.round(keys.length * 1.5f)); 200 System.arraycopy(oldkeys, 0, keys, 0, oldkeys.length); 201 System.arraycopy(oldvals, 0, values, 0, oldvals.length); 202 } 203 204 public String toString() { 205 StringBuffer result = new StringBuffer ("SparseIntList ["); result.append ("used="); result.append (used); 208 result.append (" capacity="); result.append (keys.length); 210 result.append (" keyValuePairs:"); for (int i=0; i < used; i++) { 212 result.append (keys[i]); 213 result.append (':'); result.append (values[i]); 215 if (i != used-1) { 216 result.append(','); } 218 } 219 result.append (']'); 220 return result.toString(); 221 } 222 223 } 224 | Popular Tags |