cpHandleInit(cpHandle *hand, object obj) { hand.obj = obj; hand.retain = 0; hand.stamp = 0; return hand; }
static int handleSetEql(object obj, cpHandle *hand){return (obj == hand.obj);}
cpHandleRelease(cpHandle *hand, cpArray *pooledHandles) { hand.retain--; if(hand.retain == 0) cpArrayPush(pooledHandles, hand); }
static void cpHandleRetain(cpHandle *hand){hand.retain++;}
rehash_helper(cpHandle *hand, cpSpaceHash *hash) { hashHandle(hash, hand, hash.spatialIndex.bbfunc(hand.obj)); }
hashHandle(cpSpaceHash *hash, cpHandle *hand, cpBB bb) { // Find the dimensions in cell coordinates. double dim = hash.celldim; int l = floor_int(bb.l/dim); // Fix by ShiftZ int r = floor_int(bb.r/dim); int b = floor_int(bb.b/dim); int t = floor_int(bb.t/dim); int n = hash.numcells; for(int i=l; i<=r; i++){ for(int j=b; j<=t; j++){ cpHashValue idx = hash_func(i,j,n); cpSpaceHashBin *bin = hash.table[idx]; // Don't add an object twice to the same cell. if(containsHandle(bin, hand)) continue; cpHandleRetain(hand); // Insert a new bin for the handle in this cell. cpSpaceHashBin *newBin = getEmptyBin(hash); newBin.handle = hand; newBin.next = bin; hash.table[idx] = newBin; } } }
containsHandle(cpSpaceHashBin *bin, cpHandle *hand) { while(bin){ if(bin.handle == hand) return true; bin = bin.next; } return false; }