2 * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3 * Copyright (C) 2010 - DIGITEO - Antoine ELIAS
5 * This file must be used under the terms of the CeCILL.
6 * This source file is licensed as described in the file COPYING, which
7 * you should have received as part of this distribution. The terms
8 * are also available at
9 * http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt
13 #include "arrayof.hxx"
16 #include "singlepoly.hxx"
17 #include "singlestruct.hxx"
21 #include "dynlib_ast.h"
26 /* template <typename T>
27 ArrayOf<T>* createEmptyDouble()
29 return Double::Empty();
33 int computeTuples(int* _piCountDim, int _iDims, int _iCurrentDim, int* _piIndex)
35 //if bRet == 1, previous dims has reach max value.
38 if (_iCurrentDim == 0)
41 if (_piIndex[_iCurrentDim] >= _piCountDim[_iCurrentDim])
43 _piIndex[_iCurrentDim] = 0;
49 iRet = computeTuples(_piCountDim, _iDims, _iCurrentDim - 1, _piIndex);
52 _piIndex[_iCurrentDim]++;
53 if (_piIndex[_iCurrentDim] >= _piCountDim[_iCurrentDim])
55 _piIndex[_iCurrentDim] = 0;
63 InternalType* createEmptyDouble()
65 return Double::Empty();
68 int getIntValueFromDouble(InternalType* _pIT, int _iPos)
70 return static_cast<int>(_pIT->getAs<Double>()->get(_iPos));
73 double* getDoubleArrayFromDouble(InternalType* _pIT)
75 return _pIT->getAs<Double>()->get();
78 InternalType* createDoubleVector(int _iSize)
80 int piDims[] = {1, _iSize};
81 Double* pOut = new Double(2, piDims);
82 for (int i = 0; i < _iSize; i++)
89 bool checkArgValidity(typed_list& _Arg)
91 for (int i = 0; i < (int)_Arg.size(); i++)
93 if (_Arg[i]->isDouble() == false)
98 Double* pDbl = _Arg[i]->getAs<Double>();
99 double* pdbl = pDbl->get();
100 for (int j = 0; j < pDbl->getSize(); j++)
112 template <typename T>
113 InternalType* ArrayOf<T>::insert(typed_list* _pArgs, InternalType* _pSource)
117 // An ArrayOf content in more than one Scilab variable
118 // must be cloned before to be modified.
119 ArrayOf* pClone = clone()->template getAs<ArrayOf>();
120 InternalType* pIT = pClone->insert(_pArgs, _pSource);
130 if (getScalarIndex(this, _pArgs, &index))
132 ArrayOf* pIns = _pSource->getAs<ArrayOf>();
133 int sizeIn = pIns->getSize();
134 //only scalar can be used to ".=" operation
140 T* pRealData = pIns->get();
141 T* pImgData = pIns->getImg();
143 if (isComplex() == false && pIns->isComplex() == false)
145 if (set(index, *pRealData) == true)
151 //if complex continue
154 std::vector<int> indexes;
155 if (getImplicitIndex(this, _pArgs, indexes))
157 if (indexes.size() == 0)
162 ArrayOf* pIns = _pSource->getAs<ArrayOf>();
163 int sizeIn = pIns->getSize();
164 int count = static_cast<int>(indexes.size());
165 //only scalar can be used to ".=" operation
166 if (sizeIn != 1 && count != sizeIn)
171 T* pRealData = pIns->get();
172 T* pImgData = pIns->getImg();
175 if (isComplex() == false && pIns->isComplex() == false)
179 for (int i : indexes)
181 if (set(i, *pRealData) == false)
190 for (int i : indexes)
192 if (set(i, *pRealData) == false)
206 //if status is false, continue to entire process
210 bool bNeedToResize = false;
211 int iDims = (int)_pArgs->size();
212 int iDimsOrigine = m_iDims;
215 int* piMaxDim = new int[iDims];
216 int* piCountDim = new int[iDims];
219 int* piNewDims = NULL;
221 ArrayOf* pSource = _pSource->getAs<ArrayOf>();
223 bool bIsColon = false;
225 //evaluate each argument and replace by appropriate value and compute the count of combinations
226 int iSeqCount = checkIndexesArguments(this, _pArgs, &pArg, piMaxDim, piCountDim);
232 cleanIndexesArguments(_pArgs, &pArg);
236 //only scalar can be used to ".=" operation
237 if (iSeqCount != pSource->getSize() && pSource->isScalar() == false)
242 //remove last dimension at size 1
243 //remove last dimension if are == 1
244 for (int i = (iDims - 1); i >= 2; i--)
246 if (piMaxDim[i] == 1)
249 pArg.back()->killMe();
259 if (iDims >= m_iDims)
261 //all case are good, we can do almost everything
262 //check if resize is needed
265 bNeedToResize = true;
267 else //_iDims == m_iDims
269 for (int i = 0; i < m_iDims; i++)
271 if (m_piDims[i] < piMaxDim[i])
273 bNeedToResize = true;
279 //determine new dimension of the matrix
280 if (bNeedToResize == true)
283 piNewDims = new int[iNewDims];
284 for (int i = 0; i < m_iDims; i++)
286 piNewDims[i] = std::max(piMaxDim[i], m_piDims[i]);
289 int iSource = (pSource->getDims() - 1);
290 bool bPassed = false;
291 int *piSourceDims = pSource->getDimsArray();
293 for (int i = m_iDims; i < iNewDims; ++i)
295 piNewDims[i] = piMaxDim[i];
299 else // _iDims < m_iDims
301 if (isVector() || isScalar() || getSize() == 0) //getSize() == 0, only for [] and {}
303 if (getSize() < piMaxDim[0])
305 bNeedToResize = true;
307 piNewDims = new int[2];
309 if (getCols() == 1 || getSize() == 0)
312 piNewDims[0] = piMaxDim[0];
315 else if (getRows() == 1)
319 piNewDims[1] = piMaxDim[0];
325 //each index before last index must be in range of his dimension
326 //and last given dimension can not be > prod(last dimensions)
327 for (int i = 0; i < iDims - 1; i++)
329 //indexes are always doubles
330 double* pIdx = getDoubleArrayFromDouble(pArg[i]);
331 //InternalType* pVar = pArg[i];
332 //double* pIdx = static_cast<double*>(pVar->getAs<Double>()->get());
333 int iSize = pArg[i]->getAs<ArrayOf>()->getSize();
334 for (int j = 0; j < iSize; j++)
336 if (pIdx[j] > m_piDims[i])
341 cleanIndexesArguments(_pArgs, &pArg);
348 int iMaxLastDim = getVarMaxDim(iDims - 1, iDims);
349 double* pIdx = getDoubleArrayFromDouble(pArg[pArg.size() - 1]);
350 //InternalType* pVar = pArg[pArg.size() - 1];
351 //double* pIdx = static_cast<double*>(pVar->getAs<Double>()->get());
352 int iSize = pArg[pArg.size() - 1]->getAs<GenericType>()->getSize();
353 for (int i = 0; i < iSize; i++)
355 if (pIdx[i] > iMaxLastDim)
360 cleanIndexesArguments(_pArgs, &pArg);
367 //before resize, check input dimension
371 bool bPass = resize(piNewDims, iNewDims);
377 cleanIndexesArguments(_pArgs, &pArg);
383 piNewDims = m_piDims;
388 if (pSource->isComplex() && m_pImgData == NULL)
393 int argSize = static_cast<int>(pArg.size());
394 int* piIndex = new int[argSize];
395 int* piCoord = new int[argSize];
396 int* piViewDims = new int[iDims];
397 memset(piIndex, 0x00, sizeof(int) * argSize);
399 //convert current dimension to view dimension
400 for (int i = 0; i < iDims; i++)
402 piViewDims[i] = getVarMaxDim(i, iDims);
405 T* pRealData = pSource->get();
406 T* pImgData = pSource->getImg();
407 bool bComplex = pSource->isComplex();
409 for (int i = 0; i < iSeqCount; i++)
411 computeTuples(piCountDim, argSize, argSize - 1, piIndex);
414 for (int j = 0; j < argSize; j++)
416 piCoord[j] = getIntValueFromDouble(pArg[j], piIndex[j]) - 1;
417 //InternalType* pVar = pArg[j];
418 //piCoord[j] = static_cast<int>(pVar->getAs<Double>()->get(piIndex[j]) - 1);
419 //std::cout << piCoord[j] << " ";
422 //std::cout << "]" << std::endl;
424 int iPos = getIndexWithDims(piCoord, piViewDims, iDims);
439 cleanIndexesArguments(_pArgs, &pArg);
441 wchar_t szError[bsiz];
442 os_swprintf(szError, bsiz, _W("Invalid index.\n").c_str());
443 throw ast::ScilabError(szError);
446 if (pSource->isScalar())
448 //element-wise insertion
449 set(iPos, pRealData[0]);
450 if (pImgData != NULL && bComplex)
452 setImg(iPos, pImgData[0]);
460 for (int j = 0; j < iDimsOrigine; j++)
462 iPas = iPas * m_piDims[j];
465 for (int iPost = iPos; iPost < this->getSize(); iPost += iPas)
467 set(iPost, pRealData[i]);
468 if (pImgData != NULL && bComplex)
470 setImg(iPost, pImgData[i]);
477 set(iPos, pRealData[i]);
478 if (pImgData != NULL && bComplex)
480 setImg(iPos, pImgData[i]);
485 // reset imaginary part
486 if (m_pImgData != NULL && bComplex == false)
507 cleanIndexesArguments(_pArgs, &pArg);
512 template <typename T>
513 InternalType* ArrayOf<T>::insertNew(typed_list* _pArgs, InternalType* _pSource)
516 InternalType *pOut = NULL;
517 ArrayOf* pSource = _pSource->getAs<ArrayOf>();
519 int iDims = (int)_pArgs->size();
520 int* piMaxDim = new int[iDims];
521 int* piCountDim = new int[iDims];
522 bool bComplex = pSource->getImg() != NULL;
523 bool bUndefine = false;
524 bool bIsImpli = false;
526 //evaluate each argument and replace by appropriate value and compute the count of combinations
527 int iSeqCount = checkIndexesArguments(NULL, _pArgs, &pArg, piMaxDim, piCountDim);
534 cleanIndexesArguments(_pArgs, &pArg);
535 return createEmptyDouble();
540 iSeqCount = -iSeqCount;
546 //manage : and $ in creation by insertion
547 int *piSourceDims = pSource->getDimsArray();
548 int iSourceDims = pSource->getDims();
549 int iCompteurNull = 0;
551 for (int i = 0; i < iDims; i++)
560 if ((*_pArgs)[i]->isImplicitList())
567 //if all index are : -> a = x
568 if (iCompteurNull == pArg.size())
573 cleanIndexesArguments(_pArgs, &pArg);
578 if (pSource->isVector() && iCompteurNull == 1)
580 piMaxDim[iLastNull] = pSource->getSize();
581 pArg[iLastNull] = createDoubleVector(piMaxDim[iLastNull]);
585 //matrix and hypermatrix case
586 if (iCompteurNull < pSource->getDims())
591 cleanIndexesArguments(_pArgs, &pArg);
592 //contain bad index, like <= 0, ...
596 //replace ":" by know source dimensions
598 for (int i = 0; i < iDims; ++i)
602 if (iSource < iSourceDims)
604 piMaxDim[i] = piSourceDims[iSource];
605 pArg[i] = createDoubleVector(piMaxDim[i]);
610 //fill dimensions after pSource->getDimes() with 1
612 pArg[i] = createDoubleVector(piMaxDim[i]);
619 //remove last dimension at size 1
620 //remove last dimension if are == 1
621 for (int i = (iDims - 1); i >= 2; i--)
623 if (piMaxDim[i] == 1)
626 pArg.back()->killMe();
635 if (checkArgValidity(pArg) == false)
640 cleanIndexesArguments(_pArgs, &pArg);
641 //contain bad index, like <= 0, ...
647 if (pSource->getCols() == 1)
649 int piRealDim[2] = {piMaxDim[0], 1};
650 pOut = pSource->createEmpty(2, piRealDim, bComplex);
655 int piRealDim[2] = {1, piMaxDim[0]};
656 pOut = pSource->createEmpty(2, piRealDim, bComplex);
661 pOut = pSource->createEmpty(iDims, piMaxDim, bComplex);
664 //fill with null item
665 ArrayOf* pArrayOut = pOut->getAs<ArrayOf>();
666 T* pRealData = pArrayOut->get();
669 T* pImgData = pArrayOut->getImg();
670 for (int i = 0; i < pArrayOut->getSize(); i++)
672 pArrayOut->deleteData(pRealData[i]);
673 pRealData[i] = pSource->getNullValue();
674 pArrayOut->deleteData(pImgData[i]);
675 pImgData[i] = pSource->getNullValue();
680 for (int i = 0; i < pArrayOut->getSize(); i++)
682 pArrayOut->deleteData(pRealData[i]);
683 pRealData[i] = pSource->getNullValue();
687 if (bIsImpli && (pArrayOut->getSize() != _pSource->getAs<types::GenericType>()->getSize()))
692 cleanIndexesArguments(_pArgs, &pArg);
695 //insert values in new matrix
696 InternalType* pOut2 = pArrayOut->insert(&pArg, _pSource);
705 cleanIndexesArguments(_pArgs, &pArg);
710 template <typename T>
711 bool ArrayOf<T>::append(int _iRows, int _iCols, InternalType* _poSource)
713 ArrayOf * pGT = _poSource->getAs<ArrayOf>();
714 int iRows = pGT->getRows();
715 int iCols = pGT->getCols();
717 //insert without resize
718 if (iRows + _iRows > m_iRows || iCols + _iCols > m_iCols)
723 //Update complexity if necessary
724 if (pGT->isComplex())
728 else if (isComplex())
730 pGT->setComplex(true);
733 if (pGT->isComplex())
735 for (int i = 0; i < iRows; i++)
737 for (int j = 0; j < iCols; j++)
739 set(_iRows + i, _iCols + j, pGT->get(i, j));
740 setImg(_iRows + i, _iCols + j, pGT->getImg(i, j));
746 for (int i = 0; i < iRows; i++)
748 for (int j = 0; j < iCols; j++)
750 set(_iRows + i, _iCols + j, pGT->get(i, j));
758 template <typename T>
759 InternalType* ArrayOf<T>::remove(typed_list* _pArgs)
761 ArrayOf<T>* pOut = NULL;
762 int iDims = (int)_pArgs->size();
765 int* piMaxDim = new int[iDims];
766 int* piCountDim = new int[iDims];
768 //evaluate each argument and replace by appropriate value and compute the count of combinations
769 int iSeqCount = checkIndexesArguments(this, _pArgs, &pArg, piMaxDim, piCountDim);
775 cleanIndexesArguments(_pArgs, &pArg);
776 //no Seq, no change but no error.
780 bool* pbFull = new bool[iDims];
781 //coord must represent all values on a dimension
782 for (int i = 0; i < iDims; i++)
785 int iDimToCheck = getVarMaxDim(i, iDims);
786 int iIndexSize = pArg[i]->getAs<GenericType>()->getSize();
788 //we can have index more than once
789 if (iIndexSize >= iDimToCheck)
791 //size is good, now check datas
792 double* pIndexes = getDoubleArrayFromDouble(pArg[i]);
793 for (int j = 0; j < iDimToCheck; j++)
796 for (int k = 0; k < iIndexSize; k++)
798 if ((int)pIndexes[k] == j + 1)
809 //only one dims can be not full/entire
810 bool bNotEntire = false;
812 bool bTooMuchNotEntire = false;
813 for (int i = 0; i < iDims; i++)
815 if (pbFull[i] == false)
817 if (bNotEntire == false)
824 bTooMuchNotEntire = true;
830 if (bTooMuchNotEntire == true)
833 cleanIndexesArguments(_pArgs, &pArg);
840 int iNotEntireSize = pArg[iNotEntire]->getAs<GenericType>()->getSize();
841 double* piNotEntireIndex = getDoubleArrayFromDouble(pArg[iNotEntire]);
842 int iKeepSize = getVarMaxDim(iNotEntire, iDims);
843 bool* pbKeep = new bool[iKeepSize];
845 //fill pbKeep with true value
846 for (int i = 0; i < iKeepSize; i++)
851 for (int i = 0; i < iNotEntireSize; i++)
853 int idx = (int)piNotEntireIndex[i] - 1;
855 //don't care of value out of bounds
863 for (int i = 0; i < iKeepSize; i++)
865 if (pbKeep[i] == true)
872 int* piNewDims = new int[iDims];
873 for (int i = 0; i < iDims; i++)
877 piNewDims[i] = iNewDimSize;
881 piNewDims[i] = getVarMaxDim(i, iDims);
885 //remove last dimension if are == 1
886 int iOrigDims = iDims;
887 for (int i = (iDims - 1); i >= 2; i--)
889 if (piNewDims[i] == 1)
899 if (iNewDimSize == 0)
902 cleanIndexesArguments(_pArgs, &pArg);
904 return createEmptyDouble();
909 //two cases, depends of original matrix/vector
910 if ((*_pArgs)[0]->isColon() == false && m_iDims == 2 && m_piDims[0] == 1 && m_piDims[1] != 1)
912 //special case for row vector
913 int piRealDim[2] = {1, iNewDimSize};
914 pOut = createEmpty(2, piRealDim, m_pImgData != NULL);
915 //in this case we have to care of 2nd dimension
920 int piRealDim[2] = {iNewDimSize, 1};
921 pOut = createEmpty(2, piRealDim, m_pImgData != NULL);
926 pOut = createEmpty(iDims, piNewDims, m_pImgData != NULL);
930 //find a way to copy existing data to new variable ...
932 int* piIndexes = new int[iOrigDims];
933 int* piViewDims = new int[iOrigDims];
934 for (int i = 0; i < iOrigDims; i++)
936 piViewDims[i] = getVarMaxDim(i, iOrigDims);
939 for (int i = 0; i < getSize(); i++)
941 bool bByPass = false;
942 getIndexesWithDims(i, piIndexes, piViewDims, iOrigDims);
944 //check if piIndexes use removed indexes
945 for (int j = 0; j < iNotEntireSize; j++)
947 if ((piNotEntireIndex[j] - 1) == piIndexes[iNotEntire])
955 if (bByPass == false)
958 pOut->set(iNewPos, get(i));
959 if (m_pImgData != NULL)
961 pOut->setImg(iNewPos, getImg(i));
968 cleanIndexesArguments(_pArgs, &pArg);
975 template <typename T>
976 InternalType* ArrayOf<T>::extract(typed_list* _pArgs)
978 ArrayOf<T>* pOut = NULL;
979 int iDims = (int)_pArgs->size();
982 int* piMaxDim = new int[iDims];
983 int* piCountDim = new int[iDims];
985 //evaluate each argument and replace by appropriate value and compute the count of combinations
986 int iSeqCount = checkIndexesArguments(this, _pArgs, &pArg, piMaxDim, piCountDim);
992 cleanIndexesArguments(_pArgs, &pArg);
993 return createEmptyDouble();
1001 cleanIndexesArguments(_pArgs, &pArg);
1005 if (iDims < m_iDims)
1007 for (int i = 0; i < iDims; i++)
1009 int iDimToCheck = 0;
1010 if (i == (iDims - 1))
1012 iDimToCheck = getVarMaxDim(i, iDims);
1016 iDimToCheck = m_piDims[i];
1019 if (piMaxDim[i] > iDimToCheck)
1022 delete[] piCountDim;
1024 cleanIndexesArguments(_pArgs, &pArg);
1031 if (iDims > m_iDims)
1033 for (int i = m_iDims; i < iDims; i++)
1035 if (piMaxDim[i] > 1)
1038 delete[] piCountDim;
1040 cleanIndexesArguments(_pArgs, &pArg);
1047 for (int i = 0; i < m_iDims; i++)
1049 if (piMaxDim[i] > m_piDims[i])
1052 delete[] piCountDim;
1054 cleanIndexesArguments(_pArgs, &pArg);
1055 //exrtact must be in dimension limits
1061 //remove last dimension if are == 1
1062 for (int i = (iDims - 1); i >= 2; i--)
1064 if (piCountDim[i] == 1)
1077 if (piCountDim[0] == 0)
1080 delete[] piCountDim;
1082 cleanIndexesArguments(_pArgs, &pArg);
1083 return createEmptyDouble();
1087 //two cases, depends of original matrix/vector
1088 if ((*_pArgs)[0]->isColon() == false && m_iDims == 2 && m_piDims[1] != 1 && m_piDims[0] == 1)
1090 //special case for row vector
1091 int piRealDim[2] = {1, piCountDim[0]};
1092 pOut = createEmpty(2, piRealDim, m_pImgData != NULL);
1098 //for extraction on scalar
1099 pOut = createEmpty(pArg[0]->getAs<GenericType>()->getDims(), pArg[0]->getAs<GenericType>()->getDimsArray(), m_pImgData != NULL);
1103 int piRealDim[2] = {piCountDim[0], 1};
1104 pOut = createEmpty(2, piRealDim, m_pImgData != NULL);
1112 pOut = createEmpty(iDims, piCountDim, m_pImgData != NULL);
1115 int* piIndex = new int[_pArgs->size()];
1116 int* piCoord = new int[_pArgs->size()];
1117 int* piViewDims = new int[iDims];
1118 memset(piIndex, 0x00, sizeof(int) * _pArgs->size());
1120 for (int i = 0; i < iDims; i++)
1122 piViewDims[i] = getVarMaxDim(i, iDims);
1125 for (int i = 0; i < iSeqCount; i++)
1127 //increment last dimension
1128 computeTuples(piCountDim, (int)_pArgs->size(), (int)_pArgs->size() - 1, piIndex);
1131 for (int j = 0; j < (int)_pArgs->size(); j++)
1133 piCoord[j] = getIntValueFromDouble(pArg[j], piIndex[j]) - 1;
1134 //InternalType* pVar = pArg[i];
1135 //piCoord[j] = static_cast<int>(pVar->getAs<Double>()->get(piIndex[j]) - 1);
1136 //std::cout << piCoord[j] << " ";
1138 // try to access somewhere wrong.
1143 delete[] piViewDims;
1145 delete[] piCountDim;
1148 cleanIndexesArguments(_pArgs, &pArg);
1153 //std::cout << "]" << std::endl;
1156 //put vlaue in the new matrix
1157 if ((int)_pArgs->size() < m_iDims)
1159 //compute index based on viewed matrix
1160 iPos = getIndexWithDims(piCoord, piViewDims, iDims);
1164 //compute vector index
1165 iPos = getIndex(piCoord);
1168 //convert flat dimension to 0
1169 for (int j = 0; j < iDims; j++)
1171 if (piCountDim[j] == 1)
1177 pOut->set(i, get(iPos));
1178 if (m_pImgData != NULL)
1180 pOut->setImg(i, getImg(iPos));
1188 cleanIndexesArguments(_pArgs, &pArg);
1192 delete[] piViewDims;
1194 delete[] piCountDim;
1199 template <typename T>
1200 bool ArrayOf<T>::resize(int* _piDims, int _iDims)
1202 if (_iDims == m_iDims)
1204 bool bChange = false;
1205 for (int i = 0; i < _iDims; i++)
1207 if (m_piDims[i] != _piDims[i])
1214 if (bChange == false)
1221 //alloc new data array
1222 T* pRealData = NULL;
1226 if (m_pImgData != NULL)
1228 iNewSize = get_max_size(_piDims, _iDims);
1229 if (m_iSizeMax < iNewSize)
1231 //alloc 10% bigger than asked to prevent future resize
1232 int iOldSizeMax = m_iSizeMax;
1233 m_iSizeMax = static_cast<int>(iNewSize * 1.1);
1234 pRealData = allocData(m_iSizeMax);
1235 pImgData = allocData(m_iSizeMax);
1237 //copy values into new one
1238 int* piIndexes = new int[std::max(m_iDims, _iDims)];
1239 memset(piIndexes, 0x00, sizeof(int) * std::max(m_iDims, _iDims));
1240 for (int i = 0; i < _iDims; i++)
1245 int iPreviousNewIdx = 0;
1246 for (int i = 0; i < m_iSize; i++)
1248 getIndexes(i, piIndexes);
1249 int iNewIdx = getIndexWithDims(piIndexes, _piDims, _iDims);
1250 pRealData[iNewIdx] = m_pRealData[i];
1251 pImgData[iNewIdx] = m_pImgData[i];
1252 for (int j = iPreviousNewIdx; j < iNewIdx; ++j)
1254 T pTemp = getNullValue();
1255 pRealData[j] = copyValue(pTemp);
1256 pImgData[j] = copyValue(pTemp);
1257 if (pTemp != pRealData[j])
1263 iPreviousNewIdx = iNewIdx + 1;
1266 // if it's not the first resize,
1267 // fill new data with element of last allocation
1268 if (iPreviousNewIdx < iOldSizeMax)
1270 for (int i = iPreviousNewIdx; i < iOldSizeMax; ++i)
1272 pRealData[i] = m_pRealData[i];
1273 pImgData[i] = m_pImgData[i];
1278 // first resize, iOldSizeMax don't contain the 10%
1279 iOldSizeMax = iPreviousNewIdx;
1282 for (int i = iOldSizeMax; i < m_iSizeMax; ++i)
1284 T pTemp = getNullValue();
1285 pRealData[i] = copyValue(pTemp);
1286 pImgData[i] = copyValue(pTemp);
1287 if (pTemp != pRealData[i])
1295 delete[] m_pRealData;
1296 delete[] m_pImgData;
1297 //replace old array by new one
1298 m_pRealData = pRealData;
1299 m_pImgData = pImgData;
1303 //check if only the last dims change
1304 bool bNonLastDimChange = false;
1305 for (int i = 0; i < (m_iDims - 1); i++)
1307 if (m_piDims[i] != _piDims[i])
1309 bNonLastDimChange = true;
1314 //if vector or if row dimension not change, we don't need to shift data
1315 if (m_iDims != _iDims || (!isVector() && bNonLastDimChange))
1317 //copy values into new one
1318 int* piIndexes = new int[std::max(m_iDims, _iDims)];
1319 memset(piIndexes, 0x00, sizeof(int) * std::max(m_iDims, _iDims));
1320 for (int i = m_iSize - 1; i >= 0; i--)
1322 getIndexes(i, piIndexes);
1323 int iNewIdx = getIndexWithDims(piIndexes, _piDims, _iDims);
1326 T pTemp = m_pRealData[iNewIdx];
1327 m_pRealData[iNewIdx] = m_pRealData[i];
1328 m_pRealData[i] = pTemp;
1330 pTemp = m_pImgData[iNewIdx];
1331 m_pImgData[iNewIdx] = m_pImgData[i];
1332 m_pImgData[i] = pTemp;
1341 iNewSize = get_max_size(_piDims, _iDims);
1342 if (iNewSize > m_iSizeMax)
1344 //alloc 10% bigger than asked to prevent future resize
1345 int iOldSizeMax = m_iSizeMax;
1346 m_iSizeMax = static_cast<int>(iNewSize * 1.1);
1347 pRealData = allocData(m_iSizeMax);
1349 //copy values into new one
1350 int* piIndexes = new int[std::max(m_iDims, _iDims)];
1351 memset(piIndexes, 0x00, sizeof(int) * std::max(m_iDims, _iDims));
1352 for (int i = 0; i < _iDims; i++)
1357 int iPreviousNewIdx = 0;
1358 for (int i = 0; i < m_iSize; i++)
1360 getIndexes(i, piIndexes);
1361 int iNewIdx = getIndexWithDims(piIndexes, _piDims, _iDims);
1362 pRealData[iNewIdx] = m_pRealData[i];
1363 m_pRealData[i] = NULL;
1364 for (int j = iPreviousNewIdx; j < iNewIdx; ++j)
1366 T pTemp = getNullValue();
1367 T pTemp2 = copyValue(pTemp);
1368 pRealData[j] = pTemp2;
1369 if (pTemp != pTemp2)
1375 iPreviousNewIdx = iNewIdx + 1;
1378 //clean section between m_iSize and iOldSizeMax
1379 for (int i = m_iSize; i < iOldSizeMax; ++i)
1381 deleteData(m_pRealData[i]);
1382 m_pRealData[i] = NULL;
1385 //if (iPreviousNewIdx < iOldSizeMax)
1387 // for (int i = iPreviousNewIdx; i < iOldSizeMax; ++i)
1389 // pRealData[i] = m_pRealData[i];
1390 // m_pRealData[i] = NULL;
1395 // iOldSizeMax = iPreviousNewIdx;
1398 //fill exceeded with NullValue
1399 for (int i = iPreviousNewIdx; i < m_iSizeMax; ++i)
1401 T pTemp = getNullValue();
1402 T pTemp2 = copyValue(pTemp);
1403 pRealData[i] = pTemp2;
1404 if (pTemp != pTemp2)
1412 delete[] m_pRealData;
1413 //replace old array by new one
1414 m_pRealData = pRealData;
1418 //check if only the last dims change
1419 bool bNonLastDimChange = false;
1420 for (int i = 0; i < (m_iDims - 1); i++)
1422 if (m_piDims[i] != _piDims[i])
1424 bNonLastDimChange = true;
1429 //if vector or if row dimension not change, we don't need to shift data
1430 if (m_iDims != _iDims || (!isVector() && bNonLastDimChange))
1432 //copy values into new one
1433 int* piIndexes = new int[std::max(m_iDims, _iDims)];
1434 memset(piIndexes, 0x00, sizeof(int) * std::max(m_iDims, _iDims));
1435 for (int i = m_iSize - 1; i >= 0; i--)
1437 getIndexes(i, piIndexes);
1438 int iNewIdx = getIndexWithDims(piIndexes, _piDims, _iDims);
1441 T pTemp = m_pRealData[iNewIdx];
1442 m_pRealData[iNewIdx] = m_pRealData[i];
1443 m_pRealData[i] = pTemp;
1451 if (_iDims != m_iDims)
1453 //int* piDims = new int[_iDims];
1454 for (int i = 0; i < _iDims; i++)
1456 m_piDims[i] = _piDims[i];
1458 //delete[] m_piDims;
1459 //m_piDims = piDims;
1464 for (int i = 0; i < m_iDims; i++)
1466 m_piDims[i] = _piDims[i];
1469 m_iRows = m_piDims[0];
1470 m_iCols = m_piDims[1];
1476 // used to allow definition of ArrayOf methode in this cpp file.
1477 template class EXTERN_AST ArrayOf<char>;
1478 template class EXTERN_AST ArrayOf<unsigned char>;
1479 template class EXTERN_AST ArrayOf<short>;
1480 template class EXTERN_AST ArrayOf<unsigned short>;
1481 template class EXTERN_AST ArrayOf<int>;
1482 template class EXTERN_AST ArrayOf<unsigned int>;
1483 template class EXTERN_AST ArrayOf<long long>;
1484 template class EXTERN_AST ArrayOf<unsigned long long>;
1485 template class EXTERN_AST ArrayOf<double>;
1486 template class EXTERN_AST ArrayOf<wchar_t*>;
1487 template class EXTERN_AST ArrayOf<SinglePoly*>;
1488 template class EXTERN_AST ArrayOf<SingleStruct*>;
1489 template class EXTERN_AST ArrayOf<InternalType*>; // Cell