2 * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3 * Copyright (C) 2011 - 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
17 #include "localization.hxx"
18 #include "scilabWrite.hxx"
24 m_bDisableCloneInCopyValue = false;
25 SingleStruct** pIT = NULL;
26 int piDims[2] = {0, 0};
27 create(piDims, 2, &pIT, NULL);
29 Inspector::addItem(this);
33 Struct::Struct(int _iRows, int _iCols)
35 m_bDisableCloneInCopyValue = false;
36 SingleStruct** pIT = NULL;
37 SingleStruct *p = new SingleStruct();
38 int piDims[2] = {_iRows, _iCols};
39 create(piDims, 2, &pIT, NULL);
40 for (int i = 0 ; i < getSize() ; i++)
47 Inspector::addItem(this);
51 Struct::Struct(int _iDims, int* _piDims)
53 m_bDisableCloneInCopyValue = false;
54 SingleStruct** pIT = NULL;
55 SingleStruct *p = new SingleStruct();
56 create(_piDims, _iDims, &pIT, NULL);
57 for (int i = 0 ; i < getSize() ; i++)
64 Inspector::addItem(this);
70 if (isDeletable() == true)
72 for (int i = 0 ; i < m_iSizeMax ; i++)
74 SingleStruct *pStr = m_pRealData[i];
85 Inspector::removeItem(this);
89 Struct::Struct(Struct *_oStructCopyMe)
91 m_bDisableCloneInCopyValue = false;
92 SingleStruct** pIT = NULL;
93 create(_oStructCopyMe->getDimsArray(), _oStructCopyMe->getDims(), &pIT, NULL);
94 for (int i = 0 ; i < getSize() ; i++)
96 m_pRealData[i] = NULL;
99 for (int i = 0 ; i < getSize() ; i++)
101 pIT[i] = _oStructCopyMe->get(i)->clone();
104 Inspector::addItem(this);
108 InternalType* Struct::clone()
110 return new Struct(this);
113 bool Struct::transpose(InternalType *& out)
123 int piDims[2] = {getCols(), getRows()};
124 Struct * pSt = new Struct(2, piDims);
126 for (int i = 0; i < m_iSize; ++i)
128 pSt->m_pRealData[i]->DecreaseRef();
129 pSt->m_pRealData[i]->killMe();
132 Transposition::transpose_clone(getRows(), getCols(), m_pRealData, pSt->m_pRealData);
140 bool Struct::extract(const std::wstring & name, InternalType *& out)
144 out = extractField(name);
148 wchar_t szError[bsiz];
149 os_swprintf(szError, bsiz, _W("Unknown field : %ls.\n").c_str(), name.c_str());
150 throw std::wstring(szError);
156 bool Struct::invoke(typed_list & in, optional_list & opt, int _iRetCount, typed_list & out, ast::ConstVisitor & execFunc, const ast::Exp & e)
163 else if (in.size() == 1)
165 InternalType * arg = in[0];
166 std::vector<InternalType *> _out;
169 std::vector<std::wstring> wstFields;
170 String * pString = arg->getAs<types::String>();
171 for (int i = 0; i < pString->getSize(); ++i)
173 std::wstring wstField(pString->get(i));
174 if (this->exists(wstField))
176 wstFields.push_back(wstField);
180 wchar_t szError[bsiz];
181 os_swprintf(szError, bsiz, _W("Field \"%ls\" does not exists\n").c_str(), wstField.c_str());
182 throw ast::ScilabError(szError, 999, e.getLocation());
186 _out = extractFields(wstFields);
187 if (_out.size() == 1)
189 InternalType * pIT = _out[0];
190 if (pIT->isList() && pIT->getAs<List>()->getSize() == 1)
192 out.push_back(pIT->getAs<List>()->get(0));
202 return ArrayOf<SingleStruct*>::invoke(in, opt, _iRetCount, out, execFunc, e);
205 bool Struct::set(int _iRows, int _iCols, SingleStruct* _pIT)
207 if (_iRows < getRows() && _iCols < getCols())
209 return set(_iCols * getRows() + _iRows, _pIT);
214 bool Struct::set(int _iRows, int _iCols, const SingleStruct* _pIT)
216 if (_iRows < getRows() && _iCols < getCols())
218 return set(_iCols * getRows() + _iRows, _pIT);
223 bool Struct::set(int _iIndex, SingleStruct* _pIT)
225 if (_iIndex < getSize())
227 if (m_bDisableCloneInCopyValue && m_pRealData[_iIndex] == _pIT)
232 InternalType* pOld = m_pRealData[_iIndex];
234 m_pRealData[_iIndex] = copyValue(_pIT);
235 if (m_bDisableCloneInCopyValue == false)
238 m_pRealData[_iIndex]->IncreaseRef();
252 bool Struct::set(int _iIndex, const SingleStruct* _pIT)
254 if (_iIndex < getSize())
256 InternalType* pOld = m_pRealData[_iIndex];
258 m_pRealData[_iIndex] = const_cast<SingleStruct*>(_pIT)->clone();
271 bool Struct::set(SingleStruct** _pIT)
273 for (int i = 0 ; i < getSize() ; i++)
275 if (set(i, _pIT[i]) == false)
283 String* Struct::getFieldNames()
287 return get(0)->getFieldNames();
295 bool Struct::exists(const std::wstring& _sKey)
299 return get(0)->exists(_sKey);
307 bool Struct::operator==(const InternalType& it)
309 if (const_cast<InternalType &>(it).isStruct() == false)
314 Struct* pStr = const_cast<InternalType &>(it).getAs<Struct>();
316 for (int i = 0 ; i < getDims() ; i++)
318 if (pStr->getDimsArray()[i] != getDimsArray()[i])
324 for (int i = 0 ; i < getSize() ; i++)
326 if (*get(i) != *pStr->get(i))
334 bool Struct::operator!=(const InternalType& it)
336 return !(*this == it);
339 SingleStruct* Struct::getNullValue()
341 return new SingleStruct();
344 Struct* Struct::createEmpty(int _iDims, int* _piDims, bool /*_bComplex*/)
346 Struct* pStr = new Struct(_iDims, _piDims);
347 pStr->setCloneInCopyValue(!m_bDisableCloneInCopyValue);
351 SingleStruct* Struct::copyValue(SingleStruct* _pData)
353 SingleStruct* pStr = NULL;
354 if (m_bDisableCloneInCopyValue)
358 //std::wcout << L"copyValueWithoutClone -> " << pStr << L" : " << pStr->getRef() << std::endl;
362 pStr = _pData->clone();
368 void Struct::deleteAll()
370 for (int i = 0 ; i < getSize() ; i++)
372 m_pRealData[i]->DecreaseRef();
373 m_pRealData[i]->killMe();
375 delete[] m_pRealData;
379 void Struct::deleteImg()
384 SingleStruct** Struct::allocData(int _iSize)
386 SingleStruct** pData = new SingleStruct*[_iSize];
387 for (int i = 0 ; i < _iSize ; i++)
394 bool Struct::subMatrixToString(std::wostringstream& /*ostr*/, int* /*_piDims*/, int /*_iDims*/)
399 bool Struct::addField(const std::wstring& _sKey)
403 //change dimension to 1x1 and add field
407 for (int i = 0 ; i < getSize() ; i++)
411 {//assign more than once
412 //clone it before add field
413 set(i, get(i)->clone());
416 get(i)->addField(_sKey);
421 bool Struct::addFieldFront(const std::wstring& _sKey)
425 //change dimension to 1x1 and add field
429 for (int i = 0 ; i < getSize() ; i++)
431 get(i)->addFieldFront(_sKey);
437 bool Struct::removeField(const std::wstring& _sKey)
439 for (int j = 0; j < getSize(); j++)
441 get(j)->removeField(_sKey);
447 bool Struct::toString(std::wostringstream& ostr)
451 ostr << L"0x0 struct array with no field.";
453 else if (getSize() == 1)
455 SingleStruct* pSS = get(0);
456 String* pwstFields = pSS->getFieldNames();
457 if (pwstFields->getSize() == 0)
459 ostr << L"1x1 struct array with no field.";
462 for (int i = 0 ; i < pwstFields->getSize() ; i++)
464 std::wstring wstField(pwstFields->get(i));
465 InternalType* pIT = pSS->get(wstField);
467 // ostr << L" " << wstField << ": ";
468 ostr << L" " << wstField << L": ";
469 ostr << pIT->toStringInLine();
472 pwstFields->killMe();;
477 for (int i = 0 ; i < m_iDims ; i++)
485 ostr << L" struct array with ";
487 String* pwstFields = getFieldNames();
488 ostr << L"fields:" << std::endl;
489 for (int i = 0 ; i < pwstFields->getSize() ; i++)
491 ostr << L" " << pwstFields->get(i) << std::endl;
493 pwstFields->killMe();
499 List* Struct::extractFieldWithoutClone(std::wstring _wstField)
501 List* pL = new List();
502 for (int j = 0 ; j < getSize() ; j++)
504 pL->set(j, get(j)->get(_wstField));
510 std::vector<InternalType*> Struct::extractFields(std::vector<std::wstring> _wstFields)
512 std::vector<InternalType*> ResultList;
514 for (int i = 0 ; i < (int)_wstFields.size() ; i++)
516 ResultList.push_back(extractField(_wstFields[i]));
522 InternalType * Struct::extractField(const std::wstring & wstField)
524 if (wstField == L"dims")
526 Int32 * pDims = new Int32(1, getDims());
527 for (int j = 0 ; j < getDims() ; j++)
529 pDims->set(j, getDimsArray()[j]);
538 return get(0)->get(wstField);
542 List * pL = new List();
543 for (int j = 0 ; j < getSize() ; j++)
545 pL->append(get(j)->get(wstField));
553 std::vector<InternalType*> Struct::extractFields(typed_list* _pArgs)
555 std::vector<InternalType*> ResultList;
557 int iDims = (int)_pArgs->size();
560 int* piMaxDim = new int[iDims];
561 int* piCountDim = new int[iDims];
563 int iSeqCount = checkIndexesArguments(this, _pArgs, &pArg, piMaxDim, piCountDim);
570 cleanIndexesArguments(_pArgs, &pArg);
571 ResultList.push_back(createEmptyDouble());
575 Double* pIndex = pArg[0]->getAs<Double>();
577 for (int i = 0 ; i < iSeqCount ; i++)
579 int iIndex = (int)pIndex->get(i);
584 String* pS = getFieldNames();
585 String* pFields = NULL;
588 pFields = new String(1, pS->getSize() + 2);
589 for (int j = 0; j < pS->getSize(); j++)
591 pFields->set(2 + j, pS->get(j));
598 pFields = new String(1, 2);
601 pFields->set(0, L"st");
602 pFields->set(1, L"dims");
604 ResultList.push_back(pFields);
606 else if (iIndex == 2)
609 Int32* pDims = new Int32(1, getDims());
610 for (int j = 0 ; j < getDims() ; j++)
612 pDims->set(j, getDimsArray()[j]);
615 ResultList.push_back(pDims);
617 else if (getSize() == 0)
621 else if (iIndex > (int)get(0)->getData().size() + 2)
625 else if (getSize() == 1)
628 std::list<InternalType*> pData = get(0)->getData();
629 std::list<InternalType*>::iterator it = pData.begin();
630 std::advance(it, iIndex - 3);
631 ResultList.push_back((*it)->clone());
635 //return each elements for sub structs in a list
636 List* pL = new List();
638 for (int j = 0 ; j < getSize() ; j++)
640 //-2 for fieldlist and dims, -1 for indexed at 0
641 std::list<InternalType*> pData = get(j)->getData();
642 std::list<InternalType*>::iterator it = pData.begin();
643 std::advance(it, iIndex - 3);
644 pL->append((*it)->clone());
647 ResultList.push_back(pL);
652 cleanIndexesArguments(_pArgs, &pArg);
656 bool Struct::resize(int _iNewRows, int _iNewCols)
658 int piDims[2] = {_iNewRows, _iNewCols};
659 return resize(piDims, 2);
662 bool Struct::resize(int* _piDims, int _iDims)
664 m_bDisableCloneInCopyValue = true;
665 bool bRes = ArrayOf<SingleStruct*>::resize(_piDims, _iDims);
666 m_bDisableCloneInCopyValue = false;
669 // insert field(s) only in new element(s) of current struct
670 String* pFields = getFieldNames();
671 for (int iterField = 0; iterField < pFields->getSize(); iterField++)
673 for (int iterStruct = 0; iterStruct < getSize(); iterStruct++)
675 get(iterStruct)->addField(pFields->get(iterField));
685 InternalType* Struct::insertWithoutClone(typed_list* _pArgs, InternalType* _pSource)
687 //std::wcout << L"insertWithoutClone start" << std::endl;
688 m_bDisableCloneInCopyValue = true;
689 InternalType* pIT = insert(_pArgs, _pSource);
690 _pSource->IncreaseRef();
691 //std::wcout << L"insertWithoutClone -> " << _pSource << L" : " << _pSource->getRef() << std::endl;
692 m_bDisableCloneInCopyValue = false;
693 //std::wcout << L"insertWithoutClone end" << std::endl;
697 InternalType* Struct::extractWithoutClone(typed_list* _pArgs)
699 //std::wcout << L"extractWithoutClone start" << std::endl;
700 m_bDisableCloneInCopyValue = true;
701 InternalType* pIT = extract(_pArgs);
702 m_bDisableCloneInCopyValue = false;
703 //std::wcout << L"extractWithoutClone end" << std::endl;
707 void Struct::setCloneInCopyValue(bool _val)
709 m_bDisableCloneInCopyValue = !_val;
712 void Struct::deleteData(SingleStruct* data)