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 pIT[i] = _oStructCopyMe->get(i)->clone();
97 pIT[i]->IncreaseRef();
100 Inspector::addItem(this);
104 InternalType* Struct::clone()
106 return new Struct(this);
109 bool Struct::transpose(InternalType *& out)
119 int piDims[2] = {getCols(), getRows()};
120 Struct * pSt = new Struct(2, piDims);
122 for (int i = 0; i < m_iSize; ++i)
124 pSt->m_pRealData[i]->DecreaseRef();
125 pSt->m_pRealData[i]->killMe();
128 Transposition::transpose_clone(getRows(), getCols(), m_pRealData, pSt->m_pRealData);
136 bool Struct::extract(const std::wstring & name, InternalType *& out)
140 out = extractField(name);
144 wchar_t szError[bsiz];
145 os_swprintf(szError, bsiz, _W("Unknown field : %ls.\n").c_str(), name.c_str());
146 throw std::wstring(szError);
152 bool Struct::invoke(typed_list & in, optional_list & opt, int _iRetCount, typed_list & out, ast::ConstVisitor & execFunc, const ast::Exp & e)
159 else if (in.size() == 1)
161 InternalType * arg = in[0];
162 std::vector<InternalType *> _out;
165 std::vector<std::wstring> wstFields;
166 String * pString = arg->getAs<types::String>();
167 for (int i = 0; i < pString->getSize(); ++i)
169 std::wstring wstField(pString->get(i));
170 if (this->exists(wstField))
172 wstFields.push_back(wstField);
176 wchar_t szError[bsiz];
177 os_swprintf(szError, bsiz, _W("Field \"%ls\" does not exists\n").c_str(), wstField.c_str());
178 throw ast::ScilabError(szError, 999, e.getLocation());
182 _out = extractFields(wstFields);
183 if (_out.size() == 1)
185 InternalType * pIT = _out[0];
186 if (pIT->isList() && pIT->getAs<List>()->getSize() == 1)
188 out.push_back(pIT->getAs<List>()->get(0));
198 return ArrayOf<SingleStruct*>::invoke(in, opt, _iRetCount, out, execFunc, e);
201 bool Struct::set(int _iRows, int _iCols, SingleStruct* _pIT)
203 if (_iRows < getRows() && _iCols < getCols())
205 return set(_iCols * getRows() + _iRows, _pIT);
210 bool Struct::set(int _iRows, int _iCols, const SingleStruct* _pIT)
212 if (_iRows < getRows() && _iCols < getCols())
214 return set(_iCols * getRows() + _iRows, _pIT);
219 bool Struct::set(int _iIndex, SingleStruct* _pIT)
221 if (_iIndex < getSize())
223 if (m_bDisableCloneInCopyValue && m_pRealData[_iIndex] == _pIT)
228 InternalType* pOld = m_pRealData[_iIndex];
230 m_pRealData[_iIndex] = copyValue(_pIT);
231 if (m_bDisableCloneInCopyValue == false)
234 m_pRealData[_iIndex]->IncreaseRef();
248 bool Struct::set(int _iIndex, const SingleStruct* _pIT)
250 if (_iIndex < getSize())
252 InternalType* pOld = m_pRealData[_iIndex];
254 m_pRealData[_iIndex] = const_cast<SingleStruct*>(_pIT)->clone();
267 bool Struct::set(SingleStruct** _pIT)
269 for (int i = 0 ; i < getSize() ; i++)
271 if (set(i, _pIT[i]) == false)
279 String* Struct::getFieldNames()
283 return get(0)->getFieldNames();
291 bool Struct::exists(const std::wstring& _sKey)
295 return get(0)->exists(_sKey);
303 bool Struct::operator==(const InternalType& it)
305 if (const_cast<InternalType &>(it).isStruct() == false)
310 Struct* pStr = const_cast<InternalType &>(it).getAs<Struct>();
312 for (int i = 0 ; i < getDims() ; i++)
314 if (pStr->getDimsArray()[i] != getDimsArray()[i])
320 for (int i = 0 ; i < getSize() ; i++)
322 if (*get(i) != *pStr->get(i))
330 bool Struct::operator!=(const InternalType& it)
332 return !(*this == it);
335 SingleStruct* Struct::getNullValue()
337 return new SingleStruct();
340 Struct* Struct::createEmpty(int _iDims, int* _piDims, bool /*_bComplex*/)
342 Struct* pStr = new Struct(_iDims, _piDims);
343 pStr->setCloneInCopyValue(!m_bDisableCloneInCopyValue);
347 SingleStruct* Struct::copyValue(SingleStruct* _pData)
349 SingleStruct* pStr = NULL;
350 if (m_bDisableCloneInCopyValue)
354 //std::wcout << L"copyValueWithoutClone -> " << pStr << L" : " << pStr->getRef() << std::endl;
358 pStr = _pData->clone();
364 void Struct::deleteAll()
366 for (int i = 0 ; i < getSize() ; i++)
368 m_pRealData[i]->DecreaseRef();
369 m_pRealData[i]->killMe();
371 delete[] m_pRealData;
375 void Struct::deleteImg()
380 SingleStruct** Struct::allocData(int _iSize)
382 SingleStruct** pData = new SingleStruct*[_iSize];
383 for (int i = 0 ; i < _iSize ; i++)
390 bool Struct::subMatrixToString(std::wostringstream& /*ostr*/, int* /*_piDims*/, int /*_iDims*/)
395 bool Struct::addField(const std::wstring& _sKey)
399 //change dimension to 1x1 and add field
403 for (int i = 0 ; i < getSize() ; i++)
407 {//assign more than once
408 //clone it before add field
409 set(i, get(i)->clone());
412 get(i)->addField(_sKey);
417 bool Struct::addFieldFront(const std::wstring& _sKey)
421 //change dimension to 1x1 and add field
425 for (int i = 0 ; i < getSize() ; i++)
427 get(i)->addFieldFront(_sKey);
433 bool Struct::removeField(const std::wstring& _sKey)
435 for (int j = 0; j < getSize(); j++)
437 get(j)->removeField(_sKey);
443 bool Struct::toString(std::wostringstream& ostr)
447 ostr << L"0x0 struct array with no field.";
449 else if (getSize() == 1)
451 SingleStruct* pSS = get(0);
452 String* pwstFields = pSS->getFieldNames();
453 if (pwstFields->getSize() == 0)
455 ostr << L"1x1 struct array with no field.";
458 for (int i = 0 ; i < pwstFields->getSize() ; i++)
460 std::wstring wstField(pwstFields->get(i));
461 InternalType* pIT = pSS->get(wstField);
463 // ostr << L" " << wstField << ": ";
464 ostr << L" " << wstField << L": ";
465 ostr << pIT->toStringInLine();
468 pwstFields->killMe();;
473 for (int i = 0 ; i < m_iDims ; i++)
481 ostr << L" struct array with ";
483 String* pwstFields = getFieldNames();
484 ostr << L"fields:" << std::endl;
485 for (int i = 0 ; i < pwstFields->getSize() ; i++)
487 ostr << L" " << pwstFields->get(i) << std::endl;
489 pwstFields->killMe();
495 List* Struct::extractFieldWithoutClone(std::wstring _wstField)
497 List* pL = new List();
498 for (int j = 0 ; j < getSize() ; j++)
500 pL->set(j, get(j)->get(_wstField));
506 std::vector<InternalType*> Struct::extractFields(std::vector<std::wstring> _wstFields)
508 std::vector<InternalType*> ResultList;
510 for (int i = 0 ; i < (int)_wstFields.size() ; i++)
512 ResultList.push_back(extractField(_wstFields[i]));
518 InternalType * Struct::extractField(const std::wstring & wstField)
520 if (wstField == L"dims")
522 Int32 * pDims = new Int32(1, getDims());
523 for (int j = 0 ; j < getDims() ; j++)
525 pDims->set(j, getDimsArray()[j]);
534 return get(0)->get(wstField);
538 List * pL = new List();
539 for (int j = 0 ; j < getSize() ; j++)
541 pL->append(get(j)->get(wstField));
549 std::vector<InternalType*> Struct::extractFields(typed_list* _pArgs)
551 std::vector<InternalType*> ResultList;
553 int iDims = (int)_pArgs->size();
556 int* piMaxDim = new int[iDims];
557 int* piCountDim = new int[iDims];
559 int iSeqCount = checkIndexesArguments(this, _pArgs, &pArg, piMaxDim, piCountDim);
566 cleanIndexesArguments(_pArgs, &pArg);
567 ResultList.push_back(createEmptyDouble());
571 Double* pIndex = pArg[0]->getAs<Double>();
573 for (int i = 0 ; i < iSeqCount ; i++)
575 int iIndex = (int)pIndex->get(i);
580 String* pS = getFieldNames();
581 String* pFields = NULL;
584 pFields = new String(1, pS->getSize() + 2);
585 for (int j = 0; j < pS->getSize(); j++)
587 pFields->set(2 + j, pS->get(j));
594 pFields = new String(1, 2);
597 pFields->set(0, L"st");
598 pFields->set(1, L"dims");
600 ResultList.push_back(pFields);
602 else if (iIndex == 2)
605 Int32* pDims = new Int32(1, getDims());
606 for (int j = 0 ; j < getDims() ; j++)
608 pDims->set(j, getDimsArray()[j]);
611 ResultList.push_back(pDims);
613 else if (getSize() == 0)
617 else if (iIndex > (int)get(0)->getData().size() + 2)
621 else if (getSize() == 1)
624 std::list<InternalType*> pData = get(0)->getData();
625 std::list<InternalType*>::iterator it = pData.begin();
626 std::advance(it, iIndex - 3);
627 ResultList.push_back((*it)->clone());
631 //return each elements for sub structs in a list
632 List* pL = new List();
634 for (int j = 0 ; j < getSize() ; j++)
636 //-2 for fieldlist and dims, -1 for indexed at 0
637 std::list<InternalType*> pData = get(j)->getData();
638 std::list<InternalType*>::iterator it = pData.begin();
639 std::advance(it, iIndex - 3);
640 pL->append((*it)->clone());
643 ResultList.push_back(pL);
648 cleanIndexesArguments(_pArgs, &pArg);
652 bool Struct::resize(int _iNewRows, int _iNewCols)
654 int piDims[2] = {_iNewRows, _iNewCols};
655 return resize(piDims, 2);
658 bool Struct::resize(int* _piDims, int _iDims)
660 m_bDisableCloneInCopyValue = true;
661 bool bRes = ArrayOf<SingleStruct*>::resize(_piDims, _iDims);
662 m_bDisableCloneInCopyValue = false;
665 // insert field(s) only in new element(s) of current struct
666 String* pFields = getFieldNames();
667 for (int iterField = 0; iterField < pFields->getSize(); iterField++)
669 for (int iterStruct = 0; iterStruct < getSize(); iterStruct++)
671 get(iterStruct)->addField(pFields->get(iterField));
681 InternalType* Struct::insertWithoutClone(typed_list* _pArgs, InternalType* _pSource)
683 //std::wcout << L"insertWithoutClone start" << std::endl;
684 m_bDisableCloneInCopyValue = true;
685 InternalType* pIT = insert(_pArgs, _pSource);
686 _pSource->IncreaseRef();
687 //std::wcout << L"insertWithoutClone -> " << _pSource << L" : " << _pSource->getRef() << std::endl;
688 m_bDisableCloneInCopyValue = false;
689 //std::wcout << L"insertWithoutClone end" << std::endl;
693 InternalType* Struct::extractWithoutClone(typed_list* _pArgs)
695 //std::wcout << L"extractWithoutClone start" << std::endl;
696 m_bDisableCloneInCopyValue = true;
697 InternalType* pIT = extract(_pArgs);
698 m_bDisableCloneInCopyValue = false;
699 //std::wcout << L"extractWithoutClone end" << std::endl;
703 void Struct::setCloneInCopyValue(bool _val)
705 m_bDisableCloneInCopyValue = !_val;
708 void Struct::deleteData(SingleStruct* data)