2 * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3 * Copyright (C) 2010-2010 - DIGITEO - Bruno JOFRET
4 * Copyright (C) 2011 - DIGITEO - Antoine ELIAS
6 * Copyright (C) 2012 - 2016 - Scilab Enterprises
8 * This file is hereby licensed under the terms of the GNU GPL v2.0,
9 * pursuant to article 5.3.4 of the CeCILL v.2.1.
10 * This file was originally licensed under the terms of the CeCILL v2.1,
11 * and continues to be available under such terms.
12 * For more information, see the COPYING file which you should have received
13 * along with this program.
26 #include "tostring_common.hxx"
27 #include "core_math.h"
29 #include "configvariable.hxx"
30 #include "types_tools.hxx"
35 ** Constructor & Destructor (public)
39 int piDims[2] = {0, 0};
40 createCell(2, piDims, nullptr);
43 Cell::Cell(int _iRows, int _iCols, InternalType** data)
45 int piDims[2] = {_iRows, _iCols};
46 createCell(2, piDims, data);
49 Cell::Cell(int _iDims, const int* _piDims, InternalType** data)
51 createCell(_iDims, _piDims, data);
54 bool Cell::getMemory(long long* _piSize, long long* _piSizePlusType)
58 InternalType** p = get();
59 for (int i = 0; i < getSize(); i++)
62 if (p[i]->getMemory(&piS, &piSPT))
65 *_piSizePlusType += piSPT;
68 *_piSizePlusType += sizeof(Cell);
72 void Cell::createCell(int _iDims, const int* _piDims, InternalType** data)
74 InternalType** pIT = NULL;
75 create(_piDims, _iDims, &pIT, NULL);
83 types::Double* pDbl = Double::Empty();
84 for (int i = 0; i < m_iSizeMax; i++)
86 m_pRealData[i] = pDbl;
87 m_pRealData[i]->IncreaseRef();
92 for (int i = 0; i < m_iSizeMax; i++)
94 m_pRealData[i] = data[i];
95 m_pRealData[i]->IncreaseRef();
100 Inspector::addItem(this);
106 if (isDeletable() == true)
108 for (int i = 0; i < m_iSizeMax; i++)
110 m_pRealData[i]->DecreaseRef();
111 m_pRealData[i]->killMe();
115 delete[] m_pRealData;
117 Inspector::removeItem(this);
122 ** Private Copy Constructor and data Access
124 Cell::Cell(Cell *_oCellCopyMe)
126 InternalType** pIT = NULL;
127 create(_oCellCopyMe->getDimsArray(), _oCellCopyMe->getDims(), &pIT, NULL);
128 for (int i = 0 ; i < getSize() ; i++)
130 m_pRealData[i] = NULL;
133 for (int i = 0 ; i < getSize() ; i++)
135 set(i, _oCellCopyMe->get(i)->clone());
138 Inspector::addItem(this);
142 bool Cell::transpose(InternalType *& out)
152 Cell * pC = new Cell();
154 InternalType** pIT = NULL;
155 int piDims[2] = {getCols(), getRows()};
156 pC->create(piDims, 2, &pIT, NULL);
158 for (int i = 0, k = 0; i < getCols(); i++, k += getRows())
160 for (int j = 0, l = 0; j < getRows(); j++, l += getCols())
162 pC->m_pRealData[i + l] = m_pRealData[j + k]->clone();
163 pC->m_pRealData[i + l]->IncreaseRef();
173 Cell* Cell::set(int _iRows, int _iCols, InternalType* _pIT)
175 if (_iRows < getRows() && _iCols < getCols())
177 return set(_iCols * getRows() + _iRows, _pIT);
182 Cell* Cell::set(int _iRows, int _iCols, const InternalType* _pIT)
184 if (_iRows < getRows() && _iCols < getCols())
186 return set(_iCols * getRows() + _iRows, _pIT);
191 Cell* Cell::set(int _iIndex, InternalType* _pIT)
193 if (_iIndex >= m_iSize)
198 // corner case when inserting twice
199 if (m_pRealData[_iIndex] == _pIT)
204 typedef Cell* (Cell::*set_t)(int, InternalType*);
205 Cell* pIT = checkRef(this, (set_t)&Cell::set, _iIndex, _pIT);
211 if (m_pRealData[_iIndex] != NULL)
213 m_pRealData[_iIndex]->DecreaseRef();
214 m_pRealData[_iIndex]->killMe();
218 m_pRealData[_iIndex] = _pIT;
222 Cell* Cell::set(int _iIndex, const InternalType* _pIT)
224 if (_iIndex >= m_iSize)
229 typedef Cell* (Cell::*set_t)(int, const InternalType*);
230 Cell* pIT = checkRef(this, (set_t)&Cell::set, _iIndex, _pIT);
236 if (m_pRealData[_iIndex] != NULL)
238 m_pRealData[_iIndex]->DecreaseRef();
239 m_pRealData[_iIndex]->killMe();
242 const_cast<InternalType*>(_pIT)->IncreaseRef();
243 m_pRealData[_iIndex] = const_cast<InternalType*>(_pIT);
248 Cell* Cell::set(InternalType** _pIT)
250 typedef Cell* (Cell::*set_t)(InternalType**);
251 Cell* pIT = checkRef(this, (set_t)&Cell::set, _pIT);
257 for (int i = 0; i < m_iSize; i++)
264 if (m_pRealData[i] != NULL)
266 m_pRealData[i]->DecreaseRef();
267 m_pRealData[i]->killMe();
270 _pIT[i]->IncreaseRef();
271 m_pRealData[i] = _pIT[i];
279 ** Create a new Struct and Copy all values.
283 return new Cell(this);
286 InternalType* Cell::getNullValue()
288 return Double::Empty();
291 Cell* Cell::createEmpty(int _iDims, int* _piDims, bool /*_bComplex*/)
293 return new Cell(_iDims, _piDims);
296 InternalType* Cell::copyValue(InternalType* _pData)
298 _pData->IncreaseRef();
302 void Cell::deleteAll()
304 for (int i = 0 ; i < getSize() ; i++)
306 m_pRealData[i]->DecreaseRef();
307 m_pRealData[i]->killMe();
310 delete[] m_pRealData;
314 void Cell::deleteImg()
321 if (getDims() == 2 && getRows() == 0 && getCols() == 0)
329 ** toString to display Structs
330 ** FIXME : Find a better indentation process
332 bool Cell::subMatrixToString(std::wostringstream& ostr, int* _piDims, int /*_iDims*/)
334 int iPrecision = ConfigVariable::getFormatSize();
342 //max len for each column
343 int *piTypeLen = new int[getCols()];
344 int *piSizeLen = new int[getCols()];
346 memset(piTypeLen, 0x00, getCols() * sizeof(int));
347 memset(piSizeLen, 0x00, getCols() * sizeof(int));
349 for (int j = 0 ; j < getCols() ; j++)
351 for (int i = 0 ; i < getRows() ; i++)
356 int iPos = getIndex(_piDims);
357 InternalType* pIT = get(iPos);
359 if (pIT->isAssignable())
361 //compute number of digits to write dimensions
363 if (pIT->isGenericType())
365 GenericType* pGT = pIT->getAs<GenericType>();
366 for (int k = 0 ; k < pGT->getDims() ; k++)
368 iTypeLen += static_cast<int>(log10(static_cast<double>(pGT->getDimsArray()[k])) + 1);
370 piSizeLen[j] = std::max(piSizeLen[j], iTypeLen + (pGT->getDims() - 1));//add number of "x"
374 //types non derived from ArrayOf.
375 int iSize = static_cast<int>(log10(static_cast<double>(pIT->getAs<GenericType>()->getRows())) + 1);
376 piSizeLen[j] = std::max(piSizeLen[j], iSize);
381 //no size so let a white space, size == 1
382 piSizeLen[j] = std::max(piSizeLen[j], 1);
385 piTypeLen[j] = std::max(piTypeLen[j], static_cast<int>(pIT->getTypeStr().size()));
389 for (int i = 0 ; i < getRows() ; i++)
391 for (int j = 0 ; j < getCols() ; j++)
395 int iPos = getIndex(_piDims);
396 InternalType* pIT = get(iPos);
399 if (pIT->isAssignable())
401 if (pIT->isGenericType())
404 GenericType* pGT = pIT->getAs<GenericType>();
405 std::wostringstream ostemp;
406 for (int k = 0 ; k < pGT->getDims() ; k++)
412 ostemp << pGT->getDimsArray()[k];
414 configureStream(&ostr, piSizeLen[j], iPrecision, ' ');
415 ostr << std::right << ostemp.str();
420 configureStream(&ostr, piSizeLen[j], iPrecision, ' ');
423 ostr << std::right << pIT->getAs<List>()->getSize();
427 ostr << std::right << 1;
433 configureStream(&ostr, piSizeLen[j], iPrecision, ' ');
434 ostr << L"";//fill with space
437 configureStream(&ostr, piTypeLen[j], iPrecision, ' ');
438 ostr << std::left << pIT->getTypeStr();
447 ostr << std::endl << std::resetiosflags(std::ios::adjustfield);
451 bool Cell::operator==(const InternalType& it)
453 if (const_cast<InternalType &>(it).isCell() == false)
458 Cell* pC = const_cast<InternalType &>(it).getAs<Cell>();
460 for (int i = 0 ; i < getDims() ; i++)
462 if (pC->getDimsArray()[i] != getDimsArray()[i])
468 for (int i = 0 ; i < getSize() ; i++)
470 if (*get(i) != *pC->get(i))
478 bool Cell::operator!=(const InternalType& it)
480 return !(*this == it);
483 List* Cell::extractCell(typed_list* _pArgs)
485 InternalType* pIT = extract(_pArgs);
486 if (pIT == NULL || pIT->isCell() == false)
491 List* pList = new List();
493 Cell* pCell = pIT->getAs<Cell>();
494 for (int i = 0 ; i < pCell->getSize() ; i++)
496 pList->append(pCell->get(i));
502 Cell* Cell::insertCell(typed_list* _pArgs, InternalType* _pSource)
504 Cell* pCell = new Cell(1, 1);
505 pCell->set(0, _pSource);
506 Cell* pOut = insert(_pArgs, pCell)->getAs<Cell>();
511 Cell* Cell::insertNewCell(typed_list* _pArgs, InternalType* _pSource)
513 Cell* pCell = new Cell(1, 1);
514 pCell->set(0, _pSource);
515 Cell* pOut = pCell->insertNew(_pArgs)->getAs<Cell>();
519 InternalType** Cell::allocData(int _iSize)
521 InternalType** pData = new InternalType*[_iSize];
522 for (int i = 0 ; i < _iSize ; i++)
529 void Cell::deleteData(InternalType* _pData)
537 Cell* Cell::createEmpty()