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 int piDims[2] = {0, 0};
34 createCell(2, piDims);
37 Cell::Cell(int _iRows, int _iCols)
39 int piDims[2] = {_iRows, _iCols};
40 createCell(2, piDims);
43 Cell::Cell(int _iDims, int* _piDims)
45 createCell(_iDims, _piDims);
48 void Cell::createCell(int _iDims, int* _piDims)
50 InternalType** pIT = NULL;
51 create(_piDims, _iDims, &pIT, NULL);
52 for (int i = 0; i < m_iSizeMax; i++)
54 m_pRealData[i] = Double::Empty();
55 m_pRealData[i]->IncreaseRef();
58 Inspector::addItem(this);
64 if (isDeletable() == true)
66 for (int i = 0; i < m_iSizeMax; i++)
68 m_pRealData[i]->DecreaseRef();
69 m_pRealData[i]->killMe();
75 Inspector::removeItem(this);
80 ** Private Copy Constructor and data Access
82 Cell::Cell(Cell *_oCellCopyMe)
84 InternalType** pIT = NULL;
85 create(_oCellCopyMe->getDimsArray(), _oCellCopyMe->getDims(), &pIT, NULL);
86 for (int i = 0 ; i < getSize() ; i++)
88 m_pRealData[i] = NULL;
91 for (int i = 0 ; i < getSize() ; i++)
93 set(i, _oCellCopyMe->get(i)->clone());
96 Inspector::addItem(this);
100 bool Cell::transpose(InternalType *& out)
110 Cell * pC = new Cell();
112 InternalType** pIT = NULL;
113 int piDims[2] = {getCols(), getRows()};
114 pC->create(piDims, 2, &pIT, NULL);
116 Transposition::transpose_clone(getRows(), getCols(), m_pRealData, pC->m_pRealData);
124 bool Cell::set(int _iRows, int _iCols, InternalType* _pIT)
126 if (_iRows < getRows() && _iCols < getCols())
128 return set(_iCols * getRows() + _iRows, _pIT);
133 bool Cell::set(int _iRows, int _iCols, const InternalType* _pIT)
135 if (_iRows < getRows() && _iCols < getCols())
137 return set(_iCols * getRows() + _iRows, _pIT);
142 bool Cell::set(int _iIndex, InternalType* _pIT)
144 if (_iIndex < getSize())
146 // corner case when inserting twice
147 if (m_pRealData[_iIndex] == _pIT)
152 if (m_pRealData[_iIndex] != NULL)
154 m_pRealData[_iIndex]->DecreaseRef();
155 m_pRealData[_iIndex]->killMe();
159 m_pRealData[_iIndex] = _pIT;
165 bool Cell::set(int _iIndex, const InternalType* _pIT)
167 if (_iIndex < getSize())
169 if (m_pRealData[_iIndex] != NULL)
171 m_pRealData[_iIndex]->DecreaseRef();
172 m_pRealData[_iIndex]->killMe();
175 const_cast<InternalType*>(_pIT)->IncreaseRef();
176 m_pRealData[_iIndex] = const_cast<InternalType*>(_pIT);
182 bool Cell::set(InternalType** _pIT)
184 for (int i = 0 ; i < getSize() ; i++)
186 if (set(i, _pIT[i]) == false)
196 ** Create a new Struct and Copy all values.
198 InternalType* Cell::clone()
200 return new Cell(this);
203 InternalType* Cell::getNullValue()
205 return Double::Empty();
208 Cell* Cell::createEmpty(int _iDims, int* _piDims, bool /*_bComplex*/)
210 return new Cell(_iDims, _piDims);
213 InternalType* Cell::copyValue(InternalType* _pData)
215 _pData->IncreaseRef();
219 void Cell::deleteAll()
221 for (int i = 0 ; i < getSize() ; i++)
223 m_pRealData[i]->DecreaseRef();
224 m_pRealData[i]->killMe();
227 delete[] m_pRealData;
231 void Cell::deleteImg()
238 if (getDims() == 2 && getRows() == 0 && getCols() == 0)
246 ** toString to display Structs
247 ** FIXME : Find a better indentation process
249 bool Cell::subMatrixToString(std::wostringstream& ostr, int* _piDims, int /*_iDims*/)
251 int iPrecision = ConfigVariable::getFormatSize();
259 //max len for each column
260 int *piTypeLen = new int[getCols()];
261 int *piSizeLen = new int[getCols()];
263 memset(piTypeLen, 0x00, getCols() * sizeof(int));
264 memset(piSizeLen, 0x00, getCols() * sizeof(int));
266 for (int j = 0 ; j < getCols() ; j++)
268 for (int i = 0 ; i < getRows() ; i++)
273 int iPos = getIndex(_piDims);
274 InternalType* pIT = get(iPos);
276 if (pIT->isAssignable())
278 //compute number of digits to write dimensions
280 if (pIT->isGenericType())
282 GenericType* pGT = pIT->getAs<GenericType>();
283 for (int k = 0 ; k < pGT->getDims() ; k++)
285 iTypeLen += static_cast<int>(log10(static_cast<double>(pGT->getDimsArray()[k])) + 1);
287 piSizeLen[j] = std::max(piSizeLen[j], iTypeLen + (pGT->getDims() - 1));//add number of "x"
291 //types non derived from ArrayOf.
292 int iSize = static_cast<int>(log10(static_cast<double>(pIT->getAs<GenericType>()->getRows())) + 1);
293 piSizeLen[j] = std::max(piSizeLen[j], iSize);
298 //no size so let a white space, size == 1
299 piSizeLen[j] = std::max(piSizeLen[j], 1);
302 piTypeLen[j] = std::max(piTypeLen[j], static_cast<int>(pIT->getTypeStr().size()));
306 for (int i = 0 ; i < getRows() ; i++)
308 for (int j = 0 ; j < getCols() ; j++)
312 int iPos = getIndex(_piDims);
313 InternalType* pIT = get(iPos);
316 if (pIT->isAssignable())
318 if (pIT->isGenericType())
321 GenericType* pGT = pIT->getAs<GenericType>();
322 std::wostringstream ostemp;
323 for (int k = 0 ; k < pGT->getDims() ; k++)
329 ostemp << pGT->getDimsArray()[k];
331 configureStream(&ostr, piSizeLen[j], iPrecision, ' ');
332 ostr << std::right << ostemp.str();
337 configureStream(&ostr, piSizeLen[j], iPrecision, ' ');
340 ostr << std::right << pIT->getAs<List>()->getSize();
344 ostr << std::right << 1;
350 configureStream(&ostr, piSizeLen[j], iPrecision, ' ');
351 ostr << L"";//fill with space
354 configureStream(&ostr, piTypeLen[j], iPrecision, ' ');
355 ostr << std::left << pIT->getTypeStr();
368 //bool Cell::append(int _iRows, int _iCols, Cell *_poSource)
373 bool Cell::operator==(const InternalType& it)
375 if (const_cast<InternalType &>(it).isCell() == false)
380 Cell* pC = const_cast<InternalType &>(it).getAs<Cell>();
382 for (int i = 0 ; i < getDims() ; i++)
384 if (pC->getDimsArray()[i] != getDimsArray()[i])
390 for (int i = 0 ; i < getSize() ; i++)
392 if (get(i) != pC->get(i))
400 bool Cell::operator!=(const InternalType& it)
402 return !(*this == it);
405 List* Cell::extractCell(typed_list* _pArgs)
407 InternalType* pIT = extract(_pArgs);
408 if (pIT == NULL || pIT->isCell() == false)
413 List* pList = new List();
415 Cell* pCell = pIT->getAs<Cell>();
416 for (int i = 0 ; i < pCell->getSize() ; i++)
418 pList->append(pCell->get(i));
424 Cell* Cell::insertCell(typed_list* _pArgs, InternalType* _pSource)
426 Cell* pCell = new Cell(1, 1);
427 pCell->set(0, _pSource);
428 Cell* pOut = insert(_pArgs, pCell)->getAs<Cell>();
433 Cell* Cell::insertNewCell(typed_list* _pArgs, InternalType* _pSource)
435 Cell* pCell = new Cell(1, 1);
436 pCell->set(0, _pSource);
437 Cell* pOut = Cell::insertNew(_pArgs, pCell)->getAs<Cell>();
441 InternalType** Cell::allocData(int _iSize)
443 InternalType** pData = new InternalType*[_iSize];
444 for (int i = 0 ; i < _iSize ; i++)
451 void Cell::deleteData(InternalType* _pData)
455 _pData->DecreaseRef();