static int serialize_sparse(void *_pvCtx, int *_piAddr, int **_piBuffer, int *_piBufferSize, BOOL _bData) { int iRet = 0; int iRows = 0; int iCols = 0; int iItemCount = 0; int *piRowCount = 0; int *piColPos = 0; int iComplex = 0; double *pdblR = NULL; double *pdblI = NULL; int *piOut = NULL; int iOutLen = 0; if (_bData) { //sparse iComplex = isVarComplex(_pvCtx, _piAddr); if (iComplex) { iRet = getAllocatedComplexSparseMatrix(_pvCtx, _piAddr, &iRows, &iCols, &iItemCount, &piRowCount, &piColPos, &pdblR, &pdblI); } else { iRet = getAllocatedSparseMatrix(_pvCtx, _piAddr, &iRows, &iCols, &iItemCount, &piRowCount, &piColPos, &pdblR); } } else { //boolean sparse iRet = getAllocatedBooleanSparseMatrix(_pvCtx, _piAddr, &iRows, &iCols, &iItemCount, &piRowCount, &piColPos); } if (iRet) { return 1; } //5 -> 4 for header + 1 for item count iOutLen = 5 + iRows + iItemCount; if (_bData) { iOutLen += iItemCount * (iComplex + 1) * sizeof(double) / sizeof(int); } piOut = (int *)MALLOC(iOutLen * sizeof(int)); piOut[0] = _bData ? sci_sparse : sci_boolean_sparse; piOut[1] = iRows; piOut[2] = iCols; piOut[3] = iComplex; piOut[4] = iItemCount; memcpy(piOut + 5, piRowCount, iRows * sizeof(int)); memcpy(piOut + 5 + iRows, piColPos, iItemCount * sizeof(int)); if (_bData) { int iOne = 1; double *pRealData = (double *)(piOut + 5 + iRows + iItemCount); C2F(dcopy) (&iItemCount, pdblR, &iOne, pRealData, &iOne); if (iComplex) { double *pImgData = pRealData + iItemCount; C2F(dcopy) (&iItemCount, pdblI, &iOne, pImgData, &iOne); } } *_piBuffer = piOut; *_piBufferSize = iOutLen; if (_bData) { if (iComplex) { freeAllocatedSparseMatrix(piRowCount, piColPos, pdblR); } else { freeAllocatedComplexSparseMatrix(piRowCount, piColPos, pdblR, pdblI); } } else { freeAllocatedBooleanSparse(piRowCount, piColPos); } return 0; }
/*--------------------------------------------------------------------------*/ void freeAllocatedComplexSparseMatrix(int* _piNbItemRows, int* _piColPos, double* _pdblReal, double* _pdblImg) { freeAllocatedSparseMatrix(_piNbItemRows, _piColPos, _pdblReal); FREE(_pdblImg); }
int sparseExample(char *fname,unsigned long fname_len) { SciErr sciErr; int* piAddr = NULL; int iType = 0; int iRet = 0; CheckInputArgument(pvApiCtx, 1, 1); CheckOutputArgument(pvApiCtx, 0, 1); sciErr = getVarAddressFromPosition(pvApiCtx, 1, &piAddr); if(sciErr.iErr) { printError(&sciErr, 0); return 0; } if(isSparseType(pvApiCtx, piAddr)) { int iRows = 0; int iCols = 0; int iNbItem = 0; int* piNbItemRow = NULL; int* piColPos = NULL; double* pdblReal = NULL; double* pdblImg = NULL; if(isVarComplex(pvApiCtx, piAddr)) { iRet = getAllocatedComplexSparseMatrix(pvApiCtx, piAddr, &iRows, &iCols, &iNbItem, &piNbItemRow, &piColPos, &pdblReal, &pdblImg); if(iRet) { freeAllocatedComplexSparseMatrix(piNbItemRow, piColPos, pdblReal, pdblImg); return iRet; } sciErr = createComplexSparseMatrix(pvApiCtx, InputArgument + 1, iRows, iCols, iNbItem, piNbItemRow, piColPos, pdblReal, pdblImg); if(sciErr.iErr) { freeAllocatedComplexSparseMatrix(piNbItemRow, piColPos, pdblReal, pdblImg); printError(&sciErr, 0); return sciErr.iErr; } freeAllocatedComplexSparseMatrix(piNbItemRow, piColPos, pdblReal, pdblImg); } else { iRet = getAllocatedSparseMatrix(pvApiCtx, piAddr, &iRows, &iCols, &iNbItem, &piNbItemRow, &piColPos, &pdblReal); if(iRet) { freeAllocatedSparseMatrix(piNbItemRow, piColPos, pdblReal); return iRet; } sciErr = createSparseMatrix(pvApiCtx, InputArgument + 1, iRows, iCols, iNbItem, piNbItemRow, piColPos, pdblReal); if(sciErr.iErr) { freeAllocatedSparseMatrix(piNbItemRow, piColPos, pdblReal); printError(&sciErr, 0); return sciErr.iErr; } freeAllocatedSparseMatrix(piNbItemRow, piColPos, pdblReal); } AssignOutputVariable(1) = InputArgument + 1; } else { AssignOutputVariable(1) = 0; } return 0; }