void MaxHeap::pushDown(int pos, int key){ int keyLeft = 0; int keyRight = 0; int leftPos = pos * 2; int rightPos = pos * 2 + 1; if(rightPos < length){ keyLeft = (songs + leftPos)->getKey(¤tTime); keyRight = (songs + rightPos)->getKey(¤tTime); if(keyLeft > keyRight){ if(keyLeft > key){ swap(pos, leftPos); pushDown(leftPos, key); } }else{ if(keyRight > key){ swap(pos, rightPos); pushDown(rightPos, key); } } }else{ if(leftPos < length){ keyLeft = (songs + leftPos)->getKey(¤tTime); if(keyLeft > key){ swap(pos, leftPos); pushDown(leftPos, key); } } } }
inline int suc(int x) { pushDown(x); int y = child[x][1]; pushDown(y); while (child[y][0]) { y = child[y][0]; pushDown(y); } return y; }
inline void rotate(int x, int c) { int y = fa[x]; pushDown(y); pushDown(x); child[y][!c] = child[x][c]; fa[child[x][c]] = y; fa[x] = fa[y]; if (fa[x]) { child[fa[x]][child[fa[y]][1] == y] = x; } child[x][c] = y; fa[y] = x; pushUp(y); }
__int64 query(__int64 rt,__int64 l,__int64 r) { if(N[rt].l>r||N[rt].r<l) { //printf("None\n"); return 0; } if(N[rt].l>=l&&N[rt].r<=r) { return N[rt].sum; }//printf("N[%d].l is %d,N[].r is %d\n",rt,N[rt].l,N[rt].r); pushDown(rt); __int64 mid=(N[rt].l+N[rt].r)>>1; __int64 t1=0,t2=0; if(l<=mid) { //printf("left\n"); t1=query(rt<<1,l,r); } if(mid<r) { //printf("right\n"); t2=query(rt<<1|1,l,r); } return (t1+t2); }
inline void splay(int x, int goal) { pushDown(x); while (fa[x] != goal) { if (fa[fa[x]] == goal) { rotate(x, x == child[fa[x]][0]); } else { int y = fa[x]; int z = fa[y]; int f = child[z][0] == y; if (child[y][f] == x) { rotate(x, !f); rotate(x, f); } else { rotate(y, f); rotate(x, f); } } } pushUp(x); if (goal == 0) { root = x; } }
//////////////////////////////////////////////////////////// // Push down any data from the old tree onto the new tree // by doing a traversal over the deleted clusters ////////////////////////////////////////////////////////// void pushDown(cluster* cl) { cluster* ccl; int i; assert(cl); ccl = GET_CL(cl); deprintf("pushDown: cl = %d\n",ccl->id); cluster** clusters = ccl->getClusters(); for(i=0; i < MAX_DEGREE; ++i) { cluster* child = clusters[i]; cluster* ccp = GET_CL(child); if(child) { pushDownFun(ccl,ccp); if(isDeleted(ccp)) { if(ccp->child) { deprintf("Child\n"); pushDown(child); deprintf("ret\n"); } freeCluster(ccp); } } } free(clusters); }
inline void rotateTo(int k, int goal) { int x = root; pushDown(x); k++; while (tree_size[child[x][0]] != k - 1) { if (k - 1 < tree_size[child[x][0]]) { x = child[x][0]; } else { k = k - tree_size[child[x][0]] - 1; x = child[x][1]; } pushDown(x); } splay(x, goal); }
strNode* strNode::pushDown(const std::string &value_){ strNode *newNode = new strNode(value_); newNode->info = info; newNode->depth = depth + 1; return pushDown(newNode); };
Song MaxHeap::getMax(){ Song retSong = *(songs + 1); currentTime = currentTime + (songs + 1)->length; (songs + 1)->lastPlayed = currentTime; int key = (songs + 1)->getKey(¤tTime); pushDown(1, key); return retSong; }
Boolean Btree<T>::pushDown(T& x, unsigned ptr, T& item, unsigned &itemRST) // // Purpose: recursive function that inserts item x in the B-tree. // The function returns TRUE if a the height of the B-tree needs // to be increased. Otherwise, the function yields FALSE. // // Parameters: // // input: x - the inserted data // ptr - the index to the manipulated node // // output: // item - the median element // itemRST - the index to the right subtree of the node // which contains item // { unsigned i; Bstruct<T> *buf; if (ptr == BTREE_NIL) { // cannot insert into an empty tree item = x; itemRST = BTREE_NIL; return TRUE; } else { buf = new Bstruct<T>; readNode(buf, ptr); // attempt to isnert a duplicate key in the current node? if (searchNode(x, buf, i)) { strcpy(errMsg, "Cannot insert duplicates"); return FALSE; } // reinsert the median element if (pushDown(x, buf->nodeLink[i], item, itemRST)) { if (buf->count < BTREE_MAX) { pushIn(item, itemRST, buf, i); writeNode(buf, ptr); delete buf; return FALSE; } else { cutNode(item, itemRST, ptr, i, item, itemRST); delete buf; return TRUE; } } else { delete buf; return FALSE; } } }
inline void printNode(int x) { pushDown(x); if (child[x][0]) { printNode(child[x][0]); } printf("%d ", value[x]); if (child[x][1]) { printNode(child[x][1]); } }
/////////////////////////////////////////////////////// // Go over the list of old trees, and for each one of // them push down their data onto the new trees /////////////////////////////////////////////////////// void pushDownList(clusterList* rootList) { while(rootList->head != NULL) { cluster* cl = removeCluster(rootList); pushDown(cl); freeBlock(currentTree->UClusters,(char*) cl); deprintf("done 1\n"); } deprintf("done\n"); }
void update(int L,int R,int c,int l,int r,int rt) { if(L<=l&&R>=r) { col[rt]=c; sum[rt]=(r-l+1)*c; return; } pushDown(rt, r-l+1); int m=(l+r)>>1; if(L<=m)update(L,R,c,l,m,rt<<1); if(R>m)update(L,R,c,m+1,r,rt<<1|1); pushUp(rt); }
void update(int u, int l, int r, int b, int e, int v) { if(b <= l && e >= r) { node[u] = lazy[u] = v; return; } pushDown(u); int m = l + (r - l) / 2; if(b <= m) { update(lson(u), l, m, b, e, v); } if(e > m) { update(rson(u), m + 1, r, b, e, v); } pushUp(u); }
void change(int i, int l, int r, int dgr) { Node_t &x = node[i]; if(x.l > r || x.r < l) return; if(l <= x.l && x.r <= r) { mark(i, dgr); return; } pushDown(i); change(i * 2, l, r, dgr); change(i * 2 + 1, l, r, dgr); update(i); }
void update(__int64 uL,__int64 uR,__int64 c,__int64 rt) { if(N[rt].l>=uL&&uR>=N[rt].r) { N[rt].addMark+=c; N[rt].sum+=(__int64)c*(N[rt].r-N[rt].l+1); return; } pushDown(rt); __int64 mid=(N[rt].l+N[rt].r)>>1; if(uL<=mid)update(uL,uR,c,rt<<1); if(uR>mid)update(uL,uR,c,rt<<1|1); pushUp(rt); }
int query(int u, int l, int r, int b, int e) { if(b <= l && e >= r) { return node[u]; } pushDown(u); int m = l + (r - l) / 2; int res = 0; if(b <= m) { res = max(res, query(lson(u), l, m, b, e)); } if(e > m) { res = max(res, query(rson(u), m + 1, r, b, e)); } return res; }
void MaxHeap::dislike(std::string title){ Song tmp = Song(); tmp.title = title; int pos = hashmap.getValue(tmp); if(pos == -1){ // std::cout << title << " is not in your playlist" << std::endl; return; } if((songs + pos)->likeability < 0){ (songs + pos)->likeability --; }else{ (songs + pos)->likeability = -1; } int key = (songs + pos)->getKey(¤tTime); pushDown(pos, key); }
void update(int order, int left, int right) { int mid = (segment[order].left + segment[order].right) >> 1; if (segment[order].left >= left && segment[order].right <= right) { swapBlackWhite(order); segment[order].lazy ^= 1; return ; } pushDown(order); if (right <= mid) { update(order * 2, left, right); } else if (left > mid) { update(order * 2 + 1, left, right); } else { update(order * 2, left, mid); update(order * 2 + 1, mid + 1, right); } pushUp(order); }
int query(int order, int left, int right) { int mid = (segment[order].left + segment[order].right) >> 1; int left_answer, right_answer, mid_answer, final_answer; if (segment[order].left >= left && segment[order].right <= right) { return segment[order].black_length; } pushDown(order); if (right <= mid) { return query(order * 2, left, right); } else if (left > mid) { return query(order * 2 + 1, left, right); } else { left_answer = query(order * 2, left, mid); right_answer = query(order * 2 + 1, mid + 1, right); mid_answer = segment[order * 2].right_black + segment[order * 2 + 1].left_black; final_answer = max(left_answer, right_answer); final_answer = max(final_answer, mid_answer); return final_answer; } }
Boolean Btree<T>::insert(T& x) // // Purpose: performs node insertion in a B-tree. // // Parameters: // // input: x - the inserted data // { T r; // data for the node that is inserted as the new root unsigned p, rRST; // the right subtree of 'r' Bstruct<T> *buf; // if item x is in the tree exit if (search(x)) return FALSE; // did the tree grow in height? if (pushDown(x, root, r, rRST)) { // get the index of a new node p = getNodeIndex(); // make new root buf = new Bstruct<T>; numNodes++; buf->count = 1; buf->data[1] = r; buf->nodeLink[0] = root; buf->nodeLink[1] = rRST; for (unsigned i = 2; i <= BTREE_MAX; i++) buf->nodeLink[i] = BTREE_NIL; root = p; // save the new node writeNode(buf, p); delete buf; } numData++; return TRUE; }
//增加下一个地图的步骤 void addNxtStep(struct sokomap *header,struct sokomap **currentTail,struct sokomap *current,struct mappos * boxPos,int boxCount) { //计算人所在的列表 if(current->pos==NULL) { current->pos = personCanWalkPoint(current); } getBoxList(current,boxPos); //进行循环判断箱子可以移动的方向 for(int i=0;i < boxCount;i++) { int x = boxPos[i].x; int y = boxPos[i].y; //向上推的操作 if(isCanPushUp(current,x,y)) { if(isInMappos(current->pos,boxPos[i].x,boxPos[i].y+1)) { struct sokomap * tmpMap = copyMap(current); tmpMap ->parent = current; pushUp(tmpMap,x,y); //判断地图是否已经棍了,没棍的话就加入到尾步 if(isMapDead(tmpMap,x,y+1,UP)) { freeMap(tmpMap); }else if(isInSokoMap(header,tmpMap))//已经在列表中,就把这个新地图删除 { freeMap(tmpMap); }else { (*currentTail)->next = tmpMap; *currentTail = tmpMap; } } } //向下推的操作 if(isCanPushDown(current,boxPos[i].x,boxPos[i].y)) { if(isInMappos(current->pos,boxPos[i].x,boxPos[i].y - 1)) { struct sokomap * tmpMap = copyMap(current); tmpMap ->parent = current; pushDown(tmpMap,x,y); //判断地图是否已经棍了,没棍的话加入到尾部 if(isMapDead(tmpMap,x,y-1,DOWN)) { freeMap(tmpMap); }else if(isInSokoMap(header,tmpMap))//已经在列表中,就把这个新地图删除 { freeMap(tmpMap); }else { (*currentTail)->next = tmpMap; *currentTail = tmpMap; } } } //向左推的操作 if(isCanPushLeft(current,boxPos[i].x,boxPos[i].y)) { if(isInMappos(current->pos,boxPos[i].x + 1,boxPos[i].y)) { struct sokomap * tmpMap = copyMap(current); tmpMap ->parent = current; pushLeft(tmpMap,x,y); //判断地图是否已经棍了,没棍的话加入到尾部 if(isMapDead(tmpMap,x - 1,y,LEFT)) { freeMap(tmpMap); }else if(isInSokoMap(header,tmpMap))//已经在列表中,就把这个新地图删除 { freeMap(tmpMap); }else { (*currentTail)->next = tmpMap; *currentTail = tmpMap; } } } //向右推的操作 if(isCanPushRight(current,boxPos[i].x,boxPos[i].y)) { if(isInMappos(current->pos,boxPos[i].x - 1,boxPos[i].y)) { struct sokomap * tmpMap = copyMap(current); tmpMap ->parent = current; pushRight(tmpMap,x,y); //判断地图是否已经棍了,没棍的话加入到尾部 if(isMapDead(tmpMap,x + 1,y,RIGHT)) { freeMap(tmpMap); }else if(isInSokoMap(header,tmpMap))//已经在列表中,就把这个新地图删除 { freeMap(tmpMap); }else { (*currentTail)->next = tmpMap; *currentTail = tmpMap; } } } } }
/**Function******************************************************************** Synopsis [Exact variable ordering algorithm.] Description [Exact variable ordering algorithm. Finds an optimum order for the variables between lower and upper. Returns 1 if successful; 0 otherwise.] SideEffects [None] SeeAlso [] ******************************************************************************/ int cuddExact( DdManager * table, int lower, int upper) { int k, i, j; int maxBinomial, oldSubsets, newSubsets; int subsetCost; int size; /* number of variables to be reordered */ int unused, nvars, level, result; int upperBound, lowerBound, cost; int roots; char *mask = NULL; DdHalfWord *symmInfo = NULL; DdHalfWord **newOrder = NULL; DdHalfWord **oldOrder = NULL; int *newCost = NULL; int *oldCost = NULL; DdHalfWord **tmpOrder; int *tmpCost; DdHalfWord *bestOrder = NULL; DdHalfWord *order; #ifdef DD_STATS int ddTotalSubsets; #endif /* Restrict the range to be reordered by excluding unused variables ** at the two ends. */ while (table->subtables[lower].keys == 1 && table->vars[table->invperm[lower]]->ref == 1 && lower < upper) lower++; while (table->subtables[upper].keys == 1 && table->vars[table->invperm[upper]]->ref == 1 && lower < upper) upper--; if (lower == upper) return(1); /* trivial problem */ /* Apply symmetric sifting to get a good upper bound and to extract ** symmetry information. */ result = cuddSymmSiftingConv(table,lower,upper); if (result == 0) goto cuddExactOutOfMem; #ifdef DD_STATS (void) fprintf(table->out,"\n"); ddTotalShuffles = 0; ddTotalSubsets = 0; #endif /* Initialization. */ nvars = table->size; size = upper - lower + 1; /* Count unused variable among those to be reordered. This is only ** used to compute maxBinomial. */ unused = 0; for (i = lower + 1; i < upper; i++) { if (table->subtables[i].keys == 1 && table->vars[table->invperm[i]]->ref == 1) unused++; } /* Find the maximum number of subsets we may have to store. */ maxBinomial = getMaxBinomial(size - unused); if (maxBinomial == -1) goto cuddExactOutOfMem; newOrder = getMatrix(maxBinomial, size); if (newOrder == NULL) goto cuddExactOutOfMem; newCost = ALLOC(int, maxBinomial); if (newCost == NULL) goto cuddExactOutOfMem; oldOrder = getMatrix(maxBinomial, size); if (oldOrder == NULL) goto cuddExactOutOfMem; oldCost = ALLOC(int, maxBinomial); if (oldCost == NULL) goto cuddExactOutOfMem; bestOrder = ALLOC(DdHalfWord, size); if (bestOrder == NULL) goto cuddExactOutOfMem; mask = ALLOC(char, nvars); if (mask == NULL) goto cuddExactOutOfMem; symmInfo = initSymmInfo(table, lower, upper); if (symmInfo == NULL) goto cuddExactOutOfMem; roots = ddCountRoots(table, lower, upper); /* Initialize the old order matrix for the empty subset and the best ** order to the current order. The cost for the empty subset includes ** the cost of the levels between upper and the constants. These levels ** are not going to change. Hence, we count them only once. */ oldSubsets = 1; for (i = 0; i < size; i++) { oldOrder[0][i] = bestOrder[i] = (DdHalfWord) table->invperm[i+lower]; } subsetCost = table->constants.keys; for (i = upper + 1; i < nvars; i++) subsetCost += getLevelKeys(table,i); oldCost[0] = subsetCost; /* The upper bound is initialized to the current size of the BDDs. */ upperBound = table->keys - table->isolated; /* Now consider subsets of increasing size. */ for (k = 1; k <= size; k++) { #if DD_STATS (void) fprintf(table->out,"Processing subsets of size %d\n", k); fflush(table->out); #endif newSubsets = 0; level = size - k; /* offset of first bottom variable */ for (i = 0; i < oldSubsets; i++) { /* for each subset of size k-1 */ order = oldOrder[i]; cost = oldCost[i]; lowerBound = computeLB(table, order, roots, cost, lower, upper, level); if (lowerBound >= upperBound) continue; /* Impose new order. */ result = ddShuffle(table, order, lower, upper); if (result == 0) goto cuddExactOutOfMem; upperBound = updateUB(table,upperBound,bestOrder,lower,upper); /* For each top bottom variable. */ for (j = level; j >= 0; j--) { /* Skip unused variables. */ if (table->subtables[j+lower-1].keys == 1 && table->vars[table->invperm[j+lower-1]]->ref == 1) continue; /* Find cost under this order. */ subsetCost = cost + getLevelKeys(table, lower + level); newSubsets = updateEntry(table, order, level, subsetCost, newOrder, newCost, newSubsets, mask, lower, upper); if (j == 0) break; if (checkSymmInfo(table, symmInfo, order[j-1], level) == 0) continue; pushDown(order,j-1,level); /* Impose new order. */ result = ddShuffle(table, order, lower, upper); if (result == 0) goto cuddExactOutOfMem; upperBound = updateUB(table,upperBound,bestOrder,lower,upper); } /* for each bottom variable */ } /* for each subset of size k */ /* New orders become old orders in preparation for next iteration. */ tmpOrder = oldOrder; tmpCost = oldCost; oldOrder = newOrder; oldCost = newCost; newOrder = tmpOrder; newCost = tmpCost; #ifdef DD_STATS ddTotalSubsets += newSubsets; #endif oldSubsets = newSubsets; } result = ddShuffle(table, bestOrder, lower, upper); if (result == 0) goto cuddExactOutOfMem; #ifdef DD_STATS #ifdef DD_VERBOSE (void) fprintf(table->out,"\n"); #endif (void) fprintf(table->out,"#:S_EXACT %8d: total subsets\n", ddTotalSubsets); (void) fprintf(table->out,"#:H_EXACT %8d: total shuffles", ddTotalShuffles); #endif freeMatrix(newOrder); freeMatrix(oldOrder); FREE(bestOrder); FREE(oldCost); FREE(newCost); FREE(symmInfo); FREE(mask); return(1); cuddExactOutOfMem: if (newOrder != NULL) freeMatrix(newOrder); if (oldOrder != NULL) freeMatrix(oldOrder); if (bestOrder != NULL) FREE(bestOrder); if (oldCost != NULL) FREE(oldCost); if (newCost != NULL) FREE(newCost); if (symmInfo != NULL) FREE(symmInfo); if (mask != NULL) FREE(mask); table->errorCode = CUDD_MEMORY_OUT; return(0); } /* end of cuddExact */