improve insert and insertNew of native types 61/17761/4
Antoine ELIAS [Tue, 2 Feb 2016 11:05:54 +0000 (12:05 +0100)]
Change-Id: I0f8cfc9c1c42dba207d1b371821fc9da0d0c8e55

scilab/modules/ast/includes/types/arrayof.hxx
scilab/modules/ast/includes/types/bool.hxx
scilab/modules/ast/includes/types/double.hxx
scilab/modules/ast/includes/types/int.hxx
scilab/modules/ast/includes/types/types_tools.hxx
scilab/modules/ast/src/cpp/types/arrayof.cpp
scilab/modules/ast/src/cpp/types/bool.cpp
scilab/modules/ast/src/cpp/types/types_tools.cpp

index d0ce13e..2e578e7 100644 (file)
@@ -210,6 +210,32 @@ public :
         return m_pImgData != NULL;
     }
 
+    //type does not need to delete or clone ( int, double, ... )
+    virtual bool isNativeType()
+    {
+        return false;
+    }
+
+    virtual void fillDefaultValues()
+    {
+        int size = getSize();
+        if (isComplex())
+        {
+            for (int i = 0; i < size; ++i)
+            {
+                set(i, getNullValue());
+                setImg(i, getNullValue());
+            }
+        }
+        else
+        {
+            for (int i = 0; i < size; ++i)
+            {
+                set(i, getNullValue());
+            }
+        }
+    }
+
     virtual ArrayOf<T>* setComplex(bool _bComplex)
     {
         typedef ArrayOf<T>* (ArrayOf<T>::*setcplx_t)(bool);
index 6da0053..be8820f 100644 (file)
@@ -53,8 +53,8 @@ public:
 
 
     /*zero or one set filler*/
-    bool                    setFalse();
-    bool                    setTrue();
+    Bool*                   setFalse();
+    Bool*                   setTrue();
 
     /*Config management*/
     void                    whoAmI();
@@ -80,6 +80,17 @@ public:
 
     virtual bool transpose(InternalType *& out);
 
+    virtual bool isNativeType() override
+    {
+        return true;
+    }
+
+    virtual void fillDefaultValues() override
+    {
+        int size = getSize();
+        memset(m_pRealData, 0x00, sizeof(int) * size);
+    }
+
 protected :
     inline ScilabType       getType(void)
     {
index 0d17535..e39f3a8 100644 (file)
@@ -338,6 +338,20 @@ public :
         return this;
     }
 
+    virtual bool isNativeType() override
+    {
+        return true;
+    }
+
+    virtual void fillDefaultValues() override
+    {
+        int size = getSize();
+        memset(m_pRealData, 0x00, sizeof(double) * size);
+        if (isComplex())
+        {
+            memset(m_pImgData, 0x00, sizeof(double) * size);
+        }
+    }
 
 private:
     virtual bool                subMatrixToString(std::wostringstream& ostr, int* _piDims, int _iDims) override;
index e25f5e1..01cf64b 100644 (file)
@@ -161,6 +161,17 @@ public :
         return L"i";
     }
 
+    virtual bool isNativeType() override
+    {
+        return true;
+    }
+
+    virtual void fillDefaultValues() override
+    {
+        int size = GenericType::getSize();
+        memset(this->m_pRealData, 0x00, sizeof(T) * size);
+    }
+
 protected :
     inline InternalType::ScilabType getType(void);
     inline InternalType::ScilabId   getId(void);
