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);
93 Int<T>* clone() override
95 Int<T> *pbClone = new Int<T>(GenericType::getDims(), GenericType::getDimsArray());
96 pbClone->set(ArrayOf<T>::get());
100 /*Config management*/
101 void whoAmI() override;
103 bool isInt() override
108 bool neg(InternalType *& out) override
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) override
118 return type_traits::transpose(*this, out);
121 bool operator==(const InternalType& it) override
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) override
152 return !(*this == it);
155 /* return type as string ( double, int, cell, list, ... )*/
156 virtual std::wstring getTypeStr() const override;
158 /* return type as short string ( s, i, ce, l, ... )*/
159 virtual std::wstring getShortTypeStr() const override
164 virtual bool isNativeType() override
169 virtual void fillDefaultValues() override
171 int size = GenericType::getSize();
172 memset(this->m_pRealData, 0x00, sizeof(T) * size);
177 Int<T> *pOut = new Int<T>(GenericType::getDims(), GenericType::getDimsArray());
179 int size = GenericType::getSize();
180 for (int i = 0; i < size; ++i)
182 p[i] = (this->m_pRealData[i] > T(0)) - (this->m_pRealData[i] < T(0));
189 inline InternalType::ScilabType getType(void) override;
190 inline InternalType::ScilabId getId(void) override;
193 virtual bool subMatrixToString(std::wostringstream& ostr, int* _piDims, int /*_iDims*/) override
195 int iCurrentLine = 0;
196 int iLineLen = ConfigVariable::getConsoleWidth();
197 int iMaxLines = ConfigVariable::getConsoleLines();
199 if (GenericType::isIdentity())
201 ostr << L"eye *" << std::endl << std::endl;
205 getSignedIntFormat(ArrayOf<T>::get(0), &iWidth);
206 addSignedIntValue(&ostr, ArrayOf<T>::get(0), iWidth);
210 getUnsignedIntFormat(ArrayOf<T>::get(0), &iWidth);
211 addUnsignedIntValue(&ostr, ArrayOf<T>::get(0), iWidth);
215 else if (GenericType::isScalar())
221 int iPos = ArrayOf<T>::getIndex(_piDims);
225 getSignedIntFormat(ArrayOf<T>::get(iPos), &iWidth);
226 addSignedIntValue(&ostr, ArrayOf<T>::get(iPos), iWidth);
230 getUnsignedIntFormat(ArrayOf<T>::get(iPos), &iWidth);
231 addUnsignedIntValue(&ostr, ArrayOf<T>::get(iPos), iWidth);
235 else if (GenericType::getCols() == 1)
240 //Array with the max printed size of each col
241 for (int i = 0 ; i < GenericType::getRows() ; i++)
246 int iPos = ArrayOf<T>::getIndex(_piDims);
249 getSignedIntFormat(ArrayOf<T>::get(iPos), &iWidth);
253 getUnsignedIntFormat(ArrayOf<T>::get(iPos), &iWidth);
255 iWidthMax = (std::max)(iWidthMax, iWidth);
258 for (int i = this->m_iRows1PrintState ; i < this->getRows() ; i++)
261 if ((iMaxLines == 0 && iCurrentLine >= MAX_LINES) || (iMaxLines != 0 && iCurrentLine >= iMaxLines))
263 this->m_iRows1PrintState = i;
269 int iPos = ArrayOf<T>::getIndex(_piDims);
273 addSignedIntValue(&ostr, ArrayOf<T>::get(iPos), iWidthMax);
277 addUnsignedIntValue(&ostr, ArrayOf<T>::get(iPos), iWidthMax);
282 else if (GenericType::getRows() == 1)
285 std::wostringstream ostemp;
286 int iLastVal = this->m_iCols1PrintState;
288 for (int i = this->m_iCols1PrintState ; i < this->getCols() ; i++)
294 int iPos = ArrayOf<T>::getIndex(_piDims);
298 getSignedIntFormat(ArrayOf<T>::get(iPos), &iWidth);
302 getUnsignedIntFormat(ArrayOf<T>::get(iPos), &iWidth);
305 iLen = iWidth + static_cast<int>(ostemp.str().size());
306 if (iLen > iLineLen && iLastVal != i)
308 //Max length, new line
309 iCurrentLine += 4; //"column x to Y" + empty line + value + empty line
310 if ((iMaxLines == 0 && iCurrentLine >= MAX_LINES) || (iMaxLines != 0 && iCurrentLine >= iMaxLines))
312 this->m_iCols1PrintState = iLastVal;
316 addColumnString(ostr, iLastVal + 1, i);
317 ostr << ostemp.str() << std::endl;
324 addSignedIntValue(&ostemp, ArrayOf<T>::get(iPos), iWidth);
328 addUnsignedIntValue(&ostemp, ArrayOf<T>::get(iPos), iWidth);
334 addColumnString(ostr, iLastVal + 1, GenericType::getCols());
338 ostr << ostemp.str();
342 std::wostringstream ostemp;
344 int iLastCol = this->m_iCols1PrintState;
346 //Array with the max printed size of each col
347 int *piSize = new int[GenericType::getCols()];
348 memset(piSize, 0x00, GenericType::getCols() * sizeof(int));
350 //compute the row size for padding for each printed bloc.
351 for (int iCols1 = this->m_iCols1PrintState ; iCols1 < this->getCols() ; iCols1++)
353 for (int iRows1 = 0 ; iRows1 < this->getRows() ; iRows1++)
358 int iPos = ArrayOf<T>::getIndex(_piDims);
362 getSignedIntFormat(ArrayOf<T>::get(iPos), &iWidth);
366 getUnsignedIntFormat(ArrayOf<T>::get(iPos), &iWidth);
368 piSize[iCols1] = (std::max)(piSize[iCols1], iWidth);
371 if (iLen + piSize[iCols1] > iLineLen && iCols1 != iLastCol)
373 //find the limit, print this part
374 for (int iRows2 = this->m_iRows2PrintState ; iRows2 < this->getRows() ; iRows2++)
377 if ((iMaxLines == 0 && iCurrentLine >= MAX_LINES) ||
378 ( (iMaxLines != 0 && iCurrentLine + 3 >= iMaxLines && iRows2 == this->m_iRows2PrintState) ||
379 (iMaxLines != 0 && iCurrentLine + 1 >= iMaxLines && iRows2 != this->m_iRows2PrintState)))
381 if (this->m_iRows2PrintState == 0 && iRows2 != 0)
384 addColumnString(ostr, iLastCol + 1, iCols1);
387 ostr << ostemp.str();
388 this->m_iRows2PrintState = iRows2;
389 this->m_iCols1PrintState = iLastCol;
393 for (int iCols2 = iLastCol ; iCols2 < iCols1 ; iCols2++)
397 int iPos = ArrayOf<T>::getIndex(_piDims);
401 addSignedIntValue(&ostemp, ArrayOf<T>::get(iPos), piSize[iCols2]);
405 addUnsignedIntValue(&ostemp, ArrayOf<T>::get(iPos), piSize[iCols2]);
413 if (this->m_iRows2PrintState == 0)
416 addColumnString(ostr, iLastCol + 1, iCols1);
419 ostr << ostemp.str();
422 this->m_iRows2PrintState = 0;
423 this->m_iCols1PrintState = 0;
426 iLen += piSize[iCols1] + SIGN_LENGTH + SIZE_BETWEEN_TWO_VALUES;
429 for (int iRows2 = this->m_iRows2PrintState ; iRows2 < this->getRows() ; iRows2++)
432 if ((iMaxLines == 0 && iCurrentLine >= MAX_LINES) || (iMaxLines != 0 && iCurrentLine >= iMaxLines))
434 if (this->m_iRows2PrintState == 0 && iLastCol != 0)
437 addColumnString(ostr, iLastCol + 1, GenericType::getCols());
440 ostr << ostemp.str();
441 this->m_iRows2PrintState = iRows2;
442 this->m_iCols1PrintState = iLastCol;
446 for (int iCols2 = iLastCol ; iCols2 < GenericType::getCols() ; iCols2++)
450 int iPos = ArrayOf<T>::getIndex(_piDims);
454 addSignedIntValue(&ostemp, ArrayOf<T>::get(iPos), piSize[iCols2]);
458 addUnsignedIntValue(&ostemp, ArrayOf<T>::get(iPos), piSize[iCols2]);
464 if (this->m_iRows2PrintState == 0 && iLastCol != 0)
466 addColumnString(ostr, iLastCol + 1, GenericType::getCols());
469 ostr << ostemp.str();
476 virtual T getNullValue() override
481 virtual Int<T>* createEmpty(int _iDims, int* _piDims, bool /*_bComplex*/) override
483 return new Int<T>(_iDims, _piDims);
486 virtual T copyValue(T _data) override
491 virtual void deleteAll() override
493 delete[] ArrayOf<T>::m_pRealData;
494 ArrayOf<T>::m_pRealData = NULL;
498 virtual void deleteImg() override { }
500 virtual T* allocData(int _iSize) override
502 return new T[_iSize];
505 template<typename U, typename V> struct is_same_int
507 const static bool value = false;
509 template<typename U> struct is_same_int<U, U>
511 const static bool value = true;
515 bool isInt8() override
517 return is_same_int<T, char>::value;
519 bool isInt16() override
521 return is_same_int<T, short>::value;
523 bool isInt32() override
525 return is_same_int<T, int>::value;
527 bool isInt64() override
529 return is_same_int<T, long long>::value;
531 bool isUInt8() override
533 return is_same_int<T, unsigned char>::value;
535 bool isUInt16() override
537 return is_same_int<T, unsigned short>::value;
539 bool isUInt32() override
541 return is_same_int<T, unsigned int>::value;
543 bool isUInt64() override
545 return is_same_int<T, unsigned long long>::value;
555 return std::numeric_limits<T>::is_signed;
559 template<> inline InternalType::ScilabType Int<char>::getType()
563 template<> inline InternalType::ScilabType Int<unsigned char>::getType()
567 template<> inline InternalType::ScilabType Int<short>::getType()
571 template<> inline InternalType::ScilabType Int<unsigned short>::getType()
575 template<> inline InternalType::ScilabType Int<int>::getType()
579 template<> inline InternalType::ScilabType Int<unsigned int>::getType()
583 template<> inline InternalType::ScilabType Int<long long>::getType()
587 template<> inline InternalType::ScilabType Int<unsigned long long>::getType()
592 template<> inline InternalType::ScilabId Int<char>::getId()
594 return GenericType::isScalar() ? IdScalarInt8 : IdInt8;
596 template<> inline InternalType::ScilabId Int<unsigned char>::getId()
598 return GenericType::isScalar() ? IdScalarUInt8 : IdUInt8;
600 template<> inline InternalType::ScilabId Int<short>::getId()
602 return GenericType::isScalar() ? IdScalarInt16 : IdInt16;
604 template<> inline InternalType::ScilabId Int<unsigned short>::getId()
606 return GenericType::isScalar() ? IdScalarUInt16 : IdUInt16;
608 template<> inline InternalType::ScilabId Int<int>::getId()
610 return GenericType::isScalar() ? IdScalarInt32 : IdInt32;
612 template<> inline InternalType::ScilabId Int<unsigned int>::getId()
614 return GenericType::isScalar() ? IdScalarUInt32 : IdUInt32;
616 template<> inline InternalType::ScilabId Int<long long>::getId()
618 return GenericType::isScalar() ? IdScalarInt64 : IdInt64;
620 template<> inline InternalType::ScilabId Int<unsigned long long>::getId()
622 return GenericType::isScalar() ? IdScalarUInt64 : IdUInt64;
626 template<> inline std::wstring Int<char>::getTypeStr() const
631 template<> inline std::wstring Int<short>::getTypeStr() const
636 template<> inline std::wstring Int<int>::getTypeStr() const
641 template<> inline std::wstring Int<long long>::getTypeStr() const
646 template<> inline std::wstring Int<unsigned char>::getTypeStr() const
651 template<> inline std::wstring Int<unsigned short>::getTypeStr() const
656 template<> inline std::wstring Int<unsigned int>::getTypeStr() const
661 template<> inline std::wstring Int<unsigned long long>::getTypeStr() const
666 template<> inline void Int<char>::whoAmI()
668 std::cout << "types::Int8";
671 template<> inline void Int<short>::whoAmI()
673 std::cout << "types::Int16";
676 template<> inline void Int<int>::whoAmI()
678 std::cout << "types::Int32";
681 template<> inline void Int<long long>::whoAmI()
683 std::cout << "types::Int64";
686 template<> inline void Int<unsigned char>::whoAmI()
688 std::cout << "types::UInt8";
691 template<> inline void Int<unsigned short>::whoAmI()
693 std::cout << "types::UInt16";
696 template<> inline void Int<unsigned int>::whoAmI()
698 std::cout << "types::UInt32";
701 template<> inline void Int<unsigned long long>::whoAmI()
703 std::cout << "types::UInt64";
707 typedef Int<char> Int8;
708 typedef Int<short> Int16;
709 typedef Int<int> Int32;
710 typedef Int<long long> Int64;
712 typedef Int<unsigned char> UInt8;
713 typedef Int<unsigned short> UInt16;
714 typedef Int<unsigned int> UInt32;
715 typedef Int<unsigned long long> UInt64;
718 template class Int<char>;
719 template class Int<unsigned char>;
720 template class Int<short>;
721 template class Int<unsigned short>;
722 template class Int<int>;
723 template class Int<unsigned int>;
724 template class Int<long long>;
725 template class Int<unsigned long long>;
730 #endif /* !__INT_HXX__ */