1 16 19 21 package org.hammurapi.inspectors.metrics.statistics; 22 23 33 public class IntVector implements Cloneable 34 { 35 36 37 protected int m_blocksize; 38 39 40 protected int m_map[]; 42 43 protected int m_firstFree = 0; 44 45 46 protected int m_mapSize; 47 48 52 public IntVector() 53 { 54 55 m_blocksize = 32; 56 m_mapSize = m_blocksize; 57 m_map = new int[m_blocksize]; 58 } 59 60 public IntVector(int[] intarray ) 61 { 62 m_blocksize = 32; 63 m_mapSize = m_blocksize; 64 m_map = new int[m_blocksize]; 65 for( int i=0; i< intarray.length; i++){ 66 this.addElement(intarray[i]); 67 } 68 } 69 70 75 public IntVector(int blocksize) 76 { 77 78 m_blocksize = blocksize; 79 m_mapSize = blocksize; 80 m_map = new int[blocksize]; 81 } 82 83 88 public IntVector(int blocksize, int increaseSize) 89 { 90 91 m_blocksize = increaseSize; 92 m_mapSize = blocksize; 93 m_map = new int[blocksize]; 94 } 95 96 101 public IntVector(IntVector v) 102 { 103 m_map = new int[v.m_mapSize]; 104 m_mapSize = v.m_mapSize; 105 m_firstFree = v.m_firstFree; 106 m_blocksize = v.m_blocksize; 107 System.arraycopy(v.m_map, 0, m_map, 0, m_firstFree); 108 } 109 110 115 public final int size() 116 { 117 return m_firstFree; 118 } 119 120 public boolean isEmpty(){ 121 return m_firstFree == 0; 122 } 123 124 129 public final void setSize(int sz) 130 { 131 m_firstFree = sz; 132 } 133 134 135 140 public final void addElement(int value) 141 { 142 143 if ((m_firstFree + 1) >= m_mapSize) 144 { 145 m_mapSize += m_blocksize; 146 147 int newMap[] = new int[m_mapSize]; 148 149 System.arraycopy(m_map, 0, newMap, 0, m_firstFree + 1); 150 151 m_map = newMap; 152 } 153 154 m_map[m_firstFree] = value; 155 156 m_firstFree++; 157 } 158 159 164 public final void addElements(int value, int numberOfElements) 165 { 166 167 if ((m_firstFree + numberOfElements) >= m_mapSize) 168 { 169 m_mapSize += (m_blocksize+numberOfElements); 170 171 int newMap[] = new int[m_mapSize]; 172 173 System.arraycopy(m_map, 0, newMap, 0, m_firstFree + 1); 174 175 m_map = newMap; 176 } 177 178 for (int i = 0; i < numberOfElements; i++) 179 { 180 m_map[m_firstFree] = value; 181 m_firstFree++; 182 } 183 } 184 185 193 194 public void sort(){ 195 196 quickSort1( this.m_map, 0, this.m_firstFree); 197 } 198 199 202 private static void quickSort1( final int x[], int off, int len) { 203 int SMALL = 7; 204 int MEDIUM = 40; 205 206 IntComparator comp = new IntComparator() { 207 public int compare(int a, int b) { 208 return a==b ? 0 : (a<b ? -1 : 1); 210 } 211 }; 212 213 if (len < SMALL) { 215 for (int i=off; i<len+off; i++) 216 for (int j=i; j>off && comp.compare(x[j-1],x[j])>0; j--) 217 swap(x, j, j-1); 218 return; 219 } 220 221 int m = off + len/2; if (len > SMALL) { 224 int l = off; 225 int n = off + len - 1; 226 if (len > MEDIUM) { int s = len/8; 228 l = med3(x, l, l+s, l+2*s, comp); 229 m = med3(x, m-s, m, m+s, comp); 230 n = med3(x, n-2*s, n-s, n, comp); 231 } 232 m = med3(x, l, m, n, comp); } 234 int v = x[m]; 235 236 int a = off, b = a, c = off + len - 1, d = c; 238 while(true) { 239 int comparison; 240 while (b <= c && (comparison=comp.compare(x[b],v))<=0) { 241 if (comparison == 0) 242 swap(x, a++, b); 243 b++; 244 } 245 while (c >= b && (comparison=comp.compare(x[c],v))>=0) { 246 if (comparison == 0) 247 swap(x, c, d--); 248 c--; 249 } 250 if (b > c) 251 break; 252 swap(x, b++, c--); 253 } 254 255 int s, n = off + len; 257 s = Math.min(a-off, b-a ); vecswap(x, off, b-s, s); 258 s = Math.min(d-c, n-d-1); vecswap(x, b, n-s, s); 259 260 if ((s = b-a) > 1) 262 quickSort1(x, off, s); 263 if ((s = d-c) > 1) 264 quickSort1(x, n-s, s); 265 } 266 267 270 private static void swap(int x[], int a, int b) { 271 int t = x[a]; 272 x[a] = x[b]; 273 x[b] = t; 274 } 275 276 279 private static int med3(int x[], int a, int b, int c, IntComparator comp) { 280 int ab = comp.compare(x[a],x[b]); 281 int ac = comp.compare(x[a],x[c]); 282 int bc = comp.compare(x[b],x[c]); 283 return (ab<0 ? 284 (bc<0 ? b : ac<0 ? c : a) : 285 (bc>0 ? b : ac>0 ? c : a)); 286 } 287 290 private static void vecswap(int x[], int a, int b, int n) { 291 for (int i=0; i<n; i++, a++, b++) 292 swap(x, a, b); 293 } 294 299 public final void addElements(int numberOfElements) 300 { 301 302 if ((m_firstFree + numberOfElements) >= m_mapSize) 303 { 304 m_mapSize += (m_blocksize+numberOfElements); 305 306 int newMap[] = new int[m_mapSize]; 307 308 System.arraycopy(m_map, 0, newMap, 0, m_firstFree + 1); 309 310 m_map = newMap; 311 } 312 313 m_firstFree += numberOfElements; 314 } 315 316 317 326 public final void insertElementAt(int value, int at) 327 { 328 329 if ((m_firstFree + 1) >= m_mapSize) 330 { 331 m_mapSize += m_blocksize; 332 333 int newMap[] = new int[m_mapSize]; 334 335 System.arraycopy(m_map, 0, newMap, 0, m_firstFree + 1); 336 337 m_map = newMap; 338 } 339 340 if (at <= (m_firstFree - 1)) 341 { 342 System.arraycopy(m_map, at, m_map, at + 1, m_firstFree - at); 343 } 344 345 m_map[at] = value; 346 347 m_firstFree++; 348 } 349 350 356 public final void removeAllElements() 357 { 358 359 for (int i = 0; i < m_firstFree; i++) 360 { 361 m_map[i] = java.lang.Integer.MIN_VALUE; 362 } 363 364 m_firstFree = 0; 365 } 366 367 378 public final boolean removeElement(int s) 379 { 380 381 for (int i = 0; i < m_firstFree; i++) 382 { 383 if (m_map[i] == s) 384 { 385 if ((i + 1) < m_firstFree) 386 System.arraycopy(m_map, i + 1, m_map, i - 1, m_firstFree - i); 387 else 388 m_map[i] = java.lang.Integer.MIN_VALUE; 389 390 m_firstFree--; 391 392 return true; 393 } 394 } 395 396 return false; 397 } 398 399 407 public final void removeElementAt(int i) 408 { 409 410 if (i > m_firstFree) 411 System.arraycopy(m_map, i + 1, m_map, i, m_firstFree); 412 else 413 m_map[i] = java.lang.Integer.MIN_VALUE; 414 415 m_firstFree--; 416 } 417 418 428 public final void setElementAt(int value, int index) 429 { 430 m_map[index] = value; 431 } 432 433 440 public final int elementAt(int i) 441 { 442 return m_map[i]; 443 } 444 445 452 public final boolean contains(int s) 453 { 454 455 for (int i = 0; i < m_firstFree; i++) 456 { 457 if (m_map[i] == s) 458 return true; 459 } 460 461 return false; 462 } 463 464 475 public final int indexOf(int elem, int index) 476 { 477 478 for (int i = index; i < m_firstFree; i++) 479 { 480 if (m_map[i] == elem) 481 return i; 482 } 483 484 return java.lang.Integer.MIN_VALUE; 485 } 486 487 497 public final int indexOf(int elem) 498 { 499 500 for (int i = 0; i < m_firstFree; i++) 501 { 502 if (m_map[i] == elem) 503 return i; 504 } 505 506 return java.lang.Integer.MIN_VALUE; 507 } 508 509 519 public final int lastIndexOf(int elem) 520 { 521 522 for (int i = (m_firstFree - 1); i >= 0; i--) 523 { 524 if (m_map[i] == elem) 525 return i; 526 } 527 528 return java.lang.Integer.MIN_VALUE; 529 } 530 531 536 public Object clone() 537 throws CloneNotSupportedException 538 { 539 return new IntVector(this); 540 } 541 542 543 public String toString(){ 544 StringBuffer strb = new StringBuffer (); 545 strb.append ( "["); 546 for ( int i= 0; i< m_firstFree; i++){ 547 strb.append ( this.elementAt(i) ); 548 if( i+1 == m_firstFree){ 549 strb.append ( "]"); 550 }else{ 551 strb.append ( "," ); 552 } 553 } 554 555 return strb.toString(); 556 } 557 } 558 | Popular Tags |