2 * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3 * Copyright (C) 2008-2008 - DIGITEO - Antoine ELIAS
4 * Copyright (C) 2014 - Scilab Enterprises - Calixte DENIZET
6 * Copyright (C) 2012 - 2016 - Scilab Enterprises
8 * This file is hereby licensed under the terms of the GNU GPL v2.0,
9 * pursuant to article 5.3.4 of the CeCILL v.2.1.
10 * This file was originally licensed under the terms of the CeCILL v2.1,
11 * and continues to be available under such terms.
12 * For more information, see the COPYING file which you should have received
13 * along with this program.
17 //#ifndef __ARRAYOF_HXX__
18 // #error This file must only be include by arrayof.hxx
24 #include "arrayof.hxx"
26 #include "internal.hxx"
27 #include "types_transposition.hxx"
28 #include "configvariable.hxx"
29 #include "type_traits.hxx"
33 #include <limits> // std::numeric_limits
34 #include "tostring_common.hxx"
39 class EXTERN_AST Int : public ArrayOf<T>
43 Int(int _iRows, int _iCols)
45 int piDims[2] = {_iRows, _iCols};
47 this->create(piDims, 2, &pInt, NULL);
49 //Inspector::addItem(this);
55 int piDims[2] = {1, 1};
57 this->create(piDims, 2, &pInt, NULL);
60 //Inspector::addItem(this);
64 Int(int _iRows, int _iCols, T** _pData)
66 const int piDims[2] = {_iRows, _iCols};
67 this->create(piDims, 2, _pData, NULL);
69 //Inspector::addItem(this);
73 Int(int _iDims, const int* _piDims)
76 this->create(_piDims, _iDims, &pInt, NULL);
78 //Inspector::addItem(this);
84 if (InternalType::isDeletable() == true)
89 //Inspector::removeItem(this);
95 Int<T> *pbClone = new Int<T>(GenericType::getDims(), GenericType::getDimsArray());
96 pbClone->set(ArrayOf<T>::get());
100 /*Config management*/
108 bool neg(InternalType *& out)
110 out = new Int<T>(this->getDims(), this->getDimsArray());
111 type_traits::bin_neg<T, T>(this->m_iSize, this->m_pRealData, static_cast<Int<T> *>(out)->get());
116 virtual bool transpose(InternalType *& out)
118 return type_traits::transpose(*this, out);
121 bool operator==(const InternalType& it)
123 if (const_cast<InternalType &>(it).getType() != getType())
128 Int<T>* pb = const_cast<InternalType &>(it).getAs<typename types::Int<T> >();
130 if (pb->getDims() != GenericType::getDims())
135 for (int i = 0 ; i < GenericType::getDims() ; i++)
137 if (pb->getDimsArray()[i] != GenericType::getDimsArray()[i])
143 if (memcmp(ArrayOf<T>::get(), pb->get(), GenericType::getSize() * sizeof(T)) != 0)
150 bool operator!=(const InternalType& it)
152 return !(*this == it);
155 /* return type as string ( double, int, cell, list, ... )*/
156 virtual std::wstring getTypeStr() const;
158 /* return type as short string ( s, i, ce, l, ... )*/
159 virtual std::wstring getShortTypeStr() const
164 virtual bool isNativeType() override
169 virtual void fillDefaultValues() override
171 int size = GenericType::getSize();
172 memset(this->m_pRealData, 0x00, sizeof(T) * size);
176 inline InternalType::ScilabType getType(void);
177 inline InternalType::ScilabId getId(void);
180 virtual bool subMatrixToString(std::wostringstream& ostr, int* _piDims, int /*_iDims*/) override
182 int iCurrentLine = 0;
183 int iLineLen = ConfigVariable::getConsoleWidth();
184 int iMaxLines = ConfigVariable::getConsoleLines();
186 if (GenericType::isIdentity())
188 ostr << L"eye *" << std::endl << std::endl;
192 getSignedIntFormat(ArrayOf<T>::get(0), &iWidth);
193 addSignedIntValue(&ostr, ArrayOf<T>::get(0), iWidth);
197 getUnsignedIntFormat(ArrayOf<T>::get(0), &iWidth);
198 addUnsignedIntValue(&ostr, ArrayOf<T>::get(0), iWidth);
202 else if (GenericType::isScalar())
208 int iPos = ArrayOf<T>::getIndex(_piDims);
212 getSignedIntFormat(ArrayOf<T>::get(iPos), &iWidth);
213 addSignedIntValue(&ostr, ArrayOf<T>::get(iPos), iWidth);
217 getUnsignedIntFormat(ArrayOf<T>::get(iPos), &iWidth);
218 addUnsignedIntValue(&ostr, ArrayOf<T>::get(iPos), iWidth);
222 else if (GenericType::getCols() == 1)
227 //Array with the max printed size of each col
228 for (int i = 0 ; i < GenericType::getRows() ; i++)
233 int iPos = ArrayOf<T>::getIndex(_piDims);
236 getSignedIntFormat(ArrayOf<T>::get(iPos), &iWidth);
240 getUnsignedIntFormat(ArrayOf<T>::get(iPos), &iWidth);
242 iWidthMax = (std::max)(iWidthMax, iWidth);
245 for (int i = this->m_iRows1PrintState ; i < this->getRows() ; i++)
248 if ((iMaxLines == 0 && iCurrentLine >= MAX_LINES) || (iMaxLines != 0 && iCurrentLine >= iMaxLines))
250 this->m_iRows1PrintState = i;
256 int iPos = ArrayOf<T>::getIndex(_piDims);
260 addSignedIntValue(&ostr, ArrayOf<T>::get(iPos), iWidthMax);
264 addUnsignedIntValue(&ostr, ArrayOf<T>::get(iPos), iWidthMax);
269 else if (GenericType::getRows() == 1)
272 std::wostringstream ostemp;
273 int iLastVal = this->m_iCols1PrintState;
275 for (int i = this->m_iCols1PrintState ; i < this->getCols() ; i++)
281 int iPos = ArrayOf<T>::getIndex(_piDims);
285 getSignedIntFormat(ArrayOf<T>::get(iPos), &iWidth);
289 getUnsignedIntFormat(ArrayOf<T>::get(iPos), &iWidth);
292 iLen = iWidth + static_cast<int>(ostemp.str().size());
293 if (iLen > iLineLen && iLastVal != i)
295 //Max length, new line
296 iCurrentLine += 4; //"column x to Y" + empty line + value + empty line
297 if ((iMaxLines == 0 && iCurrentLine >= MAX_LINES) || (iMaxLines != 0 && iCurrentLine >= iMaxLines))
299 this->m_iCols1PrintState = iLastVal;
303 addColumnString(ostr, iLastVal + 1, i);
304 ostr << ostemp.str() << std::endl;
311 addSignedIntValue(&ostemp, ArrayOf<T>::get(iPos), iWidth);
315 addUnsignedIntValue(&ostemp, ArrayOf<T>::get(iPos), iWidth);
321 addColumnString(ostr, iLastVal + 1, GenericType::getCols());
325 ostr << ostemp.str();
329 std::wostringstream ostemp;
331 int iLastCol = this->m_iCols1PrintState;
333 //Array with the max printed size of each col
334 int *piSize = new int[GenericType::getCols()];
335 memset(piSize, 0x00, GenericType::getCols() * sizeof(int));
337 //compute the row size for padding for each printed bloc.
338 for (int iCols1 = this->m_iCols1PrintState ; iCols1 < this->getCols() ; iCols1++)
340 for (int iRows1 = 0 ; iRows1 < this->getRows() ; iRows1++)
345 int iPos = ArrayOf<T>::getIndex(_piDims);
349 getSignedIntFormat(ArrayOf<T>::get(iPos), &iWidth);
353 getUnsignedIntFormat(ArrayOf<T>::get(iPos), &iWidth);
355 piSize[iCols1] = (std::max)(piSize[iCols1], iWidth);
358 if (iLen + piSize[iCols1] > iLineLen && iCols1 != iLastCol)
360 //find the limit, print this part
361 for (int iRows2 = this->m_iRows2PrintState ; iRows2 < this->getRows() ; iRows2++)
364 if ((iMaxLines == 0 && iCurrentLine >= MAX_LINES) ||
365 ( (iMaxLines != 0 && iCurrentLine + 3 >= iMaxLines && iRows2 == this->m_iRows2PrintState) ||
366 (iMaxLines != 0 && iCurrentLine + 1 >= iMaxLines && iRows2 != this->m_iRows2PrintState)))
368 if (this->m_iRows2PrintState == 0 && iRows2 != 0)
371 addColumnString(ostr, iLastCol + 1, iCols1);
374 ostr << ostemp.str();
375 this->m_iRows2PrintState = iRows2;
376 this->m_iCols1PrintState = iLastCol;
380 for (int iCols2 = iLastCol ; iCols2 < iCols1 ; iCols2++)
384 int iPos = ArrayOf<T>::getIndex(_piDims);
388 addSignedIntValue(&ostemp, ArrayOf<T>::get(iPos), piSize[iCols2]);
392 addUnsignedIntValue(&ostemp, ArrayOf<T>::get(iPos), piSize[iCols2]);
400 if (this->m_iRows2PrintState == 0)
403 addColumnString(ostr, iLastCol + 1, iCols1);
406 ostr << ostemp.str();
409 this->m_iRows2PrintState = 0;
410 this->m_iCols1PrintState = 0;
413 iLen += piSize[iCols1] + SIGN_LENGTH + SIZE_BETWEEN_TWO_VALUES;
416 for (int iRows2 = this->m_iRows2PrintState ; iRows2 < this->getRows() ; iRows2++)
419 if ((iMaxLines == 0 && iCurrentLine >= MAX_LINES) || (iMaxLines != 0 && iCurrentLine >= iMaxLines))
421 if (this->m_iRows2PrintState == 0 && iLastCol != 0)
424 addColumnString(ostr, iLastCol + 1, GenericType::getCols());
427 ostr << ostemp.str();
428 this->m_iRows2PrintState = iRows2;
429 this->m_iCols1PrintState = iLastCol;
433 for (int iCols2 = iLastCol ; iCols2 < GenericType::getCols() ; iCols2++)
437 int iPos = ArrayOf<T>::getIndex(_piDims);
441 addSignedIntValue(&ostemp, ArrayOf<T>::get(iPos), piSize[iCols2]);
445 addUnsignedIntValue(&ostemp, ArrayOf<T>::get(iPos), piSize[iCols2]);
451 if (this->m_iRows2PrintState == 0 && iLastCol != 0)
453 addColumnString(ostr, iLastCol + 1, GenericType::getCols());
456 ostr << ostemp.str();
462 virtual T getNullValue()
467 virtual Int<T>* createEmpty(int _iDims, int* _piDims, bool /*_bComplex*/)
469 return new Int<T>(_iDims, _piDims);
472 virtual T copyValue(T _data)
477 virtual void deleteAll()
479 delete[] ArrayOf<T>::m_pRealData;
480 ArrayOf<T>::m_pRealData = NULL;
484 virtual void deleteImg() { }
486 virtual T* allocData(int _iSize)
488 return new T[_iSize];
491 template<typename U, typename V> struct is_same_int
493 const static bool value = false;
495 template<typename U> struct is_same_int<U, U>
497 const static bool value = true;
503 return is_same_int<T, char>::value;
507 return is_same_int<T, short>::value;
511 return is_same_int<T, int>::value;
515 return is_same_int<T, long long>::value;
519 return is_same_int<T, unsigned char>::value;
523 return is_same_int<T, unsigned short>::value;
527 return is_same_int<T, unsigned int>::value;
531 return is_same_int<T, unsigned long long>::value;
541 return std::numeric_limits<T>::is_signed;
545 template<> inline InternalType::ScilabType Int<char>::getType()
549 template<> inline InternalType::ScilabType Int<unsigned char>::getType()
553 template<> inline InternalType::ScilabType Int<short>::getType()
557 template<> inline InternalType::ScilabType Int<unsigned short>::getType()
561 template<> inline InternalType::ScilabType Int<int>::getType()
565 template<> inline InternalType::ScilabType Int<unsigned int>::getType()
569 template<> inline InternalType::ScilabType Int<long long>::getType()
573 template<> inline InternalType::ScilabType Int<unsigned long long>::getType()
578 template<> inline InternalType::ScilabId Int<char>::getId()
580 return GenericType::isScalar() ? IdScalarInt8 : IdInt8;
582 template<> inline InternalType::ScilabId Int<unsigned char>::getId()
584 return GenericType::isScalar() ? IdScalarUInt8 : IdUInt8;
586 template<> inline InternalType::ScilabId Int<short>::getId()
588 return GenericType::isScalar() ? IdScalarInt16 : IdInt16;
590 template<> inline InternalType::ScilabId Int<unsigned short>::getId()
592 return GenericType::isScalar() ? IdScalarUInt16 : IdUInt16;
594 template<> inline InternalType::ScilabId Int<int>::getId()
596 return GenericType::isScalar() ? IdScalarInt32 : IdInt32;
598 template<> inline InternalType::ScilabId Int<unsigned int>::getId()
600 return GenericType::isScalar() ? IdScalarUInt32 : IdUInt32;
602 template<> inline InternalType::ScilabId Int<long long>::getId()
604 return GenericType::isScalar() ? IdScalarInt64 : IdInt64;
606 template<> inline InternalType::ScilabId Int<unsigned long long>::getId()
608 return GenericType::isScalar() ? IdScalarUInt64 : IdUInt64;
612 template<> inline std::wstring Int<char>::getTypeStr() const
617 template<> inline std::wstring Int<short>::getTypeStr() const
622 template<> inline std::wstring Int<int>::getTypeStr() const
627 template<> inline std::wstring Int<long long>::getTypeStr() const
632 template<> inline std::wstring Int<unsigned char>::getTypeStr() const
637 template<> inline std::wstring Int<unsigned short>::getTypeStr() const
642 template<> inline std::wstring Int<unsigned int>::getTypeStr() const
647 template<> inline std::wstring Int<unsigned long long>::getTypeStr() const
652 template<> inline void Int<char>::whoAmI()
654 std::cout << "types::Int8";
657 template<> inline void Int<short>::whoAmI()
659 std::cout << "types::Int16";
662 template<> inline void Int<int>::whoAmI()
664 std::cout << "types::Int32";
667 template<> inline void Int<long long>::whoAmI()
669 std::cout << "types::Int64";
672 template<> inline void Int<unsigned char>::whoAmI()
674 std::cout << "types::UInt8";
677 template<> inline void Int<unsigned short>::whoAmI()
679 std::cout << "types::UInt16";
682 template<> inline void Int<unsigned int>::whoAmI()
684 std::cout << "types::UInt32";
687 template<> inline void Int<unsigned long long>::whoAmI()
689 std::cout << "types::UInt64";
693 typedef Int<char> Int8;
694 typedef Int<short> Int16;
695 typedef Int<int> Int32;
696 typedef Int<long long> Int64;
698 typedef Int<unsigned char> UInt8;
699 typedef Int<unsigned short> UInt16;
700 typedef Int<unsigned int> UInt32;
701 typedef Int<unsigned long long> UInt64;
704 template class Int<char>;
705 template class Int<unsigned char>;
706 template class Int<short>;
707 template class Int<unsigned short>;
708 template class Int<int>;
709 template class Int<unsigned int>;
710 template class Int<long long>;
711 template class Int<unsigned long long>;
716 #endif /* !__INT_HXX__ */