1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834
|
package bloom;
import java.lang.Thread.State;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Random;
import java.util.concurrent.atomic.AtomicIntegerArray;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import shared.KillSwitch;
import shared.Primes;
import shared.Shared;
import shared.Timer;
import shared.Tools;
import structures.ByteBuilder;
/**
*
* Uses prime numbers for array lengths.
* Uses atomic integers for concurrency control.
* Allows an optional prefilter.
*
* @author Brian Bushnell
* @date Aug 17, 2012
*
*/
public final class KCountArray7MTA extends KCountArray {
/**
*
*/
private static final long serialVersionUID = 568264681638739631L;
public static void main(String[] args){
long cells=Long.parseLong(args[0]);
int bits=Integer.parseInt(args[1]);
int hashes=Integer.parseInt(args[2]);
verbose=false;
KCountArray7MTA kca=new KCountArray7MTA(cells, bits, hashes, null, 0);
System.out.println(kca.read(0));
kca.increment(0);
System.out.println(kca.read(0));
kca.increment(0);
System.out.println(kca.read(0));
System.out.println();
System.out.println(kca.read(1));
kca.increment(1);
System.out.println(kca.read(1));
kca.increment(1);
System.out.println(kca.read(1));
System.out.println();
System.out.println(kca.read(100));
kca.increment(100);
System.out.println(kca.read(100));
kca.increment(100);
System.out.println(kca.read(100));
kca.increment(100);
System.out.println(kca.read(100));
System.out.println();
System.out.println(kca.read(150));
kca.increment(150);
System.out.println(kca.read(150));
System.out.println();
}
public KCountArray7MTA(long cells_, int bits_, int hashes_, KCountArray prefilter_, int prefilterLimit_){
super(getPrimeCells(cells_, bits_), bits_, getDesiredArrays(cells_, bits_));
// verbose=false;
// System.out.println(cells);
cellsPerArray=cells/numArrays;
wordsPerArray=(int)((cellsPerArray%cellsPerWord)==0 ? (cellsPerArray/cellsPerWord) : (cellsPerArray/cellsPerWord+1));
cellMod=cellsPerArray;
hashes=hashes_;
prefilter=prefilter_;
prefilterLimit=(prefilter==null ? 0 : Tools.min(prefilter.maxValue, prefilterLimit_));
// System.out.println("cells="+cells+", words="+words+", wordsPerArray="+wordsPerArray+", numArrays="+numArrays+", hashes="+hashes);
matrix=allocMatrix(numArrays, wordsPerArray);
useLocks=(cellBits>1 && hashes>1) && (LOCKED_INCREMENT || !SET_LOCKED_INCREMENT);
if(useLocks){
locks=new Lock[NUM_LOCKS];
for(int i=0; i<NUM_LOCKS; i++){locks[i]=new ReentrantLock();}
}else{locks=null;}
// matrix=new AtomicIntegerArray[numArrays];
// for(int i=0; i<matrix.length; i++){
// matrix[i]=new AtomicIntegerArray(wordsPerArray);
// }
assert(hashes>0 && hashes<=hashMasks.length);
}
private static int getDesiredArrays(long desiredCells, int bits){
long words=Tools.max((desiredCells*bits+31)/32, minArrays);
int arrays=minArrays;
while(words/arrays>=Integer.MAX_VALUE){
arrays*=2;
}
// assert(false) : arrays;
return arrays;
// return Tools.max(arrays, Data.LOGICAL_PROCESSORS*4);
}
private static long getPrimeCells(long desiredCells, int bits){
int arrays=getDesiredArrays(desiredCells, bits);
long x=(desiredCells+arrays-1)/arrays;
long x2=Primes.primeAtMost(x);
return x2*arrays;
}
@Override
public final int read(final long rawKey){
if(verbose){System.err.println("Reading raw key "+rawKey);}
if(prefilter!=null){
int pre=prefilter.read(rawKey);
if(pre<prefilterLimit){return pre;}
}
long key2=hash(rawKey, 0);
int min=readHashed(key2);
for(int i=1; i<hashes && min>0; i++){
if(verbose){System.err.println("Reading. i="+i+", key2="+key2);}
key2=Long.rotateRight(key2, hashBits);
key2=hash(key2, i);
if(verbose){System.err.println("Rot/hash. i="+i+", key2="+key2);}
min=min(min, readHashed(key2));
}
return min;
}
@Override
public final int read(final long[] rawKeys){
if(verbose){System.err.println("Reading raw key "+Arrays.toString(rawKeys));}
if(prefilter!=null){
int pre=prefilter.read(rawKeys);
if(pre<prefilterLimit){return pre;}
}
long key2=hash(rawKeys[0], (int)(1+(rawKeys[0])%5));
int min=maxValue;
for(int i=0; i<hashes; i++){
for(int keynum=0; keynum<rawKeys.length; keynum++){
if(verbose){System.err.println("Reading. i="+i+", key2="+key2);}
key2=hash(key2^rawKeys[keynum], i);
if(verbose){System.err.println("Rot/hash. i="+i+", key2="+key2);}
}
min=min(min, readHashed(key2));
key2=Long.rotateRight(key2, hashBits);
}
return min;
}
@Override
public final int readLeft(final long key, final int k, boolean makeCanonical){
assert(k<=32);
final long key2=key>>>2;
final int shift=2*(k-1);
final long akey=key2|(0L<<shift);
final long ckey=key2|(1L<<shift);
final long gkey=key2|(2L<<shift);
final long tkey=key2|(3L<<shift);
final int a=read(makeCanonical ? makeCanonical2(akey, k) : akey);
final int c=read(makeCanonical ? makeCanonical2(ckey, k) : ckey);
final int g=read(makeCanonical ? makeCanonical2(gkey, k) : gkey);
final int t=read(makeCanonical ? makeCanonical2(tkey, k) : tkey);
return a+c+g+t;
}
@Override
public final int readRight(final long key, final int k, boolean makeCanonical){
assert(k<=32);
final long mask=(k>=32 ? -1L : ~((-1L)<<(2*k)));
final long key2=(key<<2)&mask;
final long akey=key2|0L;
final long ckey=key2|1L;
final long gkey=key2|2L;
final long tkey=key2|3L;
final int a=read(makeCanonical ? makeCanonical2(akey, k) : akey);
final int c=read(makeCanonical ? makeCanonical2(ckey, k) : ckey);
final int g=read(makeCanonical ? makeCanonical2(gkey, k) : gkey);
final int t=read(makeCanonical ? makeCanonical2(tkey, k) : tkey);
return a+c+g+t;
}
@Override
public final int[] readAllLeft(final long key, final int k, boolean makeCanonical, int[] rvec){
assert(k<=32);
if(rvec==null){rvec=KillSwitch.allocInt1D(4);}
final long key2=key>>>2;
final int shift=2*(k-1);
final long akey=key2|(0L<<shift);
final long ckey=key2|(1L<<shift);
final long gkey=key2|(2L<<shift);
final long tkey=key2|(3L<<shift);
rvec[0]=read(makeCanonical ? makeCanonical2(akey, k) : akey);
rvec[1]=read(makeCanonical ? makeCanonical2(ckey, k) : ckey);
rvec[2]=read(makeCanonical ? makeCanonical2(gkey, k) : gkey);
rvec[3]=read(makeCanonical ? makeCanonical2(tkey, k) : tkey);
return rvec;
}
@Override
public final int[] readAllRight(final long key, final int k, boolean makeCanonical, int[] rvec){
assert(k<=32);
final long mask=(k>=32 ? -1L : ~((-1L)<<(2*k)));
final long key2=(key<<2)&mask;
final long akey=key2|0L;
final long ckey=key2|1L;
final long gkey=key2|2L;
final long tkey=key2|3L;
rvec[0]=read(makeCanonical ? makeCanonical2(akey, k) : akey);
rvec[1]=read(makeCanonical ? makeCanonical2(ckey, k) : ckey);
rvec[2]=read(makeCanonical ? makeCanonical2(gkey, k) : gkey);
rvec[3]=read(makeCanonical ? makeCanonical2(tkey, k) : tkey);
return rvec;
}
private final int readHashed(long key){
if(verbose){System.err.print("Reading hashed key "+key);}
// System.out.println("key="+key);
int arrayNum=(int)(key&arrayMask);
key=(key>>>arrayBits)%(cellMod);
// key=(key>>>(arrayBits+1))%(cellMod);
// System.out.println("array="+arrayNum);
// System.out.println("key2="+key);
AtomicIntegerArray array=matrix[arrayNum];
int index=(int)(key>>>indexShift);
// assert(false) : indexShift;
// System.out.println("index="+index);
int word=array.get(index);
// System.out.println("word="+Integer.toHexString(word));
assert(word>>>(cellBits*key) == word>>>(cellBits*(key&cellMask)));
// int cellShift=(int)(cellBits*(key&cellMask));
int cellShift=(int)(cellBits*key);
if(verbose){System.err.println(", array="+arrayNum+", index="+index+", cellShift="+(cellShift%32)+", value="+((int)((word>>>cellShift)&valueMask)));}
// System.out.println("cellShift="+cellShift);
return (int)((word>>>cellShift)&valueMask);
}
@Override
public final void write(final long key, int value){
throw new RuntimeException("Not allowed for this class.");
}
@Override
public final void increment(long[] keys){
// assert(false) : "This method is not really needed.";
for(int i=0; i<keys.length; i++){
increment(keys[i]);
// keys[i]=hash(keys[i], 0);
// incrementPartiallyHashed(hash(keys[i], 0));
}
}
@Override
public final void increment(final long rawKey, final int amt){
// if(verbose){System.err.println("\n*** Incrementing raw key "+rawKey+" ***");}
//
// if(prefilter!=null){
// int x=prefilter.read(rawKey);
// if(x<prefilterLimit){return;/* x;*/}
// }
//
// if(useLocks){incrementLocked(rawKey, amt);}
// else{incrementUnlocked(rawKey, amt);}
incrementAndReturnUnincremented(rawKey, amt);
}
/*
private void incrementUnlocked(long rawKey, int amt){
long key2=rawKey;
// int min=maxValue;
for(int i=0; i<hashes; i++){
key2=hash(key2, i);
if(verbose){System.err.println("key2="+key2+", value="+readHashed(key2));}
// min=min(min, incrementHashedLocal(key2, amt));
incrementHashedLocal(key2, amt);
key2=Long.rotateRight(key2, hashBits);
}
// return min;
}
private void incrementLocked(long rawKey, int amt){
assert(amt>0) : "Don't increment by zero, and negatives should use decrement amt="+amt;
final Lock lock=getLock(rawKey);
lock.lock();
final int min=read(rawKey);
final long newMinL=Tools.min(min+amt, maxValue);
final int newMin=(int)newMinL;
assert(newMin==newMinL && newMin>0) : newMin+", "+newMinL+", "+min+", "+amt;
if(newMin<=min){
assert(newMin==min) : min;
lock.unlock();
return;
}
long key2=rawKey;
for(int i=0; i<hashes; i++){
key2=hash(key2, i);
if(verbose){System.err.println("key2="+key2+", value="+readHashed(key2));}
//incrementHashedLocal_ifAtMost(key2, min);
incrementHashedLocal_toAtLeast(key2, newMin);
key2=Long.rotateRight(key2, hashBits);
}
lock.unlock();
// return max(min, newMin);
}
*/
@Override
public final void decrement(final long rawKey, int amt){
if(verbose){System.err.println("\n*** Decrementing raw key "+rawKey+" ***");}
assert(prefilter!=null); //Why?
long key2=rawKey;
// int min=maxValue;
for(int i=0; i<hashes; i++){
key2=hash(key2, i);
if(verbose){System.err.println("key2="+key2+", value="+readHashed(key2));}
decrementHashedLocal(key2, amt);
// min=min(min, decrementHashedLocal(key2, amt));
key2=Long.rotateRight(key2, hashBits);
}
// return min;
}
@Override
public int incrementAndReturnUnincremented(final long rawKey, final int incr){
if(verbose){System.err.println("\n*** Incrementing raw key "+rawKey+" ***");}
if(prefilter!=null){
int x=prefilter.read(rawKey);
if(x<prefilterLimit){return x;}
}
if(useLocks){return incrementAndReturnUnincrementedLocked(rawKey, incr);}
else{return incrementAndReturnUnincrementedUnlocked(rawKey, incr);}
}
private int incrementAndReturnUnincrementedLocked(final long rawKey, final int incr){
assert(incr>0) : incr;
final Lock lock=getLock(rawKey);
lock.lock();
final int min=read(rawKey);
if(min>=maxValue){
lock.unlock();
return min;
}
final int newMin=(int)Tools.min(min+(long)incr, maxValue);
long key2=rawKey;
int value=maxValue;
for(int i=0; i<hashes; i++){
key2=hash(key2, i);
if(verbose){System.err.println("key2="+key2+", value="+readHashed(key2));}
int x=incrementHashedLocalAndReturnUnincremented_toAtLeast(key2, newMin);
value=min(value, x);
key2=Long.rotateRight(key2, hashBits);
}
lock.unlock();
return value;
}
private int incrementAndReturnUnincrementedUnlocked(final long rawKey, final int incr){
long key2=rawKey;
int value=maxValue;
for(int i=0; i<hashes; i++){
key2=hash(key2, i);
if(verbose){System.err.println("key2="+key2+", value="+readHashed(key2));}
int x=incrementHashedLocalAndReturnUnincremented(key2, incr);
value=min(value, x);
key2=Long.rotateRight(key2, hashBits);
}
return value;
}
@Override
public int incrementAndReturnUnincremented(final long[] rawKeys, final int incr){
if(verbose){System.err.println("\n*** Incrementing raw keys "+Arrays.toString(rawKeys)+" ***");}
if(prefilter!=null){
int x=prefilter.read(rawKeys);
if(x<prefilterLimit){return x;}
}
long key2=hash(rawKeys[0], (int)(1+(rawKeys[0])%5));
int value=maxValue;
for(int i=0; i<hashes; i++){
for(int keynum=0; keynum<rawKeys.length; keynum++){
key2=hash(key2^rawKeys[keynum], i);
if(verbose){System.err.println("key2="+key2+", value="+readHashed(key2));}
}
int x=incrementHashedLocalAndReturnUnincremented(key2, incr);
value=min(value, x);
key2=Long.rotateRight(key2, hashBits);
}
// assert(value+1==read(rawKeys) || value==maxValue) : value+", "+read(rawKeys);
return value;
}
@Override
public long[] transformToFrequency(){
return transformToFrequency(matrix);
}
@Override
public ByteBuilder toContentsString(){
ByteBuilder sb=new ByteBuilder();
sb.append('[');
String comma="";
for(AtomicIntegerArray array : matrix){
for(int i=0; i<array.length(); i++){
int word=array.get(i);
for(int j=0; j<cellsPerWord; j++){
int x=word&valueMask;
sb.append(comma);
sb.append(x);
word>>>=cellBits;
comma=", ";
}
}
}
sb.append(']');
return sb;
}
@Override
public double usedFraction(){return cellsUsed()/(double)cells;}
@Override
public double usedFraction(int mindepth){return cellsUsed(mindepth)/(double)cells;}
@Override
public long cellsUsed(int mindepth){
return cellsUsedMT(mindepth);
}
public long cellsUsedMT(int mindepth){
// assert(false) : matrix.length;
ArrayList<CountUsedThread> list=new ArrayList<CountUsedThread>(matrix.length);
for(AtomicIntegerArray aia : matrix){
CountUsedThread ctt=new CountUsedThread(aia, mindepth);
ctt.start();
list.add(ctt);
}
long x=0;
for(CountUsedThread ctt : list){
while(ctt.getState()!=State.TERMINATED){
try {
ctt.join();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
x+=ctt.count;
}
return x;
}
private class CountUsedThread extends Thread{
public CountUsedThread(AtomicIntegerArray a_, int mindepth_){
array=a_;
mindepth=mindepth_;
}
@Override
public void run(){
long temp=0;
if(array!=null){
// System.out.println("C");
// assert(false) : Integer.toBinaryString(valueMask);
if(cellBits==32){
for(int i=0, max=array.length(); i<max; i++){
int word=array.get(i);
if(word!=0){
int x=word&valueMask;
if(x>=mindepth){temp++;}
}
}
}else{
for(int i=0, max=array.length(); i<max; i++){
// System.out.println("D: "+Integer.toHexString(word));
int word=array.get(i);
while(word!=0){
int x=word&valueMask;
// System.out.println("E: "+x+", "+mindepth);
if(x>=mindepth){temp++;}
word=word>>>cellBits;
}
}
}
}
count=temp;
}
private final AtomicIntegerArray array;
private final int mindepth;
public long count;
}
@Override
final long hash(long key, int row){
int cell=(int)((Long.MAX_VALUE&key)%(hashArrayLength-1));
// int cell=(int)(hashCellMask&(key));
if(row==0){//Doublehash only first time
key=key^hashMasks[(row+4)&7][cell];
// key=key^hashMasks[4][cell];
cell=(int)(hashCellMask&(key>>5));
// cell=(int)(hashCellMask&(key>>hashBits));
// cell=(int)((Long.MAX_VALUE&key)%(hashArrayLength-1));
}
assert(row>=0 && row<hashMasks.length) : row+", "+hashMasks.length;
assert(cell>=0 && cell<hashMasks[0].length) : cell+", "+hashMasks[0].length;
return key^hashMasks[row][cell];
}
// @Override
// final long hash(long key, int row){
// long code=key;
// for(int i=0; i<6; i++){
// int row2=((row+i)&7);
// int cell=(int)(key&hashCellMask);
// code^=hashMasks[row2][cell];
// key>>=6;
// }
// return code;
// }
/**
* @param i
* @param j
* @return
*/
private static long[][] makeMasks(int rows, int cols) {
long seed;
synchronized(KCountArray7MTA.class){
seed=counter^SEEDMASK;
counter+=7;
}
Timer t=new Timer();
long[][] r=new long[rows][cols];
Random randy=Shared.threadLocalRandom(seed);
for(int i=0; i<r.length; i++){
fillMasks(r[i], randy);
}
t.stop();
if(t.elapsed>200000000L){System.out.println("Mask-creation time: "+t);}
return r;
}
private static void fillMasks(long[] r, Random randy) {
// for(int i=0; i<r.length; i++){
// long x=0;
// while(Long.bitCount(x&0xFFFFFFFF)!=16){
// x=randy.nextLong();
// }
// r[i]=(x&Long.MAX_VALUE);
// }
final int hlen=(1<<hashBits);
assert(r.length==hlen);
int[] count1=new int[hlen];
int[] count2=new int[hlen];
final long mask=hlen-1;
for(int i=0; i<r.length; i++){
long x=0;
int y=0;
int z=0;
while(Long.bitCount(x&0xFFFFFFFFL)!=16){
x=randy.nextLong();
while(Long.bitCount(x&0xFFFFFFFFL)<16){
x|=(1L<<randy.nextInt(32));
}
while(Long.bitCount(x&0xFFFFFFFFL)>16){
x&=(~(1L<<randy.nextInt(32)));
}
while(Long.bitCount(x&0xFFFFFFFF00000000L)<16){
x|=(1L<<(randy.nextInt(32)+32));
}
while(Long.bitCount(x&0xFFFFFFFF00000000L)>16){
x&=(~(1L<<(randy.nextInt(32)+32)));
}
// System.out.print(".");
// y=(((int)(x&mask))^i);
y=(((int)(x&mask)));
z=(int)((x>>hashBits)&mask);
if(count1[y]>0 || count2[z]>0){
x=0;
}
}
// System.out.println(Long.toBinaryString(x));
r[i]=(x&Long.MAX_VALUE);
count1[y]++;
count2[z]++;
}
}
@Override
public void initialize(){}
@Override
public void shutdown(){
if(finished){return;}
synchronized(this){
if(finished){return;}
cellsUsed=-1;
// for(int i=0; i<numArrays; i++){
// cellsUsed+=cellsUsedPersonal.get(i);
// }
cellsUsed();
assert(!finished);
finished=true;
}
}
private final Lock getLock(long rawKey){
final Lock lock=locks[(int)((rawKey&Long.MAX_VALUE)%(NUM_LOCKS))];
return lock;
}
private int incrementHashedLocal(long key, int amt){
final int num=(int)(key&arrayMask);
final AtomicIntegerArray array=matrix[num];
key=(key>>>arrayBits)%(cellMod);
// key=(key>>>(arrayBits+1))%(cellMod);
int index=(int)(key>>>indexShift);
int cellShift=(int)(cellBits*key);
int value, word, word2;
do{
assert(index>=0) : key+", "+cellMod+", "+cellBits+", "+valueMask+", "+arrayMask+", "+index+", "+num;
word=array.get(index);
value=((word>>>cellShift)&valueMask);
value=(int)min(value+(long)amt, maxValue);
word2=(value<<cellShift)|(word&~((valueMask)<<cellShift));
}while(word!=word2 && !array.compareAndSet(index, word, word2));
// if(value==1){cellsUsedPersonal.incrementAndGet(num);}
return value;
}
// private int incrementHashedLocal_ifAtMost(long key, final int thresh){
// final int num=(int)(key&arrayMask);
// final AtomicIntegerArray array=matrix[num];
// key=(key>>>arrayBits)%(cellMod);
//// key=(key>>>(arrayBits+1))%(cellMod);
// int index=(int)(key>>>indexShift);
// int cellShift=(int)(cellBits*key);
// int value, word, word2;
// do{
// assert(index>=0) : key+", "+cellMod+", "+cellBits+", "+valueMask+", "+arrayMask+", "+index+", "+num;
// word=array.get(index);
// value=((word>>>cellShift)&valueMask);
// if(value>thresh){return value;}//Too high; don't increment
// value=min(value+1, maxValue);
// word2=(value<<cellShift)|(word&~((valueMask)<<cellShift));
// }while(word!=word2 && !array.compareAndSet(index, word, word2));
// return value;
// }
private int incrementHashedLocal_toAtLeast(long key, final int newMin){
assert(newMin<=maxValue);
final int num=(int)(key&arrayMask);
final AtomicIntegerArray array=matrix[num];
key=(key>>>arrayBits)%(cellMod);
// key=(key>>>(arrayBits+1))%(cellMod);
int index=(int)(key>>>indexShift);
int cellShift=(int)(cellBits*key);
int value, word, word2;
do{
assert(index>=0) : key+", "+cellMod+", "+cellBits+", "+valueMask+", "+arrayMask+", "+index+", "+num;
word=array.get(index);
value=((word>>>cellShift)&valueMask);
if(value>=newMin){return value;}//Too high; don't increment
value=min(value+1, maxValue);
word2=(value<<cellShift)|(word&~((valueMask)<<cellShift));
}while(word!=word2 && !array.compareAndSet(index, word, word2));
return value;
}
private int incrementHashedLocalAndReturnUnincremented(long key, int incr){
assert(incr>=0);
final int num=(int)(key&arrayMask);
final AtomicIntegerArray array=matrix[num];
key=(key>>>arrayBits)%(cellMod);
// key=(key>>>(arrayBits+1))%(cellMod);
int index=(int)(key>>>indexShift);
int cellShift=(int)(cellBits*key);
int value, word, word2;
do{
word=array.get(index);
value=((word>>>cellShift)&valueMask);
int value2=min(value+incr, maxValue);
word2=(value2<<cellShift)|(word&~((valueMask)<<cellShift));
}while(word!=word2 && !array.compareAndSet(index, word, word2));
// if(value==1){cellsUsedPersonal.incrementAndGet(num);}
return value;
}
// private int incrementHashedLocalAndReturnUnincremented_ifAtMost(long key, int incr, final int thresh){
// assert(incr>=0);
// final int num=(int)(key&arrayMask);
// final AtomicIntegerArray array=matrix[num];
// key=(key>>>arrayBits)%(cellMod);
//// key=(key>>>(arrayBits+1))%(cellMod);
// int index=(int)(key>>>indexShift);
// int cellShift=(int)(cellBits*key);
// int value, word, word2;
// do{
// word=array.get(index);
// value=((word>>>cellShift)&valueMask);
// if(value>thresh){return value;}//Too high; don't increment
// int value2=min(value+incr, maxValue);
// word2=(value2<<cellShift)|(word&~((valueMask)<<cellShift));
// }while(word!=word2 && !array.compareAndSet(index, word, word2));
//// if(value==1){cellsUsedPersonal.incrementAndGet(num);}
// return value;
// }
private int incrementHashedLocalAndReturnUnincremented_toAtLeast(long key, int newMin){
assert(newMin>0 && newMin<=maxValue) : newMin;
final int num=(int)(key&arrayMask);
final AtomicIntegerArray array=matrix[num];
key=(key>>>arrayBits)%(cellMod);
// key=(key>>>(arrayBits+1))%(cellMod);
int index=(int)(key>>>indexShift);
int cellShift=(int)(cellBits*key);
int value, word, word2;
final int value2s=newMin<<cellShift;
do{
word=array.get(index);
value=((word>>>cellShift)&valueMask);
if(value>=newMin){return value;}//Too high; don't increment
word2=value2s|(word&~((valueMask)<<cellShift));
}while(word!=word2 && !array.compareAndSet(index, word, word2));
// if(value==1){cellsUsedPersonal.incrementAndGet(num);}
return value;
}
private int decrementHashedLocal(long key, int amt){
final int num=(int)(key&arrayMask);
final AtomicIntegerArray array=matrix[num];
key=(key>>>arrayBits)%(cellMod);
// key=(key>>>(arrayBits+1))%(cellMod);
int index=(int)(key>>>indexShift);
int cellShift=(int)(cellBits*key);
int value, word, word2;
do{
word=array.get(index);
value=((word>>>cellShift)&valueMask);
value=max(value-amt, 0);
word2=(value<<cellShift)|(word&~((valueMask)<<cellShift));
}while(word!=word2 && !array.compareAndSet(index, word, word2));
// if(value==1){cellsUsedPersonal.incrementAndGet(num);}
return value;
}
public long cellsUsed(){
if(cellsUsed<0){
synchronized(this){
if(cellsUsed<0){
cellsUsed=cellsUsed(1);
}
}
}
return cellsUsed;
}
@Override
public KCountArray prefilter(){
return prefilter;
}
@Override
public void purgeFilter(){
prefilter=null;
}
public static synchronized void setSeed(long seed){
if(seed>=0){SEEDMASK=seed;}
else{
Random randy=new Random();
SEEDMASK=randy.nextLong();
}
}
private boolean finished=false;
private long cellsUsed;
private final AtomicIntegerArray[] matrix;
private final int hashes;
private final int wordsPerArray;
private final long cellsPerArray;
private final long cellMod;
private final long[][] hashMasks=makeMasks(8, hashArrayLength);
private final int prefilterLimit;
private static final int hashBits=6;
private static final int hashArrayLength=1<<hashBits;
private static final int hashCellMask=hashArrayLength-1;
private KCountArray prefilter;
private final transient boolean useLocks;
private final transient Lock[] locks;
private static final transient int NUM_LOCKS=1999;
private static long counter=0;
private static long SEEDMASK=0;
}
|