2 * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3 * Copyright (C) 2009 - DIGITEO - Antoine ELIAS
5 * Copyright (C) 2012 - 2016 - Scilab Enterprises
7 * This file is hereby licensed under the terms of the GNU GPL v2.0,
8 * pursuant to article 5.3.4 of the CeCILL v.2.1.
9 * This file was originally licensed under the terms of the CeCILL v2.1,
10 * and continues to be available under such terms.
11 * For more information, see the COPYING file which you should have received
12 * along with this program.
14 * Please note that piece of code will be rewrited for the Scilab 6 family
15 * However, the API (profile of the functions in the header files) will be
16 * still available and supported in Scilab 6.
20 #include "context.hxx"
21 #include "gatewaystruct.hxx"
26 #include "sci_malloc.h"
27 #include "api_scilab.h"
28 #include "call_scilab.h"
29 #include "core_math.h"
30 #include "api_internal_common.h"
31 #include "api_stack_boolean_sparse.h"
32 #include "api_internal_boolean_sparse.h"
33 #include "localization.h"
36 SciErr getBooleanSparseMatrix(void* _pvCtx, int* _piAddress, int* _piRows, int* _piCols, int* _piNbItem, int** _piNbItemRow, int** _piColPos)
38 SciErr sciErr = sciErrInit();
41 if (_piAddress == NULL)
43 addErrorMessage(&sciErr, API_ERROR_INVALID_POINTER, _("%s: Invalid argument address"), "getBooleanSparseMatrix");
47 sciErr = getVarType(_pvCtx, _piAddress, &iType);
48 if (sciErr.iErr || iType != sci_boolean_sparse)
50 addErrorMessage(&sciErr, API_ERROR_GET_BOOLEAN_SPARSE, _("%s: Unable to get argument #%d"), "getBooleanSparseMatrix", getRhsFromAddress(_pvCtx, _piAddress));
54 sciErr = getVarDimension(_pvCtx, _piAddress, _piRows, _piCols);
57 addErrorMessage(&sciErr, API_ERROR_GET_BOOLEAN_SPARSE, _("%s: Unable to get argument #%d"), "getBooleanSparseMatrix", getRhsFromAddress(_pvCtx, _piAddress));
61 types::SparseBool* pSpBool = ((types::InternalType*)_piAddress)->getAs<types::SparseBool>();
62 *_piNbItem = (int)pSpBool->nbTrue();
64 if (!sciErr.iErr && _piNbItemRow != NULL)
66 int* piNbItemRows = (int*)MALLOC(sizeof(int) **_piRows);
67 *_piNbItemRow = pSpBool->getNbItemByRow(piNbItemRows);
69 if (!sciErr.iErr && _piColPos != NULL)
71 int* piColPos = (int*)MALLOC(sizeof(int) **_piNbItem);
72 *_piColPos = pSpBool->getColPos(piColPos);
78 SciErr allocBooleanSparseMatrix(void* _pvCtx, int _iVar, int _iRows, int _iCols, int _iNbItem, int** _piNbItemRow, int** _piColPos)
80 SciErr sciErr = sciErrInit();
81 // We cant rewrite this function in Scilab 6
82 // because sparses are not stored like scilab 5.
83 // We cant return pointer to _piNbItemRow and
84 // _piColPos and let user fill it.
88 SciErr fillBooleanSparseMatrix(void* _pvCtx, int* _piAddress, int _iRows, int _iCols, int _iNbItem, const int* _piNbItemRow, const int* _piColPos)
90 SciErr sciErr = sciErrInit();
92 if (_piAddress == NULL)
94 addErrorMessage(&sciErr, API_ERROR_INVALID_POINTER, _("%s: Invalid argument address"), "fillBooleanSparseMatrix");
98 types::SparseBool* pSparse = (types::SparseBool*)_piAddress;
100 for (int i = 0; i < _iRows; i++)
102 for (int j = 0; j < _piNbItemRow[i]; j++)
104 int iIndex = (*_piColPos++ - 1) * _iRows + i;
105 pSparse->set(iIndex, true);
112 SciErr createBooleanSparseMatrix(void* _pvCtx, int _iVar, int _iRows, int _iCols, int _iNbItem, const int* _piNbItemRow, const int* _piColPos)
114 SciErr sciErr = sciErrInit();
116 if (_iRows == 0 && _iCols == 0)
119 sciErr = createMatrixOfDouble(_pvCtx, _iVar, 0, 0, &dblReal);
122 addErrorMessage(&sciErr, API_ERROR_CREATE_EMPTY_MATRIX, _("%s: Unable to create variable in Scilab memory"), "createEmptyMatrix");
127 types::GatewayStruct* pStr = (types::GatewayStruct*)_pvCtx;
128 types::InternalType** out = pStr->m_pOut;
130 types::SparseBool* pSparse = new types::SparseBool(_iRows, _iCols);
133 addErrorMessage(&sciErr, API_ERROR_CREATE_BOOLEAN_SPARSE, _("%s: Unable to create variable in Scilab memory"), "createBooleanSparseMatrix");
137 int rhs = _iVar - *getNbInputArgument(_pvCtx);
138 out[rhs - 1] = pSparse;
140 sciErr = fillBooleanSparseMatrix(_pvCtx, (int*)pSparse, _iRows, _iCols, _iNbItem, _piNbItemRow, _piColPos);
144 SciErr createNamedBooleanSparseMatrix(void* _pvCtx, const char* _pstName, int _iRows, int _iCols, int _iNbItem, const int* _piNbItemRow, const int* _piColPos)
146 SciErr sciErr = sciErrInit();
148 // check variable name
149 if (checkNamedVarFormat(_pvCtx, _pstName) == 0)
151 addErrorMessage(&sciErr, API_ERROR_CREATE_EMPTY_MATRIX, _("%s: Invalid variable name: %s."), "createNamedMatrixOfBoolean", _pstName);
155 //return named empty matrix
156 if (_iRows == 0 && _iCols == 0)
159 sciErr = createNamedMatrixOfDouble(_pvCtx, _pstName, 0, 0, &dblReal);
162 addErrorMessage(&sciErr, API_ERROR_CREATE_NAMED_EMPTY_MATRIX, _("%s: Unable to create variable in Scilab memory"), "createNamedEmptyMatrix");
167 if (!checkNamedVarFormat(_pvCtx, _pstName))
169 addErrorMessage(&sciErr, API_ERROR_INVALID_NAME, _("%s: Invalid variable name: %s."), "createNamedBooleanSparseMatrix", _pstName);
173 types::SparseBool* pSparse = new types::SparseBool(_iRows, _iCols);
176 addErrorMessage(&sciErr, API_ERROR_CREATE_NAMED_BOOLEAN_SPARSE, _("%s: Unable to create %s named \"%s\""), "createNamedBooleanSparseMatrix", _("boolean sparse matrix"), _pstName);
180 sciErr = fillBooleanSparseMatrix(_pvCtx, (int*)pSparse, _iRows, _iCols, _iNbItem, _piNbItemRow, _piColPos);
182 wchar_t* pwstName = to_wide_string(_pstName);
183 symbol::Context* ctx = symbol::Context::getInstance();
184 symbol::Symbol sym = symbol::Symbol(pwstName);
186 if (ctx->isprotected(sym) == false)
188 ctx->put(sym, pSparse);
193 addErrorMessage(&sciErr, API_ERROR_REDEFINE_PERMANENT_VAR, _("Redefining permanent variable.\n"));
198 SciErr readNamedBooleanSparseMatrix(void* _pvCtx, const char* _pstName, int* _piRows, int* _piCols, int* _piNbItem, int* _piNbItemRow, int* _piColPos)
200 SciErr sciErr = sciErrInit();
207 Attr() : piAddr(NULL), piNbItemRow(NULL), piColPos(NULL) {}
225 sciErr = getVarAddressFromName(_pvCtx, _pstName, &attr.piAddr);
228 addErrorMessage(&sciErr, API_ERROR_READ_NAMED_BOOLEAN_SPARSE, _("%s: Unable to get variable \"%s\""), "readNamedBooleanSparseMatrix", _pstName);
232 sciErr = getBooleanSparseMatrix(_pvCtx, attr.piAddr, _piRows, _piCols, _piNbItem, &attr.piNbItemRow, &attr.piColPos);
235 addErrorMessage(&sciErr, API_ERROR_READ_NAMED_BOOLEAN_SPARSE, _("API_ERROR_READ_NAMED_BOOLEAN_SPARSE"));
239 if (_piNbItemRow == NULL)
244 memcpy(_piNbItemRow, attr.piNbItemRow, *_piRows * sizeof(int));
246 if (_piColPos == NULL)
251 memcpy(_piColPos, attr.piColPos, *_piNbItem * sizeof(int));
254 /*--------------------------------------------------------------------------*/
255 int isBooleanSparseType(void* _pvCtx, int* _piAddress)
257 return checkVarType(_pvCtx, _piAddress, sci_boolean_sparse);
259 /*--------------------------------------------------------------------------*/
260 int isNamedBooleanSparseType(void* _pvCtx, const char* _pstName)
262 return checkNamedVarType(_pvCtx, _pstName, sci_boolean_sparse);
264 /*--------------------------------------------------------------------------*/
265 int getAllocatedBooleanSparseMatrix(void* _pvCtx, int* _piAddress, int* _piRows, int* _piCols, int* _piNbItem, int** _piNbItemRow, int** _piColPos)
267 SciErr sciErr = sciErrInit();
268 int* piNbItemRow = NULL;
269 int* piColPos = NULL;
271 sciErr = getBooleanSparseMatrix(_pvCtx, _piAddress, _piRows, _piCols, _piNbItem, &piNbItemRow, &piColPos);
274 addErrorMessage(&sciErr, API_ERROR_GET_ALLOC_BOOLEAN_SPARSE, _("%s: Unable to get argument #%d"), "getAllocatedBooleanSparseMatrix", getRhsFromAddress(_pvCtx, _piAddress));
275 printError(&sciErr, 0);
281 *_piNbItemRow = (int*)MALLOC(sizeof(int) **_piRows);
282 memcpy(*_piNbItemRow, piNbItemRow, sizeof(int) **_piRows);
284 *_piColPos = (int*)MALLOC(sizeof(int) **_piNbItem);
285 memcpy(*_piColPos, piColPos, sizeof(int) **_piNbItem);
291 /*--------------------------------------------------------------------------*/
292 int getNamedAllocatedBooleanSparseMatrix(void* _pvCtx, const char* _pstName, int* _piRows, int* _piCols, int* _piNbItem, int** _piNbItemRow, int** _piColPos)
294 SciErr sciErr = sciErrInit();
295 sciErr = readNamedBooleanSparseMatrix(_pvCtx, _pstName, _piRows, _piCols, _piNbItem, NULL, NULL);
298 addErrorMessage(&sciErr, API_ERROR_GET_NAMED_ALLOC_BOOLEAN_SPARSE, _("%s: Unable to get argument \"%s\""), "getNamedAllocatedBooleanSparseMatrix", _pstName);
299 printError(&sciErr, 0);
303 *_piNbItemRow = (int*)MALLOC(sizeof(int) **_piRows);
304 *_piColPos = (int*)MALLOC(sizeof(int) **_piNbItem);
306 sciErr = readNamedBooleanSparseMatrix(_pvCtx, _pstName, _piRows, _piCols, _piNbItem, *_piNbItemRow, *_piColPos);
309 addErrorMessage(&sciErr, API_ERROR_GET_NAMED_ALLOC_BOOLEAN_SPARSE, _("%s: Unable to get argument \"%s\""), "getNamedAllocatedBooleanSparseMatrix", _pstName);
310 printError(&sciErr, 0);
316 /*--------------------------------------------------------------------------*/
317 void freeAllocatedBooleanSparse(int* _piNbItemRow, int* _piColPos)