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 * This file must be used under the terms of the CeCILL.
7 * This source file is licensed as described in the file COPYING, which
8 * you should have received as part of this distribution. The terms
9 * are also available at
10 * http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt
21 #include "tostring_common.hxx"
22 #include "core_math.h"
24 #include "configvariable.hxx"
29 ** Constructor & Destructor (public)
33 InternalType** pIT = NULL;
34 int piDims[2] = {0, 0};
35 create(piDims, 2, &pIT, NULL);
38 Cell::Cell(int _iRows, int _iCols)
40 InternalType** pIT = NULL;
41 int piDims[2] = {_iRows, _iCols};
42 create(piDims, 2, &pIT, NULL);
43 Double* pEmpty = Double::Empty();
44 for (int i = 0 ; i < getSize() ; i++)
46 pEmpty->IncreaseRef();
47 m_pRealData[i] = pEmpty;
51 Cell::Cell(int _iDims, int* _piDims)
53 InternalType** pIT = NULL;
54 create(_piDims, _iDims, &pIT, NULL);
55 Double* pEmpty = Double::Empty();
56 for (int i = 0 ; i < getSize() ; i++)
58 pEmpty->IncreaseRef();
59 m_pRealData[i] = pEmpty;
65 if (isDeletable() == true)
67 for (int i = 0 ; i < getSize() ; i++)
69 m_pRealData[i]->DecreaseRef();
70 if (m_pRealData[i]->isDeletable())
72 delete m_pRealData[i];
77 Inspector::removeItem(this);
82 ** Private Copy Constructor and data Access
84 Cell::Cell(Cell *_oCellCopyMe)
86 InternalType** pIT = NULL;
87 create(_oCellCopyMe->getDimsArray(), _oCellCopyMe->getDims(), &pIT, NULL);
88 for (int i = 0 ; i < getSize() ; i++)
90 m_pRealData[i] = NULL;
93 for (int i = 0 ; i < getSize() ; i++)
95 set(i, _oCellCopyMe->get(i)->clone());
98 Inspector::addItem(this);
102 bool Cell::transpose(InternalType *& out)
112 Cell * pC = new Cell();
114 InternalType** pIT = NULL;
115 int piDims[2] = {getCols(), getRows()};
116 pC->create(piDims, 2, &pIT, NULL);
118 Transposition::transpose_clone(getRows(), getCols(), m_pRealData, pC->m_pRealData);
126 bool Cell::set(int _iRows, int _iCols, InternalType* _pIT)
128 if (_iRows < getRows() && _iCols < getCols())
130 return set(_iCols * getRows() + _iRows, _pIT);
135 bool Cell::set(int _iRows, int _iCols, const InternalType* _pIT)
137 if (_iRows < getRows() && _iCols < getCols())
139 return set(_iCols * getRows() + _iRows, _pIT);
144 bool Cell::set(int _iIndex, InternalType* _pIT)
146 if (m_pRealData[_iIndex] == _pIT)
151 if (_iIndex < getSize())
153 if (m_pRealData[_iIndex] != NULL)
155 m_pRealData[_iIndex]->DecreaseRef();
156 if (m_pRealData[_iIndex]->isDeletable())
158 delete m_pRealData[_iIndex];
163 m_pRealData[_iIndex] = _pIT;
169 bool Cell::set(int _iIndex, const InternalType* _pIT)
171 if (_iIndex < getSize())
173 if (m_pRealData[_iIndex] != NULL)
175 m_pRealData[_iIndex]->DecreaseRef();
176 if (m_pRealData[_iIndex]->isDeletable())
178 delete m_pRealData[_iIndex];
182 const_cast<InternalType*>(_pIT)->IncreaseRef();
183 m_pRealData[_iIndex] = const_cast<InternalType*>(_pIT);
189 bool Cell::set(InternalType** _pIT)
191 for (int i = 0 ; i < getSize() ; i++)
193 if (set(i, _pIT[i]) == false)
203 ** Create a new Struct and Copy all values.
205 InternalType* Cell::clone()
207 return new Cell(this);
210 InternalType* Cell::getNullValue()
212 return Double::Empty();
215 Cell* Cell::createEmpty(int _iDims, int* _piDims, bool /*_bComplex*/)
217 return new Cell(_iDims, _piDims);
220 InternalType* Cell::copyValue(InternalType* _pData)
222 _pData->IncreaseRef();
226 void Cell::deleteAll()
228 for (int i = 0 ; i < getSize() ; i++)
230 m_pRealData[i]->DecreaseRef();
231 if (m_pRealData[i]->isDeletable())
233 delete m_pRealData[i];
236 delete[] m_pRealData;
240 void Cell::deleteImg()
247 if (getDims() == 2 && getRows() == 0 && getCols() == 0)
255 ** toString to display Structs
256 ** FIXME : Find a better indentation process
258 bool Cell::subMatrixToString(std::wostringstream& ostr, int* _piDims, int /*_iDims*/)
260 int iPrecision = ConfigVariable::getFormatSize();
268 //max len for each column
269 int *piTypeLen = new int[getCols()];
270 int *piSizeLen = new int[getCols()];
272 memset(piTypeLen, 0x00, getCols() * sizeof(int));
273 memset(piSizeLen, 0x00, getCols() * sizeof(int));
275 for (int j = 0 ; j < getCols() ; j++)
277 for (int i = 0 ; i < getRows() ; i++)
282 int iPos = getIndex(_piDims);
283 InternalType* pIT = get(iPos);
285 if (pIT->isAssignable())
287 //compute number of digits to write dimensions
289 if (pIT->getAs<GenericType>())
291 GenericType* pGT = pIT->getAs<GenericType>();
292 for (int k = 0 ; k < pGT->getDims() ; k++)
294 iTypeLen += static_cast<int>(log10(static_cast<double>(pGT->getDimsArray()[k])) + 1);
296 piSizeLen[j] = std::max(piSizeLen[j], iTypeLen + (pGT->getDims() - 1));//add number of "x"
300 //types non derived from ArrayOf.
301 int iSize = static_cast<int>(log10(static_cast<double>(pIT->getAs<GenericType>()->getRows())) + 1);
302 piSizeLen[j] = std::max(piSizeLen[j], iSize);
307 //no size so let a white space, size == 1
308 piSizeLen[j] = std::max(piSizeLen[j], 1);
311 piTypeLen[j] = std::max(piTypeLen[j], static_cast<int>(pIT->getTypeStr().size()));
315 for (int i = 0 ; i < getRows() ; i++)
317 for (int j = 0 ; j < getCols() ; j++)
321 int iPos = getIndex(_piDims);
322 InternalType* pIT = get(iPos);
325 if (pIT->isAssignable())
327 if (pIT->isGenericType())
330 GenericType* pGT = pIT->getAs<GenericType>();
331 std::wostringstream ostemp;
332 for (int k = 0 ; k < pGT->getDims() ; k++)
338 ostemp << pGT->getDimsArray()[k];
340 configureStream(&ostr, piSizeLen[j], iPrecision, ' ');
341 ostr << std::right << ostemp.str();
346 configureStream(&ostr, piSizeLen[j], iPrecision, ' ');
349 ostr << std::right << pIT->getAs<List>()->getSize();
353 ostr << std::right << 1;
359 configureStream(&ostr, piSizeLen[j], iPrecision, ' ');
360 ostr << L"";//fill with space
363 configureStream(&ostr, piTypeLen[j], iPrecision, ' ');
364 ostr << std::left << pIT->getTypeStr();
377 //bool Cell::append(int _iRows, int _iCols, Cell *_poSource)
382 bool Cell::operator==(const InternalType& it)
384 if (const_cast<InternalType &>(it).isCell())
389 Cell* pC = const_cast<InternalType &>(it).getAs<Cell>();
391 for (int i = 0 ; i < getDims() ; i++)
393 if (pC->getDimsArray()[i] != getDimsArray()[i])
399 for (int i = 0 ; i < getSize() ; i++)
401 if (get(i) != pC->get(i))
409 bool Cell::operator!=(const InternalType& it)
411 return !(*this == it);
414 List* Cell::extractCell(typed_list* _pArgs)
416 InternalType* pIT = extract(_pArgs);
417 if (pIT->isCell() == false)
422 List* pList = new List();
424 Cell* pCell = pIT->getAs<Cell>();
425 for (int i = 0 ; i < pCell->getSize() ; i++)
427 pList->append(pCell->get(i));
433 Cell* Cell::insertCell(typed_list* _pArgs, InternalType* _pSource)
435 Cell* pCell = new Cell(1, 1);
436 pCell->set(0, _pSource);
437 Cell* pOut = insert(_pArgs, pCell)->getAs<Cell>();
441 Cell* Cell::insertNewCell(typed_list* _pArgs, InternalType* _pSource)
443 Cell* pCell = new Cell(1, 1);
444 pCell->set(0, _pSource);
445 Cell* pOut = Cell::insertNew(_pArgs, pCell)->getAs<Cell>();
449 InternalType** Cell::allocData(int _iSize)
451 InternalType** pData = new InternalType*[_iSize];
452 Double* pEmpty = Double::Empty();
453 for (int i = 0 ; i < _iSize ; i++)
455 pEmpty->IncreaseRef();