summaryrefslogtreecommitdiffstats
path: root/scilab
diff options
context:
space:
mode:
authorCedric Delamarre <cdl@esi-group.com>2021-01-11 13:54:40 +0100
committerClément David <clement.david@esi-group.com>2021-02-18 15:36:35 +0100
commit5b1796f81da0774a6f931df586c936d6a8914d8b (patch)
tree35403afa631efd902f5152437670e626c110bc97 /scilab
parentf2d68bb1444b2b3bfdb46d171ac3a96d7ba6ad3a (diff)
downloadscilab-5b1796f81da0774a6f931df586c936d6a8914d8b.zip
scilab-5b1796f81da0774a6f931df586c936d6a8914d8b.tar.gz
[sparse] insertion with negativ or null index fixed
* a=spzeros(3,3);a(0) = 1 Change-Id: I0f075a7a22c6e83a67e0b514ea9f17e597134715
Diffstat (limited to 'scilab')
-rw-r--r--scilab/modules/ast/src/cpp/types/sparse.cpp122
-rw-r--r--scilab/modules/ast/tests/unit_tests/insert.tst5
-rw-r--r--scilab/modules/sparse/sci_gateway/cpp/sci_sparse.cpp10
-rw-r--r--scilab/modules/sparse/sci_gateway/cpp/sci_spzeros.cpp16
4 files changed, 57 insertions, 96 deletions
diff --git a/scilab/modules/ast/src/cpp/types/sparse.cpp b/scilab/modules/ast/src/cpp/types/sparse.cpp
index b57bab9..7b52ea7 100644
--- a/scilab/modules/ast/src/cpp/types/sparse.cpp
+++ b/scilab/modules/ast/src/cpp/types/sparse.cpp
@@ -872,72 +872,21 @@ Sparse* Sparse::resize(int _iNewRows, int _iNewCols)
872 return this; 872 return this;
873 } 873 }
874 874
875 if (((double)_iNewRows) * ((double)_iNewCols) > INT_MAX)
876 {
877 return NULL;
878 }
879
875 Sparse* res = NULL; 880 Sparse* res = NULL;
876 try 881 try
877 { 882 {
878 if (matrixReal) 883 if (matrixReal)
879 { 884 {
880 //item count 885 matrixReal->conservativeResize(_iNewRows, _iNewCols);
881 size_t iNonZeros = nonZeros();
882 RealSparse_t *newReal = new RealSparse_t(_iNewRows, _iNewCols);
883 newReal->reserve((int)iNonZeros);
884
885
886 //coords
887 int* pRows = new int[iNonZeros * 2];
888 outputRowCol(pRows);
889 int* pCols = pRows + iNonZeros;
890
891 //values
892 double* pNonZeroR = new double[iNonZeros];
893 double* pNonZeroI = new double[iNonZeros];
894 outputValues(pNonZeroR, pNonZeroI);
895
896 std::vector<RealTriplet_t> tripletList;
897 for (size_t i = 0; i < iNonZeros; i++)
898 {
899 tripletList.emplace_back((int)pRows[i] - 1, (int)pCols[i] - 1, pNonZeroR[i]);
900 }
901
902 newReal->setFromTriplets(tripletList.begin(), tripletList.end(), DupFunctor<double>());
903
904 delete matrixReal;
905 matrixReal = newReal;
906 delete[] pRows;
907 delete[] pNonZeroR;
908 delete[] pNonZeroI;
909 } 886 }
910 else 887 else
911 { 888 {
912 //item count 889 matrixCplx->conservativeResize(_iNewRows, _iNewCols);
913 size_t iNonZeros = nonZeros();
914 CplxSparse_t *newCplx = new CplxSparse_t(_iNewRows, _iNewCols);
915 newCplx->reserve((int)iNonZeros);
916
917 //coords
918 int* pRows = new int[iNonZeros * 2];
919 outputRowCol(pRows);
920 int* pCols = pRows + iNonZeros;
921
922 //values
923 double* pNonZeroR = new double[iNonZeros];
924 double* pNonZeroI = new double[iNonZeros];
925 outputValues(pNonZeroR, pNonZeroI);
926
927 std::vector<CplxTriplet_t> tripletList;
928 for (size_t i = 0; i < iNonZeros; i++)
929 {
930 tripletList.emplace_back((int)pRows[i] - 1, (int)pCols[i] - 1, std::complex<double>(pNonZeroR[i], pNonZeroI[i]));
931 }
932
933 newCplx->setFromTriplets(tripletList.begin(), tripletList.end(), DupFunctor<std::complex<double>>());
934
935
936 delete matrixCplx;
937 matrixCplx = newCplx;
938 delete[] pRows;
939 delete[] pNonZeroR;
940 delete[] pNonZeroI;
941 } 890 }
942 891
943 m_iRows = _iNewRows; 892 m_iRows = _iNewRows;
@@ -1158,8 +1107,8 @@ Sparse* Sparse::insert(typed_list* _pArgs, InternalType* _pSource)
1158 1107
1159 typed_list pArg; 1108 typed_list pArg;
1160 1109
1161 int piMaxDim[2]; 1110 int* piMaxDim = new int[2];
1162 int piCountDim[2]; 1111 int* piCountDim = new int[2];
1163 1112
1164 //on case of resize 1113 //on case of resize
1165 int iNewRows = 0; 1114 int iNewRows = 0;
@@ -1170,6 +1119,8 @@ Sparse* Sparse::insert(typed_list* _pArgs, InternalType* _pSource)
1170 int iSeqCount = checkIndexesArguments(this, _pArgs, &pArg, piMaxDim, piCountDim); 1119 int iSeqCount = checkIndexesArguments(this, _pArgs, &pArg, piMaxDim, piCountDim);
1171 if (iSeqCount == 0) 1120 if (iSeqCount == 0)
1172 { 1121 {
1122 delete[] piMaxDim;
1123 delete[] piCountDim;
1173 //free pArg content 1124 //free pArg content
1174 cleanIndexesArguments(_pArgs, &pArg); 1125 cleanIndexesArguments(_pArgs, &pArg);
1175 return this; 1126 return this;
@@ -1202,6 +1153,8 @@ Sparse* Sparse::insert(typed_list* _pArgs, InternalType* _pSource)
1202 } 1153 }
1203 else if ((size_t)getRows() * (size_t)getCols() < (size_t)piMaxDim[0]) 1154 else if ((size_t)getRows() * (size_t)getCols() < (size_t)piMaxDim[0])
1204 { 1155 {
1156 delete[] piMaxDim;
1157 delete[] piCountDim;
1205 //free pArg content 1158 //free pArg content
1206 cleanIndexesArguments(_pArgs, &pArg); 1159 cleanIndexesArguments(_pArgs, &pArg);
1207 //out of range 1160 //out of range
@@ -1218,6 +1171,9 @@ Sparse* Sparse::insert(typed_list* _pArgs, InternalType* _pSource)
1218 } 1171 }
1219 } 1172 }
1220 1173
1174 delete[] piMaxDim;
1175 delete[] piCountDim;
1176
1221 //check number of insertion 1177 //check number of insertion
1222 if (pSource->isScalar() == false && pSource->getSize() != iSeqCount) 1178 if (pSource->isScalar() == false && pSource->getSize() != iSeqCount)
1223 { 1179 {
@@ -1553,7 +1509,7 @@ Sparse* Sparse::insert(typed_list* _pArgs, InternalType* _pSource)
1553 { 1509 {
1554 int iRow = static_cast<int>(i % iRowSize); 1510 int iRow = static_cast<int>(i % iRowSize);
1555 int iCol = static_cast<int>(i / iRowSize); 1511 int iCol = static_cast<int>(i / iRowSize);
1556 tripletList.emplace_back(static_cast<int>(pI[iRow]) - 1, static_cast<int>(pJ[iCol]) - 1, *src); 1512 tripletList.emplace_back(static_cast<int>(pI[iRow] - 1) , static_cast<int>(pJ[iCol] - 1) , *src);
1557 src += inc; 1513 src += inc;
1558 } 1514 }
1559 1515
@@ -3176,8 +3132,7 @@ Sparse* Sparse::reshape(int _iNewRows, int _iNewCols)
3176 3132
3177 //values 3133 //values
3178 double* pNonZeroR = new double[iNonZeros]; 3134 double* pNonZeroR = new double[iNonZeros];
3179 double* pNonZeroI = new double[iNonZeros]; 3135 outputValues(pNonZeroR, NULL);
3180 outputValues(pNonZeroR, pNonZeroI);
3181 3136
3182 std::vector<RealTriplet_t> tripletList; 3137 std::vector<RealTriplet_t> tripletList;
3183 for (size_t i = 0; i < iNonZeros; i++) 3138 for (size_t i = 0; i < iNonZeros; i++)
@@ -3192,7 +3147,6 @@ Sparse* Sparse::reshape(int _iNewRows, int _iNewCols)
3192 matrixReal = newReal; 3147 matrixReal = newReal;
3193 delete[] pRows; 3148 delete[] pRows;
3194 delete[] pNonZeroR; 3149 delete[] pNonZeroR;
3195 delete[] pNonZeroI;
3196 } 3150 }
3197 else 3151 else
3198 { 3152 {
@@ -3436,32 +3390,15 @@ SparseBool* SparseBool::resize(int _iNewRows, int _iNewCols)
3436 return this; 3390 return this;
3437 } 3391 }
3438 3392
3393 if (((double)_iNewRows) * ((double)_iNewCols) > INT_MAX)
3394 {
3395 return NULL;
3396 }
3397
3439 SparseBool* res = NULL; 3398 SparseBool* res = NULL;
3440 try 3399 try
3441 { 3400 {
3442 //item count 3401 matrixBool->conservativeResize(_iNewRows, _iNewCols);
3443 size_t iNonZeros = nbTrue();
3444
3445 BoolSparse_t *newBool = new BoolSparse_t(_iNewRows, _iNewCols);
3446 newBool->reserve((int)iNonZeros);
3447
3448 //coords
3449 int* pRows = new int[iNonZeros * 2];
3450 outputRowCol(pRows);
3451 int* pCols = pRows + iNonZeros;
3452
3453 std::vector<BoolTriplet_t> tripletList;
3454
3455 for (size_t i = 0; i < iNonZeros; i++)
3456 {
3457 tripletList.emplace_back((int)pRows[i] - 1, (int)pCols[i] - 1, true);
3458 }
3459
3460 newBool->setFromTriplets(tripletList.begin(), tripletList.end(), DupFunctor<bool>());
3461
3462 delete matrixBool;
3463 matrixBool = newBool;
3464 delete[] pRows;
3465 3402
3466 m_iRows = _iNewRows; 3403 m_iRows = _iNewRows;
3467 m_iCols = _iNewCols; 3404 m_iCols = _iNewCols;
@@ -3490,8 +3427,8 @@ SparseBool* SparseBool::insert(typed_list* _pArgs, SparseBool* _pSource)
3490 3427
3491 typed_list pArg; 3428 typed_list pArg;
3492 3429
3493 int piMaxDim[2]; 3430 int* piMaxDim = new int[2];
3494 int piCountDim[2]; 3431 int* piCountDim = new int[2];
3495 3432
3496 //on case of resize 3433 //on case of resize
3497 int iNewRows = 0; 3434 int iNewRows = 0;
@@ -3501,6 +3438,8 @@ SparseBool* SparseBool::insert(typed_list* _pArgs, SparseBool* _pSource)
3501 int iSeqCount = checkIndexesArguments(this, _pArgs, &pArg, piMaxDim, piCountDim); 3438 int iSeqCount = checkIndexesArguments(this, _pArgs, &pArg, piMaxDim, piCountDim);
3502 if (iSeqCount == 0) 3439 if (iSeqCount == 0)
3503 { 3440 {
3441 delete[] piMaxDim;
3442 delete[] piCountDim;
3504 //free pArg content 3443 //free pArg content
3505 cleanIndexesArguments(_pArgs, &pArg); 3444 cleanIndexesArguments(_pArgs, &pArg);
3506 return this; 3445 return this;
@@ -3533,6 +3472,8 @@ SparseBool* SparseBool::insert(typed_list* _pArgs, SparseBool* _pSource)
3533 } 3472 }
3534 else if (getRows() * getCols() < piMaxDim[0]) 3473 else if (getRows() * getCols() < piMaxDim[0])
3535 { 3474 {
3475 delete[] piMaxDim;
3476 delete[] piCountDim;
3536 //free pArg content 3477 //free pArg content
3537 cleanIndexesArguments(_pArgs, &pArg); 3478 cleanIndexesArguments(_pArgs, &pArg);
3538 //out of range 3479 //out of range
@@ -3549,6 +3490,9 @@ SparseBool* SparseBool::insert(typed_list* _pArgs, SparseBool* _pSource)
3549 } 3490 }
3550 } 3491 }
3551 3492
3493 delete[] piMaxDim;
3494 delete[] piCountDim;
3495
3552 //check number of insertion 3496 //check number of insertion
3553 if (_pSource->isScalar() == false && _pSource->getSize() != iSeqCount) 3497 if (_pSource->isScalar() == false && _pSource->getSize() != iSeqCount)
3554 { 3498 {
diff --git a/scilab/modules/ast/tests/unit_tests/insert.tst b/scilab/modules/ast/tests/unit_tests/insert.tst
index 8ed3275..316f834 100644
--- a/scilab/modules/ast/tests/unit_tests/insert.tst
+++ b/scilab/modules/ast/tests/unit_tests/insert.tst
@@ -548,3 +548,8 @@ twin=origin(1)(1);
548twin.test = 5; 548twin.test = 5;
549assert_checkequal(origin(1)(1).test, 1); 549assert_checkequal(origin(1)(1).test, 1);
550assert_checkequal(twin.test, 5); 550assert_checkequal(twin.test, 5);
551
552// insert in sparse with invalid index
553a=spzeros(3,3);
554errMsg = msprintf(_("Invalid index.\n"));
555assert_checkerror("a(0) = 1", errMsg);
diff --git a/scilab/modules/sparse/sci_gateway/cpp/sci_sparse.cpp b/scilab/modules/sparse/sci_gateway/cpp/sci_sparse.cpp
index 1dfead1..69e1028 100644
--- a/scilab/modules/sparse/sci_gateway/cpp/sci_sparse.cpp
+++ b/scilab/modules/sparse/sci_gateway/cpp/sci_sparse.cpp
@@ -152,6 +152,12 @@ types::Function::ReturnValue sci_sparse(types::typed_list &in, int _piRetCount,
152 Scierror(999, _("%s: Wrong values for input argument #%d: Positive integers expected.\n"), "sparse", 3); 152 Scierror(999, _("%s: Wrong values for input argument #%d: Positive integers expected.\n"), "sparse", 3);
153 return types::Function::Error; 153 return types::Function::Error;
154 } 154 }
155
156 if (pDdims->get(0) * pDdims->get(1) > INT_MAX)
157 {
158 Scierror(999, _("%s: Wrong value for input argument #%d: The maximum total size expected is %d.\n"), "sparse", 3, INT_MAX);
159 return types::Function::Error;
160 }
155 } 161 }
156 162
157 bool alloc = false; 163 bool alloc = false;
@@ -168,7 +174,7 @@ types::Function::ReturnValue sci_sparse(types::typed_list &in, int _piRetCount,
168 Scierror(999, _("%s: Invalid index.\n"), "sparse"); 174 Scierror(999, _("%s: Invalid index.\n"), "sparse");
169 return types::Function::Error; 175 return types::Function::Error;
170 } 176 }
171 177
172 if (pDdims == nullptr) 178 if (pDdims == nullptr)
173 { 179 {
174 pDdims = new types::Double(1, 2, false); 180 pDdims = new types::Double(1, 2, false);
@@ -180,7 +186,7 @@ types::Function::ReturnValue sci_sparse(types::typed_list &in, int _piRetCount,
180 } 186 }
181 alloc = true; 187 alloc = true;
182 } 188 }
183 else if ( (size > 0) && ((pDdims->get(0) < *std::max_element(pdbli, pdbli + size)) 189 else if ( (size > 0) && ((pDdims->get(0) < *std::max_element(pdbli, pdbli + size))
184 || (pDdims->get(1) < *std::max_element(pdblj, pdblj + size))) ) 190 || (pDdims->get(1) < *std::max_element(pdblj, pdblj + size))) )
185 { 191 {
186 Scierror(999, _("%s: Invalid index.\n"),"sparse"); 192 Scierror(999, _("%s: Invalid index.\n"),"sparse");
diff --git a/scilab/modules/sparse/sci_gateway/cpp/sci_spzeros.cpp b/scilab/modules/sparse/sci_gateway/cpp/sci_spzeros.cpp
index ae18e9a..606347e 100644
--- a/scilab/modules/sparse/sci_gateway/cpp/sci_spzeros.cpp
+++ b/scilab/modules/sparse/sci_gateway/cpp/sci_spzeros.cpp
@@ -71,27 +71,33 @@ types::Function::ReturnValue sci_spzeros(types::typed_list &in, int _iRetCount,
71 return types::Function::Error; 71 return types::Function::Error;
72 } 72 }
73 73
74 if (pDblRows->get(0) != (double) ((unsigned int) pDblRows->get(0))) 74 double dblRows = pDblRows->get(0);
75 double dblCols = pDblCols->get(0);
76 if (dblRows != (double) ((unsigned int) dblRows))
75 { 77 {
76
77 Scierror(999, _("%s: Wrong value for input argument #%d: Scalar positive integer expected.\n"), "spzeros", 1); 78 Scierror(999, _("%s: Wrong value for input argument #%d: Scalar positive integer expected.\n"), "spzeros", 1);
78 return types::Function::Error; 79 return types::Function::Error;
79 } 80 }
80 81
81 if (pDblCols->get(0) != (double) ((unsigned int) pDblCols->get(0))) 82 if (dblCols != (double) ((unsigned int) dblCols))
82 { 83 {
83
84 Scierror(999, _("%s: Wrong value for input argument #%d: Scalar positive integer expected.\n"), "spzeros", 2); 84 Scierror(999, _("%s: Wrong value for input argument #%d: Scalar positive integer expected.\n"), "spzeros", 2);
85 return types::Function::Error; 85 return types::Function::Error;
86 } 86 }
87 87
88 if (dblRows * dblCols > INT_MAX)
89 {
90 Scierror(999, _("%s: Wrong value for input arguments: The maximum total size expected is %d.\n"), "spzeros", INT_MAX);
91 return types::Function::Error;
92 }
93
88 if (pDblRows->get(0) == 0. || pDblCols->get(0) == 0.) 94 if (pDblRows->get(0) == 0. || pDblCols->get(0) == 0.)
89 { 95 {
90 pSpOut = new types::Sparse(0, 0, false); 96 pSpOut = new types::Sparse(0, 0, false);
91 } 97 }
92 else 98 else
93 { 99 {
94 pSpOut = new types::Sparse((int)pDblRows->get(0), (int)pDblCols->get(0), false); 100 pSpOut = new types::Sparse((int)pDblRows->get(0), (int)pDblCols->get(0), false);
95 } 101 }
96 102
97 } 103 }