index 36a1365..1b32847 100644 (file)
@@ -23,6 +23,7 @@
 namespace types
 {
 bool getScalarIndex(GenericType* _pRef, typed_list* _pArgsIn, int* index);
+bool getArgsDims(typed_list* _pArgsIn, std::vector<int>& dims); //only for insertNew
 bool getImplicitIndex(GenericType* _pRef, typed_list* _pArgsIn, std::vector<int>& index, std::vector<int>& dims);
 bool getScalarImplicitIndex(GenericType* _pRef, typed_list* _pArgsIn, std::vector<double>& index);
 
index ba6a2f5..ec9b6a5 100644 (file)
@@ -82,10 +82,18 @@ ArrayOf<T>* ArrayOf<T>::insert(typed_list* _pArgs, InternalType* _pSource)
 
         if (isComplex() == false && pIns->isComplex() == false)
         {
-            if (set(index, *pRealData) != NULL)
+            if (isNativeType() && index < m_iSize)
             {
+                m_pRealData[index] = *pRealData;
                 return this;
             }
+            else
+            {
+                if (set(index, *pRealData) != NULL)
+                {
+                    return this;
+                }
+            }
         }
 
         //if complex continue
@@ -117,25 +125,58 @@ ArrayOf<T>* ArrayOf<T>::insert(typed_list* _pArgs, InternalType* _pSource)
         {
             if (sizeIn == 1)
             {
-                for (int & i : indexes)
+                if (isNativeType())
                 {
-                    if (set(i, *pRealData) == NULL)
+                    for (int i : indexes)
                     {
-                        status = false;
-                        break;
+                        if (i >= m_iSize)
+                        {
+                            status = false;
+                            break;
+                        }
+
+                        m_pRealData[i] = *pRealData;
+                    }
+                }
+                else
+                {
+                    for (int i : indexes)
+                    {
+                        if (set(i, *pRealData) == NULL)
+                        {
+                            status = false;
+                            break;
+                        }
                     }
                 }
             }
             else
             {
-                for (int & i : indexes)
+                if (isNativeType())
                 {
-                    if (set(i, *pRealData) == NULL)
+                    for (int i : indexes)
                     {
-                        status = false;
-                        break;
+                        if (i >= m_iSize)
+                        {
+                            status = false;
+                            break;
+                        }
+                        
+                        m_pRealData[i] = *pRealData;
+                        ++pRealData;
+                    }
+                }
+                else
+                {
+                    for (int i : indexes)
+                    {
+                        if (set(i, *pRealData) == NULL)
+                        {
+                            status = false;
+                            break;
+                        }
+                        ++pRealData;
                     }
-                    ++pRealData;
                 }
             }
 
@@ -452,13 +493,34 @@ ArrayOf<T>* ArrayOf<T>::insert(typed_list* _pArgs, InternalType* _pSource)
 template <typename T>
 GenericType* ArrayOf<T>::insertNew(typed_list* _pArgs)
 {
+    bool bComplex = getImg() != NULL;
+
+    std::vector<int> dims;
+    if (getArgsDims(_pArgs, dims))
+    {
+        InternalType *pOut = NULL;
+        while (dims.size() < 2)
+        {
+            dims.push_back(1);
+        }
+        pOut = createEmpty((int)dims.size(), dims.data(), bComplex);
+        ArrayOf* pArrayOut = pOut->getAs<ArrayOf>();
+        pArrayOut->fillDefaultValues();
+        ArrayOf* pOut2 = pArrayOut->insert(_pArgs, this);
+        if (pOut != pOut2)
+        {
+            delete pOut;
+        }
+
+        return pOut2;
+    }
+
     typed_list pArg;
     InternalType *pOut = NULL;
 
     int iDims = (int)_pArgs->size();
     int* piMaxDim = new int[iDims];
     int* piCountDim = new int[iDims];
-    bool bComplex = getImg() != NULL;
     bool bUndefine = false;
     bool bIsImpli = false;
 
@@ -602,26 +664,53 @@ GenericType* ArrayOf<T>::insertNew(typed_list* _pArgs)
 
     //fill with null item
     ArrayOf* pArrayOut = pOut->getAs<ArrayOf>();
-    T* pRealData = pArrayOut->get();
-    if (bComplex)
-    {
-        T* pImgData = pArrayOut->getImg();
-        for (int i = 0; i < pArrayOut->getSize(); i++)
-        {
-            pArrayOut->deleteData(pRealData[i]);
-            pRealData[i] = getNullValue();
-            pArrayOut->deleteData(pImgData[i]);
-            pImgData[i] = getNullValue();
-        }
-    }
-    else
-    {
-        for (int i = 0; i < pArrayOut->getSize(); i++)
-        {
-            pArrayOut->deleteData(pRealData[i]);
-            pRealData[i] = getNullValue();
-        }
-    }
+    pArrayOut->fillDefaultValues();
+    //T* pRealData = pArrayOut->get();
+    //if (bComplex)
+    //{
+    //    int size = pArrayOut->getSize();
+    //    T* pImgData = pArrayOut->getImg();
+
+    //    if (isNativeType())
+    //    {
+    //        T val = getNullValue();
+    //        for (int i = 0; i < size; i++)
+    //        {
+    //            pRealData[i] = val;
+    //            pImgData[i] = val;
+    //        }
+    //    }
+    //    else
+    //    {
+    //        for (int i = 0; i < size; i++)
+    //        {
+    //            pArrayOut->deleteData(pRealData[i]);
+    //            pRealData[i] = getNullValue();
+    //            pArrayOut->deleteData(pImgData[i]);
+    //            pImgData[i] = getNullValue();
+    //        }
+    //    }
+    //}
+    //else
+    //{
+    //    int size = pArrayOut->getSize();
+    //    if (isNativeType())
+    //    {
+    //        T val = getNullValue();
+    //        for (int i = 0; i < size; i++)
+    //        {
+    //            pRealData[i] = val;
+    //        }
+    //    }
+    //    else
+    //    {
+    //        for (int i = 0; i < size; i++)
+    //        {
+    //            pArrayOut->deleteData(pRealData[i]);
+    //            pRealData[i] = getNullValue();
+    //        }
+    //    }
+    //}
 
     if (bIsImpli && (pArrayOut->getSize() != getSize()))
     {
index c74d906..8f5fa0e 100644 (file)
@@ -91,22 +91,38 @@ void Bool::whoAmI()
     std::cout << "types::Bool";
 }
 
-bool Bool::setFalse()
+Bool* Bool::setFalse()
 {
-    for (int i = 0 ; i < getSize() ; i++)
+    Bool* pb = checkRef(this, &Bool::setFalse);
+    if (pb != this)
     {
-        set(i, 0);
+        return pb;
     }
-    return true;
+
+    int size = getSize();
+    for (int i = 0 ; i < size ; i++)
+    {
+        m_pRealData[i] = 0;
+    }
+
+    return this;
 }
 
-bool Bool::setTrue()
+Bool* Bool::setTrue()
 {
-    for (int i = 0 ; i < getSize() ; i++)
+    Bool* pb = checkRef(this, &Bool::setTrue);
+    if (pb != this)
     {
-        set(i, 1);
+        return pb;
     }
-    return true;
+
+    int size = getSize();
+    for (int i = 0; i < size; i++)
+    {
+        m_pRealData[i] = 1;
+    }
+
+    return this;
 }
 
 bool Bool::subMatrixToString(std::wostringstream& ostr, int* _piDims, int /*_iDims*/)
index ce6458b..e4d91f7 100644 (file)
@@ -108,6 +108,88 @@ double getIndex(InternalType* val)
 }
 
 //get only scalar index
+bool getArgsDims(typed_list* _pArgsIn, std::vector<int>& dims)
+{
+    //input size must be equal to ref dims
+    int dimsIn = static_cast<int>(_pArgsIn->size());
+
+    //same dims and less than internal limit
+    if (dimsIn > MAX_DIMS)
+    {
+        return false;
+    }
+
+    dims.reserve(dimsIn);
+
+    for (int i = 0; i < dimsIn; ++i)
+    {
+        InternalType* in = (*_pArgsIn)[i];
+        //input arg type must be scalar double, int8, int16, ...
+        if (in->isGenericType() && in->getAs<GenericType>()->isScalar())
+        {
+            int ind = static_cast<int>(getIndex(in));
+            if (ind == 0)
+            {
+                return false;
+            }
+
+            dims.push_back(ind);
+        }
+        else if (in->isImplicitList())
+        {
+            ImplicitList* pIL = in->getAs<ImplicitList>();
+            if (pIL->isComputable() == false)
+            {
+                return false;
+            }
+
+            int size = pIL->getSize();
+            if (size <= 0)
+            {
+                return false;
+            }
+
+
+            double start = getIndex(pIL->getStart());
+            double step = getIndex(pIL->getStep());
+            if (step > 0)
+            {
+                double real_end = start + step * (size - 1);
+                dims.push_back(real_end);
+            }
+            else if (step < 0)
+            {
+                dims.push_back(start);
+            }
+            else
+            {
+                return false;
+            }
+        }
+        else
+        {
+            //failed, so use entire process
+            return false;
+        }
+    }
+
+
+    //remove last dims == 1
+    while (dims.size() > 2)
+    {
+        if (dims.back() != 1)
+        {
+            break;
+        }
+
+        dims.pop_back();
+    }
+
+    return true;
+}
+
+
+//get only scalar index
 bool getScalarIndex(GenericType* _pRef, typed_list* _pArgsIn, int* index)
 {
     //input size must be equal to ref dims
@@ -282,11 +364,8 @@ bool getImplicitIndex(GenericType* _pRef, typed_list* _pArgsIn, std::vector<int>
                 int sizeRef = viewAsVector ? _pRef->getSize() : pdims[i];
                 double start = evalute(pIL->getStart(), sizeRef);
                 double step = evalute(pIL->getStep(), sizeRef);
-                double end = evalute(pIL->getEnd(), sizeRef);
 
-                //printf("%.2f : %.2f : %.2f\n", start, step, end);
-
-                int size = (end - start) / step + 1;
+                int size = pIL->getSize();
                 if (size <= 0)
                 {
                     //manage implicit that return []
@@ -295,12 +374,9 @@ bool getImplicitIndex(GenericType* _pRef, typed_list* _pArgsIn, std::vector<int>
                 }
 
                 std::vector<int> idx(size);
-                int* pi = idx.data();
-                pi[0] = start - 1; //0-indexed
-                for (int j = 1; j < size; ++j)
-                {
-                    pi[j] = pi[j - 1] + step;
-                }
+
+                double val = start - 1;
+                std::generate(idx.begin(), idx.end(), [&val, step]{ double s = val; val += step; return (int)s; });
 
                 lstIdx.push_back(idx);
                 finalSize *= size;
@@ -324,6 +400,7 @@ bool getImplicitIndex(GenericType* _pRef, typed_list* _pArgsIn, std::vector<int>
     int previousSize = 1;
     int currentDim = 0;
     int previousDims = 1;
+    int* p = index.data();
     while (lstIdx.empty() == false)
     {
         std::vector<int>& v = lstIdx.front();
@@ -347,13 +424,14 @@ bool getImplicitIndex(GenericType* _pRef, typed_list* _pArgsIn, std::vector<int>
                     int idx3 = previousDims * m;
                     for (int j = 0; j < previousSize; ++j)
                     {
-                        index[idx2 + j] += idx3;
+                        p[idx2 + j] += idx3;
                     }
                 }
             }
         }
         else
         {
+            int* p = index.data();
             int occ = finalSize / (currentSize * previousSize);
             for (int n = 0; n < occ; ++n)
             {
@@ -368,7 +446,7 @@ bool getImplicitIndex(GenericType* _pRef, typed_list* _pArgsIn, std::vector<int>
                     int idx3 = previousDims * pv[m];
                     for (int j = 0; j < previousSize; ++j)
                     {
-                        index[idx2 + j] += idx3;
+                        p[idx2 + j] += idx3;
                     }
                 }
             }
@@ -460,7 +538,7 @@ int checkIndexesArguments(InternalType* _pRef, typed_list* _pArgsIn, typed_list*
                     //not enough information to compute indexes.
                     _pArgsOut->push_back(NULL);
                     bUndefine = true;
-                    pIL->killMe();;
+                    pIL->killMe();
                     continue;
                 }
                 //evalute polynom with "MaxDim"