📄 heap.c
字号:
ubound = (int)mxGetScalar(prhs[13]);
uoffset = (double)mxGetScalar(prhs[14]);
ns = heapInsertL(key, pY, pZ, dim, cnum, pweight, utarget, delta, pA, sqradius, lbound, ubound, uoffset, pH);
break;
default:
myprintf("\nUsage: nSwaps = heap('Insert', myHeap, ...);\n\n");
myprintf(" Inserts a node into the heap having the specified properties.\n");
myprintf(" Returns the number of swaps/bubbleUp operations performed.\n\n");
myerrmsg("Invalid heap operation, please try again.\n\n");
break;
}
plhs[0] = mycreatedoublescalar((double)ns);
} else if (mystrncasecmp(Op,"ReplaceMin",3) == 0) {
if ((nrhs < 6) || (nlhs != 1)) {
type = -1;
} else {
pH = (struct heap *)mxGetPr(prhs[1]);
type = pH->type;
key = (double)mxGetScalar(prhs[2]);
pY = mxGetPr(prhs[3]);
nY = mxGetN(prhs[3])*mxGetM(prhs[3]);
pZ = mxGetPr(prhs[4]);
nZ = mxGetN(prhs[4])*mxGetM(prhs[4]);
dim = (int)mxGetScalar(prhs[5]);
if ((nY != dim) || (nY+nZ != pH->spdim)) {
myprintf("\nError: Either dim(parentResidualTarget) ~= residualDimension or\n");
myprintf(" dim(pRT)+dim(accumulatedSymbolDecisions) ~= problemDimension.\n\n");
myerrmsg("Invalid heap operation, please try again.\n\n");
}
}
switch (type) {
case BASIC:
if (nrhs != 6) {
myprintf("\nUsage: nSwaps = heap('ReplaceMin', myHeap,\n");
myprintf(" biasedNodeWeight,\n");
myprintf(" parentResidualTarget,\n");
myprintf(" accumulatedSymbolDecisions,\n");
myprintf(" residualDimension);\n\n");
myprintf(" Replaces the minimum weight (root) node of myHeap by a node\n");
myprintf(" having the specified properties. Returns the number of swaps/\n");
myprintf(" bubbleDown operations performed.\n\n");
myerrmsg("Invalid heap operation, please try again.\n\n");
}
ns = heapReplaceMinb(key, pY, pZ, dim, pH);
break;
case LATTICE:
if (nrhs != 10) {
myprintf("\nUsage: nSwaps = heap('ReplaceMin', myHeap,\n");
myprintf(" biasedNodeWeight,\n");
myprintf(" parentResidualTarget,\n");
myprintf(" accumulatedSymbolDecisions,\n");
myprintf(" residualDimension,\n");
myprintf(" childOrdinal,\n");
myprintf(" unbiasedParentNodeWeight,\n");
myprintf(" unconstrainedTarget,\n");
myprintf(" previousSiblingDecision);\n\n");
myprintf(" Replaces the minimum weight (root) node of myHeap by a node\n");
myprintf(" having the specified properties. Returns the number of swaps/\n");
myprintf(" bubbleDown operations performed.\n\n");
myerrmsg("Invalid heap operation, please try again.\n\n");
}
cnum = (int)mxGetScalar(prhs[6]);
pweight = (double)mxGetScalar(prhs[7]);
utarget = (double)mxGetScalar(prhs[8]);
delta = (double)mxGetScalar(prhs[9]);
ns = heapReplaceMini(key, pY, pZ, dim, cnum, pweight, utarget, delta, pH);
break;
case LAST:
if (nrhs != 15) {
myprintf("\nUsage: nSwaps = heap('ReplaceMin', myHeap,\n");
myprintf(" biasedNodeWeight,\n");
myprintf(" parentResidualTarget,\n");
myprintf(" accumulatedSymbolDecisions,\n");
myprintf(" residualDimension,\n");
myprintf(" childOrdinal,\n");
myprintf(" unbiasedParentNodeWeight,\n");
myprintf(" unconstrainedTarget,\n");
myprintf(" previousSiblingDecision,\n");
myprintf(" parentShapingSphereOffset,\n");
myprintf(" shapingSphereSquaredRadius,\n");
myprintf(" siblingRangeLowerBound,\n");
myprintf(" siblingRangeUpperBound,\n");
myprintf(" unconstrainedOffset);\n\n");
myprintf(" Replaces the minimum weight (root) node of myHeap by a node\n");
myprintf(" having the specified properties. Returns the number of swaps/\n");
myprintf(" bubbleDown operations performed.\n\n");
myerrmsg("Invalid heap operation, please try again.\n\n");
}
cnum = (int)mxGetScalar(prhs[6]);
pweight = (double)mxGetScalar(prhs[7]);
utarget = (double)mxGetScalar(prhs[8]);
delta = (double)mxGetScalar(prhs[9]);
pA = mxGetPr(prhs[10]);
sqradius = (double)mxGetScalar(prhs[11]);
lbound = (int)mxGetScalar(prhs[12]);
ubound = (int)mxGetScalar(prhs[13]);
uoffset = (double)mxGetScalar(prhs[14]);
ns = heapReplaceMinl(key, pY, pZ, dim, cnum, pweight, utarget, delta, pA, sqradius, lbound, ubound, uoffset, pH);
break;
default:
myprintf("\nUsage: nSwaps = heap('ReplaceMin', myHeap, ...);\n\n");
myprintf(" Replaces the minimum weight (root) node of myHeap by a node\n");
myprintf(" having the specified properties. Returns the number of swaps/\n");
myprintf(" bubbleDown operations performed.\n\n");
myerrmsg("Invalid heap operation, please try again.\n\n");
break;
}
plhs[0] = mycreatedoublescalar((double)ns);
} else if (mystrncasecmp(Op,"GetMin",6) == 0) {
if ((nrhs != 2) || (nlhs < 4)) {
type = -1;
} else {
pH = (struct heap *)mxGetPr(prhs[1]);
type = pH->type;
pNode = heapGetMin(pH);
if (pNode) {
plhs[0] = mycreatedoublescalar((double)(pNode->key));
plhs[1] = mxCreateDoubleMatrix(pNode->dim,1,mxREAL);
memcpy(mxGetPr(plhs[1]),pNode->pY,pNode->dim*sizeof(double));
plhs[2] = mxCreateDoubleMatrix(pH->spdim-pNode->dim,1,mxREAL);
memcpy(mxGetPr(plhs[2]),pNode->pZ,(pH->spdim-pNode->dim)*sizeof(double));
plhs[3] = mycreatedoublescalar((double)(pNode->dim));
} else {
plhs[0] = mycreatedoublescalar(-1);
plhs[1] = mxCreateDoubleMatrix(0,0,mxREAL);
plhs[2] = mxCreateDoubleMatrix(0,0,mxREAL);
plhs[3] = mxCreateDoubleMatrix(0,0,mxREAL);
}
}
switch (type) {
case BASIC:
if (nlhs != 4) {
myprintf("\nUsage: [biasedNodeWeight,\n");
myprintf(" parentResidualTarget,\n");
myprintf(" accumulatedSymbolDecisions,\n");
myprintf(" residualDimension] = heap('GetMin', myHeap);\n\n");
myprintf(" Returns the attributes of the node in myHeap having the\n");
myprintf(" smallest biasedWeight. The number of swaps/bubbleDown\n");
myprintf(" operations is also returned.\n\n");
myerrmsg("Invalid heap operation, please try again.\n\n");
}
break;
case LATTICE:
if (nlhs != 8) {
myprintf("\nUsage: [biasedNodeWeight,\n");
myprintf(" parentResidualTarget,\n");
myprintf(" accumulatedSymbolDecisions,\n");
myprintf(" residualDimension,\n");
myprintf(" childOrdinal,\n");
myprintf(" unbiasedParentNodeWeight,\n");
myprintf(" unconstrainedTarget,\n");
myprintf(" previousSiblingDecision] = heap('GetMin', myHeap);\n\n");
myprintf(" Returns the attributes of the node in myHeap having the\n");
myprintf(" smallest biasedWeight. The number of swaps/bubbleDown\n");
myprintf(" operations is also returned.\n\n");
myerrmsg("Invalid heap operation, please try again.\n\n");
}
pNodei = (struct hNodeI *)pNode;
if (pNodei) {
plhs[4] = mycreatedoublescalar((double)(pNodei->cnum));
plhs[5] = mycreatedoublescalar((double)(pNodei->pweight));
plhs[6] = mycreatedoublescalar((double)(pNodei->utarget));
plhs[7] = mycreatedoublescalar((double)(pNodei->delta));
} else {
plhs[4] = mxCreateDoubleMatrix(0,0,mxREAL);
plhs[5] = mxCreateDoubleMatrix(0,0,mxREAL);
plhs[6] = mxCreateDoubleMatrix(0,0,mxREAL);
plhs[7] = mxCreateDoubleMatrix(0,0,mxREAL);
}
break;
case LAST:
if (nlhs != 13) {
myprintf("\nUsage: [biasedNodeWeight,\n");
myprintf(" parentResidualTarget,\n");
myprintf(" accumulatedSymbolDecisions,\n");
myprintf(" residualDimension,\n");
myprintf(" childOrdinal,\n");
myprintf(" unbiasedParentNodeWeight,\n");
myprintf(" unconstrainedTarget,\n");
myprintf(" previousSiblingDecision,\n");
myprintf(" parentShapingSphereOffset,\n");
myprintf(" shapingSphereSquaredRadius,\n");
myprintf(" siblingRangeLowerBound,\n");
myprintf(" siblingRangeUpperBound,\n");
myprintf(" unconstrainedOffset] = heap('GetMin', myHeap);\n\n");
myprintf(" Returns the attributes of the node in the myHeap having the\n");
myprintf(" smallest biasedWeight. The number of swaps/bubbleDown\n");
myprintf(" operations is also returned.\n\n");
myerrmsg("Invalid heap operation, please try again.\n\n");
}
pNodel = (struct hNodeL *)pNode;
if (pNodel) {
plhs[4] = mycreatedoublescalar((double)(pNodel->cnum));
plhs[5] = mycreatedoublescalar((double)(pNodel->pweight));
plhs[6] = mycreatedoublescalar((double)(pNodel->utarget));
plhs[7] = mycreatedoublescalar((double)(pNodel->delta));
plhs[8] = mxCreateDoubleMatrix(pNodel->dim,1,mxREAL);
memcpy(mxGetPr(plhs[8]),pNodel->pA,pNodel->dim*sizeof(double));
plhs[9] = mycreatedoublescalar((double)(pNodel->sqradius));
plhs[10] = mycreatedoublescalar((double)(pNodel->lbound));
plhs[11] = mycreatedoublescalar((double)(pNodel->ubound));
plhs[12] = mycreatedoublescalar((double)(pNodel->uoffset));
} else {
plhs[4] = mxCreateDoubleMatrix(0,0,mxREAL);
plhs[5] = mxCreateDoubleMatrix(0,0,mxREAL);
plhs[6] = mxCreateDoubleMatrix(0,0,mxREAL);
plhs[7] = mxCreateDoubleMatrix(0,0,mxREAL);
plhs[8] = mxCreateDoubleMatrix(0,0,mxREAL);
plhs[9] = mxCreateDoubleMatrix(0,0,mxREAL);
plhs[10] = mxCreateDoubleMatrix(0,0,mxREAL);
plhs[11] = mxCreateDoubleMatrix(0,0,mxREAL);
plhs[12] = mxCreateDoubleMatrix(0,0,mxREAL);
}
break;
default:
myprintf("\nUsage: [ ... ] = heap('GetMin', myHeap);\n\n");
myprintf(" Returns the attributes of the node in myHeap having the\n");
myprintf(" smallest biasedWeight.\n\n");
myerrmsg("Invalid heap operation, please try again.\n\n");
break;
}
} else if (mystrncasecmp(Op,"DelMin",6) == 0) {
if ((nrhs != 2) || (nlhs != 1)) {
type = -1;
} else {
pH = (struct heap *)mxGetPr(prhs[1]);
type = pH->type;
}
switch (type) {
case BASIC:
case LATTICE:
ns = heapDelMini(pH);
break;
case LAST:
ns = heapDelMinl(pH);
break;
default:
myprintf("\nUsage: nSwaps = heap('DelMin', myHeap);\n\n");
myprintf(" Deletes the minimum weight (root) node of myHeap. Returns the\n");
myprintf(" number of swaps/bubbleDown operations performed.\n\n");
myerrmsg("Invalid heap operation, please try again.\n\n");
break;
}
plhs[0] = mycreatedoublescalar((double)ns);
} else if (mystrncasecmp(Op,"GetSize",6)==0) {
if ((nrhs != 2) || (nlhs != 1)) {
myprintf("\nUsage: nNodes = heap('GetSize', myHeap);\n\n");
myprintf(" Returns the number of nodes in myHeap.\n\n");
myerrmsg("Invalid heap operation, please try again.\n\n");
}
pH = (struct heap *)mxGetPr(prhs[1]);
nn = heapGetSize(pH);
plhs[0] = mycreatedoublescalar((double)nn);
} else if (mystrncasecmp(Op,"GetMaxSize",6)==0) {
if ((nrhs != 2) || (nlhs != 1)) {
myprintf("\nUsage: T = heap('GetMaxSize', myHeap);\n\n");
myprintf(" Returns the maximum number of nodes stored in myHeap.\n\n");
myerrmsg("Invalid heap operation, please try again.\n\n");
}
pH = (struct heap *)mxGetPr(prhs[1]);
T = heapGetMaxSize(pH);
plhs[0] = mycreatedoublescalar((double)T);
} else if (mystrncasecmp(Op,"GetDim",6)==0) {
if ((nrhs != 2) || (nlhs != 1)) {
myprintf("\nUsage: m = heap('GetDim', myHeap);\n\n");
myprintf(" Returns the problem dimension associated with myHeap.\n\n");
myerrmsg("Invalid heap operation, please try again.\n\n");
}
pH = (struct heap *)mxGetPr(prhs[1]);
spdim = heapGetDim(pH);
plhs[0] = mycreatedoublescalar((double)spdim);
} else if (mystrncasecmp(Op,"Clear",3) == 0) {
if ((nrhs != 2) || (nlhs != 0)) {
myprintf("\nUsage: heap('Clear', myHeap);\n\n");
myprintf(" Clears myHeap, i.e., deletes all nodes and frees any memory\n");
myprintf(" allocated to them. The heap itself remains in place.\n\n");
myerrmsg("Invalid heap operation, please try again.\n\n");
}
pH = (struct heap *)mxGetPr(prhs[1]);
pH->pLastDel = NULL;
heapClear(pH);
} else if (mystrncasecmp(Op,"Delete",6) == 0) {
if ((nrhs != 3) || (nlhs != 0)) {
myprintf("\nUsage: heap('Delete', myHeap, 'myHeap');\n\n");
myprintf(" Deletes myHeap, i.e., deletes all nodes and frees any memory\n");
myprintf(" allocated to them, as well as to the heap itself. The variable\n");
myprintf(" myHeap is cleared.\n\n");
myerrmsg("Invalid heap operation, please try again.\n\n");
}
pH = (struct heap *)mxGetPr(prhs[1]);
pH->pLastDel = NULL;
heapClear(pH);
/* tempArray = mxCreateString("H"); */
/* mxGetName(prhs[1]); */
mexCallMATLAB(0,NULL,1,(mxArray **)&prhs[2],"clear");
} else if (mystrncasecmp(Op,"Print",3) == 0) {
if ((nrhs != 2) || (nlhs != 0)) {
myprintf("\nUsage: heap('Print', myHeap);\n\n");
myprintf(" Prints some possibly useful information about myHeap.\n\n");
myerrmsg("Invalid heap operation, please try again.\n\n");
}
pH = (struct heap *)mxGetPr(prhs[1]);
heapPrintContents(pH);
} else { heapPrintUsage(); }
}
#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -