2 * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3 * Copyright (C) 2010-2010 - DIGITEO - Bruno JOFRET
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
16 #include "listundefined.hxx"
17 #include "listinsert.hxx"
18 #include "types_tools.hxx"
19 #include "scilabexception.hxx"
22 #include "inspector.hxx"
27 #include "localization.h"
28 #include "charEncoding.h"
34 ** Constructor & Destructor (public)
36 List::List() : Container()
38 m_plData = new std::vector<InternalType *>();
40 Inspector::addItem(this);
46 if (isDeletable() == true)
48 std::vector<InternalType *>::iterator itValues;
49 for (itValues = m_plData->begin() ; itValues != m_plData->end() ; ++itValues)
51 (*itValues)->DecreaseRef();
52 if ((*itValues)->isDeletable())
60 Inspector::removeItem(this);
65 ** Private Copy Constructor and data Access
67 List::List(List *_oListCopyMe)
69 std::vector<InternalType *>::iterator itValues;
70 m_plData = new std::vector<InternalType *>;
72 for (int i = 0 ; i < _oListCopyMe->getData()->size() ; i++)
74 InternalType* pIT = (*_oListCopyMe->getData())[i];
78 m_iSize = static_cast<int>(m_plData->size());
80 Inspector::addItem(this);
84 std::vector<InternalType *> *List::getData()
91 ** Return the number of elements in list
95 return static_cast<int>(m_plData->size());
99 ** append(InternalType *_typedValue)
100 ** Append the given value to the end of the List
102 void List::append(InternalType *_typedValue)
104 m_plData->push_back(_typedValue->clone());
105 m_iSize = static_cast<int>(m_plData->size());
110 ** Create a new List and Copy all values.
112 InternalType *List::clone()
114 return new List(this);
117 GenericType* List::getColumnValues(int _iPos)
123 ** toString to display Lists
124 ** FIXME : Find a better indentation process
126 bool List::toString(std::wostringstream& ostr)
130 ostr << L"()" << std::endl;
135 std::vector<InternalType *>::iterator itValues;
136 for (itValues = m_plData->begin() ; itValues != m_plData->end() ; ++itValues, ++iPosition)
138 ostr << L" (" << iPosition << L")" << std::endl;
140 bool bFinish = (*itValues)->toString(ostr);
147 std::vector<InternalType*> List::extract(typed_list* _pArgs)
149 std::vector<InternalType*> outList;
151 if (_pArgs->size() != 1)
157 int iDims = (int)_pArgs->size();
159 int* piMaxDim = new int[iDims];
160 int* piCountDim = new int[iDims];
162 //evaluate each argument and replace by appropriate value and compute the count of combinations
163 int iSeqCount = checkIndexesArguments(this, _pArgs, &pArg, piMaxDim, piCountDim);
166 //outList.push_back(Double::Empty());
169 for (int i = 0 ; i < iSeqCount ; i++)
171 int idx = (int)pArg[0]->getAs<Double>()->get(i);
172 if (idx > getSize() || idx < 1)
177 InternalType* pIT = (*m_plData)[idx - 1];
178 outList.push_back(pIT->clone());
181 for (int iArg = 0 ; iArg < pArg.size() ; iArg++)
183 if (pArg[iArg] != (*_pArgs)[iArg] && pArg[iArg]->isDeletable())
191 InternalType* List::insert(typed_list* _pArgs, InternalType* _pSource)
194 if (_pArgs->size() != 1)
200 int iDims = (int)_pArgs->size();
202 int* piMaxDim = new int[iDims];
203 int* piCountDim = new int[iDims];
205 int iSeqCount = checkIndexesArguments(this, _pArgs, &pArg, piMaxDim, piCountDim);
211 else if (iSeqCount > 1)
213 std::wostringstream os;
214 os << _W("Unable to insert multiple item in a list.\n");
215 throw ast::ScilabError(os.str());
219 int idx = (int)pArg[0]->getAs<Double>()->get(0);
220 if (_pSource->isListDelete())
228 else if (idx <= m_plData->size())
230 InternalType* pIT = (*m_plData)[idx - 1];
231 if (pIT && pIT->isDeletable())
235 m_plData->erase(m_plData->begin() + idx - 1);
238 else if (_pSource->isListInsert())
243 std::wostringstream os;
244 os << _W("Index out of bounds.\n");
245 throw ast::ScilabError(os.str());
248 InternalType* pInsert = _pSource->getAs<ListInsert>()->getInsert()->clone();
249 if (idx > m_plData->size())
251 //try to insert after the last index, increase list size and assign value
252 while (m_plData->size() < idx)
254 //incease list size and fill with "Undefined"
255 m_plData->push_back(new ListUndefined());
257 (*m_plData)[idx - 1] = pInsert;
261 m_plData->insert(m_plData->begin() + idx - 1, pInsert);
266 //special cazse to insert at the first position
267 InternalType* pInsert = NULL;
268 if (_pSource->isListInsert())
270 pInsert = _pSource->getAs<ListInsert>()->getInsert();
277 m_plData->insert(m_plData->begin(), pInsert->clone());
281 while (m_plData->size() < idx)
283 //incease list size and fill with "Undefined"
284 m_plData->push_back(new ListUndefined());
287 InternalType* pIT = (*m_plData)[idx - 1];
288 if (pIT && pIT->isDeletable())
293 (*m_plData)[idx - 1] = _pSource->clone();
296 m_iSize = (int)m_plData->size();
300 InternalType* List::get(const int _iIndex)
302 if (_iIndex >= 0 && _iIndex < m_plData->size())
304 return (*m_plData)[_iIndex];
309 bool List::operator==(const InternalType& it)
311 if (const_cast<InternalType &>(it).isList() == false)
316 List* plst = const_cast<InternalType &>(it).getAs<List>();
318 if (getSize() != plst->getSize())
323 for (int i = 0; i < getSize(); i++)
325 if (*(*m_plData)[i] != *plst->get(i))