1 7 8 package javax.swing; 9 10 97 98 105 106 public class SizeSequence { 107 108 private static int[] emptyArray = new int[0]; 109 private int a[]; 110 111 119 public SizeSequence() { 120 a = emptyArray; 121 } 122 123 132 public SizeSequence(int numEntries) { 133 this(numEntries, 0); 134 } 135 136 144 public SizeSequence(int numEntries, int value) { 145 this(); 146 insertEntries(0, numEntries, value); 147 } 148 149 156 public SizeSequence(int[] sizes) { 157 this(); 158 setSizes(sizes); 159 } 160 161 172 public void setSizes(int[] sizes) { 173 if (a.length != sizes.length) { 174 a = new int[sizes.length]; 175 } 176 setSizes(0, a.length, sizes); 177 } 178 179 private int setSizes(int from, int to, int[] sizes) { 180 if (to <= from) { 181 return 0; 182 } 183 int m = (from + to)/2; 184 a[m] = sizes[m] + setSizes(from, m, sizes); 185 return a[m] + setSizes(m + 1, to, sizes); 186 } 187 188 193 public int[] getSizes() { 194 int n = a.length; 195 int[] sizes = new int[n]; 196 getSizes(0, n, sizes); 197 return sizes; 198 } 199 200 private int getSizes(int from, int to, int[] sizes) { 201 if (to <= from) { 202 return 0; 203 } 204 int m = (from + to)/2; 205 sizes[m] = a[m] - getSizes(from, m, sizes); 206 return a[m] + getSizes(m + 1, to, sizes); 207 } 208 209 224 public int getPosition(int index) { 225 return getPosition(0, a.length, index); 226 } 227 228 private int getPosition(int from, int to, int index) { 229 if (to <= from) { 230 return 0; 231 } 232 int m = (from + to)/2; 233 if (index <= m) { 234 return getPosition(from, m, index); 235 } 236 else { 237 return a[m] + getPosition(m + 1, to, index); 238 } 239 } 240 241 250 public int getIndex(int position) { 251 return getIndex(0, a.length, position); 252 } 253 254 private int getIndex(int from, int to, int position) { 255 if (to <= from) { 256 return from; 257 } 258 int m = (from + to)/2; 259 int pivot = a[m]; 260 if (position < pivot) { 261 return getIndex(from, m, position); 262 } 263 else { 264 return getIndex(m + 1, to, position - pivot); 265 } 266 } 267 268 277 public int getSize(int index) { 278 return getPosition(index + 1) - getPosition(index); 279 } 280 281 291 public void setSize(int index, int size) { 292 changeSize(0, a.length, index, size - getSize(index)); 293 } 294 295 private void changeSize(int from, int to, int index, int delta) { 296 if (to <= from) { 297 return; 298 } 299 int m = (from + to)/2; 300 if (index <= m) { 301 a[m] += delta; 302 changeSize(from, m, index, delta); 303 } 304 else { 305 changeSize(m + 1, to, index, delta); 306 } 307 } 308 309 326 public void insertEntries(int start, int length, int value) { 327 int sizes[] = getSizes(); 328 int end = start + length; 329 int n = a.length + length; 330 a = new int[n]; 331 for (int i = 0; i < start; i++) { 332 a[i] = sizes[i] ; 333 } 334 for (int i = start; i < end; i++) { 335 a[i] = value ; 336 } 337 for (int i = end; i < n; i++) { 338 a[i] = sizes[i-length] ; 339 } 340 setSizes(a); 341 } 342 343 356 public void removeEntries(int start, int length) { 357 int sizes[] = getSizes(); 358 int end = start + length; 359 int n = a.length - length; 360 a = new int[n]; 361 for (int i = 0; i < start; i++) { 362 a[i] = sizes[i] ; 363 } 364 for (int i = start; i < n; i++) { 365 a[i] = sizes[i+length] ; 366 } 367 setSizes(a); 368 } 369 } 370 | Popular Tags |